You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Exception.cxx 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (C) 2002-2003 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <rdr/Exception.h>
  19. #ifdef _WIN32
  20. #define WIN32_LEAN_AND_MEAN
  21. #include <windows.h>
  22. #include <winsock2.h>
  23. #endif
  24. using namespace rdr;
  25. SystemException::SystemException(const char* s, int err_)
  26. : Exception(s, "rdr::SystemException"), err(err_)
  27. {
  28. strncat(str_, ": ", len-1-strlen(str_));
  29. #ifdef _WIN32
  30. // Windows error messages are crap, so use unix ones for common errors.
  31. const char* msg = 0;
  32. switch (err) {
  33. case WSAECONNREFUSED: msg = "Connection refused"; break;
  34. case WSAETIMEDOUT: msg = "Connection timed out"; break;
  35. case WSAECONNRESET: msg = "Connection reset by peer"; break;
  36. case WSAECONNABORTED: msg = "Connection aborted"; break;
  37. }
  38. if (msg) {
  39. strncat(str_, msg, len-1-strlen(str_));
  40. } else {
  41. #ifdef UNICODE
  42. WCHAR* tmsg = new WCHAR[len-strlen(str_)];
  43. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  44. 0, err, 0, tmsg, len-1-strlen(str_), 0);
  45. WideCharToMultiByte(CP_ACP, 0, tmsg, wcslen(tmsg)+1,
  46. str_+strlen(str_), len-strlen(str_), 0, 0);
  47. delete [] tmsg;
  48. #else
  49. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  50. 0, err, 0, str_+strlen(str_), len-1-strlen(str_), 0);
  51. #endif
  52. }
  53. #else
  54. strncat(str_, strerror(err), len-1-strlen(str_));
  55. #endif
  56. strncat(str_, " (", len-1-strlen(str_));
  57. char buf[20];
  58. sprintf(buf,"%d",err);
  59. strncat(str_, buf, len-1-strlen(str_));
  60. strncat(str_, ")", len-1-strlen(str_));
  61. }