Commit fbf736303ba0e768016939c8eeb6c482a3410caf

Authored by Perry Werneck
1 parent ed9d4bf9
Exists in master

Melhorando tratamento de excessões.

pw3270-sharp.cbp
@@ -50,7 +50,7 @@ @@ -50,7 +50,7 @@
50 <Unit filename="src/native/private.h" /> 50 <Unit filename="src/native/private.h" />
51 <Unit filename="src/native/screen.cc" /> 51 <Unit filename="src/native/screen.cc" />
52 <Unit filename="src/native/set.cc" /> 52 <Unit filename="src/native/set.cc" />
53 - <Unit filename="src/tn3270-sharp/tn3270-sharp.cs" /> 53 + <Unit filename="src/pw3270-sharp/pw3270-sharp.cs" />
54 <Extensions> 54 <Extensions>
55 <code_completion /> 55 <code_completion />
56 <envvars /> 56 <envvars />
src/native/actions.cc
@@ -32,34 +32,74 @@ @@ -32,34 +32,74 @@
32 /*---[ Implement ]----------------------------------------------------------------------------------*/ 32 /*---[ Implement ]----------------------------------------------------------------------------------*/
33 33
34 int tn3270_enter(h3270::session *ses) { 34 int tn3270_enter(h3270::session *ses) {
35 - return ses->enter(); 35 + try {
  36 + return ses->enter();
  37 + } catch(std::exception &e) {
  38 + tn3270_lasterror = e.what();
  39 + return -1;
  40 + }
36 } 41 }
37 42
38 int tn3270_pfkey(h3270::session *ses, int key) { 43 int tn3270_pfkey(h3270::session *ses, int key) {
39 - return ses->pfkey(key); 44 + try {
  45 + return ses->pfkey(key);
  46 + } catch(std::exception &e) {
  47 + tn3270_lasterror = e.what();
  48 + return -1;
  49 + }
40 } 50 }
41 51
42 int tn3270_pakey(h3270::session *ses, int key) { 52 int tn3270_pakey(h3270::session *ses, int key) {
43 - return ses->pakey(key); 53 + try {
  54 + return ses->pakey(key);
  55 + } catch(std::exception &e) {
  56 + tn3270_lasterror = e.what();
  57 + return -1;
  58 + }
44 } 59 }
45 60
46 int tn3270_action(h3270::session *ses, const char *name) { 61 int tn3270_action(h3270::session *ses, const char *name) {
47 - return ses->action(name); 62 + try {
  63 + return ses->action(name);
  64 + } catch(std::exception &e) {
  65 + tn3270_lasterror = e.what();
  66 + return -1;
  67 + }
48 } 68 }
49 69
50 int tn3270_erase(h3270::session *ses) { 70 int tn3270_erase(h3270::session *ses) {
51 - return ses->erase(); 71 + try {
  72 + return ses->erase();
  73 + } catch(std::exception &e) {
  74 + tn3270_lasterror = e.what();
  75 + return -1;
  76 + }
52 } 77 }
53 78
54 int tn3270_erase_eof(h3270::session *ses) { 79 int tn3270_erase_eof(h3270::session *ses) {
55 - return ses->erase_eof(); 80 + try {
  81 + return ses->erase_eof();
  82 + } catch(std::exception &e) {
  83 + tn3270_lasterror = e.what();
  84 + return -1;
  85 + }
56 } 86 }
57 87
58 int tn3270_erase_eol(h3270::session *ses) { 88 int tn3270_erase_eol(h3270::session *ses) {
59 - return ses->erase_eol(); 89 + try {
  90 + return ses->erase_eol();
  91 + } catch(std::exception &e) {
  92 + tn3270_lasterror = e.what();
  93 + return -1;
  94 + }
60 } 95 }
61 96
62 int tn3270_erase_input(h3270::session *ses) { 97 int tn3270_erase_input(h3270::session *ses) {
63 - return ses->erase_input(); 98 + try {
  99 + return ses->erase_input();
  100 + } catch(std::exception &e) {
  101 + tn3270_lasterror = e.what();
  102 + return -1;
  103 + }
64 } 104 }
65 105
src/native/get.cc
@@ -37,7 +37,12 @@ @@ -37,7 +37,12 @@
37 * 37 *
38 */ 38 */
39 int tn3270_get_version(h3270::session *ses, char* str, int strlen) { 39 int tn3270_get_version(h3270::session *ses, char* str, int strlen) {
40 - strncpy(str,ses->get_version().c_str(),strlen); 40 + try {
  41 + strncpy(str,ses->get_version().c_str(),strlen);
  42 + } catch(std::exception &e) {
  43 + tn3270_lasterror = e.what();
  44 + return -1;
  45 + }
41 return 0; 46 return 0;
42 } 47 }
43 48
@@ -46,35 +51,98 @@ @@ -46,35 +51,98 @@
46 * 51 *
47 */ 52 */
48 int tn3270_get_revision(h3270::session *ses, char* str, int strlen) { 53 int tn3270_get_revision(h3270::session *ses, char* str, int strlen) {
49 - strncpy(str,ses->get_revision().c_str(),strlen); 54 +
  55 + try {
  56 + strncpy(str,ses->get_revision().c_str(),strlen);
  57 + } catch(std::exception &e) {
  58 + tn3270_lasterror = e.what();
  59 + return -1;
  60 + }
50 return 0; 61 return 0;
51 } 62 }
52 63
53 int tn3270_get_cstate(h3270::session *ses) { 64 int tn3270_get_cstate(h3270::session *ses) {
54 - return (int) ses->get_cstate(); 65 +
  66 + try {
  67 + return (int) ses->get_cstate();
  68 + } catch(std::exception &e) {
  69 + tn3270_lasterror = e.what();
  70 + }
  71 + return -1;
  72 +
55 } 73 }
56 74
57 int tn3270_get_program_message(h3270::session *ses) { 75 int tn3270_get_program_message(h3270::session *ses) {
58 - return (int) ses->get_program_message(); 76 +
  77 + try {
  78 + return (int) ses->get_program_message();
  79 + } catch(std::exception &e) {
  80 + tn3270_lasterror = e.what();
  81 + }
  82 + return -1;
  83 +
59 } 84 }
60 85
61 int tn3270_get_secure(h3270::session *ses) { 86 int tn3270_get_secure(h3270::session *ses) {
62 - return (int) ses->get_secure(); 87 + try {
  88 + return (int) ses->get_secure();
  89 + } catch(std::exception &e) {
  90 + tn3270_lasterror = e.what();
  91 + }
  92 + return -1;
  93 +
63 } 94 }
64 95
65 int tn3270_get_width(h3270::session *ses) { 96 int tn3270_get_width(h3270::session *ses) {
66 - return (int) ses->get_width(); 97 +
  98 + try {
  99 + return (int) ses->get_width();
  100 + } catch(std::exception &e) {
  101 + tn3270_lasterror = e.what();
  102 + }
  103 + return -1;
  104 +
67 } 105 }
68 106
69 int tn3270_get_height(h3270::session *ses) { 107 int tn3270_get_height(h3270::session *ses) {
70 - return (int) ses->get_height(); 108 +
  109 + try {
  110 + return (int) ses->get_height();
  111 + } catch(std::exception &e) {
  112 + tn3270_lasterror = e.what();
  113 + }
  114 + return -1;
  115 +
71 } 116 }
72 117
73 int tn3270_get_length(h3270::session *ses) { 118 int tn3270_get_length(h3270::session *ses) {
74 - return (int) ses->get_length(); 119 +
  120 + try {
  121 + return (int) ses->get_length();
  122 + } catch(std::exception &e) {
  123 + tn3270_lasterror = e.what();
  124 + }
  125 + return -1;
  126 +
75 } 127 }
76 128
77 int tn3270_get_url(h3270::session *ses, char* str, int strlen) { 129 int tn3270_get_url(h3270::session *ses, char* str, int strlen) {
78 - strncpy(str,ses->get_url().c_str(),strlen); 130 +
  131 + try {
  132 +
  133 + strncpy(str,ses->get_url().c_str(),strlen);
  134 + return 0;
  135 +
  136 + } catch(std::exception &e) {
  137 + tn3270_lasterror = e.what();
  138 + }
  139 + return -1;
  140 +
  141 + }
  142 +
  143 + int tn3270_get_error_message(h3270::session *ses, char* str, int strlen) {
  144 +
  145 + strncpy(str,tn3270_lasterror.c_str(),strlen);
79 return 0; 146 return 0;
  147 +
80 } 148 }
src/native/init.cc
@@ -31,6 +31,8 @@ @@ -31,6 +31,8 @@
31 31
32 /*---[ Implement ]----------------------------------------------------------------------------------*/ 32 /*---[ Implement ]----------------------------------------------------------------------------------*/
33 33
  34 + std::string tn3270_lasterror = "";
  35 +
