1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/* -*- C++ -*-
* Copyright (C) 2003-2005 Thiago Macieira <thiago.macieira@kdemail.net>
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <config.h>
#include <tqmutex.h>
#include "klocale.h"
#include "tdesocketbase.h"
#include "tdesocketdevice.h"
using namespace KNetwork;
class KNetwork::KSocketBasePrivate
{
public:
int socketOptions;
int socketError;
int capabilities;
mutable KSocketDevice* device;
TQMutex mutex;
KSocketBasePrivate()
: mutex(true) // create recursive
{ }
};
KSocketBase::KSocketBase()
: d(new KSocketBasePrivate)
{
d->socketOptions = Blocking;
d->socketError = 0;
d->device = 0L;
d->capabilities = 0;
}
KSocketBase::~KSocketBase()
{
delete d->device;
delete d;
}
bool KSocketBase::setSocketOptions(int opts)
{
d->socketOptions = opts;
return true;
}
int KSocketBase::socketOptions() const
{
return d->socketOptions;
}
bool KSocketBase::setBlocking(bool enable)
{
return setSocketOptions((socketOptions() & ~Blocking) | (enable ? Blocking : 0));
}
bool KSocketBase::blocking() const
{
return socketOptions() & Blocking;
}
bool KSocketBase::setAddressReuseable(bool enable)
{
return setSocketOptions((socketOptions() & ~AddressReuseable) | (enable ? AddressReuseable : 0));
}
bool KSocketBase::addressReuseable() const
{
return socketOptions() & AddressReuseable;
}
bool KSocketBase::setIPv6Only(bool enable)
{
return setSocketOptions((socketOptions() & ~IPv6Only) | (enable ? IPv6Only : 0));
}
bool KSocketBase::isIPv6Only() const
{
return socketOptions() & IPv6Only;
}
bool KSocketBase::setBroadcast(bool enable)
{
return setSocketOptions((socketOptions() & ~Broadcast) | (enable ? Broadcast : 0));
}
bool KSocketBase::broadcast() const
{
return socketOptions() & Broadcast;
}
KSocketDevice* KSocketBase::socketDevice() const
{
if (d->device)
return d->device;
// it doesn't exist, so create it
TQMutexLocker locker(mutex());
if (d->device)
return d->device;
KSocketBase* that = const_cast<KSocketBase*>(this);
KSocketDevice* dev = 0;
if (d->capabilities)
dev = KSocketDevice::createDefault(that, d->capabilities);
if (!dev)
dev = KSocketDevice::createDefault(that);
that->setSocketDevice(dev);
return d->device;
}
void KSocketBase::setSocketDevice(KSocketDevice* device)
{
TQMutexLocker locker(mutex());
if (d->device == 0L)
d->device = device;
}
int KSocketBase::setRequestedCapabilities(int add, int remove)
{
d->capabilities |= add;
d->capabilities &= ~remove;
return d->capabilities;
}
bool KSocketBase::hasDevice() const
{
return d->device != 0L;
}
void KSocketBase::setError(SocketError error)
{
d->socketError = error;
}
KSocketBase::SocketError KSocketBase::error() const
{
return static_cast<KSocketBase::SocketError>(d->socketError);
}
// static
TQString KSocketBase::errorString(KSocketBase::SocketError code)
{
TQString reason;
switch (code)
{
case NoError:
reason = i18n("Socket error code NoError", "no error");
break;
case LookupFailure:
reason = i18n("Socket error code LookupFailure",
"name lookup has failed");
break;
case AddressInUse:
reason = i18n("Socket error code AddressInUse",
"address already in use");
break;
case AlreadyBound:
reason = i18n("Socket error code AlreadyBound",
"socket is already bound");
break;
case AlreadyCreated:
reason = i18n("Socket error code AlreadyCreated",
"socket is already created");
break;
case NotBound:
reason = i18n("Socket error code NotBound",
"socket is not bound");
break;
case NotCreated:
reason = i18n("Socket error code NotCreated",
"socket has not been created");
break;
case WouldBlock:
reason = i18n("Socket error code WouldBlock",
"operation would block");
break;
case ConnectionRefused:
reason = i18n("Socket error code ConnectionRefused",
"connection actively refused");
break;
case ConnectionTimedOut:
reason = i18n("Socket error code ConnectionTimedOut",
"connection timed out");
break;
case InProgress:
reason = i18n("Socket error code InProgress",
"operation is already in progress");
break;
case NetFailure:
reason = i18n("Socket error code NetFailure",
"network failure occurred");
break;
case NotSupported:
reason = i18n("Socket error code NotSupported",
"operation is not supported");
break;
case Timeout:
reason = i18n("Socket error code Timeout",
"timed operation timed out");
break;
case UnknownError:
reason = i18n("Socket error code UnknownError",
"an unknown/unexpected error has happened");
break;
case RemotelyDisconnected:
reason = i18n("Socket error code RemotelyDisconnected",
"remote host closed connection");
break;
default:
reason = TQString::null;
break;
}
return reason;
}
// static
bool KSocketBase::isFatalError(int code)
{
switch (code)
{
case WouldBlock:
case InProgress:
case NoError:
case RemotelyDisconnected:
return false;
}
return true;
}
void KSocketBase::unsetSocketDevice()
{
d->device = 0L;
}
TQMutex* KSocketBase::mutex() const
{
return &d->mutex;
}
KActiveSocketBase::KActiveSocketBase()
{
}
KActiveSocketBase::~KActiveSocketBase()
{
}
int KActiveSocketBase::getch()
{
unsigned char c;
if (tqreadBlock((char*)&c, 1) != 1)
return -1;
return c;
}
int KActiveSocketBase::putch(int ch)
{
unsigned char c = (unsigned char)ch;
if (tqwriteBlock((char*)&c, 1) != 1)
return -1;
return c;
}
void KActiveSocketBase::setError(int status, SocketError error)
{
KSocketBase::setError(error);
setStatus(status);
}
void KActiveSocketBase::resetError()
{
KSocketBase::setError(NoError);
resetStatus();
}
KPassiveSocketBase::KPassiveSocketBase()
{
}
KPassiveSocketBase::~KPassiveSocketBase()
{
}
|