• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

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

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.8
This website is maintained by Timothy Pearson.