34 /** 36 /**
35 * @brief Cria uma sessão tn3270. 37 * @brief Cria uma sessão tn3270.
36 * 38 *
@@ -40,7 +42,12 @@ @@ -40,7 +42,12 @@
40 * 42 *
41 */ 43 */
42 h3270::session * tn3270_create_session(const char *name) { 44 h3270::session * tn3270_create_session(const char *name) {
43 - return h3270::session::create(name); 45 + try {
  46 + return h3270::session::create(name);
  47 + } catch(std::exception &e) {
  48 + tn3270_lasterror = e.what();
  49 + }
  50 + return nullptr;
44 } 51 }
45 52
46 /** 53 /**
@@ -48,7 +55,12 @@ @@ -48,7 +55,12 @@
48 * 55 *
49 */ 56 */
50 int tn3270_destroy_session(h3270::session *ses) { 57 int tn3270_destroy_session(h3270::session *ses) {
51 - delete ses; 58 + try {
  59 + delete ses;
  60 + } catch(std::exception &e) {
  61 + tn3270_lasterror = e.what();
  62 + return -1;
  63 + }
52 return 0; 64 return 0;
53 } 65 }
54 66
src/native/network.cc
@@ -32,27 +32,58 @@ @@ -32,27 +32,58 @@
32 /*---[ Implement ]----------------------------------------------------------------------------------*/ 32 /*---[ Implement ]----------------------------------------------------------------------------------*/
33 33
34 int tn3270_connect(h3270::session *ses, const char *host, time_t wait) { 34 int tn3270_connect(h3270::session *ses, const char *host, time_t wait) {
35 - debug("%s(%s,%d)",__FUNCTION__,host,(int) wait);  
36 - return ses->connect(host,wait); 35 + try {
  36 + debug("%s(%s,%d)",__FUNCTION__,host,(int) wait);
  37 + return ses->connect(host,wait);
  38 + } catch(std::exception &e) {
  39 + tn3270_lasterror = e.what();
  40 + }
  41 + return -1;
37 } 42 }
38 43
39 int tn3270_disconnect(h3270::session *ses) { 44 int tn3270_disconnect(h3270::session *ses) {
40 - return ses->disconnect(); 45 + try {
  46 + return ses->disconnect();
  47 + } catch(std::exception &e) {
  48 + tn3270_lasterror = e.what();
  49 + }
  50 + return -1;
41 } 51 }
42 52
43 int tn3270_is_connected(h3270::session *ses) { 53 int tn3270_is_connected(h3270::session *ses) {
44 - return (int) ses->is_connected(); 54 + try {
  55 + return (int) ses->is_connected();
  56 + } catch(std::exception &e) {
  57 + tn3270_lasterror = e.what();
  58 + }
  59 + return -1;
45 } 60 }
46 61
47 int tn3270_is_ready(h3270::session *ses) { 62 int tn3270_is_ready(h3270::session *ses) {
48 - return (int) ses->is_ready(); 63 + try {
  64 + return (int) ses->is_ready();
  65 + } catch(std::exception &e) {
  66 + tn3270_lasterror = e.what();
  67 + }
  68 + return -1;
49 } 69 }
50 70
51 int tn3270_wait_for_ready(h3270::session *ses, int seconds) { 71 int tn3270_wait_for_ready(h3270::session *ses, int seconds) {
52 - return (int) ses->wait_for_ready(seconds); 72 + try {
  73 + return (int) ses->wait_for_ready(seconds);
  74 + } catch(std::exception &e) {
  75 + tn3270_lasterror = e.what();
  76 + }
  77 + return -1;
  78 +
53 } 79 }
54 80
55 int tn3270_wait(h3270::session *ses, int seconds) { 81 int tn3270_wait(h3270::session *ses, int seconds) {
56 - return (int) ses->wait(seconds); 82 + try {
  83 + return (int) ses->wait(seconds);
  84 + } catch(std::exception &e) {
  85 + tn3270_lasterror = e.what();
  86 + }
  87 + return -1;
57 } 88 }
58 89
src/native/private.h
@@ -55,6 +55,7 @@ @@ -55,6 +55,7 @@
55 #endif 55 #endif
56 56
57 #include <cstdio> 57 #include <cstdio>
  58 + #include <string>
