blob: 72a03e1147d03207406b8da78d42f8c273a047f8 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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
Adam Tkac20e0d712008-11-14 14:48:21 +000025#include <os/print.h>
26
Adam Tkac75586e62008-12-03 15:03:08 +000027#include <string.h>
28
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000029using namespace rdr;
30
Adam Tkac20e0d712008-11-14 14:48:21 +000031Exception::Exception(const char *format, ...) {
32 va_list ap;
33
34 va_start(ap, format);
35 (void) vsnprintf(str_, len, format, ap);
36 va_end(ap);
37}
38
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000039SystemException::SystemException(const char* s, int err_)
40 : Exception(s), err(err_)
41{
42 strncat(str_, ": ", len-1-strlen(str_));
43#ifdef _WIN32
44 // Windows error messages are crap, so use unix ones for common errors.
45 const char* msg = 0;
46 switch (err) {
47 case WSAECONNREFUSED: msg = "Connection refused"; break;
48 case WSAETIMEDOUT: msg = "Connection timed out"; break;
49 case WSAECONNRESET: msg = "Connection reset by peer"; break;
50 case WSAECONNABORTED: msg = "Connection aborted"; break;
51 }
52 if (msg) {
53 strncat(str_, msg, len-1-strlen(str_));
54 } else {
55#ifdef UNICODE
56 WCHAR* tmsg = new WCHAR[len-strlen(str_)];
57 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
58 0, err, 0, tmsg, len-1-strlen(str_), 0);
59 WideCharToMultiByte(CP_ACP, 0, tmsg, wcslen(tmsg)+1,
60 str_+strlen(str_), len-strlen(str_), 0, 0);
61 delete [] tmsg;
62#else
63 char* currStr = str_+strlen(str_);
64 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
65 0, err, 0, currStr, len-1-strlen(str_), 0);
66#endif
67 int l = strlen(str_);
68 if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
69 str_[l-2] = 0;
70 }
71
72#else
73 strncat(str_, strerror(err), len-1-strlen(str_));
74#endif
75 strncat(str_, " (", len-1-strlen(str_));
76 char buf[20];
77#ifdef WIN32
78 if (err < 0)
79 sprintf(buf, "%x", err);
80 else
81#endif
82 sprintf(buf,"%d",err);
83 strncat(str_, buf, len-1-strlen(str_));
84 strncat(str_, ")", len-1-strlen(str_));
85}