• 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
61using namespace KNetwork;
62
63class KNetwork::TDESocketDevicePrivate
64{
65public:
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
78TDESocketDevice::TDESocketDevice(const TDESocketBase* parent)
79 : m_sockfd(-1), d(new TDESocketDevicePrivate)
80{
81 setSocketDevice(this);
82 if (parent)
83 setSocketOptions(parent->socketOptions());
84}
85
86TDESocketDevice::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
95TDESocketDevice::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
103TDESocketDevice::~TDESocketDevice()
104{
105 close(); // deletes the notifiers
106 unsetSocketDevice(); // prevent double deletion
107 delete d;
108}
109
110bool 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
174bool TDESocketDevice::open(int)
175{
176 resetError();
177 return false;
178}
179
180void 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
201bool 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
227bool TDESocketDevice::create(const KResolverEntry& address)
228{
229 return create(address.family(), address.socketType(), address.protocol());
230}
231
232bool 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
255bool 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
276bool 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
309TDESocketDevice* 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
333bool 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
367TQ_LONG TDESocketDevice::bytesAvailable() const
368{
369 if (m_sockfd == -1)
370 return -1; // there's nothing to read in a closed socket
371
372 int nchars;
373 if (ioctl(m_sockfd, FIONREAD, &nchars) == -1)
374 return -1; // error!
375
376 return nchars;
377}
378
379TQ_LONG TDESocketDevice::waitForMore(int msecs, bool *timeout)
380{
381 if (m_sockfd == -1)
382 return -1; // there won't ever be anything to read...
383
384 bool input;
385 if (!poll(&input, 0, 0, msecs, timeout))
386 return -1; // failed polling
387
388 return bytesAvailable();
389}
390
391static int do_read_common(int sockfd, char *data, TQ_ULONG maxlen, TDESocketAddress* from, ssize_t &retval, bool peek = false)
392{
393 socklen_t len;
394 if (from)
395 {
396 from->setLength(len = 128); // arbitrary length
397 retval = ::recvfrom(sockfd, data, maxlen, peek ? MSG_PEEK : 0, from->address(), &len);
398 }
399 else
400 retval = ::recvfrom(sockfd, data, maxlen, peek ? MSG_PEEK : 0, NULL, NULL);
401
402 if (retval == -1)
403 {
404 if (errno == EAGAIN || errno == EWOULDBLOCK)
405 return TDESocketDevice::WouldBlock;
406 else
407 return TDESocketDevice::UnknownError;
408 }
409 if (retval == 0)
410 return TDESocketDevice::RemotelyDisconnected;
411
412 if (from)
413 from->setLength(len);
414 return 0;
415}
416
417TQ_LONG TDESocketDevice::readBlock(char *data, TQ_ULONG maxlen)
418{
419 resetError();
420 if (m_sockfd == -1)
421 return -1;
422
423 if (maxlen == 0 || data == 0L)
424 return 0; // can't read
425
426 ssize_t retval;
427 int err = do_read_common(m_sockfd, data, maxlen, 0L, retval);
428
429 if (err)
430 {
431 setError(IO_ReadError, static_cast<SocketError>(err));
432 return -1;
433 }
434
435 return retval;
436}
437
438TQ_LONG TDESocketDevice::readBlock(char *data, TQ_ULONG maxlen, TDESocketAddress &from)
439{
440 resetError();
441 if (m_sockfd == -1)
442 return -1; // nothing to do here
443
444 if (data == 0L || maxlen == 0)
445 return 0; // user doesn't want to read
446
447 ssize_t retval;
448 int err = do_read_common(m_sockfd, data, maxlen, &from, retval);
449
450 if (err)
451 {
452 setError(IO_ReadError, static_cast<SocketError>(err));
453 return -1;
454 }
455
456 return retval;
457}
458
459TQ_LONG TDESocketDevice::peekBlock(char *data, TQ_ULONG maxlen)
460{
461 resetError();
462 if (m_sockfd == -1)
463 return -1;
464
465 if (maxlen == 0 || data == 0L)
466 return 0; // can't read
467
468 ssize_t retval;
469 int err = do_read_common(m_sockfd, data, maxlen, 0L, retval, true);
470
471 if (err)
472 {
473 setError(IO_ReadError, static_cast<SocketError>(err));
474 return -1;
475 }
476
477 return retval;
478}
479
480TQ_LONG TDESocketDevice::peekBlock(char *data, TQ_ULONG maxlen, TDESocketAddress& from)
481{
482 resetError();
483 if (m_sockfd == -1)
484 return -1; // nothing to do here
485
486 if (data == 0L || maxlen == 0)
487 return 0; // user doesn't want to read
488
489 ssize_t retval;
490 int err = do_read_common(m_sockfd, data, maxlen, &from, retval, true);
491
492 if (err)
493 {
494 setError(IO_ReadError, static_cast<SocketError>(err));
495 return -1;
496 }
497
498 return retval;
499}
500
501TQ_LONG TDESocketDevice::writeBlock(const char *data, TQ_ULONG len)
502{
503 return writeBlock(data, len, TDESocketAddress());
504}
505
506TQ_LONG TDESocketDevice::writeBlock(const char *data, TQ_ULONG len, const TDESocketAddress& to)
507{
508 resetError();
509 if (m_sockfd == -1)
510 return -1; // can't write to unopen socket
511
512 if (data == 0L || len == 0)
513 return 0; // nothing to be written
514
515 ssize_t retval = ::sendto(m_sockfd, data, len, 0, to.address(), to.length());
516 if (retval == -1)
517 {
518 if (errno == EAGAIN || errno == EWOULDBLOCK)
519 setError(IO_WriteError, WouldBlock);
520 else
521 setError(IO_WriteError, UnknownError);
522 return -1; // nothing written
523 }
524 else if (retval == 0)
525 setError(IO_WriteError, RemotelyDisconnected);
526
527 return retval;
528}
529
530TDESocketAddress TDESocketDevice::localAddress() const
531{
532 if (m_sockfd == -1)
533 return TDESocketAddress(); // not open, empty value
534
535 if (d->local.family() != AF_UNSPEC)
536 return d->local;
537
538 socklen_t len;
539 TDESocketAddress localAddress;
540 localAddress.setLength(len = 32); // arbitrary value
541 if (kde_getsockname(m_sockfd, localAddress.address(), &len) == -1)
542 // error!
543 return d->local = TDESocketAddress();
544
545#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
546 len = localAddress.address()->sa_len;
547#endif
548
549 if (len <= localAddress.length())
550 {
551 // it has fit already
552 localAddress.setLength(len);
553 return d->local = localAddress;
554 }
555
556 // no, the socket address is actually larger than we had anticipated
557 // call again
558 localAddress.setLength(len);
559 if (kde_getsockname(m_sockfd, localAddress.address(), &len) == -1)
560 // error!
561 return d->local = TDESocketAddress();
562
563 return d->local = localAddress;
564}
565
566TDESocketAddress TDESocketDevice::peerAddress() const
567{
568 if (m_sockfd == -1)
569 return TDESocketAddress(); // not open, empty value
570
571 if (d->peer.family() != AF_UNSPEC)
572 return d->peer;
573
574 socklen_t len;
575 TDESocketAddress peerAddress;
576 peerAddress.setLength(len = 32); // arbitrary value
577 if (kde_getpeername(m_sockfd, peerAddress.address(), &len) == -1)
578 // error!
579 return d->peer = TDESocketAddress();
580
581#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
582 len = peerAddress.address()->sa_len;
583#endif
584
585 if (len <= peerAddress.length())
586 {
587 // it has fit already
588 peerAddress.setLength(len);
589 return d->peer = peerAddress;
590 }
591
592 // no, the socket address is actually larger than we had anticipated
593 // call again
594 peerAddress.setLength(len);
595 if (kde_getpeername(m_sockfd, peerAddress.address(), &len) == -1)
596 // error!
597 return d->peer = TDESocketAddress();
598
599 return d->peer = peerAddress;
600}
601
602TDESocketAddress TDESocketDevice::externalAddress() const
603{
604 // for normal sockets, the externally visible address is the same
605 // as the local address
606 return localAddress();
607}
608
609TQSocketNotifier* TDESocketDevice::readNotifier() const
610{
611 if (d->input)
612 return d->input;
613
614 TQMutexLocker locker(mutex());
615 if (d->input)
616 return d->input;
617
618 if (m_sockfd == -1)
619 {
620 // socket doesn't exist; can't create notifier
621 return 0L;
622 }
623
624 return d->input = createNotifier(TQSocketNotifier::Read);
625}
626
627TQSocketNotifier* TDESocketDevice::writeNotifier() const
628{
629 if (d->output)
630 return d->output;
631
632 TQMutexLocker locker(mutex());
633 if (d->output)
634 return d->output;
635
636 if (m_sockfd == -1)
637 {
638 // socket doesn't exist; can't create notifier
639 return 0L;
640 }
641
642 return d->output = createNotifier(TQSocketNotifier::Write);
643}
644
645TQSocketNotifier* TDESocketDevice::exceptionNotifier() const
646{
647 if (d->exception)
648 return d->exception;
649
650 TQMutexLocker locker(mutex());
651 if (d->exception)
652 return d->exception;
653
654 if (m_sockfd == -1)
655 {
656 // socket doesn't exist; can't create notifier
657 return 0L;
658 }
659
660 return d->exception = createNotifier(TQSocketNotifier::Exception);
661}
662
663bool TDESocketDevice::poll(bool *input, bool *output, bool *exception,
664 int timeout, bool* timedout)
665{
666 if (m_sockfd == -1)
667 {
668 setError(IO_UnspecifiedError, NotCreated);
669 return false;
670 }
671
672 resetError();
673#ifdef HAVE_POLL
674 struct pollfd fds;
675 fds.fd = m_sockfd;
676 fds.events = 0;
677
678 if (input)
679 {
680 fds.events |= POLLIN;
681 *input = false;
682 }
683 if (output)
684 {
685 fds.events |= POLLOUT;
686 *output = false;
687 }
688 if (exception)
689 {
690 fds.events |= POLLPRI;
691 *exception = false;
692 }
693
694 int retval = ::poll(&fds, 1, timeout);
695 if (retval == -1)
696 {
697 setError(IO_UnspecifiedError, UnknownError);
698 return false;
699 }
700 if (retval == 0)
701 {
702 // timeout
703 if (timedout)
704 *timedout = true;
705 return true;
706 }
707
708 if (input && fds.revents & POLLIN)
709 *input = true;
710 if (output && fds.revents & POLLOUT)
711 *output = true;
712 if (exception && fds.revents & POLLPRI)
713 *exception = true;
714 if (timedout)
715 *timedout = false;
716
717 return true;
718#else
719 /*
720 * We don't have poll(2). We'll have to make do with select(2).
721 */
722
723 fd_set readfds, writefds, exceptfds;
724 fd_set *preadfds = 0L, *pwritefds = 0L, *pexceptfds = 0L;
725
726 if (input)
727 {
728 preadfds = &readfds;
729 FD_ZERO(preadfds);
730 FD_SET(m_sockfd, preadfds);
731 *input = false;
732 }
733 if (output)
734 {
735 pwritefds = &writefds;
736 FD_ZERO(pwritefds);
737 FD_SET(m_sockfd, pwritefds);
738 *output = false;
739 }
740 if (exception)
741 {
742 pexceptfds = &exceptfds;
743 FD_ZERO(pexceptfds);
744 FD_SET(m_sockfd, pexceptfds);
745 *exception = false;
746 }
747
748 int retval;
749 if (timeout < 0)
750 retval = select(m_sockfd + 1, preadfds, pwritefds, pexceptfds, 0L);
751 else
752 {
753 // convert the milliseconds to timeval
754 struct timeval tv;
755 tv.tv_sec = timeout / 1000;
756 tv.tv_usec = timeout % 1000 * 1000;
757
758 retval = select(m_sockfd + 1, preadfds, pwritefds, pexceptfds, &tv);
759 }
760
761 if (retval == -1)
762 {
763 setError(IO_UnspecifiedError, UnknownError);
764 return false;
765 }
766 if (retval == 0)
767 {
768 // timeout
769 if (timedout)
770 *timedout = true;
771 return true;
772 }
773
774 if (input && FD_ISSET(m_sockfd, preadfds))
775 *input = true;
776 if (output && FD_ISSET(m_sockfd, pwritefds))
777 *output = true;
778 if (exception && FD_ISSET(m_sockfd, pexceptfds))
779 *exception = true;
780
781 return true;
782#endif
783}
784
785bool TDESocketDevice::poll(int timeout, bool *timedout)
786{
787 bool input, output, exception;
788 return poll(&input, &output, &exception, timeout, timedout);
789}
790
791TQSocketNotifier* TDESocketDevice::createNotifier(TQSocketNotifier::Type type) const
792{
793 if (m_sockfd == -1)
794 return 0L;
795
796 return new TQSocketNotifier(m_sockfd, type);
797}
798
799namespace
800{
801 // simple class to avoid pointer stuff
802 template<class T> class ptr
803 {
804 typedef T type;
805 type* obj;
806 public:
807 ptr() : obj(0)
808 { }
809
810 ptr(const ptr<T>& other) : obj(other.obj)
811 { }
812
813 ptr(type* _obj) : obj(_obj)
814 { }
815
816 ~ptr()
817 { }
818
819 ptr<T>& operator=(const ptr<T>& other)
820 { obj = other.obj; return *this; }
821
822 ptr<T>& operator=(T* _obj)
823 { obj = _obj; return *this; }
824
825 type* operator->() const { return obj; }
826
827 operator T*() const { return obj; }
828
829 bool isNull() const
830 { return obj == 0; }
831 };
832}
833
834static TDESocketDeviceFactoryBase* defaultImplFactory;
835static TQMutex defaultImplFactoryMutex;
836typedef TQMap<int, TDESocketDeviceFactoryBase* > factoryMap;
837static factoryMap factories;
838
839TDESocketDevice* TDESocketDevice::createDefault(TDESocketBase* parent)
840{
841 TDESocketDevice* device = dynamic_cast<TDESocketDevice*>(parent);
842 if (device != 0L)
843 return device;
844
845 KSocksSocketDevice::initSocks();
846
847 if (defaultImplFactory)
848 return defaultImplFactory->create(parent);
849
850 // the really default
851 return new TDESocketDevice(parent);
852}
853
854TDESocketDevice* TDESocketDevice::createDefault(TDESocketBase* parent, int capabilities)
855{
856 TDESocketDevice* device = dynamic_cast<TDESocketDevice*>(parent);
857 if (device != 0L)
858 return device;
859
860 TQMutexLocker locker(&defaultImplFactoryMutex);
861 factoryMap::ConstIterator it = factories.constBegin();
862 for ( ; it != factories.constEnd(); ++it)
863 if ((it.key() & capabilities) == capabilities)
864 // found a match
865 return it.data()->create(parent);
866
867 return 0L; // no default
868}
869
870TDESocketDeviceFactoryBase*
871TDESocketDevice::setDefaultImpl(TDESocketDeviceFactoryBase* factory)
872{
873 TQMutexLocker locker(&defaultImplFactoryMutex);
874 TDESocketDeviceFactoryBase* old = defaultImplFactory;
875 defaultImplFactory = factory;
876 return old;
877}
878
879void TDESocketDevice::addNewImpl(TDESocketDeviceFactoryBase* factory, int capabilities)
880{
881 TQMutexLocker locker(&defaultImplFactoryMutex);
882 if (factories.contains(capabilities))
883 delete factories[capabilities];
884 factories.insert(capabilities, factory);
885}
886
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::resetError
void resetError()
Resets the socket error code and the I/O Device's status.
Definition: tdesocketbase.cpp:315
KNetwork::KResolverEntry
One resolution entry.
Definition: kresolver.h:67
KNetwork::KResolverEntry::protocol
int protocol() const
Retrieves the protocol associated with this entry.
Definition: kresolver.cpp:178
KNetwork::KResolverEntry::length
TQ_UINT16 length() const
Retrieves the length of the socket address structure.
Definition: kresolver.cpp:148
KNetwork::KResolverEntry::socketType
int socketType() const
Retrieves the socket type associated with this entry.
Definition: kresolver.cpp:172
KNetwork::KResolverEntry::address
TDESocketAddress address() const
Retrieves the socket address associated with this entry.
Definition: kresolver.cpp:142
KNetwork::KResolverEntry::family
int family() const
Retrieves the family associated with this socket address.
Definition: kresolver.cpp:154
KNetwork::TDESocketAddress
A generic socket address.
Definition: tdesocketaddress.h:424
KNetwork::TDESocketAddress::family
int family() const
Returns the family of this address.
Definition: tdesocketaddress.cpp:499
KNetwork::TDESocketAddress::setFamily
virtual TDESocketAddress & setFamily(int family)
Sets the family of this object.
Definition: tdesocketaddress.cpp:506
KNetwork::TDESocketAddress::setLength
TDESocketAddress & setLength(TQ_UINT16 len)
Sets the length of this socket structure.
Definition: tdesocketaddress.cpp:492
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::TDESocketAddress::length
TQ_UINT16 length() const
Returns the length of this socket address structure.
Definition: tdesocketaddress.cpp:485
KNetwork::TDESocketBase
Basic socket functionality.
Definition: tdesocketbase.h:98
KNetwork::TDESocketBase::setSocketDevice
virtual void setSocketDevice(TDESocketDevice *device)
Sets the socket implementation to be used on this socket.
Definition: tdesocketbase.cpp:136
KNetwork::TDESocketBase::mutex
TQMutex * mutex() const
Returns the internal mutex for this class.
Definition: tdesocketbase.cpp:278
KNetwork::TDESocketBase::SocketError
SocketError
Possible socket error codes.
Definition: tdesocketbase.h:153
KNetwork::TDESocketBase::socketOptions
virtual int socketOptions() const
Retrieves the socket options that have been set.
Definition: tdesocketbase.cpp:71
KNetwork::TDESocketBase::setSocketOptions
virtual bool setSocketOptions(int opts)
Set the given socket options.
Definition: tdesocketbase.cpp:65
KNetwork::TDESocketDevice
Low-level socket functionality.
Definition: tdesocketdevice.h:51
KNetwork::TDESocketDevice::capabilities
virtual int capabilities() const
Returns the set of capabilities this socket class implements.
Definition: tdesocketdevice.h:134
KNetwork::TDESocketDevice::createNotifier
virtual TQSocketNotifier * createNotifier(TQSocketNotifier::Type type) const
Creates a socket notifier of the given type.
Definition: tdesocketdevice.cpp:791
KNetwork::TDESocketDevice::externalAddress
virtual TDESocketAddress externalAddress() const
Returns this socket's externally visible local address.
Definition: tdesocketdevice.cpp:602
KNetwork::TDESocketDevice::disconnect
virtual bool disconnect()
Disconnects this socket.
Definition: tdesocketdevice.cpp:333
KNetwork::TDESocketDevice::peekBlock
virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen)
Peeks data in the socket.
Definition: tdesocketdevice.cpp:459
KNetwork::TDESocketDevice::setDefaultImpl
static TDESocketDeviceFactoryBase * setDefaultImpl(TDESocketDeviceFactoryBase *factory)
Sets the default TDESocketDevice implementation to use and return the old factory.
Definition: tdesocketdevice.cpp:871
KNetwork::TDESocketDevice::writeBlock
virtual TQ_LONG writeBlock(const char *data, TQ_ULONG len)
Writes data to the socket.
Definition: tdesocketdevice.cpp:501
KNetwork::TDESocketDevice::setSocketOptions
virtual bool setSocketOptions(int opts)
This implementation sets the options on the socket.
Definition: tdesocketdevice.cpp:110
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:663
KNetwork::TDESocketDevice::createDefault
static TDESocketDevice * createDefault(TDESocketBase *parent)
Creates a new default TDESocketDevice object given the parent object.
Definition: tdesocketdevice.cpp:839
KNetwork::TDESocketDevice::close
virtual void close()
Closes the socket.
Definition: tdesocketdevice.cpp:180
KNetwork::TDESocketDevice::readBlock
virtual TQ_LONG readBlock(char *data, TQ_ULONG maxlen)
Reads data from this socket.
Definition: tdesocketdevice.cpp:417
KNetwork::TDESocketDevice::waitForMore
virtual TQ_LONG waitForMore(int msecs, bool *timeout=0L)
Waits up to msecs for more data to be available on this socket.
Definition: tdesocketdevice.cpp:379
KNetwork::TDESocketDevice::m_sockfd
int m_sockfd
The socket file descriptor.
Definition: tdesocketdevice.h:95
KNetwork::TDESocketDevice::accept
virtual TDESocketDevice * accept()
Accepts a new incoming connection.
Definition: tdesocketdevice.cpp:309
KNetwork::TDESocketDevice::bytesAvailable
virtual TQ_LONG bytesAvailable() const
Returns the number of bytes available for reading without blocking.
Definition: tdesocketdevice.cpp:367
KNetwork::TDESocketDevice::~TDESocketDevice
virtual ~TDESocketDevice()
Destructor.
Definition: tdesocketdevice.cpp:103
KNetwork::TDESocketDevice::connect
virtual bool connect(const KResolverEntry &address)
Connect to a remote host.
Definition: tdesocketdevice.cpp:276
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:879
KNetwork::TDESocketDevice::listen
virtual bool listen(int backlog=5)
Puts this socket into listening mode.
Definition: tdesocketdevice.cpp:255
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::open
virtual bool open(int mode)
Reimplementation from TQIODevice.
Definition: tdesocketdevice.cpp:174
KNetwork::TDESocketDevice::writeNotifier
TQSocketNotifier * writeNotifier() const
Returns a socket notifier for output on this socket.
Definition: tdesocketdevice.cpp:627
KNetwork::TDESocketDevice::localAddress
virtual TDESocketAddress localAddress() const
Returns this socket's local address.
Definition: tdesocketdevice.cpp:530
KNetwork::TDESocketDevice::peerAddress
virtual TDESocketAddress peerAddress() const
Returns this socket's peer address.
Definition: tdesocketdevice.cpp:566
KNetwork::TDESocketDevice::exceptionNotifier
TQSocketNotifier * exceptionNotifier() const
Returns a socket notifier for exceptional events on this socket.
Definition: tdesocketdevice.cpp:645
KNetwork::TDESocketDevice::bind
virtual bool bind(const KResolverEntry &address)
Binds this socket to the given address.
Definition: tdesocketdevice.cpp:232
KNetwork::TDESocketDevice::readNotifier
TQSocketNotifier * readNotifier() const
Returns a socket notifier for input on this socket.
Definition: tdesocketdevice.cpp:609
KNetwork
A namespace to store all networking-related (socket) classes.
Definition: kbufferedsocket.h:36

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
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.