58 59
59 #ifdef DEBUG 60 #ifdef DEBUG
60 #define debug( fmt, ... ) fprintf(stderr, "%s(%d) " fmt "\n" , __FILE__, (int) __LINE__, __VA_ARGS__ ); fflush(stderr); 61 #define debug( fmt, ... ) fprintf(stderr, "%s(%d) " fmt "\n" , __FILE__, (int) __LINE__, __VA_ARGS__ ); fflush(stderr);
@@ -66,6 +67,8 @@ @@ -66,6 +67,8 @@
66 #include <cerrno> 67 #include <cerrno>
67 #include <cstring> 68 #include <cstring>
68 69
  70 + DLL_PRIVATE std::string tn3270_lasterror;
  71 +
69 extern "C" { 72 extern "C" {
70 73
71 DLL_PUBLIC h3270::session * tn3270_create_session(const char *name); 74 DLL_PUBLIC h3270::session * tn3270_create_session(const char *name);
@@ -83,6 +86,9 @@ @@ -83,6 +86,9 @@
83 DLL_PUBLIC int tn3270_set_url(h3270::session *ses, const char *url); 86 DLL_PUBLIC int tn3270_set_url(h3270::session *ses, const char *url);
84 DLL_PUBLIC int tn3270_get_url(h3270::session *ses, char* str, int strlen); 87 DLL_PUBLIC int tn3270_get_url(h3270::session *ses, char* str, int strlen);
85 88
  89 + DLL_PUBLIC int tn3270_set_error_message(h3270::session *ses, const char *url);
  90 + DLL_PUBLIC int tn3270_get_error_message(h3270::session *ses, char* str, int strlen);
  91 +
86 DLL_PUBLIC int tn3270_set_cursor_addr(h3270::session *ses, int addr); 92 DLL_PUBLIC int tn3270_set_cursor_addr(h3270::session *ses, int addr);
87 DLL_PUBLIC int tn3270_get_cursor_addr(h3270::session *ses); 93 DLL_PUBLIC int tn3270_get_cursor_addr(h3270::session *ses);
88 94
src/native/screen.cc
@@ -33,13 +33,20 @@ @@ -33,13 +33,20 @@
33 33
34 int tn3270_get_contents(h3270::session *ses, char* str, int sz) { 34 int tn3270_get_contents(h3270::session *ses, char* str, int sz) {
35 35
36 - std::string contents = ses->get_contents(); 36 + try {
37 37
38 - memset(str,0,sz);  
39 - strncpy(str,contents.c_str(),sz);  
40 - if(contents.size() < ((size_t) sz)) {  
41 - str[contents.size()] = 0;  
42 - return contents.size(); 38 + std::string contents = ses->get_contents();
  39 +
  40 + memset(str,0,sz);
  41 + strncpy(str,contents.c_str(),sz);
  42 + if(contents.size() < ((size_t) sz)) {
  43 + str[contents.size()] = 0;
  44 + return contents.size();
  45 + }
  46 +
  47 + } catch(std::exception &e) {
  48 + tn3270_lasterror = e.what();
  49 + return -1;
43 } 50 }
44 51
45 return sz; 52 return sz;
@@ -47,27 +54,55 @@ int tn3270_get_contents(h3270::session *ses, char* str, int sz) { @@ -47,27 +54,55 @@ int tn3270_get_contents(h3270::session *ses, char* str, int sz) {
47 } 54 }
48 55
49 int tn3270_get_string(h3270::session *ses, int addr, char* str, int strlen) { 56 int tn3270_get_string(h3270::session *ses, int addr, char* str, int strlen) {
50 - memset(str,0,strlen);  
51 - strncpy(str,ses->get_string(addr,strlen).c_str(),strlen); 57 +
  58 + try {
  59 + memset(str,0,strlen);
  60 + strncpy(str,ses->get_string(addr,strlen).c_str(),strlen);
  61 + } catch(std::exception &e) {
  62 + tn3270_lasterror = e.what();
  63 + return -1;
  64 + }
  65 +
52 return 0; 66 return 0;
53 } 67 }
54 68
55 int tn3270_get_string_at(h3270::session *ses, int row, int col, char* str, int sz) { 69 int tn3270_get_string_at(h3270::session *ses, int row, int col, char* str, int sz) {
56 - memset(str,0,sz+1);  
57 - strncpy(str,ses->get_string_at(row,col,sz).c_str(),sz); 70 + try {
  71 + memset(str,0,sz+1);
  72 + strncpy(str,ses->get_string_at(row,col,sz).c_str(),sz);
  73 + } catch(std::exception &e) {
  74 + tn3270_lasterror = e.what();
  75 + return -1;
  76 + }
58 return (int) strlen(str); 77 return (int) strlen(str);
59 } 78 }
60 79
61 int tn3270_set_string_at(h3270::session *ses, int row, int col, const char* str) { 80 int tn3270_set_string_at(h3270::session *ses, int row, int col, const char* str) {
62 - debug("%s(%d,%d,\"%s\")",__FUNCTION__,row,col,str);  
63 - ses->set_string_at(row,col,str); 81 + try {
  82 + debug("%s(%d,%d,\"%s\")",__FUNCTION__,row,col,str);
  83 + ses->set_string_at(row,col,str);
  84 + } catch(std::exception &e) {
  85 + tn3270_lasterror = e.what();
  86 + return -1;
  87 + }
64 return 0; 88 return 0;
65 } 89 }
66 90
67 int tn3270_wait_for_string_at(h3270::session *ses, int row, int col, const char *key, int timeout) { 91 int tn3270_wait_for_string_at(h3270::session *ses, int row, int col, const char *key, int timeout) {
68 - return ses->wait_for_string_at(row,col,key,timeout); 92 + try {
  93 + return ses->wait_for_string_at(row,col,key,timeout);
  94 + } catch(std::exception &e) {
  95 + tn3270_lasterror = e.what();
  96 + }
  97 + return -1;
  98 +
69 } 99 }
70 100
71 int tn3270_cmp_string_at(h3270::session *ses, int row, int col, const char* str) { 101 int tn3270_cmp_string_at(h3270::session *ses, int row, int col, const char* str) {
72 - return ses->cmp_string_at(row,col,str); 102 + try {
  103 + return ses->cmp_string_at(row,col,str);
  104 + } catch(std::exception &e) {
  105 + tn3270_lasterror = e.what();
  106 + }
  107 + return -1;
73 } 108 }
src/native/set.cc
@@ -32,29 +32,57 @@ @@ -32,29 +32,57 @@
32 /*---[ Implement ]----------------------------------------------------------------------------------*/ 32 /*---[ Implement ]----------------------------------------------------------------------------------*/
33 33
34 int tn3270_set_unlock_delay(h3270::session *ses, int ms) { 34 int tn3270_set_unlock_delay(h3270::session *ses, int ms) {
35 - ses->set_unlock_delay((unsigned short) ms); 35 + try {
  36 + ses->set_unlock_delay((unsigned short) ms);
  37 + } catch(std::exception &e) {
  38 + tn3270_lasterror = e.what();
  39 + return -1;
  40 + }
36 return 0; 41 return 0;
37 } 42 }
38 43
39 int tn3270_set_cursor_position(h3270::session *ses, int row, int col) { 44 int tn3270_set_cursor_position(h3270::session *ses, int row, int col) {
40 - ses->set_cursor_position(row,col); 45 + try {
  46 + ses->set_cursor_position(row,col);
  47 + } catch(std::exception &e) {
  48 + tn3270_lasterror = e.what();
  49 + return -1;
  50 + }
41 return 0; 51 return 0;
42 } 52 }
43 53
44 int tn3270_set_cursor_addr(h3270::session *ses, int addr) { 54 int tn3270_set_cursor_addr(h3270::session *ses, int addr) {
45 - ses->set_cursor_addr(addr); 55 + try {
  56 + ses->set_cursor_addr(addr);
  57 + } catch(std::exception &e) {
  58 + tn3270_lasterror = e.what();
  59 + return -1;
  60 + }
46 return 0; 61 return 0;
47 } 62 }
48 63
49 int tn3270_set_charset(h3270::session *ses, const char* str) { 64 int tn3270_set_charset(h3270::session *ses, const char* str) {
50 - debug("%s(%s)",__FUNCTION__,str);  
51 - ses->set_display_charset(NULL, str); 65 + try {
  66 + ses->set_display_charset(NULL, str);
  67 + } catch(std::exception &e) {
  68 + tn3270_lasterror = e.what();
  69 + return -1;
  70 + }
52 return 0; 71 return 0;
53 } 72 }
54 73
55 int tn3270_set_url(h3270::session *ses, const char *url) { 74 int tn3270_set_url(h3270::session *ses, const char *url) {
56 - debug("%s(%s)",__FUNCTION__,url);  
57 - ses->set_url(url); 75 + try {
  76 + debug("%s(%s)",__FUNCTION__,url);
  77 + ses->set_url(url);
  78 + } catch(std::exception &e) {
  79 + tn3270_lasterror = e.what();
  80 + return -1;
  81 + }
58 return 0; 82 return 0;
59 } 83 }
60 84
  85 +int tn3270_set_error_message(h3270::session *ses, const char *str) {
  86 + tn3270_lasterror = str;
  87 + return 0;
  88 +}
src/pw3270-sharp/pw3270-sharp.cs
@@ -140,6 +140,12 @@ namespace pw3270 { @@ -140,6 +140,12 @@ namespace pw3270 {
140 [DllImport ("lib3270-mono",CallingConvention=CallingConvention.Cdecl)] 140 [DllImport ("lib3270-mono",CallingConvention=CallingConvention.Cdecl)]
141 extern static int tn3270_get_url(IntPtr Session, StringBuilder str, int strlen); 141 extern static int tn3270_get_url(IntPtr Session, StringBuilder str, int strlen);
142 142
  143 + [DllImport ("lib3270-mono",CallingConvention=CallingConvention.Cdecl)]
  144 + extern static int tn3270_set_error_message(IntPtr Session, string str);
  145 +
  146 + [DllImport ("lib3270-mono",CallingConvention=CallingConvention.Cdecl)]
  147 + extern static int tn3270_get_error_message(IntPtr Session, StringBuilder str, int strlen);
  148 +
143 /// <summary> 149 /// <summary>
144 /// Create a new session with lib3270/pw3270 150 /// Create a new session with lib3270/pw3270
145 /// </summary> 151 /// </summary>
@@ -481,6 +487,17 @@ namespace pw3270 { @@ -481,6 +487,17 @@ namespace pw3270 {
481 } 487 }
482 } 488 }
483 489
  490 + public string Error {
  491 + set {
  492 + tn3270_set_error_message(hSession,value);
  493 + }
  494 + get {
  495 + StringBuilder str = new StringBuilder(1025);
  496 + tn3270_get_error_message(hSession, str, 1024);
  497 + return str.ToString();
  498 + }
  499 + }
  500 +
484 } 501 }
485 502
486 } 503 }
testprograms/sample.cs
@@ -51,6 +51,9 @@ class sample { @@ -51,6 +51,9 @@ class sample {
51 51
52 host.Disconnect(); 52 host.Disconnect();
53 } 53 }
  54 +
  55 + System.Console.WriteLine("Error: " + host.Error);
  56 +
54 57
55 } 58 }
56 59