# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kextsock.h

    00001 /*
    00002  *  This file is part of the KDE libraries
    00003  *  Copyright (C) 2000-2002 Thiago Macieira <thiagom@mail.com>
    00004  *
    00005  *  This library is free software; you can redistribute it and/or
    00006  *  modify it under the terms of the GNU Library General Public
    00007  *  License as published by the Free Software Foundation; either
    00008  *  version 2 of the License, or (at your option) any later version.
    00009  *
    00010  *  This library is distributed in the hope that it will be useful,
    00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013  *  Library General Public License for more details.
    00014  *
    00015  *  You should have received a copy of the GNU Library General Public License
    00016  *  along with this library; see the file COPYING.LIB.  If not, write to
    00017  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018  *  Boston, MA 02111-1307, USA.
    00019  */
    00020 #ifndef KEXTSOCK_H
    00021 #define KEXTSOCK_H
    00022
    00023 #include <sys/time.h>
    00024
    00025 #include <qstring.h>
    00026 #include <qptrlist.h>
    00027 #include <qiodevice.h>
    00028
    00029 #include "kbufferedio.h"
    00030 #include "ksockaddr.h"
    00031
    00032 /* External reference to netdb.h */
    00033 struct addrinfo;
    00034 struct kde_addrinfo;
    00035 class KAddressInfo;             /* our abstraction of it */
    00036 class QSocketNotifier;
    00037
    00038 /*
    00039  * This is extending QIODevice's error codes
    00040  *
    00041  * According to qiodevice.h, the last error is IO_UnspecifiedError
    00042  * These errors will never occur in functions declared in QIODevice
    00043  * (except open, but you shouldn't call open)
    00044  */
    00045 #define IO_ListenError          (IO_UnspecifiedError+1)
    00046 #define IO_AcceptError          (IO_UnspecifiedError+2)
    00047 #define IO_LookupError          (IO_UnspecifiedError+3)
    00048
    00049 class KExtendedSocketPrivate;
    00083 class KExtendedSocket: public KBufferedIO // public QObject, public QIODevice
    00084 {
    00085   Q_OBJECT
    00086
    00087 public:
    00091   enum Flags
    00092   {
    00093     /* socket address families */
    00094     /*
    00095      * NOTE: if you change this, you have to change function valid_socket() as well
    00096      * These values are hard coded!
    00097      */
    00098     anySocket = 0x00,
    00099     knownSocket = 0x01,
    00100     unixSocket = knownSocket | 0x02,
    00101     inetSocket = knownSocket | 0x04,
    00102     ipv4Socket = inetSocket | 0x100,
    00103     ipv6Socket = inetSocket | 0x200,
    00104
    00105     passiveSocket = 0x1000,     /* passive socket (i.e., one that accepts connections) */
    00106     canonName = 0x2000,         /* request that the canon name be found */
    00107     noResolve = 0x4000,         /* do not attempt to resolve, treat as numeric host */
    00108
    00109     streamSocket = 0x8000,      /* request a streaming socket (e.g., TCP) */
    00110     datagramSocket = 0x10000,   /* request a datagram socket (e.g., UDP) */
    00111     rawSocket = 0x20000,        /* request a raw socket. This probably requires privileges */
    00112
    00113     inputBufferedSocket = 0x200000, /* buffer input in this socket */
    00114     outputBufferedSocket = 0x400000, /* buffer output in this socket */
    00115     bufferedSocket = 0x600000   /* make this a fully buffered socket */
    00116   };
    00117
    00123   enum SockStatus
    00124   {
    00125     // the numbers are scattered so that we leave room for future expansion
    00126     error = -1,                 // invalid status!
    00127
    00128     nothing = 0,                // no status, the class has just been created
    00129
    00130     lookupInProgress = 50,      // lookup is in progress. Signals will be sent
    00131     lookupDone = 70,            // lookup has been done. Flags cannot be changed
    00132                                 // from this point on
    00133
    00134     created = 100,              // ::socket() has been called, a socket exists
    00135     bound = 140,                // socket has been bound
    00136
    00137     connecting = 200,           // socket is connecting (not passiveSocket)
    00138     connected = 220,            // socket has connected (not passiveSocket)
    00139
    00140     listening = 200,            // socket is listening (passiveSocket)
    00141     accepting = 220,            // socket is accepting (passiveSocket)
    00142
    00143     closing = 350,              // socket is closing (delayed close)
    00144
    00145     done = 400                  // socket has been closed
    00146   };
    00147
    00148 public:
    00152   KExtendedSocket();
    00153
    00160   KExtendedSocket(const QString& host, int port, int flags = 0);
    00161
    00168   KExtendedSocket(const QString& host, const QString& service, int flags = 0);
    00169
    00174   virtual ~KExtendedSocket();
    00175
    00181   void reset();
    00182
    00183   /*
    00184    * --- status, flags and internal variables --- *
    00185    */
    00186
    00192   int socketStatus() const;
    00193
    00200   int systemError() const;
    00201
    00207   int setSocketFlags(int flags);
    00208
    00214   int socketFlags() const;
    00215
    00221   bool setHost(const QString& host);
    00222
    00227   QString host() const;
    00228
    00233   bool setPort(int port);
    00234
    00240   bool setPort(const QString& service);
    00241
    00246   QString port() const;
    00247
    00254   bool setAddress(const QString& host, int port);
    00255
    00262   bool setAddress(const QString& host, const QString& serv);
    00263
    00269   bool setBindHost(const QString& host);
    00270
    00275   bool unsetBindHost();
    00276
    00281   QString bindHost() const;
    00282
    00288   bool setBindPort(int port);
    00289
    00295   bool setBindPort(const QString& service);
    00296
    00301   bool unsetBindPort();
    00302
    00307   QString bindPort() const;
    00308
    00316   bool setBindAddress(const QString& host, int port);
    00317
    00325   bool setBindAddress(const QString& host, const QString& service);
    00326
    00332   bool unsetBindAddress();
    00333
    00345   bool setTimeout(int secs, int usecs = 0);
    00346
    00351   timeval timeout() const;
    00352
    00361   bool setBlockingMode(bool enable);
    00362
    00367   bool blockingMode();
    00368
    00378   bool setAddressReusable(bool enable);
    00379
    00384   bool addressReusable();
    00385
    00404   bool setIPv6Only(bool enable);
    00405
    00412   bool isIPv6Only();
    00413
    00435   virtual bool setBufferSize(int rsize, int wsize = -2);
    00436
    00442   const KSocketAddress *localAddress();
    00443
    00450   const KSocketAddress *peerAddress();
    00451
    00456   inline int fd() const
    00457   { return sockfd; }
    00458
    00459   /*
    00460    * -- socket creation -- *
    00461    */
    00462
    00470   virtual int lookup();
    00471
    00490   virtual int startAsyncLookup();
    00491
    00495   virtual void cancelAsyncLookup();
    00496
    00504   virtual int listen(int N = 5); // 5 is arbitrary
    00505
    00520   virtual int accept(KExtendedSocket *&sock);
    00521
    00545   virtual int connect();
    00546
    00557   virtual int startAsyncConnect();
    00558
    00562   virtual void cancelAsyncConnect();
    00563
    00574   virtual bool open(int mode = IO_Raw | IO_ReadWrite);
    00575
    00583   virtual void close();
    00584
    00590   virtual void closeNow();
    00591
    00605   virtual void release();
    00606
    00607   /*
    00608    * -- I/O --
    00609    */
    00610
    00626   virtual void flush();
    00627
    00632   virtual inline Q_ULONG size() const
    00633   { return 0; }
    00634
    00639   virtual inline Q_ULONG at() const
    00640   { return 0; }
    00641
    00647   virtual inline bool at(int i)
    00648   { Q_UNUSED(i);return true; }
    00649
    00655   virtual inline bool atEnd() const
    00656   { return false; }
    00657
    00687   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen);
    00688
    00712   virtual Q_LONG writeBlock(const char *data, Q_ULONG len);
    00713
    00728   virtual int peekBlock(char *data, uint maxlen);
    00729
    00736   virtual int unreadBlock(const char *data, uint len);
    00737
    00747   virtual int bytesAvailable() const;
    00748
    00758   virtual int waitForMore(int msec);
    00759
    00764   virtual int getch();
    00765
    00771   virtual int putch(int ch);
    00772
    00777   virtual int ungetch(int)
    00778   { return -1; }
    00779
    00790   virtual void enableRead(bool enable);
    00791
    00801   virtual void enableWrite(bool enable);
    00802
    00803 signals:
    00809   void lookupFinished(int count);
    00810
    00814   void connectionSuccess();
    00815
    00821   void connectionFailed(int error);
    00822
    00828   void readyAccept();
    00829
    00830 protected:
    00831   int sockfd;                   // file descriptor of the socket
    00832
    00833 protected slots:
    00834
    00835   void socketActivityRead();
    00836   void socketActivityWrite();
    00837   void dnsResultsReady();
    00838   void startAsyncConnectSlot();
    00839   void connectionEvent();
    00840
    00841 protected:
    00842
    00843   QSocketNotifier *readNotifier();
    00844   QSocketNotifier *writeNotifier();
    00845
    00846 private:
    00847
    00848   // protection against accidental use
    00849   KExtendedSocket(KExtendedSocket&);
    00850   KExtendedSocket& operator=(KExtendedSocket&);
    00851
    00856   static int doLookup(const QString& host, const QString& serv, addrinfo& hint,
    00857                       kde_addrinfo** result);
    00858
    00859 protected:
    00863   void setError(int errorkind, int error);
    00864
    00865   inline void cleanError()
    00866   { setError(IO_Ok, 0); }
    00867
    00871   void setSocketStatus(int status);
    00872
    00873 public:
    00886   static int resolve(sockaddr* sock, ksocklen_t len, QString& host, QString& port, int flags = 0);
    00887
    00900   static int resolve(KSocketAddress* sock, QString& host, QString& port, int flags = 0);
    00901
    00920   static QPtrList<KAddressInfo> lookup(const QString& host, const QString& port, int flags = 0, int *error = 0);
    00921
    00928   static KSocketAddress *localAddress(int fd);
    00929
    00937   static KSocketAddress *peerAddress(int fd);
    00938
    00945   static QString strError(int code, int syserr);
    00946
    00956   static bool setAddressReusable(int fd, bool enable);
    00957
    00958 protected:
    00959   virtual void virtual_hook( int id, void* data );
    00960 private:
    00961   KExtendedSocketPrivate *d;
    00962
    00963   friend class KSocket;
    00964   friend class KServerSocket;
    00965 };
    00966
    00971 class KAddressInfo
    00972 {
    00973 private:
    00974   addrinfo *ai;
    00975   KSocketAddress *addr;
    00976
    00977   inline KAddressInfo() : ai(0), addr(0)
    00978   { }
    00979
    00980   KAddressInfo(addrinfo *ai);
    00981   KAddressInfo(KAddressInfo&) { }
    00982   KAddressInfo& operator=(KAddressInfo&) { return *this; }
    00983
    00984 public:
    00985   ~KAddressInfo();
    00986
    00991   inline operator const KSocketAddress*() const
    00992   { return addr; }
    00993
    00997   inline operator const addrinfo&() const
    00998   { return *ai; }
    00999
    01004   inline operator const addrinfo*() const
    01005   { return ai; }
    01006
    01012   inline const KSocketAddress* address() const
    01013   { return addr; }
    01014
    01019   int flags() const;
    01020
    01025   int family() const;
    01026
    01031   int socktype() const;
    01032
    01037   int protocol() const;
    01038
    01039
    01045   const char* canonname() const;
    01046
    01051   inline int length() const
    01052   { if (addr) return addr->size(); return 0; }
    01053
    01054   friend class KExtendedSocket;
    01055 };
    01056
    01057 #endif // KEXTSOCK_H

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:30 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001