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

kdecore

  • kdecore
  • network
ksocketbase.cpp
1 /* -*- C++ -*-
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 "klocale.h"
28 
29 #include "ksocketbase.h"
30 #include "ksocketdevice.h"
31 
32 using namespace KNetwork;
33 
34 class KNetwork::KSocketBasePrivate
35 {
36 public:
37  int socketOptions;
38  int socketError;
39  int capabilities;
40 
41  mutable KSocketDevice* device;
42 
43  TQMutex mutex;
44 
45  KSocketBasePrivate()
46  : mutex(true) // create recursive
47  { }
48 };
49 
50 KSocketBase::KSocketBase()
51  : d(new KSocketBasePrivate)
52 {
53  d->socketOptions = Blocking;
54  d->socketError = 0;
55  d->device = 0L;
56  d->capabilities = 0;
57 }
58 
59 KSocketBase::~KSocketBase()
60 {
61  delete d->device;
62  delete d;
63 }
64 
65 bool KSocketBase::setSocketOptions(int opts)
66 {
67  d->socketOptions = opts;
68  return true;
69 }
70 
71 int KSocketBase::socketOptions() const
72 {
73  return d->socketOptions;
74 }
75 
76 bool KSocketBase::setBlocking(bool enable)
77 {
78  return setSocketOptions((socketOptions() & ~Blocking) | (enable ? Blocking : 0));
79 }
80 
81 bool KSocketBase::blocking() const
82 {
83  return socketOptions() & Blocking;
84 }
85 
86 bool KSocketBase::setAddressReuseable(bool enable)
87 {
88  return setSocketOptions((socketOptions() & ~AddressReuseable) | (enable ? AddressReuseable : 0));
89 }
90 
91 bool KSocketBase::addressReuseable() const
92 {
93  return socketOptions() & AddressReuseable;
94 }
95 
96 bool KSocketBase::setIPv6Only(bool enable)
97 {
98  return setSocketOptions((socketOptions() & ~IPv6Only) | (enable ? IPv6Only : 0));
99 }
100 
101 bool KSocketBase::isIPv6Only() const
102 {
103  return socketOptions() & IPv6Only;
104 }
105 
106 bool KSocketBase::setBroadcast(bool enable)
107 {
108  return setSocketOptions((socketOptions() & ~Broadcast) | (enable ? Broadcast : 0));
109 }
110 
111 bool KSocketBase::broadcast() const
112 {
113  return socketOptions() & Broadcast;
114 }
115 
116 KSocketDevice* KSocketBase::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  KSocketBase* that = const_cast<KSocketBase*>(this);
127  KSocketDevice* dev = 0;
128  if (d->capabilities)
129  dev = KSocketDevice::createDefault(that, d->capabilities);
130  if (!dev)
131  dev = KSocketDevice::createDefault(that);
132  that->setSocketDevice(dev);
133  return d->device;
134 }
135 
136 void KSocketBase::setSocketDevice(KSocketDevice* device)
137 {
138  TQMutexLocker locker(mutex());
139  if (d->device == 0L)
140  d->device = device;
141 }
142 
143 int KSocketBase::setRequestedCapabilities(int add, int remove)
144 {
145  d->capabilities |= add;
146  d->capabilities &= ~remove;
147  return d->capabilities;
148 }
149 
150 bool KSocketBase::hasDevice() const
151 {
152  return d->device != 0L;
153 }
154 
155 void KSocketBase::setError(SocketError error)
156 {
157  d->socketError = error;
158 }
159 
160 KSocketBase::SocketError KSocketBase::error() const
161 {
162  return static_cast<KSocketBase::SocketError>(d->socketError);
163 }
164 
165 // static
166 TQString KSocketBase::errorString(KSocketBase::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 KSocketBase::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 KSocketBase::unsetSocketDevice()
274 {
275  d->device = 0L;
276 }
277 
278 TQMutex* KSocketBase::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  KSocketBase::setError(error);
312  setStatus(status);
313 }
314 
315 void KActiveSocketBase::resetError()
316 {
317  KSocketBase::setError(NoError);
318  resetStatus();
319 }
320 
321 KPassiveSocketBase::KPassiveSocketBase()
322 {
323 }
324 
325 KPassiveSocketBase::~KPassiveSocketBase()
326 {
327 }
KNetwork::KSocketBase::isFatalError
static bool isFatalError(int code)
Returns true if the given error code is a fatal one, false otherwise.
Definition: ksocketbase.cpp:259
KNetwork::KSocketBase::hasDevice
bool hasDevice() const
Returns true if the socket device has been initialised in this object, either by calling socketDevice...
Definition: ksocketbase.cpp:150
KNetwork::KSocketBase::setAddressReuseable
virtual bool setAddressReuseable(bool enable)
Sets this socket&#39;s address reuseable flag.
Definition: ksocketbase.cpp:86
KNetwork::KSocketBase::blocking
bool blocking() const
Retrieves this socket&#39;s blocking mode.
Definition: ksocketbase.cpp:81
KNetwork::KActiveSocketBase::getch
virtual int getch()
Reads one character from the socket.
Definition: ksocketbase.cpp:291
KNetwork::KActiveSocketBase::resetError
void resetError()
Resets the socket error code and the I/O Device&#39;s status.
Definition: ksocketbase.cpp:315
KNetwork::KActiveSocketBase::putch
virtual int putch(int ch)
Writes one character to the socket.
Definition: ksocketbase.cpp:300
KNetwork::KActiveSocketBase::KActiveSocketBase
KActiveSocketBase()
Constructor.
Definition: ksocketbase.cpp:283
KNetwork::KSocketBase::socketOptions
virtual int socketOptions() const
Retrieves the socket options that have been set.
Definition: ksocketbase.cpp:71
KNetwork::KSocketBase::~KSocketBase
virtual ~KSocketBase()
Destructor.
Definition: ksocketbase.cpp:59
KNetwork::KSocketBase::mutex
TQMutex * mutex() const
Returns the internal mutex for this class.
Definition: ksocketbase.cpp:278
klocale.h
KNetwork::KPassiveSocketBase::~KPassiveSocketBase
virtual ~KPassiveSocketBase()
Destructor.
Definition: ksocketbase.cpp:325
KNetwork::KSocketBase::setRequestedCapabilities
int setRequestedCapabilities(int add, int remove=0)
Sets the internally requested capabilities for a socket device.
Definition: ksocketbase.cpp:143
KNetwork::KSocketBase::KSocketBase
KSocketBase()
Default constructor.
Definition: ksocketbase.cpp:50
KNetwork
A namespace to store all networking-related (socket) classes.
Definition: kbufferedsocket.h:36
KNetwork::KSocketBase::setBroadcast
virtual bool setBroadcast(bool enable)
Sets this socket Broadcast flag.
Definition: ksocketbase.cpp:106
KNetwork::KSocketBase::setIPv6Only
virtual bool setIPv6Only(bool enable)
Sets this socket&#39;s IPv6 Only flag.
Definition: ksocketbase.cpp:96
KNetwork::KSocketBase::SocketError
SocketError
Possible socket error codes.
Definition: ksocketbase.h:152
KNetwork::KSocketBase::setError
void setError(SocketError error)
Sets the socket&#39;s error code.
Definition: ksocketbase.cpp:155
KNetwork::KActiveSocketBase::setError
void setError(int status, SocketError error)
Sets the socket&#39;s error code and the I/O Device&#39;s status.
Definition: ksocketbase.cpp:309
KNetwork::KSocketBase::broadcast
bool broadcast() const
Retrieves this socket&#39;s Broadcast flag.
Definition: ksocketbase.cpp:111
KNetwork::KSocketBase::setSocketOptions
virtual bool setSocketOptions(int opts)
Set the given socket options.
Definition: ksocketbase.cpp:65
KNetwork::KSocketBase::addressReuseable
bool addressReuseable() const
Retrieves this socket&#39;s address reuseability flag.
Definition: ksocketbase.cpp:91
KNetwork::KSocketBase::isIPv6Only
bool isIPv6Only() const
Retrieves this socket&#39;s IPv6 Only flag.
Definition: ksocketbase.cpp:101
KNetwork::KActiveSocketBase::~KActiveSocketBase
virtual ~KActiveSocketBase()
Destructor.
Definition: ksocketbase.cpp:287
KNetwork::KSocketBase::setSocketDevice
virtual void setSocketDevice(KSocketDevice *device)
Sets the socket implementation to be used on this socket.
Definition: ksocketbase.cpp:136
KNetwork::KPassiveSocketBase::KPassiveSocketBase
KPassiveSocketBase()
Constructor.
Definition: ksocketbase.cpp:321
KNetwork::KSocketBase::errorString
TQString errorString() const
Returns the error string corresponding to this error condition.
Definition: ksocketbase.h:383
KNetwork::KSocketBase::setBlocking
virtual bool setBlocking(bool enable)
Sets this socket&#39;s blocking mode.
Definition: ksocketbase.cpp:76
KNetwork::KSocketDevice
Low-level socket functionality.
Definition: ksocketdevice.h:50
KNetwork::KSocketBase::error
SocketError error() const
Retrieves the socket error code.
Definition: ksocketbase.cpp:160
KNetwork::KSocketBase
Basic socket functionality.
Definition: ksocketbase.h:97
KNetwork::KSocketBase::socketDevice
KSocketDevice * socketDevice() const
Retrieves the socket implementation used on this socket.
Definition: ksocketbase.cpp:116
KNetwork::KSocketDevice::createDefault
static KSocketDevice * createDefault(KSocketBase *parent)
Creates a new default KSocketDevice object given the parent object.
Definition: ksocketdevice.cpp:844

kdecore

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

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.8.13
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |