Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2005 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 | #include <tchar.h> |
| 21 | #include <winsock2.h> |
| 22 | #include <windows.h> |
| 23 | #endif |
| 24 | |
| 25 | using namespace rdr; |
| 26 | |
| 27 | SystemException::SystemException(const char* s, int err_) |
| 28 | : Exception(s), err(err_) |
| 29 | { |
| 30 | strncat(str_, ": ", len-1-strlen(str_)); |
| 31 | #ifdef _WIN32 |
| 32 | // Windows error messages are crap, so use unix ones for common errors. |
| 33 | const char* msg = 0; |
| 34 | switch (err) { |
| 35 | case WSAECONNREFUSED: msg = "Connection refused"; break; |
| 36 | case WSAETIMEDOUT: msg = "Connection timed out"; break; |
| 37 | case WSAECONNRESET: msg = "Connection reset by peer"; break; |
| 38 | case WSAECONNABORTED: msg = "Connection aborted"; break; |
| 39 | } |
| 40 | if (msg) { |
| 41 | strncat(str_, msg, len-1-strlen(str_)); |
| 42 | } else { |
| 43 | #ifdef UNICODE |
| 44 | WCHAR* tmsg = new WCHAR[len-strlen(str_)]; |
| 45 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 46 | 0, err, 0, tmsg, len-1-strlen(str_), 0); |
| 47 | WideCharToMultiByte(CP_ACP, 0, tmsg, wcslen(tmsg)+1, |
| 48 | str_+strlen(str_), len-strlen(str_), 0, 0); |
| 49 | delete [] tmsg; |
| 50 | #else |
| 51 | char* currStr = str_+strlen(str_); |
| 52 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 53 | 0, err, 0, currStr, len-1-strlen(str_), 0); |
| 54 | #endif |
| 55 | int l = strlen(str_); |
| 56 | if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n')) |
| 57 | str_[l-2] = 0; |
| 58 | } |
| 59 | |
| 60 | #else |
| 61 | strncat(str_, strerror(err), len-1-strlen(str_)); |
| 62 | #endif |
| 63 | strncat(str_, " (", len-1-strlen(str_)); |
| 64 | char buf[20]; |
| 65 | #ifdef WIN32 |
| 66 | if (err < 0) |
| 67 | sprintf(buf, "%x", err); |
| 68 | else |
| 69 | #endif |
| 70 | sprintf(buf,"%d",err); |
| 71 | strncat(str_, buf, len-1-strlen(str_)); |
| 72 | strncat(str_, ")", len-1-strlen(str_)); |
| 73 | } |