OWLNext
7.0
Borland's Object Windows Library for the modern age
Toggle main menu visibility
Loading...
Searching...
No Matches
wskerr.cpp
Go to the documentation of this file.
1
//----------------------------------------------------------------------------
2
// ObjectWindows
3
// Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
4
//
5
/// \file
6
/// Winsock for OWL subsystem.
7
/// Based on work by Paul Pedriana, 70541.3223@compuserve.com
8
//----------------------------------------------------------------------------
9
#include <
owl/pch.h
>
10
#include <
owl/defs.h
>
11
#include <
owl/module.h
>
12
#include <
owl/winsock.h
>
13
#include <owl/winsock.rh>
14
#include <stdio.h>
15
16
namespace
owl
{
17
18
OWL_DIAGINFO
;
19
20
//
21
/// This function constructs the object with the error code and the size of the
22
/// buffer to allocate.
23
//
24
TSocketError::TSocketError
(
int
error,
unsigned
sizeToAllocate)
25
:
26
Error
(error),
27
SizeToAllocate
(sizeToAllocate),
28
String
(0)
29
{
30
GetErrorString
();
31
}
32
33
//
34
/// This function destroys the allocated string.
35
//
36
TSocketError::~TSocketError
()
37
{
38
delete
[]
String
;
39
}
40
41
//
42
/// Copies the error code and string.
43
//
44
TSocketError::TSocketError
(
const
TSocketError
& src)
45
:
46
Error
(src.
Error
),
47
String
(
strnewdup
(src.
String
)),
48
SizeToAllocate
(src.
SizeToAllocate
)
49
{
50
}
51
52
//
53
/// This function copies the error code and string.
54
//
55
TSocketError
&
TSocketError::operator =
(
const
TSocketError
& src)
56
{
57
Error
= src.
Error
;
58
delete
[]
String
;
59
String
=
strnewdup
(src.
String
);
60
SizeToAllocate
= src.
SizeToAllocate
;
61
62
return
*
this
;
63
}
64
65
//
66
/// This function tests for equality between two TSocketError objects. The important
67
/// criteria for determining equality is the error value. The string is unimportant.
68
//
69
bool
operator ==
(
const
TSocketError
& socketError1,
70
const
TSocketError
& socketError2)
71
{
72
return
socketError1.
Error
== socketError2.
Error
;
73
}
74
75
//
76
/// This function initializes the error code.
77
//
78
void
TSocketError::Init
(
int
error)
79
{
80
Error
= error;
81
GetErrorString
();
82
}
83
84
//
85
/// This function returns the error code.
86
//
87
int
TSocketError::GetReasonValue
()
const
88
{
89
return
Error
;
90
}
91
92
//
93
/// This function hands the pointer to the string to the caller. The caller
94
/// shouldn't alter this string (because it doesn't belong to the caller).
95
//
96
const
tchar
*
TSocketError::GetReasonString
()
const
97
{
98
return
String
;
99
}
100
101
//
102
/// This function appends the error string to whatever is in the string
103
/// 'stringToAppendErrorTo' and put the result in 'destination'. You may
104
/// want to put something specific about the error in the string and then use
105
/// AppendError() to add the Winsock error code and description to it.
106
///
107
/// For example, you could say:
108
/// \code
109
// MessageBox(TSocketError(WSAENOTCONN).AppendError("Unable to send your mail"),
110
// "Error", MB_OK);
111
/// \endcode
112
/// And AppendError() will put "\n\nWinsock Error 10057: Socket is not presently connected"
113
/// after the "Unable to send you mail" string. Quite convenient.
114
///
115
/// If destination is valid, destination provides the space for the result.
116
/// \note It is assumed that desination has enough space.
117
/// If destination is 0, our internal string buffer is used for the result.
118
///
119
/// \note Using the internal string changes the result of subsequent calls to GetReasonString.
120
/// Use Init to reset the error string.
121
//
122
tchar
*
TSocketError::AppendError
(
const
tchar
* stringToAppendErrorTo,
tchar
* destination)
123
{
124
// Create the error string.
125
//
126
tstring
s = stringToAppendErrorTo;
127
s +=
_T
(
"\n\n"
);
128
s +=
GetReasonString
();
129
130
if
(!destination) {
131
//
132
// Expand the internal string buffer if needed.
133
//
134
size_t
n = s.length() + 1;
135
if
(n >
SizeToAllocate
) {
136
delete
[]
String
;
137
String
=
new
tchar
[
SizeToAllocate
= n];
138
}
139
destination =
String
;
140
}
141
142
// Copy the string to the destination.
143
144
_tcscpy
(destination, s.c_str());
145
return
destination;
146
}
147
148
//
149
/// This function is similar to AppendError(char*, char*), but the pre-string comes
150
/// from a string resource and szStringToAppendErrorTo will be overwritten with what
151
/// is in the string resource and appended to the Windows Sockets Error description.
152
/// stringToAppendErrorTo must be able to hold at least 128 characters.
153
//
154
tchar
*
TSocketError::AppendError
(
int
stringResourceToAppendErrorTo,
tchar
* destination)
155
{
156
tstring
s = GetModule().LoadString(stringResourceToAppendErrorTo);
157
return
AppendError
(s.c_str(), destination);
158
}
159
160
//
161
/// This function gets a string, suitable for display, based on the nError value.
162
/// The previous string is deleted if necessary. Note that the string allocated must
163
/// be at least 128 characters long. Even though the error strings you see don't
164
/// have error numbers associated with them, the function prepends the error number
165
/// to the szString before returning.
166
///
167
/// If you are writing string resources for the error strings, don't put error
168
/// numbers in the string, because that will be done for you later.
169
//
170
void
TSocketError::GetErrorString
()
171
{
172
int
resIdLookup[] = {
173
WSAEINTR,
174
WSAEBADF,
175
WSAEACCES,
176
WSAEFAULT,
177
WSAEINVAL,
178
WSAEMFILE,
179
WSAEWOULDBLOCK,
180
WSAEINPROGRESS,
181
WSAEALREADY,
182
WSAENOTSOCK,
183
WSAEDESTADDRREQ,
184
WSAEMSGSIZE,
185
WSAEPROTOTYPE,
186
WSAENOPROTOOPT,
187
WSAEPROTONOSUPPORT,
188
WSAESOCKTNOSUPPORT,
189
WSAEOPNOTSUPP,
190
WSAEPFNOSUPPORT,
191
WSAEAFNOSUPPORT,
192
WSAEADDRINUSE,
193
WSAEADDRNOTAVAIL,
194
WSAENETDOWN,
195
WSAENETUNREACH,
196
WSAENETRESET,
197
WSAECONNABORTED,
198
WSAECONNRESET,
199
WSAENOBUFS,
200
WSAEISCONN,
201
WSAENOTCONN,
202
WSAESHUTDOWN,
203
WSAETOOMANYREFS,
204
WSAETIMEDOUT,
205
WSAECONNREFUSED,
206
WSAELOOP,
207
WSAENAMETOOLONG,
208
WSAEHOSTDOWN,
209
WSAEHOSTUNREACH,
210
WSAENOTEMPTY,
211
WSAEPROCLIM,
212
WSAEUSERS,
213
WSAEDQUOT,
214
WSAESTALE,
215
WSAEREMOTE,
216
WSAEDISCON,
217
WSASYSNOTREADY,
218
WSAVERNOTSUPPORTED,
219
WSANOTINITIALISED,
220
WSAHOST_NOT_FOUND,
221
WSATRY_AGAIN,
222
WSANO_RECOVERY,
223
WSANO_DATA,
224
};
225
unsigned
id
= 0;
226
while
(
id
<
COUNTOF
(resIdLookup) && resIdLookup[
id
] !=
Error
)
227
id
++;
228
229
tstring
s = GetModule().LoadString(IDS_WINSOCK_BASE+
id
+1);
230
size_t
n = s.length() + 26;
// Add space for prefix "Winsock Error 123456789: ".
231
if
(
SizeToAllocate
< n)
SizeToAllocate
= n;
232
delete
[]
String
;
233
String
=
new
tchar
[
SizeToAllocate
];
234
String
[0] = 0;
235
236
_sntprintf(
String
,
SizeToAllocate
,
_T
(
"Winsock Error %d: %s"
),
Error
, (
tchar
*) s.c_str());
237
}
238
239
//
240
/// Return the current module. Used when loading strings.
241
/// Important when linking to OWL dynamically because ::LoadString
242
/// would try to load from the EXE, but the WinSock resource
243
/// strings are in the DLL.
244
//
245
TModule
&
246
TSocketError::GetModule()
// this is a static function
247
{
248
return
GetGlobalModule
();
249
}
250
251
}
// OWL namespace
252
253
/* ========================================================================== */
254
owl::TModule
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition
module.h:78
owl::TSocketError::GetErrorString
void GetErrorString()
Convert the error code to string.
Definition
wskerr.cpp:170
owl::TSocketError::GetReasonString
const tchar * GetReasonString() const
This function hands the pointer to the string to the caller.
Definition
wskerr.cpp:96
owl::TSocketError::operator=
TSocketError & operator=(const TSocketError &src)
This function copies the error code and string.
Definition
wskerr.cpp:55
owl::TSocketError::~TSocketError
virtual ~TSocketError()
This function destroys the allocated string.
Definition
wskerr.cpp:36
owl::TSocketError::GetReasonValue
int GetReasonValue() const
This function returns the error code.
Definition
wskerr.cpp:87
owl::TSocketError::Error
int Error
Error code.
Definition
wskerr.h:50
owl::TSocketError::SizeToAllocate
size_t SizeToAllocate
Size to allocate for String.
Definition
wskerr.h:51
owl::TSocketError::String
tchar * String
Error code converted to string.
Definition
wskerr.h:53
owl::TSocketError::TSocketError
TSocketError(int error=0, unsigned sizeToAllocate=128)
This function constructs the object with the error code and the size of the buffer to allocate.
Definition
wskerr.cpp:24
owl::TSocketError::Init
void Init(int error)
This function initializes the error code.
Definition
wskerr.cpp:78
owl::TSocketError::AppendError
tchar * AppendError(const tchar *stringToAppendErrorTo, tchar *destination=0)
This function appends the error string to whatever is in the string 'stringToAppendErrorTo' and put t...
Definition
wskerr.cpp:122
_tcscpy
#define _tcscpy
Definition
cygwin.h:79
_T
#define _T(x)
Definition
cygwin.h:51
owl::operator==
int operator==(const TBitSet< T > &bs1, const TBitSet< T > &bs2)
Definition
bitset.h:86
pch.h
strnewdup
char * strnewdup(const char *s, size_t minAllocSize=0)
Definition
memory.cpp:25
module.h
Definition of class TModule.
owl
Object Windows Library (OWLNext Core).
Definition
animctrl.h:22
owl::tchar
char tchar
Definition
defs.h:77
owl::OWL_DIAGINFO
OWL_DIAGINFO
Definition
animctrl.cpp:14
owl::tstring
std::string tstring
Definition
defs.h:79
owl::GetGlobalModule
TModule & GetGlobalModule()
Definition
global.cpp:48
defs.h
General definitions used by all ObjectWindows programs.
COUNTOF
#define COUNTOF(s)
Array element count Important: Only use this with an argument of array type.
Definition
defs.h:376
winsock.h
Main header of the Winsock OWL subsystem.
source
owlcore
wskerr.cpp
Generated by
1.18.0