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

tdecore

  • tdecore
  • network
tdesocketdevice.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 
27 #include <tqmap.h>
28 
29 #ifdef USE_SOLARIS
30 # include <sys/filio.h>
31 #endif
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35 #include <sys/ioctl.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <netinet/in.h>
39 #include <unistd.h>
40 
41 #ifdef HAVE_POLL
42 # include <poll.h>
43 #else
44 # ifdef HAVE_SYS_SELECT
45 # include <sys/select.h>
46 # endif
47 #endif
48 
49 // Include syssocket before our local includes
50 #include "syssocket.h"
51 
52 #include <tqmutex.h>
53 #include <tqsocketnotifier.h>
54 
55 #include "kresolver.h"
56 #include "tdesocketaddress.h"
57 #include "tdesocketbase.h"
58 #include "tdesocketdevice.h"
59 #include "ksockssocketdevice.h"
60 
61 using namespace KNetwork;
62 
63 class KNetwork::TDESocketDevicePrivate
64 {
65 public:
66  mutable TQSocketNotifier *input, *output, *exception;
67  TDESocketAddress local, peer;
68  int af;
69 
70  inline TDESocketDevicePrivate()
71  {
72  input = output = exception = 0L;
73  af = 0;
74  }
75 };
76 
77 
78 TDESocketDevice::TDESocketDevice(const TDESocketBase* parent)
79  : m_sockfd(-1), d(new TDESocketDevicePrivate)
80 {
81  setSocketDevice(this);
82  if (parent)
83  setSocketOptions(parent->socketOptions());
84 }
85 
86 TDESocketDevice::TDESocketDevice(int fd)
87  : m_sockfd(fd), d(new TDESocketDevicePrivate)
88 {
89  setState(IO_Open);
90  setFlags(IO_Sequential | IO_Raw | IO_ReadWrite);
91  setSocketDevice(this);
92  d->af = localAddress().family();
93 }
94 
95 TDESocketDevice::TDESocketDevice(bool, const TDESocketBase* parent)
96  : m_sockfd(-1), d(new TDESocketDevicePrivate)
97 {
98  // do not set parent
99  if (parent)
100  setSocketOptions(parent->socketOptions());
101 }
102 
103 TDESocketDevice::~TDESocketDevice()
104 {
105  close(); // deletes the notifiers
106  unsetSocketDevice(); // prevent double deletion
107  delete d;
108 }
109 
110 bool TDESocketDevice::setSocketOptions(int opts)
111 {
112  // must call parent
113  TQMutexLocker locker(mutex());
114  TDESocketBase::setSocketOptions(opts);
115 
116  if (m_sockfd == -1)
117  return true; // flags are stored
118 
119  {
120  int fdflags = fcntl(m_sockfd, F_GETFL, 0);
121  if (fdflags == -1)
122  {
123  setError(IO_UnspecifiedError, UnknownError);
124  return false; // error
125  }
126 
127  if (opts & Blocking)
128  fdflags &= ~O_NONBLOCK;
129  else
130  fdflags |= O_NONBLOCK;
131 
132  if (fcntl(m_sockfd, F_SETFL, fdflags) == -1)
133  {
134  setError(IO_UnspecifiedError, UnknownError);
135  return false; // error
136  }
137  }
138 
139  {
140  int on = opts & AddressReuseable ? 1 : 0;
141  if (setsockopt(m_sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) == -1)
142  {
143  setError(IO_UnspecifiedError, UnknownError);
144  return false; // error
145  }
146  }
147 
148 #if defined(IPV6_V6ONLY) && defined(AF_INET6)
149  if (d->af == AF_INET6)
150  {
151  // don't try this on non-IPv6 sockets, or we'll get an error
152 
153  int on = opts & IPv6Only ? 1 : 0;
154  if (setsockopt(m_sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&on, sizeof(on)) == -1)
155  {
156  setError(IO_UnspecifiedError, UnknownError);
157  return false; // error
158  }
159  }
160 #endif
161 
162  {
163  int on = opts & Broadcast ? 1 : 0;
164  if (setsockopt(m_sockfd, SOL_SOCKET, SO_BROADCAST, (char*)&on, sizeof(on)) == -1)
165  {
166  setError(IO_UnspecifiedError, UnknownError);
167  return false; // error
168  }
169  }
170 
171  return true; // all went well
172 }
173 
174 bool TDESocketDevice::open(TQ_OpenMode)
175 {
176  resetError();
177  return false;
178 }
179 
180 void TDESocketDevice::close()
181 {
182  resetError();
183  if (m_sockfd != -1)
184  {
185  delete d->input;
186  delete d->output;
187  delete d->exception;
188 
189  d->input = d->output = d->exception = 0L;
190 
191  d->local.setFamily(AF_UNSPEC);
192  d->peer.setFamily(AF_UNSPEC);
193 
194  ::close(m_sockfd);
195  }
196  setState(0);
197 
198  m_sockfd = -1;
199 }
200 
201 bool TDESocketDevice::create(int family, int type, int protocol)
202 {
203  resetError();
204 
205  if (m_sockfd != -1)
206  {
207  // it's already created!
208  setError(IO_SocketCreateError, AlreadyCreated);
209  return false;
210  }
211 
212  // no socket yet; we have to create it
213  m_sockfd = kde_socket(family, type, protocol);
214 
215  if (m_sockfd == -1)
216  {
217  setError(IO_SocketCreateError, NotSupported);
218  return false;
219  }
220 
221  d->af = family;
222  setSocketOptions(socketOptions());
223  setState(IO_Open);
224  return true; // successfully created
225 }
226 
227 bool TDESocketDevice::create(const KResolverEntry& address)
228 {
229  return create(address.family(), address.socketType(), address.protocol());
230 }
231 
232 bool TDESocketDevice::bind(const KResolverEntry& address)
233 {
234  resetError();
235 
236  if (m_sockfd == -1 && !create(address))
237  return false; // failed creating
238 
239  // we have a socket, so try and bind
240  if (kde_bind(m_sockfd, address.address(), address.length()) == -1)
241  {
242  if (errno == EADDRINUSE)
243  setError(IO_BindError, AddressInUse);
244  else if (errno == EINVAL)
245  setError(IO_BindError, AlreadyBound);
246  else
247  // assume the address is the cause
248  setError(IO_BindError, NotSupported);
249  return false;
250  }
251 
252  return true;
253 }
254 
255 bool TDESocketDevice::listen(int backlog)
256 {
257  if (m_sockfd != -1)
258  {
259  if (kde_listen(m_sockfd, backlog) == -1)
260  {
261  setError(IO_ListenError, NotSupported);
262  return false;
263  }
264 
265  resetError();
266  setFlags(IO_Sequential | IO_Raw | IO_ReadWrite);
267  return true;
268  }
269 
270  // we don't have a socket
271  // can't listen
272  setError(IO_ListenError, NotCreated);
273  return false;
274 }
275 
276 bool TDESocketDevice::connect(const KResolverEntry& address)
277 {
278  resetError();
279 
280  if (m_sockfd == -1 && !create(address))
281  return false; // failed creating!
282 
283  if (kde_connect(m_sockfd, address.address(), address.length()) == -1)
284  {
285  if (errno == EISCONN)
286  return true; // we're already connected
287  else if (errno == EALREADY || errno == EINPROGRESS)
288  {
289  setError(IO_ConnectError, InProgress);
290  return true;
291  }
292  else if (errno == ECONNREFUSED)
293  setError(IO_ConnectError, ConnectionRefused);
294  else if (errno == ENETDOWN || errno == ENETUNREACH ||
295  errno == ENETRESET || errno == ECONNABORTED ||
296  errno == ECONNRESET || errno == EHOSTDOWN ||
297  errno == EHOSTUNREACH)
298  setError(IO_ConnectError, NetFailure);
299  else
300  setError(IO_ConnectError, NotSupported);
301 
302  return false;
303  }
304 
305  setFlags(IO_Sequential | IO_Raw | IO_ReadWrite);
306  return true; // all is well
307 }
308 
309 TDESocketDevice* TDESocketDevice::accept()
310 {
311  if (m_sockfd == -1)
312  {
313  // can't accept without a socket
314  setError(IO_AcceptError, NotCreated);
315  return 0L;
316  }
317 
318  struct sockaddr sa;
319  socklen_t len = sizeof(sa);
320  int newfd = kde_accept(m_sockfd, &sa, &len);
321  if (newfd == -1)
322  {
323  if (errno == EAGAIN || errno == EWOULDBLOCK)
324  setError(IO_AcceptError, WouldBlock);
325  else
326  setError(IO_AcceptError, UnknownError);
327  return NULL;
328  }
329 
330  return new TDESocketDevice(newfd);
331 }
332 
333 bool TDESocketDevice::disconnect()
334 {
335  resetError();
336 
337  if (m_sockfd == -1)
338  return false; // can't create
339 
340  TDESocketAddress address;
341  address.setFamily(AF_UNSPEC);
342  if (kde_connect(m_sockfd, address.address(), address.length()) == -1)
343  {
344  if (errno == EALREADY || errno == EINPROGRESS)
345  {
346  setError(IO_ConnectError, InProgress);
347  return false;
348  }
349  else if (errno == ECONNREFUSED)
350  setError(IO_ConnectError, ConnectionRefused);
351  else if (errno == ENETDOWN || errno == ENETUNREACH ||
352  errno == ENETRESET || errno == ECONNABORTED ||
353  errno == ECONNRESET || errno == EHOSTDOWN ||
354  errno == EHOSTUNREACH)
355  setError(IO_ConnectError, NetFailure);
356  else
357  setError(IO_ConnectError, NotSupported);
358 
359  return false;
360  }
361 
362  setFlags(IO_Sequential | IO_Raw | IO_ReadWrite);
363  setState(IO_Open);
364  return true; // all is well
365 }
366 
367 #ifdef USE_QT3
368 TQ_LONG TDESocketDevice::bytesAvailable() const
369 #endif
370 #ifdef USE_QT4
371 qint64 TDESocketDevice::bytesAvailable() const
372 #endif
373 {
374  if (m_sockfd == -1)
375  return -1; // there's nothing to read in a closed socket
376 
377  int nchars;
378  if (ioctl(m_sockfd, FIONREAD, &nchars) == -1)
379  return -1; // error!
380 
381  return nchars;
382 }
383 
384 TQ_LONG TDESocketDevice::waitForMore(int msecs, bool *timeout)
385 {
386  if (m_sockfd == -1)
387  return -1; // there won't ever be anything to read...
388 
389  bool input;
390  if (!poll(&input, 0, 0, msecs, timeout))
391  return -1; // failed polling
392 
393  return bytesAvailable();
394 }
395 
396 static int do_read_common(int sockfd, char *data, TQ_ULONG maxlen, TDESocketAddress* from, ssize_t &retval, bool peek = false)
397 {
398  socklen_t len;
399  if (from)
400  {
401  from->setLength(len = 128); // arbitrary length
402  retval = ::recvfrom(sockfd, data, maxlen, peek ? MSG_PEEK : 0, from->address(), &len);
403  }
404  else
405  retval = ::recvfrom(sockfd, data, maxlen, peek ? MSG_PEEK : 0, NULL, NULL);
406 
407  if (retval == -1)
408  {
409  if (errno == EAGAIN || errno == EWOULDBLOCK)
410  return TDESocketDevice::WouldBlock;
411  else
412  return TDESocketDevice::UnknownError;
413  }
414  if (retval == 0)
415  return TDESocketDevice::RemotelyDisconnected;
416 
417  if (from)
418  from->setLength(len);
419  return 0;
420 }
421 
422 TQT_TQIO_LONG TDESocketDevice::tqreadBlock(char *data, TQT_TQIO_ULONG maxlen)
423 {
424  resetError();
425  if (m_sockfd == -1)
426  return -1;
427 
428  if (maxlen == 0 || data == 0L)
429  return 0; // can't read
430 
431  ssize_t retval;
432  int err = do_read_common(m_sockfd, data, maxlen, 0L, retval);
433 
434  if (err)
435  {
436  setError(IO_ReadError, static_cast<SocketError>(err));
437  return -1;
438  }
439 
440  return retval;
441 }
442 
443 TQT_TQIO_LONG TDESocketDevice::tqreadBlock(char *data, TQT_TQIO_ULONG maxlen, TDESocketAddress &from)
444 {
445  resetError();
446  if (m_sockfd == -1)
447  return -1; // nothing to do here
448 
449  if (data == 0L || maxlen == 0)
450  return 0; // user doesn't want to read
451 
452  ssize_t retval;
453  int err = do_read_common(m_sockfd, data, maxlen, &from, retval);
454 
455  if (err)
456  {
457  setError(IO_ReadError, static_cast<SocketError>(err));
458  return -1;
459  }
460 
461  return retval;
462 }
463 
464 TQ_LONG TDESocketDevice::peekBlock(char *data, TQ_ULONG maxlen)
465 {
466  resetError();
467  if (m_sockfd == -1)
468  return -1;
469 
470  if (maxlen == 0 || data == 0L)
471  return 0; // can't read
472 
473  ssize_t retval;
474  int err = do_read_common(m_sockfd, data, maxlen, 0L, retval, true);
475 
476  if (err)
477  {
478  setError(IO_ReadError, static_cast<SocketError>(err));
479  return -1;
480  }
481 
482  return retval;
483 }
484 
485 TQ_LONG TDESocketDevice::peekBlock(char *data, TQ_ULONG maxlen, TDESocketAddress& from)
486 {
487  resetError();
488  if (m_sockfd == -1)
489  return -1; // nothing to do here
490 
491  if (data == 0L || maxlen == 0)
492  return 0; // user doesn't want to read
493 
494  ssize_t retval;
495  int err = do_read_common(m_sockfd, data, maxlen, &from, retval, true);
496 
497  if (err)
498  {
499  setError(IO_ReadError, static_cast<SocketError>(err));
500  return -1;
501  }
502 
503  return retval;
504 }
505 
506 TQT_TQIO_LONG TDESocketDevice::tqwriteBlock(const char *data, TQT_TQIO_ULONG len)
507 {
508  return tqwriteBlock(data, len, TDESocketAddress());
509 }
510 
511 TQT_TQIO_LONG TDESocketDevice::tqwriteBlock(const char *data, TQT_TQIO_ULONG len, const TDESocketAddress& to)
512 {
513  resetError();
514  if (m_sockfd == -1)
515  return -1; // can't write to unopen socket
516 
517  if (data == 0L || len == 0)
518  return 0; // nothing to be written
519 
520  ssize_t retval = ::sendto(m_sockfd, data, len, 0, to.address(), to.length());
521  if (retval == -1)
522  {
523  if (errno == EAGAIN || errno == EWOULDBLOCK)
524  setError(IO_WriteError, WouldBlock);
525  else
526  setError(IO_WriteError, UnknownError);
527  return -1; // nothing written
528  }
529  else if (retval == 0)
530  setError(IO_WriteError, RemotelyDisconnected);
531 
532  return retval;
533 }
534 
535 TDESocketAddress TDESocketDevice::localAddress() const
536 {
537  if (m_sockfd == -1)
538  return TDESocketAddress(); // not open, empty value
539 
540  if (d->local.family() != AF_UNSPEC)
541  return d->local;
542 
543  socklen_t len;
544  TDESocketAddress localAddress;
545  localAddress.setLength(len = 32); // arbitrary value
546  if (kde_getsockname(m_sockfd, localAddress.address(), &len) == -1)
547  // error!
548  return d->local = TDESocketAddress();
549 
550 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
551  len = localAddress.address()->sa_len;
552 #endif
553 
554  if (len <= localAddress.length())
555  {
556  // it has fit already
557  localAddress.setLength(len);
558  return d->local = localAddress;
559  }
560 
561  // no, the socket address is actually larger than we had anticipated
562  // call again
563  localAddress.setLength(len);
564  if (kde_getsockname(m_sockfd, localAddress.address(), &len) == -1)
565  // error!
566  return d->local = TDESocketAddress();
567 
568  return d->local = localAddress;
569 }
570 
571 TDESocketAddress TDESocketDevice::peerAddress() const
572 {
573  if (m_sockfd == -1)
574  return TDESocketAddress(); // not open, empty value
575 
576  if (d->peer.family() != AF_UNSPEC)
577  return d->peer;
578 
579  socklen_t len;
580  TDESocketAddress peerAddress;
581  peerAddress.setLength(len = 32); // arbitrary value
582  if (kde_getpeername(m_sockfd, peerAddress.address(), &len) == -1)
583  // error!
584  return d->peer = TDESocketAddress();
585 
586 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
587  len = peerAddress.address()->sa_len;
588 #endif
589 
590  if (len <= peerAddress.length())
591  {
592  // it has fit already
593  peerAddress.setLength(len);
594  return d->peer = peerAddress;
595  }
596 
597  // no, the socket address is actually larger than we had anticipated
598  // call again
599  peerAddress.setLength(len);
600  if (kde_getpeername(m_sockfd, peerAddress.address(), &len) == -1)
601  // error!
602  return d->peer = TDESocketAddress();
603 
604  return d->peer = peerAddress;
605 }
606 
607 TDESocketAddress TDESocketDevice::externalAddress() const
608 {
609  // for normal sockets, the externally visible address is the same
610  // as the local address
611  return localAddress();
612 }
613 
614 TQSocketNotifier* TDESocketDevice::readNotifier() const
615 {
616  if (d->input)
617  return d->input;
618 
619  TQMutexLocker locker(mutex());
620  if (d->input)
621  return d->input;
622 
623  if (m_sockfd == -1)
624  {
625  // socket doesn't exist; can't create notifier
626  return 0L;
627  }
628 
629  return d->input = createNotifier(TQSocketNotifier::Read);
630 }
631 
632 TQSocketNotifier* TDESocketDevice::writeNotifier() const
633 {
634  if (d->output)
635  return d->output;
636 
637  TQMutexLocker locker(mutex());
638  if (d->output)
639  return d->output;
640 
641  if (m_sockfd == -1)
642  {
643  // socket doesn't exist; can't create notifier
644  return 0L;
645  }
646 
647  return d->output = createNotifier(TQSocketNotifier::Write);
648 }
649 
650 TQSocketNotifier* TDESocketDevice::exceptionNotifier() const
651 {
652  if (d->exception)
653  return d->exception;
654 
655  TQMutexLocker locker(mutex());
656  if (d->exception)
657  return d->exception;
658 
659  if (m_sockfd == -1)
660  {
661  // socket doesn't exist; can't create notifier
662  return 0L;
663  }
664 
665  return d->exception = createNotifier(TQSocketNotifier::Exception);
666 }
667 
668 bool TDESocketDevice::poll(bool *input, bool *output, bool *exception,
669  int timeout, bool* timedout)
670 {
671  if (m_sockfd == -1)
672  {
673  setError(IO_UnspecifiedError, NotCreated);
674  return false;
675  }
676 
677  resetError();
678 #ifdef HAVE_POLL
679  struct pollfd fds;
680  fds.fd = m_sockfd;
681  fds.events = 0;
682 
683  if (input)
684  {
685  fds.events |= POLLIN;
686  *input = false;
687  }
688  if (output)
689  {
690  fds.events |= POLLOUT;
691  *output = false;
692  }
693  if (exception)
694  {
695  fds.events |= POLLPRI;
696  *exception = false;
697  }
698 
699  int retval = ::poll(&fds, 1, timeout);
700  if (retval == -1)
701  {
702  setError(IO_UnspecifiedError, UnknownError);
703  return false;
704  }
705  if (retval == 0)
706  {
707  // timeout
708  if (timedout)
709  *timedout = true;
710  return true;
711  }
712 
713  if (input && fds.revents & POLLIN)
714  *input = true;
715  if (output && fds.revents & POLLOUT)
716  *output = true;
717  if (exception && fds.revents & POLLPRI)
718  *exception = true;
719  if (timedout)
720  *timedout = false;
721 
722  return true;
723 #else
724  /*
725  * We don't have poll(2). We'll have to make do with select(2).
726  */
727 
728  fd_set readfds, writefds, exceptfds;
729  fd_set *preadfds = 0L, *pwritefds = 0L, *pexceptfds = 0L;
730 
731  if (input)
732  {
733  preadfds = &readfds;
734  FD_ZERO(preadfds);
735  FD_SET(m_sockfd, preadfds);
736  *input = false;
737  }
738  if (output)
739  {
740  pwritefds = &writefds;
741  FD_ZERO(pwritefds);
742  FD_SET(m_sockfd, pwritefds);
743  *output = false;
744  }
745  if (exception)
746  {
747  pexceptfds = &exceptfds;
748  FD_ZERO(pexceptfds);
749  FD_SET(m_sockfd, pexceptfds);
750  *exception = false;
751  }
752 
753  int retval;
754  if (timeout < 0)
755  retval = select(m_sockfd + 1, preadfds, pwritefds, pexceptfds, 0L);
756  else
757  {
758  // convert the milliseconds to timeval
759  struct timeval tv;
760  tv.tv_sec = timeout / 1000;
761  tv.tv_usec = timeout % 1000 * 1000;
762 
763  retval = select(m_sockfd + 1, preadfds, pwritefds, pexceptfds, &tv);
764  }
765 
766  if (retval == -1)
767  {
768  setError(IO_UnspecifiedError, UnknownError);
769  return false;
770  }
771  if (retval == 0)
772  {
773  // timeout
774  if (timedout)
775  *timedout = true;
776  return true;
777  }
778 
779  if (input && FD_ISSET(m_sockfd, preadfds))
780  *input = true;
781  if (output && FD_ISSET(m_sockfd, pwritefds))
782  *output = true;
783  if (exception && FD_ISSET(m_sockfd, pexceptfds))
784  *exception = true;
785 
786  return true;
787 #endif
788 }
789 
790 bool TDESocketDevice::poll(int timeout, bool *timedout)
791 {
792  bool input, output, exception;
793  return poll(&input, &output, &exception, timeout, timedout);
794 }
795 
796 TQSocketNotifier* TDESocketDevice::createNotifier(TQSocketNotifier::Type type) const
797 {
798  if (m_sockfd == -1)
799  return 0L;
800 
801  return new TQSocketNotifier(m_sockfd, type);
802 }
803 
804 namespace
805 {
806  // simple class to avoid pointer stuff
807  template<class T> class ptr
808  {
809  typedef T type;
810  type* obj;
811  public:
812  ptr() : obj(0)
813  { }
814 
815  ptr(const ptr<T>& other) : obj(other.obj)
816  { }
817 
818  ptr(type* _obj) : obj(_obj)
819  { }
820 
821  ~ptr()
822  { }
823 
824  ptr<T>& operator=(const ptr<T>& other)
825  { obj = other.obj; return *this; }
826 
827  ptr<T>& operator=(T* _obj)
828  { obj = _obj; return *this; }
829 
830  type* operator->() const { return obj; }
831 
832  operator T*() const { return obj; }
833 
834  bool isNull() const
835  { return obj == 0; }
836  };
837 }
838 
839 static TDESocketDeviceFactoryBase* defaultImplFactory;
840 static TQMutex defaultImplFactoryMutex;
841 typedef TQMap<int, TDESocketDeviceFactoryBase* > factoryMap;
842 static factoryMap factories;
843 
844 TDESocketDevice* TDESocketDevice::createDefault(TDESocketBase* parent)
845 {
846  TDESocketDevice* device = dynamic_cast<TDESocketDevice*>(parent);
847  if (device != 0L)
848  return device;
849 
850  KSocksSocketDevice::initSocks();
851 
852  if (defaultImplFactory)
853  return defaultImplFactory->create(parent);
854 
855  // the really default
856  return new TDESocketDevice(parent);
857 }
858 
859 TDESocketDevice* TDESocketDevice::createDefault(TDESocketBase* parent, int capabilities)
860 {
861  TDESocketDevice* device = dynamic_cast<TDESocketDevice*>(parent);
862  if (device != 0L)
863  return device;
864 
865  TQMutexLocker locker(&defaultImplFactoryMutex);
866  factoryMap::ConstIterator it = factories.constBegin();
867  for ( ; it != factories.constEnd(); ++it)
868  if ((it.key() & capabilities) == capabilities)
869  // found a match
870  return it.data()->create(parent);
871 
872  return 0L; // no default
873 }
874 
875 TDESocketDeviceFactoryBase*
876 TDESocketDevice::setDefaultImpl(TDESocketDeviceFactoryBase* factory)
877 {
878  TQMutexLocker locker(&defaultImplFactoryMutex);
879  TDESocketDeviceFactoryBase* old = defaultImplFactory;
880  defaultImplFactory = factory;
881  return old;
882 }
883 
884 void TDESocketDevice::addNewImpl(TDESocketDeviceFactoryBase* factory, int capabilities)
885 {
886  TQMutexLocker locker(&defaultImplFactoryMutex);
887  if (factories.contains(capabilities))
888  delete factories[capabilities];
889  factories.insert(capabilities, factory);
890 }
891 
KNetwork::TDESocketDevice::readNotifier
TQSocketNotifier * readNotifier() const
Returns a socket notifier for input on this socket.
Definition: tdesocketdevice.cpp:614
KNetwork::TDESocketAddress
A generic socket address.
Definition: tdesocketaddress.h:423
KNetwork::TDESocketBase::setSocketDevice
virtual void setSocketDevice(TDESocketDevice *device)
Sets the socket implementation to be used on this socket.
Definition: tdesocketbase.cpp:136
KNetwork::TDESocketDevice::disconnect
virtual bool disconnect()
Disconnects this socket.
Definition: tdesocketdevice.cpp:333
KNetwork::TDESocketDevice::externalAddress
virtual TDESocketAddress externalAddress() const
Returns this socket's externally visible local address.
Definition: tdesocketdevice.cpp:607
KNetwork::TDESocketDevice::addNewImpl
static void addNewImpl(TDESocketDeviceFactoryBase *factory, int capabilities)
Adds a factory of TDESocketDevice objects to the list, along with its capabilities flag...
Definition: tdesocketdevice.cpp:884
KNetwork::TDESocketDevice::createNotifier
virtual TQSocketNotifier * createNotifier(TQSocketNotifier::Type type) const
Creates a socket notifier of the given type.
Definition: tdesocketdevice.cpp:796
KNetwork::TDESocketDevice::exceptionNotifier
TQSocketNotifier * exceptionNotifier() const
Returns a socket notifier for exceptional events on this socket.
Definition: tdesocketdevice.cpp:650
KNetwork::KResolverEntry
One resolution entry.
Definition: kresolver.h:66
KNetwork::TDESocketDevice::accept
virtual TDESocketDevice * accept()
Accepts a new incoming connection.
Definition: tdesocketdevice.cpp:309
KNetwork::TDESocketDevice::waitForMore
virtual TQ_LONG waitForMore(int msecs, bool *timeout=0L)
Returns the number of bytes available for reading without blocking.
Definition: tdesocketdevice.cpp:384
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::TDESocketDevice::create
virtual bool create(int family, int type, int protocol)
Creates a socket but don't connect or bind anywhere.
Definition: tdesocketdevice.cpp:201
KNetwork::TDESocketDevice::writeNotifier
TQSocketNotifier * writeNotifier() const
Returns a socket notifier for output on this socket.
Definition: tdesocketdevice.cpp:632
KNetwork::TDESocketDevice
Low-level socket functionality.
Definition: tdesocketdevice.h:50
KNetwork::TDESocketDevice::tqwriteBlock
virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len)
Writes data to the socket.
Definition: tdesocketdevice.cpp:506
KNetwork::KResolverEntry::protocol
int protocol() const
Retrieves the protocol associated with this entry.
Definition: kresolver.cpp:178
KNetwork::KResolverEntry::address
TDESocketAddress address() const
Retrieves the socket address associated with this entry.
Definition: kresolver.cpp:142
KNetwork
A namespace to store all networking-related (socket) classes.
Definition: kbufferedsocket.h:36
KNetwork::TDESocketAddress::setLength
TDESocketAddress & setLength(TQ_UINT16 len)
Sets the length of this socket structure.
Definition: tdesocketaddress.cpp:492
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::TDESocketDevice::setDefaultImpl
static TDESocketDeviceFactoryBase * setDefaultImpl(TDESocketDeviceFactoryBase *factory)
Sets the default TDESocketDevice implementation to use and return the old factory.
Definition: tdesocketdevice.cpp:876
KNetwork::KResolverEntry::length
TQ_UINT16 length() const
Retrieves the length of the socket address structure.
Definition: kresolver.cpp:148
KNetwork::TDESocketDevice::m_sockfd
int m_sockfd
The socket file descriptor.
Definition: tdesocketdevice.h:95
KNetwork::TDESocketDevice::createDefault
static TDESocketDevice * createDefault(TDESocketBase *parent)
Creates a new default TDESocketDevice object given the parent object.
Definition: tdesocketdevice.cpp:844
KNetwork::TDESocketAddress::address
const sockaddr * address() const
Returns the socket address structure, to be passed down to low level functions.
Definition: tdesocketaddress.cpp:461
KNetwork::TDESocketDevice::connect
virtual bool connect(const KResolverEntry &address)
Connect to a remote host.
Definition: tdesocketdevice.cpp:276
KNetwork::TDESocketDevice::tqreadBlock
virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen)
Reads data from this socket.
Definition: tdesocketdevice.cpp:422
KNetwork::TDESocketDevice::open
virtual bool open(TQ_OpenMode mode)
Reimplementation from TQIODevice.
Definition: tdesocketdevice.cpp:174
KNetwork::TDESocketBase::socketOptions
virtual int socketOptions() const
Retrieves the socket options that have been set.
Definition: tdesocketbase.cpp:71
KNetwork::KResolverEntry::socketType
int socketType() const
Retrieves the socket type associated with this entry.
Definition: kresolver.cpp:172
KNetwork::TDESocketDevice::setSocketOptions
virtual bool setSocketOptions(int opts)
This implementation sets the options on the socket.
Definition: tdesocketdevice.cpp:110
KNetwork::TDESocketDevice::bind
virtual bool bind(const KResolverEntry &address)
Binds this socket to the given address.
Definition: tdesocketdevice.cpp:232
KNetwork::TDESocketDevice::capabilities
virtual int capabilities() const
Returns the set of capabilities this socket class implements.
Definition: tdesocketdevice.h:134
KNetwork::KResolverEntry::family
int family() const
Retrieves the family associated with this socket address.
Definition: kresolver.cpp:154
KNetwork::TDESocketDevice::~TDESocketDevice
virtual ~TDESocketDevice()
Destructor.
Definition: tdesocketdevice.cpp:103
KNetwork::TDESocketBase
Basic socket functionality.
Definition: tdesocketbase.h:97
KNetwork::TDESocketDevice::poll
virtual bool poll(bool *input, bool *output, bool *exception=0L, int timeout=-1, bool *timedout=0L)
Executes a poll in the socket, via select(2) or poll(2).
Definition: tdesocketdevice.cpp:668
KNetwork::TDESocketAddress::family
int family() const
Returns the family of this address.
Definition: tdesocketaddress.cpp:499
KNetwork::TDESocketDevice::peekBlock
virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen)
Peeks data in the socket.
Definition: tdesocketdevice.cpp:464
KNetwork::TDESocketBase::setSocketOptions
virtual bool setSocketOptions(int opts)
Set the given socket options.
Definition: tdesocketbase.cpp:65
KNetwork::TDESocketDevice::close
virtual void close()
Closes the socket.
Definition: tdesocketdevice.cpp:180
KNetwork::TDESocketDevice::TDESocketDevice
TDESocketDevice(const TDESocketBase *=0L)
Default constructor.
Definition: tdesocketdevice.cpp:78
KNetwork::TDESocketDevice::listen
virtual bool listen(int backlog=5)
Puts this socket into listening mode.
Definition: tdesocketdevice.cpp:255
KNetwork::TDESocketAddress::length
TQ_UINT16 length() const
Returns the length of this socket address structure.
Definition: tdesocketaddress.cpp:485
KNetwork::TDESocketAddress::setFamily
virtual TDESocketAddress & setFamily(int family)
Sets the family of this object.
Definition: tdesocketaddress.cpp:506
KNetwork::TDESocketDevice::peerAddress
virtual TDESocketAddress peerAddress() const
Returns this socket's peer address.
Definition: tdesocketdevice.cpp:571
KNetwork::TDESocketDevice::localAddress
virtual TDESocketAddress localAddress() const
Returns this socket's local address.
Definition: tdesocketdevice.cpp:535

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.