Commit 56fde83fbea652a89a183115700e9f76567e6c9f

Authored by perry.werneck@gmail.com
1 parent e279276f

Melhorando tratamento de erros em windows

src/classlib/exception.cc
... ... @@ -38,22 +38,63 @@
38 38 namespace PW3270_NAMESPACE
39 39 {
40 40  
41   - exception::exception(int syscode)
42   - {
  41 + exception::exception(int syscode)
  42 + {
43 43 snprintf(this->msg,4095,"%s",strerror(syscode));
44   - }
  44 + }
45 45  
46   - exception::exception(const char *fmt, ...)
47   - {
  46 + exception::exception(const char *fmt, ...)
  47 + {
48 48 va_list arg_ptr;
49 49 va_start(arg_ptr, fmt);
50 50 vsnprintf(this->msg,4095,fmt,arg_ptr);
51 51 va_end(arg_ptr);
52   - }
  52 + }
53 53  
54   - const char * exception::what() const throw()
55   - {
  54 +#ifdef WIN32
  55 + exception::exception(DWORD error, const char *fmt, ...)
  56 + {
  57 + LPVOID lpMsgBuf = 0;
  58 + char * ptr;
  59 + size_t szPrefix;
  60 +
  61 + va_list arg_ptr;
  62 + va_start(arg_ptr, fmt);
  63 + vsnprintf(this->msg,4095,fmt,arg_ptr);
  64 + va_end(arg_ptr);
  65 +
  66 + szPrefix = strlen(this->msg);
  67 +
  68 + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
  69 +
  70 + for(ptr= (char *) lpMsgBuf;*ptr && *ptr != '\n';ptr++);
  71 + *ptr = 0;
  72 +
  73 + snprintf(this->msg+szPrefix,4095-szPrefix,": %s (rc=%d)",(char *) lpMsgBuf,(int) error);
  74 +
  75 + LocalFree(lpMsgBuf);
  76 +
  77 + }
  78 +#else
  79 + exception::exception(int error, const char *fmt, ...)
  80 + {
  81 + size_t szPrefix;
  82 +
  83 + va_list arg_ptr;
  84 + va_start(arg_ptr, fmt);
  85 + vsnprintf(this->msg,4095,fmt,arg_ptr);
  86 + va_end(arg_ptr);
  87 +
  88 + szPrefix = strlen(this->msg);
  89 +
  90 + snprintf(this->msg+szPrefix,4095-szPrefix,": %s (rc=%d)",strerror(error),(int) error);
  91 +
  92 + }
  93 +#endif // WIN32
  94 +
  95 + const char * exception::what() const throw()
  96 + {
56 97 return this->msg;
57   - }
  98 + }
58 99  
59 100 }
... ...
src/classlib/remote.cc
... ... @@ -71,9 +71,36 @@
71 71 if(TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL))
72 72 return response.rc;
73 73  
74   - throw exception("Error %d in TransactNamedPipe",GetLastError());
  74 + throw exception(GetLastError(),"%s","Transaction error");
75 75 }
76 76  
  77 + string * query_string(void *query, size_t szQuery, size_t len)
  78 + {
  79 + struct hllapi_packet_text * response;
  80 + DWORD cbSize = sizeof(struct hllapi_packet_text)+len;
  81 + string * s;
  82 +
  83 + response = (struct hllapi_packet_text *) malloc(cbSize+2);
  84 + memset(response,0,cbSize+2);
  85 +
  86 + if(TransactNamedPipe(hPipe,(LPVOID) query, szQuery, &response, cbSize, &cbSize,NULL))
  87 + {
  88 + if(response->packet_id)
  89 + s = new string("");
  90 + else
  91 + s = new string(response->text);
  92 + }
  93 + else
  94 + {
  95 + s = new string("");
  96 + }
  97 +
  98 + free(response);
  99 +
  100 + return s;
  101 + }
  102 +
  103 +
