kdecore Library API Documentation

ksocketbase.h

00001 /*  -*- C++ -*-
00002  *  Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
00003  *
00004  *
00005  *  Permission is hereby granted, free of charge, to any person obtaining
00006  *  a copy of this software and associated documentation files (the
00007  *  "Software"), to deal in the Software without restriction, including
00008  *  without limitation the rights to use, copy, modify, merge, publish,
00009  *  distribute, sublicense, and/or sell copies of the Software, and to
00010  *  permit persons to whom the Software is furnished to do so, subject to
00011  *  the following conditions:
00012  *
00013  *  The above copyright notice and this permission notice shall be included 
00014  *  in all copies or substantial portions of the Software.
00015  *
00016  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00017  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00019  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00020  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00021  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00022  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023  */
00024 
00025 /*
00026  * Even before our #ifdef, clean up the namespace
00027  */
00028 #ifdef socket
00029 #undef socket
00030 #endif
00031 
00032 #ifdef bind
00033 #undef bind
00034 #endif
00035 
00036 #ifdef listen
00037 #undef listen
00038 #endif
00039 
00040 #ifdef connect
00041 #undef connect
00042 #endif
00043 
00044 #ifdef accept
00045 #undef accept
00046 #endif
00047 
00048 #ifdef getpeername
00049 #undef getpeername
00050 #endif
00051 
00052 #ifdef getsockname
00053 #undef getsockname
00054 #endif
00055 
00056 #ifndef KSOCKETBASE_H
00057 #define KSOCKETBASE_H
00058 
00059 #include <qiodevice.h>
00060 #include <qstring.h>
00061 
00062 #include "ksocketaddress.h"
00063 #include <kdelibs_export.h>
00064 
00065 /*
00066  * This is extending QIODevice's error codes
00067  *
00068  * According to qiodevice.h, the last error is IO_UnspecifiedError
00069  * These errors will never occur in functions declared in QIODevice
00070  * (except open, but you shouldn't call open)
00071  */
00072 #define IO_ListenError      (IO_UnspecifiedError+1)
00073 #define IO_AcceptError      (IO_UnspecifiedError+2)
00074 #define IO_LookupError      (IO_UnspecifiedError+3)
00075 #define IO_SocketCreateError    (IO_UnspecifiedError+4)
00076 #define IO_BindError        (IO_UnspecifiedError+5)
00077 
00078 class QMutex;
00079 
00080 namespace KNetwork {
00081 
00082 class KResolverEntry;
00083 class KSocketDevice;
00084 
00085 class KSocketBasePrivate;
00097 class KDECORE_EXPORT KSocketBase
00098 {
00099 public:
00118   enum SocketOptions
00119     {
00120       Blocking = 0x01,
00121       AddressReuseable = 0x02,
00122       IPv6Only = 0x04,
00123       Keepalive = 0x08,
00124       Broadcast = 0x10
00125     };
00126 
00151   enum SocketError
00152     {
00153       NoError = 0,
00154       LookupFailure,
00155       AddressInUse,
00156       AlreadyCreated,
00157       AlreadyBound,
00158       AlreadyConnected,
00159       NotConnected,
00160       NotBound,
00161       NotCreated,
00162       WouldBlock,
00163       ConnectionRefused,
00164       ConnectionTimedOut,
00165       InProgress,
00166       NetFailure,
00167       NotSupported,
00168       Timeout,
00169       UnknownError
00170     };
00171 
00172 public:
00176   KSocketBase();
00177 
00181   virtual ~KSocketBase();
00182 
00183   /*
00184    * The following functions are shared by all descended classes and will have
00185    * to be reimplemented.
00186    */
00187 
00188 protected:
00202   virtual bool setSocketOptions(int opts);
00203 
00213   virtual int socketOptions() const;
00214 
00215 public:
00231   virtual bool setBlocking(bool enable);
00232 
00239   bool blocking() const;
00240 
00255   virtual bool setAddressReuseable(bool enable);
00256 
00263   bool addressReuseable() const;
00264 
00280   virtual bool setIPv6Only(bool enable);
00281 
00288   bool isIPv6Only() const;
00289 
00301   virtual bool setBroadcast(bool enable);
00302 
00309   bool broadcast() const;
00310 
00317   KSocketDevice* socketDevice() const;
00318 
00332   virtual void setSocketDevice(KSocketDevice* device);
00333 
00355   int setRequestedCapabilities(int add, int remove = 0);
00356 
00357 protected:
00362   bool hasDevice() const;
00363 
00369   void setError(SocketError error);
00370 
00371 public:
00376   SocketError error() const;
00377 
00381   inline QString errorString() const
00382   { return errorString(error()); }
00383 
00399   QMutex* mutex() const;
00400 
00401 public:
00407   static QString errorString(SocketError code);
00408 
00417   static bool isFatalError(int code);
00418 
00419 private:
00422   void unsetSocketDevice();
00423 
00424   KSocketBase(const KSocketBase&);
00425   KSocketBase& operator =(const KSocketBase&);
00426 
00427   KSocketBasePrivate *d;
00428 
00429   friend class KSocketDevice;
00430 };
00431 
00441 class KDECORE_EXPORT KActiveSocketBase: public QIODevice, virtual public KSocketBase
00442 {
00443 public:
00447   KActiveSocketBase();
00448 
00452   virtual ~KActiveSocketBase();
00453 
00464   virtual bool bind(const KResolverEntry& address) = 0;
00465 
00482   virtual bool connect(const KResolverEntry& address) = 0;
00483 
00499   virtual bool disconnect() = 0;
00500 
00505   virtual Offset size() const
00506   { return 0; }
00507 
00512   virtual Offset at() const
00513   { return 0; }
00514 
00519   virtual bool at(Offset)
00520   { return false; }
00521 
00526   virtual bool atEnd() const
00527   { return true; }
00528 
00533   virtual Q_LONG bytesAvailable() const = 0;
00534 
00546   virtual Q_LONG waitForMore(int msecs, bool *timeout = 0L) = 0;
00547 
00554   virtual Q_LONG readBlock(char *data, Q_ULONG len) = 0;
00555 
00567   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0;
00568 
00580   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen) = 0;
00581 
00594   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0;
00595 
00602   virtual Q_LONG writeBlock(const char *data, Q_ULONG len) = 0;
00603 
00615   virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to) = 0;
00616 
00621   virtual int getch();
00622 
00627   virtual int putch(int ch);
00628 
00633   virtual int ungetch(int)
00634   { return -1; }
00635 
00639   virtual KSocketAddress localAddress() const = 0;
00640 
00646   virtual KSocketAddress peerAddress() const = 0;
00647 
00648 protected:
00655   void setError(int status, SocketError error);
00656 
00660   void resetError();
00661 };
00662 
00672 class KDECORE_EXPORT KPassiveSocketBase: virtual public KSocketBase
00673 {
00674 public:
00678   KPassiveSocketBase();
00679 
00683   virtual ~KPassiveSocketBase();
00684 
00695   virtual bool bind(const KResolverEntry& address) = 0;
00696 
00711   virtual bool listen(int backlog) = 0;
00712 
00717   virtual void close() = 0;
00718 
00732   virtual KActiveSocketBase* accept() = 0;
00733 
00737   virtual KSocketAddress localAddress() const = 0;
00738 
00742   virtual KSocketAddress externalAddress() const = 0;
00743 
00744 private:
00745   KPassiveSocketBase(const KPassiveSocketBase&);
00746   KPassiveSocketBase& operator = (const KPassiveSocketBase&);
00747 };
00748 
00749 }               // namespace KNetwork
00750 
00751 #endif
KDE Logo
This file is part of the documentation for kdecore Library Version 3.3.90.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 30 10:09:40 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003