blob: 7d38711919def4cac7e3dc2b61acca394732247c [file] [log] [blame]
Constantin Kaplinskyb0f89f82005-09-28 10:37:29 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 * 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
Constantin Kaplinskyb0f89f82005-09-28 10:37:29 +000020#include <tchar.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000021#include <winsock2.h>
Constantin Kaplinsky4ae8b8e2005-09-28 16:46:56 +000022#include <windows.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000023#endif
24
25using namespace rdr;
26
27SystemException::SystemException(const char* s, int err_)
Constantin Kaplinskyb0f89f82005-09-28 10:37:29 +000028 : Exception(s), err(err_)
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000029{
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
Constantin Kaplinskyb0f89f82005-09-28 10:37:29 +000051 char* currStr = str_+strlen(str_);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000052 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
Constantin Kaplinskyb0f89f82005-09-28 10:37:29 +000053 0, err, 0, currStr, len-1-strlen(str_), 0);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000054#endif
Constantin Kaplinskyb0f89f82005-09-28 10:37:29 +000055 int l = strlen(str_);
56 if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
57 str_[l-2] = 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000058 }
Constantin Kaplinskyb0f89f82005-09-28 10:37:29 +000059
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000060#else
61 strncat(str_, strerror(err), len-1-strlen(str_));
62#endif
63 strncat(str_, " (", len-1-strlen(str_));
64 char buf[20];
Constantin Kaplinskyb0f89f82005-09-28 10:37:29 +000065#ifdef WIN32
66 if (err < 0)
67 sprintf(buf, "%x", err);
68 else
69#endif
70 sprintf(buf,"%d",err);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000071 strncat(str_, buf, len-1-strlen(str_));
72 strncat(str_, ")", len-1-strlen(str_));
73}