From fbf736303ba0e768016939c8eeb6c482a3410caf Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Thu, 6 Apr 2017 17:12:41 -0300 Subject: [PATCH] Melhorando tratamento de excessões. --- pw3270-sharp.cbp | 2 +- src/native/actions.cc | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-------- src/native/get.cc | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- src/native/init.cc | 16 ++++++++++++++-- src/native/network.cc | 45 ++++++++++++++++++++++++++++++++++++++------- src/native/private.h | 6 ++++++ src/native/screen.cc | 63 +++++++++++++++++++++++++++++++++++++++++++++++++-------------- src/native/set.cc | 42 +++++++++++++++++++++++++++++++++++------- src/pw3270-sharp/pw3270-sharp.cs | 17 +++++++++++++++++ testprograms/sample.cs | 3 +++ 10 files changed, 288 insertions(+), 48 deletions(-) diff --git a/pw3270-sharp.cbp b/pw3270-sharp.cbp index 55c4fee..24c7894 100644 --- a/pw3270-sharp.cbp +++ b/pw3270-sharp.cbp @@ -50,7 +50,7 @@ - + diff --git a/src/native/actions.cc b/src/native/actions.cc index a390ea9..7b7952b 100644 --- a/src/native/actions.cc +++ b/src/native/actions.cc @@ -32,34 +32,74 @@ /*---[ Implement ]----------------------------------------------------------------------------------*/ int tn3270_enter(h3270::session *ses) { - return ses->enter(); + try { + return ses->enter(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } } int tn3270_pfkey(h3270::session *ses, int key) { - return ses->pfkey(key); + try { + return ses->pfkey(key); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } } int tn3270_pakey(h3270::session *ses, int key) { - return ses->pakey(key); + try { + return ses->pakey(key); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } } int tn3270_action(h3270::session *ses, const char *name) { - return ses->action(name); + try { + return ses->action(name); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } } int tn3270_erase(h3270::session *ses) { - return ses->erase(); + try { + return ses->erase(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } } int tn3270_erase_eof(h3270::session *ses) { - return ses->erase_eof(); + try { + return ses->erase_eof(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } } int tn3270_erase_eol(h3270::session *ses) { - return ses->erase_eol(); + try { + return ses->erase_eol(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } } int tn3270_erase_input(h3270::session *ses) { - return ses->erase_input(); + try { + return ses->erase_input(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } } diff --git a/src/native/get.cc b/src/native/get.cc index 66ef118..505c811 100644 --- a/src/native/get.cc +++ b/src/native/get.cc @@ -37,7 +37,12 @@ * */ int tn3270_get_version(h3270::session *ses, char* str, int strlen) { - strncpy(str,ses->get_version().c_str(),strlen); + try { + strncpy(str,ses->get_version().c_str(),strlen); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return 0; } @@ -46,35 +51,98 @@ * */ int tn3270_get_revision(h3270::session *ses, char* str, int strlen) { - strncpy(str,ses->get_revision().c_str(),strlen); + + try { + strncpy(str,ses->get_revision().c_str(),strlen); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return 0; } int tn3270_get_cstate(h3270::session *ses) { - return (int) ses->get_cstate(); + + try { + return (int) ses->get_cstate(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; + } int tn3270_get_program_message(h3270::session *ses) { - return (int) ses->get_program_message(); + + try { + return (int) ses->get_program_message(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; + } int tn3270_get_secure(h3270::session *ses) { - return (int) ses->get_secure(); + try { + return (int) ses->get_secure(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; + } int tn3270_get_width(h3270::session *ses) { - return (int) ses->get_width(); + + try { + return (int) ses->get_width(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; + } int tn3270_get_height(h3270::session *ses) { - return (int) ses->get_height(); + + try { + return (int) ses->get_height(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; + } int tn3270_get_length(h3270::session *ses) { - return (int) ses->get_length(); + + try { + return (int) ses->get_length(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; + } int tn3270_get_url(h3270::session *ses, char* str, int strlen) { - strncpy(str,ses->get_url().c_str(),strlen); + + try { + + strncpy(str,ses->get_url().c_str(),strlen); + return 0; + + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; + + } + + int tn3270_get_error_message(h3270::session *ses, char* str, int strlen) { + + strncpy(str,tn3270_lasterror.c_str(),strlen); return 0; + } diff --git a/src/native/init.cc b/src/native/init.cc index 3e5816d..4db5deb 100644 --- a/src/native/init.cc +++ b/src/native/init.cc @@ -31,6 +31,8 @@ /*---[ Implement ]----------------------------------------------------------------------------------*/ + std::string tn3270_lasterror = ""; + /** * @brief Cria uma sessão tn3270. * @@ -40,7 +42,12 @@ * */ h3270::session * tn3270_create_session(const char *name) { - return h3270::session::create(name); + try { + return h3270::session::create(name); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return nullptr; } /** @@ -48,7 +55,12 @@ * */ int tn3270_destroy_session(h3270::session *ses) { - delete ses; + try { + delete ses; + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return 0; } diff --git a/src/native/network.cc b/src/native/network.cc index dcf37bd..d1db075 100644 --- a/src/native/network.cc +++ b/src/native/network.cc @@ -32,27 +32,58 @@ /*---[ Implement ]----------------------------------------------------------------------------------*/ int tn3270_connect(h3270::session *ses, const char *host, time_t wait) { - debug("%s(%s,%d)",__FUNCTION__,host,(int) wait); - return ses->connect(host,wait); + try { + debug("%s(%s,%d)",__FUNCTION__,host,(int) wait); + return ses->connect(host,wait); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; } int tn3270_disconnect(h3270::session *ses) { - return ses->disconnect(); + try { + return ses->disconnect(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; } int tn3270_is_connected(h3270::session *ses) { - return (int) ses->is_connected(); + try { + return (int) ses->is_connected(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; } int tn3270_is_ready(h3270::session *ses) { - return (int) ses->is_ready(); + try { + return (int) ses->is_ready(); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; } int tn3270_wait_for_ready(h3270::session *ses, int seconds) { - return (int) ses->wait_for_ready(seconds); + try { + return (int) ses->wait_for_ready(seconds); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; + } int tn3270_wait(h3270::session *ses, int seconds) { - return (int) ses->wait(seconds); + try { + return (int) ses->wait(seconds); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; } diff --git a/src/native/private.h b/src/native/private.h index b31e2eb..1ec70fc 100644 --- a/src/native/private.h +++ b/src/native/private.h @@ -55,6 +55,7 @@ #endif #include + #include #ifdef DEBUG #define debug( fmt, ... ) fprintf(stderr, "%s(%d) " fmt "\n" , __FILE__, (int) __LINE__, __VA_ARGS__ ); fflush(stderr); @@ -66,6 +67,8 @@ #include #include + DLL_PRIVATE std::string tn3270_lasterror; + extern "C" { DLL_PUBLIC h3270::session * tn3270_create_session(const char *name); @@ -83,6 +86,9 @@ DLL_PUBLIC int tn3270_set_url(h3270::session *ses, const char *url); DLL_PUBLIC int tn3270_get_url(h3270::session *ses, char* str, int strlen); + DLL_PUBLIC int tn3270_set_error_message(h3270::session *ses, const char *url); + DLL_PUBLIC int tn3270_get_error_message(h3270::session *ses, char* str, int strlen); + DLL_PUBLIC int tn3270_set_cursor_addr(h3270::session *ses, int addr); DLL_PUBLIC int tn3270_get_cursor_addr(h3270::session *ses); diff --git a/src/native/screen.cc b/src/native/screen.cc index 1246048..c778c75 100644 --- a/src/native/screen.cc +++ b/src/native/screen.cc @@ -33,13 +33,20 @@ int tn3270_get_contents(h3270::session *ses, char* str, int sz) { - std::string contents = ses->get_contents(); + try { - memset(str,0,sz); - strncpy(str,contents.c_str(),sz); - if(contents.size() < ((size_t) sz)) { - str[contents.size()] = 0; - return contents.size(); + std::string contents = ses->get_contents(); + + memset(str,0,sz); + strncpy(str,contents.c_str(),sz); + if(contents.size() < ((size_t) sz)) { + str[contents.size()] = 0; + return contents.size(); + } + + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; } return sz; @@ -47,27 +54,55 @@ int tn3270_get_contents(h3270::session *ses, char* str, int sz) { } int tn3270_get_string(h3270::session *ses, int addr, char* str, int strlen) { - memset(str,0,strlen); - strncpy(str,ses->get_string(addr,strlen).c_str(),strlen); + + try { + memset(str,0,strlen); + strncpy(str,ses->get_string(addr,strlen).c_str(),strlen); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } + return 0; } int tn3270_get_string_at(h3270::session *ses, int row, int col, char* str, int sz) { - memset(str,0,sz+1); - strncpy(str,ses->get_string_at(row,col,sz).c_str(),sz); + try { + memset(str,0,sz+1); + strncpy(str,ses->get_string_at(row,col,sz).c_str(),sz); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return (int) strlen(str); } int tn3270_set_string_at(h3270::session *ses, int row, int col, const char* str) { - debug("%s(%d,%d,\"%s\")",__FUNCTION__,row,col,str); - ses->set_string_at(row,col,str); + try { + debug("%s(%d,%d,\"%s\")",__FUNCTION__,row,col,str); + ses->set_string_at(row,col,str); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return 0; } int tn3270_wait_for_string_at(h3270::session *ses, int row, int col, const char *key, int timeout) { - return ses->wait_for_string_at(row,col,key,timeout); + try { + return ses->wait_for_string_at(row,col,key,timeout); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; + } int tn3270_cmp_string_at(h3270::session *ses, int row, int col, const char* str) { - return ses->cmp_string_at(row,col,str); + try { + return ses->cmp_string_at(row,col,str); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + } + return -1; } diff --git a/src/native/set.cc b/src/native/set.cc index c0c2cad..7249194 100644 --- a/src/native/set.cc +++ b/src/native/set.cc @@ -32,29 +32,57 @@ /*---[ Implement ]----------------------------------------------------------------------------------*/ int tn3270_set_unlock_delay(h3270::session *ses, int ms) { - ses->set_unlock_delay((unsigned short) ms); + try { + ses->set_unlock_delay((unsigned short) ms); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return 0; } int tn3270_set_cursor_position(h3270::session *ses, int row, int col) { - ses->set_cursor_position(row,col); + try { + ses->set_cursor_position(row,col); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return 0; } int tn3270_set_cursor_addr(h3270::session *ses, int addr) { - ses->set_cursor_addr(addr); + try { + ses->set_cursor_addr(addr); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return 0; } int tn3270_set_charset(h3270::session *ses, const char* str) { - debug("%s(%s)",__FUNCTION__,str); - ses->set_display_charset(NULL, str); + try { + ses->set_display_charset(NULL, str); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return 0; } int tn3270_set_url(h3270::session *ses, const char *url) { - debug("%s(%s)",__FUNCTION__,url); - ses->set_url(url); + try { + debug("%s(%s)",__FUNCTION__,url); + ses->set_url(url); + } catch(std::exception &e) { + tn3270_lasterror = e.what(); + return -1; + } return 0; } +int tn3270_set_error_message(h3270::session *ses, const char *str) { + tn3270_lasterror = str; + return 0; +} diff --git a/src/pw3270-sharp/pw3270-sharp.cs b/src/pw3270-sharp/pw3270-sharp.cs index 060f922..28e0f39 100644 --- a/src/pw3270-sharp/pw3270-sharp.cs +++ b/src/pw3270-sharp/pw3270-sharp.cs @@ -140,6 +140,12 @@ namespace pw3270 { [DllImport ("lib3270-mono",CallingConvention=CallingConvention.Cdecl)] extern static int tn3270_get_url(IntPtr Session, StringBuilder str, int strlen); + [DllImport ("lib3270-mono",CallingConvention=CallingConvention.Cdecl)] + extern static int tn3270_set_error_message(IntPtr Session, string str); + + [DllImport ("lib3270-mono",CallingConvention=CallingConvention.Cdecl)] + extern static int tn3270_get_error_message(IntPtr Session, StringBuilder str, int strlen); + /// /// Create a new session with lib3270/pw3270 /// @@ -481,6 +487,17 @@ namespace pw3270 { } } + public string Error { + set { + tn3270_set_error_message(hSession,value); + } + get { + StringBuilder str = new StringBuilder(1025); + tn3270_get_error_message(hSession, str, 1024); + return str.ToString(); + } + } + } } diff --git a/testprograms/sample.cs b/testprograms/sample.cs index 2df6141..c051e2e 100644 --- a/testprograms/sample.cs +++ b/testprograms/sample.cs @@ -51,6 +51,9 @@ class sample { host.Disconnect(); } + + System.Console.WriteLine("Error: " + host.Error); + } -- libgit2 0.21.2