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