77 104 #elif defined(HAVE_DBUS)
78 105  
79 106 DBusConnection * conn;
... ... @@ -209,7 +236,9 @@
209 236  
210 237 if(!WaitNamedPipe(buffer,NMPWAIT_USE_DEFAULT_WAIT))
211 238 {
212   - throw exception("Invalid service instance: %s",name);
  239 + exception e = exception(GetLastError(),"Service instance %s unavailable",str);
  240 + free(str);
  241 + throw e;
213 242 return;
214 243 }
215 244  
... ... @@ -221,7 +250,7 @@
221 250 }
222 251 else if(!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL))
223 252 {
224   - exception e = exception("%s","Can´t set pipe state");
  253 + exception e = exception(GetLastError(),"%s","Can´t set pipe state");
225 254 CloseHandle(hPipe);
226 255 hPipe = INVALID_HANDLE_VALUE;
227 256 throw e;
... ... @@ -309,6 +338,7 @@
309 338 exception e = exception("DBUS Connection Error (%s)", err.message);
310 339 dbus_error_free(&err);
311 340 throw e;
  341 + return;
312 342 }
313 343  
314 344 if(!conn)
... ... @@ -449,12 +479,12 @@
449 479 if(TransactNamedPipe(hPipe,(LPVOID) pkt, cbSize, &response, sizeof(response), &cbSize,NULL))
450 480 {
451 481 free(pkt);
452   - return response.rc
  482 + return response.rc;
453 483 }
454 484  
455 485 free(pkt);
456 486  
457   - throw exception("Transaction error %d",GetLastError());
  487 + throw exception(GetLastError(),"%s","Transaction error");
458 488  
459 489 #elif defined(HAVE_DBUS)
460 490  
... ... @@ -616,29 +646,9 @@
616 646 {
617 647 #if defined(WIN32)
618 648  
619   - struct hllapi_packet_query_at query = { HLLAPI_PACKET_GET_TEXT_AT, (unsigned short) row, (unsigned short) col, (unsigned short) sz };
620   - struct hllapi_packet_text * response;
621   - DWORD cbSize = sizeof(struct hllapi_packet_text)+sz;
622   - string * s;
623   -
624   - response = (struct hllapi_packet_text *) malloc(cbSize+2);
625   - memset(response,0,cbSize+2);
626   -
627   - if(TransactNamedPipe(hPipe,(LPVOID) &query, sizeof(struct hllapi_packet_query_at), &response, cbSize, &cbSize,NULL))
628   - {
629   - if(response->packet_id)
630   - s = new string("");
631   - else
632   - s = new string(response->text);
633   - }
634   - else
635   - {
636   - s = new string("");
637   - }
638   -
639   - free(response);
  649 + struct hllapi_packet_query_at query = { HLLAPI_PACKET_GET_TEXT_AT, (unsigned short) row, (unsigned short) col, (unsigned short) sz };
640 650  
641   - return s;
  651 + return query_string(&query,sizeof(query),sz);
642 652  
643 653 #elif defined(HAVE_DBUS)
644 654  
... ... @@ -766,8 +776,13 @@
766 776  
767 777 string * get_text(int baddr, size_t len)
768 778 {
  779 +#if defined(WIN32)
  780 + struct hllapi_packet_query_offset query = { HLLAPI_PACKET_GET_TEXT_AT_OFFSET, (unsigned short) baddr, (unsigned short) len };
  781 + return query_string(&query,sizeof(query),len);
  782 +#else
769 783 #warning IMPLEMENTAR
770 784 return NULL;
  785 +#endif
771 786 }
772 787  
773 788  
... ...
src/include/pw3270/class.h
... ... @@ -64,6 +64,10 @@
64 64 exception(int syserror = errno);
65 65 exception(const char *fmt, ...);
66 66  
  67 +#ifdef WIN32
  68 + exception(DWORD error, const char *fmt, ...);
  69 +#endif // WIN32
  70 +
67 71 virtual const char * what() const throw();
68 72  
69 73 private:
... ... @@ -71,6 +75,7 @@
71 75  
72 76 };
73 77  
  78 +
74 79 #if defined (HAVE_GNUC_VISIBILITY)
75 80 class __attribute__((visibility("default"))) session
76 81 #elif defined(WIN32)
... ...