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

tdecore

  • tdecore
  • network
tdesocketbase.h
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 /*
26  * Even before our #ifdef, clean up the namespace
27  */
28 #ifdef socket
29 #undef socket
30 #endif
31 
32 #ifdef bind
33 #undef bind
34 #endif
35 
36 #ifdef listen
37 #undef listen
38 #endif
39 
40 #ifdef connect
41 #undef connect
42 #endif
43 
44 #ifdef accept
45 #undef accept
46 #endif
47 
48 #ifdef getpeername
49 #undef getpeername
50 #endif
51 
52 #ifdef getsockname
53 #undef getsockname
54 #endif
55 
56 #ifndef TDESOCKETBASE_H
57 #define TDESOCKETBASE_H
58 
59 #include <tqiodevice.h>
60 #include <tqstring.h>
61 
62 #include "tdesocketaddress.h"
63 #include <tdelibs_export.h>
64 
65 /*
66  * This is extending TQIODevice's error codes
67  *
68  * According to tqiodevice.h, the last error is IO_UnspecifiedError
69  * These errors will never occur in functions declared in TQIODevice
70  * (except open, but you shouldn't call open)
71  */
72 #define IO_ListenError (IO_UnspecifiedError+1)
73 #define IO_AcceptError (IO_UnspecifiedError+2)
74 #define IO_LookupError (IO_UnspecifiedError+3)
75 #define IO_SocketCreateError (IO_UnspecifiedError+4)
76 #define IO_BindError (IO_UnspecifiedError+5)
77 
78 class TQMutex;
79 
80 namespace KNetwork {
81 
82 class KResolverEntry;
83 class TDESocketDevice;
84 
85 class TDESocketBasePrivate;
97 class TDECORE_EXPORT TDESocketBase
98 {
99 public:
118  enum SocketOptions
119  {
120  Blocking = 0x01,
121  AddressReuseable = 0x02,
122  IPv6Only = 0x04,
123  Keepalive = 0x08,
124  Broadcast = 0x10
125  };
126 
152  enum SocketError
153  {
154  NoError = 0,
155  LookupFailure,
156  AddressInUse,
157  AlreadyCreated,
158  AlreadyBound,
159  AlreadyConnected,
160  NotConnected,
161  NotBound,
162  NotCreated,
163  WouldBlock,
164  ConnectionRefused,
165  ConnectionTimedOut,
166  InProgress,
167  NetFailure,
168  NotSupported,
169  Timeout,
170  UnknownError,
171  RemotelyDisconnected
172  };
173 
174 public:
178  TDESocketBase();
179 
183  virtual ~TDESocketBase();
184 
185  /*
186  * The following functions are shared by all descended classes and will have
187  * to be reimplemented.
188  */
189 
190 protected:
204  virtual bool setSocketOptions(int opts);
205 
215  virtual int socketOptions() const;
216 
217 public:
233  virtual bool setBlocking(bool enable);
234 
241  bool blocking() const;
242 
257  virtual bool setAddressReuseable(bool enable);
258 
265  bool addressReuseable() const;
266 
282  virtual bool setIPv6Only(bool enable);
283 
290  bool isIPv6Only() const;
291 
303  virtual bool setBroadcast(bool enable);
304 
311  bool broadcast() const;
312 
319  TDESocketDevice* socketDevice() const;
320 
334  virtual void setSocketDevice(TDESocketDevice* device);
335 
357  int setRequestedCapabilities(int add, int remove = 0);
358 
359 protected:
364  bool hasDevice() const;
365 
371  void setError(SocketError error);
372 
373 public:
378  SocketError error() const;
379 
383  inline TQString errorString() const
384  { return errorString(error()); }
385 
401  TQMutex* mutex() const;
402 
403 public:
409  static TQString errorString(SocketError code);
410 
419  static bool isFatalError(int code);
420 
421 private:
424  void unsetSocketDevice();
425 
426  TDESocketBase(const TDESocketBase&);
427  TDESocketBase& operator =(const TDESocketBase&);
428 
429  TDESocketBasePrivate *d;
430 
431  friend class TDESocketDevice;
432 };
433 
443 class TDECORE_EXPORT KActiveSocketBase: public TQIODevice, virtual public TDESocketBase
444 {
445 public:
449  KActiveSocketBase();
450 
454  virtual ~KActiveSocketBase();
455 
466  virtual bool bind(const KResolverEntry& address) = 0;
467 
484  virtual bool connect(const KResolverEntry& address) = 0;
485 
501  virtual bool disconnect() = 0;
502 
507  virtual Offset size() const
508  { return 0; }
509 
514  virtual Offset at() const
515  { return 0; }
516 
521  virtual bool at(Offset)
522  { return false; }
523 
528  virtual bool atEnd() const
529  { return true; }
530 
535  virtual TQ_LONG bytesAvailable() const = 0;
536 
548  virtual TQ_LONG waitForMore(int msecs, bool *timeout = 0L) = 0;
549 
556  virtual TQ_LONG readBlock(char *data, TQ_ULONG len) = 0;
557 
569  virtual TQ_LONG readBlock(char *data, TQ_ULONG maxlen, TDESocketAddress& from) = 0;
570 
582  virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen) = 0;
583 
596  virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen, TDESocketAddress& from) = 0;
597 
604  virtual TQ_LONG writeBlock(const char *data, TQ_ULONG len) = 0;
605 
617  virtual TQ_LONG writeBlock(const char *data, TQ_ULONG len, const TDESocketAddress& to) = 0;
618 
623  virtual int getch();
624 
629  virtual int putch(int ch);
630 
635  virtual int ungetch(int)
636  { return -1; }
637 
641  virtual TDESocketAddress localAddress() const = 0;
642 
648  virtual TDESocketAddress peerAddress() const = 0;
649 
650  // FIXME KDE 4.0:
651  // enable this function
652 #if 0
656  virtual TDESocketAddress externalAddress() const = 0;
657 #endif
658 
659 protected:
666  void setError(int status, SocketError error);
667 
671  void resetError();
672 };
673 
683 class TDECORE_EXPORT KPassiveSocketBase: virtual public TDESocketBase
684 {
685 public:
689  KPassiveSocketBase();
690 
694  virtual ~KPassiveSocketBase();
695 
706  virtual bool bind(const KResolverEntry& address) = 0;
707 
722  virtual bool listen(int backlog) = 0;
723 
728  virtual void close() = 0;
729 
743  virtual KActiveSocketBase* accept() = 0;
744 
748  virtual TDESocketAddress localAddress() const = 0;
749 
753  virtual TDESocketAddress externalAddress() const = 0;
754 
755 private:
756  KPassiveSocketBase(const KPassiveSocketBase&);
757  KPassiveSocketBase& operator = (const KPassiveSocketBase&);
758 };
759 
760 } // namespace KNetwork
761 
762 #endif
KNetwork::KActiveSocketBase
Abstract class for active sockets.
Definition: tdesocketbase.h:444
KNetwork::KActiveSocketBase::at
virtual bool at(Offset)
This call is not supported on sockets.
Definition: tdesocketbase.h:521
KNetwork::KActiveSocketBase::disconnect
virtual bool disconnect()=0
Disconnects this socket from a connection, if possible.
KNetwork::KActiveSocketBase::peekBlock
virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen, TDESocketAddress &from)=0
This is an overloaded member function, provided for convenience. It differs from the above function o...
KNetwork::KActiveSocketBase::size
virtual Offset size() const
This call is not supported on sockets.
Definition: tdesocketbase.h:507
KNetwork::KActiveSocketBase::ungetch
virtual int ungetch(int)
This call is not supported on sockets.
Definition: tdesocketbase.h:635
KNetwork::KActiveSocketBase::writeBlock
virtual TQ_LONG writeBlock(const char *data, TQ_ULONG len, const TDESocketAddress &to)=0
This is an overloaded member function, provided for convenience. It differs from the above function o...
KNetwork::KActiveSocketBase::readBlock
virtual TQ_LONG readBlock(char *data, TQ_ULONG maxlen, TDESocketAddress &from)=0
This is an overloaded member function, provided for convenience. It differs from the above function o...
KNetwork::KActiveSocketBase::connect
virtual bool connect(const KResolverEntry &address)=0
Connect to a remote host.
KNetwork::KActiveSocketBase::peerAddress
virtual TDESocketAddress peerAddress() const =0
Return this socket's peer address, if we are connected.
KNetwork::KActiveSocketBase::bind
virtual bool bind(const KResolverEntry &address)=0
Binds this socket to the given address.
KNetwork::KActiveSocketBase::localAddress
virtual TDESocketAddress localAddress() const =0
Returns this socket's local address.
KNetwork::KActiveSocketBase::waitForMore
virtual TQ_LONG waitForMore(int msecs, bool *timeout=0L)=0
Waits up to msecs for more data to be available on this socket.
KNetwork::KActiveSocketBase::bytesAvailable
virtual TQ_LONG bytesAvailable() const =0
Returns the number of bytes available for reading without blocking.
KNetwork::KActiveSocketBase::atEnd
virtual bool atEnd() const
This call is not supported on sockets.
Definition: tdesocketbase.h:528
KNetwork::KActiveSocketBase::at
virtual Offset at() const
This call is not supported on sockets.
Definition: tdesocketbase.h:514
KNetwork::KActiveSocketBase::readBlock
virtual TQ_LONG readBlock(char *data, TQ_ULONG len)=0
Reads data from the socket.
KNetwork::KActiveSocketBase::writeBlock
virtual TQ_LONG writeBlock(const char *data, TQ_ULONG len)=0
Writes the given data to the socket.
KNetwork::KActiveSocketBase::peekBlock
virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen)=0
Peeks the data in the socket.
KNetwork::KPassiveSocketBase
Abstract base class for passive sockets.
Definition: tdesocketbase.h:684
KNetwork::KPassiveSocketBase::accept
virtual KActiveSocketBase * accept()=0
Accepts a new incoming connection.
KNetwork::KPassiveSocketBase::localAddress
virtual TDESocketAddress localAddress() const =0
Returns this socket's local address.
KNetwork::KPassiveSocketBase::listen
virtual bool listen(int backlog)=0
Puts this socket into listening mode.
KNetwork::KPassiveSocketBase::externalAddress
virtual TDESocketAddress externalAddress() const =0
Returns this socket's externally-visible address if known.
KNetwork::KPassiveSocketBase::close
virtual void close()=0
Closes this socket.
KNetwork::KPassiveSocketBase::bind
virtual bool bind(const KResolverEntry &address)=0
Binds this socket to the given address.
KNetwork::KResolverEntry
One resolution entry.
Definition: kresolver.h:67
KNetwork::TDESocketAddress
A generic socket address.
Definition: tdesocketaddress.h:424
KNetwork::TDESocketBase
Basic socket functionality.
Definition: tdesocketbase.h:98
KNetwork::TDESocketBase::errorString
TQString errorString() const
Returns the error string corresponding to this error condition.
Definition: tdesocketbase.h:383
KNetwork::TDESocketBase::SocketError
SocketError
Possible socket error codes.
Definition: tdesocketbase.h:153
KNetwork::TDESocketBase::SocketOptions
SocketOptions
Possible socket options.
Definition: tdesocketbase.h:119
KNetwork::TDESocketDevice
Low-level socket functionality.
Definition: tdesocketdevice.h:51
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.1
This website is maintained by Timothy Pearson.