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

tdecore

  • tdecore
kextsock.h
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2000-2004 Thiago Macieira <thiago.macieira@kdemail.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 #ifndef KEXTSOCK_H
21 #define KEXTSOCK_H
22 
23 #include "tdelibs_export.h"
24 
25 #ifdef Q_MOC_RUN
26 #define Q_OS_UNIX
27 #endif // Q_MOC_RUN
28 
29 #ifdef Q_OS_UNIX
30 
31 #include <sys/time.h>
32 
33 #include <tqstring.h>
34 #include <tqptrlist.h>
35 #include <tqiodevice.h>
36 
37 #include "kbufferedio.h"
38 #include "ksockaddr.h"
39 
40 /* External reference to netdb.h */
41 struct addrinfo;
42 struct kde_addrinfo;
43 class KAddressInfo; /* our abstraction of it */
44 class TQSocketNotifier;
45 
46 /*
47  * This is extending QIODevice's error codes
48  *
49  * According to tqiodevice.h, the last error is IO_UnspecifiedError
50  * These errors will never occur in functions declared in QIODevice
51  * (except open, but you shouldn't call open)
52  */
53 #define IO_ListenError (IO_UnspecifiedError+1)
54 #define IO_AcceptError (IO_UnspecifiedError+2)
55 #define IO_LookupError (IO_UnspecifiedError+3)
56 
57 class KExtendedSocketPrivate;
95 class TDECORE_EXPORT KExtendedSocket: public TDEBufferedIO // public TQObject, public QIODevice
96 {
97  Q_OBJECT
98 
99 
100 public:
104  enum Flags
105  {
106  /* socket address families */
107  /*
108  * NOTE: if you change this, you have to change function valid_socket() as well
109  * These values are hard coded!
110  */
111  anySocket = 0x00,
112  knownSocket = 0x01,
113  unixSocket = knownSocket | 0x02,
114  inetSocket = knownSocket | 0x04,
115  ipv4Socket = inetSocket | 0x100,
116  ipv6Socket = inetSocket | 0x200,
117 
118  passiveSocket = 0x1000, /* passive socket (i.e., one that accepts connections) */
119  canonName = 0x2000, /* request that the canon name be found */
120  noResolve = 0x4000, /* do not attempt to resolve, treat as numeric host */
121 
122  streamSocket = 0x8000, /* request a streaming socket (e.g., TCP) */
123  datagramSocket = 0x10000, /* request a datagram socket (e.g., UDP) */
124  rawSocket = 0x20000, /* request a raw socket. This probably requires privileges */
125 
126  inputBufferedSocket = 0x200000, /* buffer input in this socket */
127  outputBufferedSocket = 0x400000, /* buffer output in this socket */
128  bufferedSocket = 0x600000 /* make this a fully buffered socket */
129  };
130 
136  enum SockStatus
137  {
138  // the numbers are scattered so that we leave room for future expansion
139  error = -1, // invalid status!
140 
141  nothing = 0, // no status, the class has just been created
142 
143  lookupInProgress = 50, // lookup is in progress. Signals will be sent
144  lookupDone = 70, // lookup has been done. Flags cannot be changed
145  // from this point on
146 
147  created = 100, // ::socket() has been called, a socket exists
148  bound = 140, // socket has been bound
149 
150  connecting = 200, // socket is connecting (not passiveSocket)
151  connected = 220, // socket has connected (not passiveSocket)
152 
153  listening = 200, // socket is listening (passiveSocket)
154  accepting = 220, // socket is accepting (passiveSocket)
155 
156  closing = 350, // socket is closing (delayed close)
157 
158  done = 400 // socket has been closed
159  };
160 
161 public:
165  KExtendedSocket();
166 
183  KExtendedSocket(const TQString& host, int port, int flags = 0);
184 
201  KExtendedSocket(const TQString& host, const TQString& service, int flags = 0);
202 
207  virtual ~KExtendedSocket();
208 
214 #ifdef USE_QT3
215  void reset();
216 #endif // USE_QT3
217 #ifdef USE_QT4
218  bool reset();
219 #endif // USE_QT4
220 
221  /*
222  * --- status, flags and internal variables --- *
223  */
224 
230  int socketStatus() const;
231 
238  int systemError() const;
239 
245  int setSocketFlags(int flags);
246 
252  int socketFlags() const;
253 
267  bool setHost(const TQString& host);
268 
273  TQString host() const;
274 
279  bool setPort(int port);
280 
290  bool setPort(const TQString& port);
291 
296  TQString port() const;
297 
307  bool setAddress(const TQString& host, int port);
308 
318  bool setAddress(const TQString& host, const TQString& serv);
319 
325  bool setBindHost(const TQString& host);
326 
331  bool unsetBindHost();
332 
337  TQString bindHost() const;
338 
344  bool setBindPort(int port);
345 
351  bool setBindPort(const TQString& service);
352 
357  bool unsetBindPort();
358 
363  TQString bindPort() const;
364 
372  bool setBindAddress(const TQString& host, int port);
373 
381  bool setBindAddress(const TQString& host, const TQString& service);
382 
388  bool unsetBindAddress();
389 
401  bool setTimeout(int secs, int usecs = 0);
402 
407  timeval timeout() const;
408 
417  bool setBlockingMode(bool enable);
418 
423  bool blockingMode();
424 
434  bool setAddressReusable(bool enable);
435 
440  bool addressReusable();
441 
460  bool setIPv6Only(bool enable);
461 
468  bool isIPv6Only();
469 
487  virtual bool setBufferSize(int rsize, int wsize = -2);
488 
494  const ::TDESocketAddress *localAddress();
495 
502  const ::TDESocketAddress *peerAddress();
503 
508  inline int fd() const
509  { return sockfd; }
510 
511  /*
512  * -- socket creation -- *
513  */
514 
522  virtual int lookup();
523 
542  virtual int startAsyncLookup();
543 
547  virtual void cancelAsyncLookup();
548 
556  virtual int listen(int N = 5); // 5 is arbitrary
557 
572  virtual int accept(KExtendedSocket *&sock);
573 
597  virtual int connect();
598 
613  virtual int startAsyncConnect();
614 
618  virtual void cancelAsyncConnect();
619 
630  virtual bool open(TQ_OpenMode mode = (TQ_OpenMode)(IO_Raw | IO_ReadWrite));
631 
639  virtual void close();
640 
646  virtual void closeNow();
647 
661  virtual void release();
662 
663  /*
664  * -- I/O --
665  */
666 
682  virtual void flush();
683 
688 #ifdef USE_QT3
689  virtual inline TQ_ULONG size() const
690 #endif // USE_QT3
691 #ifdef USE_QT4
692  virtual inline qint64 size() const
693 #endif // USE_QT4
694  { return 0; }
695 
700  virtual inline TQ_ULONG at() const
701  { return 0; }
702 
708  virtual inline bool at(int i)
709  { Q_UNUSED(i);return true; }
710 
716  virtual inline bool atEnd() const
717  { return false; }
718 
748  virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen);
749 
773  virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len);
774 
789  virtual int peekBlock(char *data, uint maxlen);
790 
797  virtual int unreadBlock(const char *data, uint len);
798 
808 #ifdef USE_QT3
809  virtual int bytesAvailable() const;
810 #endif // USE_QT3
811 #ifdef USE_QT4
812  virtual qint64 bytesAvailable() const;
813 #endif // USE_QT4
814 
824  virtual int waitForMore(int msec);
825 
830  virtual int getch();
831 
837  virtual int putch(int ch);
838 
843  virtual int ungetch(int)
844  { return -1; }
845 
856  virtual void enableRead(bool enable);
857 
867  virtual void enableWrite(bool enable);
868 
869 signals:
875  void lookupFinished(int count);
876 
880  void connectionSuccess();
881 
887  void connectionFailed(int error);
888 
894  void readyAccept();
895 
896 protected:
897  int sockfd; // file descriptor of the socket
898 
899 protected slots:
900 
901  void socketActivityRead();
902  void socketActivityWrite();
903  void dnsResultsReady();
904  void startAsyncConnectSlot();
905  void connectionEvent();
906 
907 protected:
908 
909  TQSocketNotifier *readNotifier();
910  TQSocketNotifier *writeNotifier();
911 
912 private:
913 
914  // protection against accidental use
915  KExtendedSocket(KExtendedSocket&);
916  KExtendedSocket& operator=(KExtendedSocket&);
917 
922  static int doLookup(const TQString& host, const TQString& serv, addrinfo& hint,
923  kde_addrinfo** result);
924 
925 protected:
929  void setError(int errorkind, int error);
930 
931  inline void cleanError()
932  { setError(IO_Ok, 0); }
933 
937  void setSocketStatus(int status);
938 
939 public:
953  static int resolve(sockaddr* sock, ksocklen_t len, TQString& host, TQString& port, int flags = 0) KDE_DEPRECATED;
954 
967  static int resolve(::TDESocketAddress* sock, TQString& host, TQString& port, int flags = 0) KDE_DEPRECATED;
968 
989  static TQPtrList<KAddressInfo> lookup(const TQString& host, const TQString& port, int flags = 0, int *error = 0) KDE_DEPRECATED;
990 
997  static ::TDESocketAddress *localAddress(int fd) KDE_DEPRECATED;
998 
1006  static ::TDESocketAddress *peerAddress(int fd) KDE_DEPRECATED;
1007 
1014  static TQString strError(int code, int syserr);
1015 
1025  static bool setAddressReusable(int fd, bool enable) KDE_DEPRECATED;
1026 
1027 protected:
1028  virtual void virtual_hook( int id, void* data );
1029 private:
1030  KExtendedSocketPrivate *d;
1031 
1032  friend class TDESocket;
1033  friend class TDEServerSocket;
1034 };
1035 
1042 class TDECORE_EXPORT KAddressInfo
1043 {
1044 private:
1045  addrinfo *ai;
1046  ::TDESocketAddress *addr;
1047 
1048  inline KAddressInfo() : ai(0), addr(0)
1049  { }
1050 
1051  // KAddressInfo(addrinfo *ai);
1052  KAddressInfo(KAddressInfo&) { }
1053  KAddressInfo& operator=(KAddressInfo&) { return *this; }
1054 
1055 public:
1056  ~KAddressInfo();
1057 
1062  inline KDE_DEPRECATED operator const ::TDESocketAddress*() const
1063  { return addr; }
1064 
1068  inline KDE_DEPRECATED operator const addrinfo&() const
1069  { return *ai; }
1070 
1075  inline KDE_DEPRECATED operator const addrinfo*() const
1076  { return ai; }
1077 
1083  inline KDE_DEPRECATED const ::TDESocketAddress* address() const
1084  { return addr; }
1085 
1090  int flags() const KDE_DEPRECATED;
1091 
1096  int family() const KDE_DEPRECATED;
1097 
1102  int socktype() const KDE_DEPRECATED;
1103 
1108  int protocol() const KDE_DEPRECATED;
1109 
1110 
1116  const char* canonname() const KDE_DEPRECATED;
1117 
1122  inline int length() const
1123  { if (addr) return addr->size(); return 0; }
1124 
1125  friend class KExtendedSocket;
1126 };
1127 
1128 #endif //Q_OS_UNIX
1129 
1130 #endif // KEXTSOCK_H
KExtendedSocket::ungetch
virtual int ungetch(int)
Unreads one character from the stream.
Definition: kextsock.h:843
TDESocketAddress::size
virtual ksocklen_t size() const
Returns sockaddr structure size.
Definition: ksockaddr.h:85
KAddressInfo::address
KDE_DEPRECATEDconst::TDESocketAddress * address() const
Returns the KAddressInfo's TDESocketAddress.
Definition: kextsock.h:1083
flush
kndbgstream & flush(kndbgstream &s)
Does nothing.
Definition: kdebug.h:589
KAsyncIO::enableWrite
virtual void enableWrite(bool enable)=0
Toggles the emission of the readyWrite() signal whenever the device is ready for writing.
TDEServerSocket
Monitors a port for incoming TCP/IP connections.
Definition: ksock.h:254
KExtendedSocket::at
virtual bool at(int i)
Returns true if we are at position.
Definition: kextsock.h:708
TDESocketAddress
A socket address.
Definition: ksockaddr.h:46
TDEBufferedIO::unreadBlock
virtual int unreadBlock(const char *data, uint len)
Unreads some data.
Definition: kbufferedio.cpp:165
KExtendedSocket::fd
int fd() const
Returns the file descriptor.
Definition: kextsock.h:508
TDESocket
A TCP/IP client socket.
Definition: ksock.h:91
TDEBufferedIO::setBufferSize
virtual bool setBufferSize(int rsize, int wsize=-2)
Sets the internal buffer size to value.
Definition: kbufferedio.cpp:104
KAddressInfo
Definition: kextsock.h:1042
TDEBufferedIO::peekBlock
virtual int peekBlock(char *data, uint maxlen)=0
Reads into the user buffer at most maxlen bytes, but does not consume that data from the read buffer...
TDEBufferedIO::closeNow
virtual void closeNow()=0
Closes the stream now, discarding the contents of the write buffer.
KExtendedSocket
The extended socket class.
Definition: kextsock.h:95
KExtendedSocket::SockStatus
SockStatus
status of the class The status are sequential.
Definition: kextsock.h:136
KExtendedSocket::Flags
Flags
flags that can be passed down to the member functions
Definition: kextsock.h:104
KAsyncIO::enableRead
virtual void enableRead(bool enable)=0
Toggles the emission of the readyRead() signal whenever the device is ready for reading.
KExtendedSocket::at
virtual TQ_ULONG at() const
Returns length of this socket.
Definition: kextsock.h:700
KExtendedSocket::atEnd
virtual bool atEnd() const
Returns true if we are at the end.
Definition: kextsock.h:716
TDEBufferedIO
This abstract class implements basic functionality for buffered input/output.
Definition: kbufferedio.h:56
TDEBufferedIO::waitForMore
virtual int waitForMore(int msec)=0
Returns the number of bytes available for reading in the read buffer.

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.