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 
00064 /*
00065  * This is extending QIODevice's error codes
00066  *
00067  * According to qiodevice.h, the last error is IO_UnspecifiedError
00068  * These errors will never occur in functions declared in QIODevice
00069  * (except open, but you shouldn't call open)
00070  */
00071 #define IO_ListenError      (IO_UnspecifiedError+1)
00072 #define IO_AcceptError      (IO_UnspecifiedError+2)
00073 #define IO_LookupError      (IO_UnspecifiedError+3)
00074 #define IO_SocketCreateError    (IO_UnspecifiedError+4)
00075 #define IO_BindError        (IO_UnspecifiedError+5)
00076 
00077 class QMutex;
00078 
00079 namespace KNetwork {
00080 
00081 class KResolverEntry;
00082 class KSocketDevice;
00083 
00084 class KSocketBasePrivate;
00096 class KSocketBase
00097 {
00098 public:
00117   enum SocketOptions
00118     {
00119       Blocking = 0x01,
00120       AddressReuseable = 0x02,
00121       IPv6Only = 0x04,
00122       Keepalive = 0x08,
00123       Broadcast = 0x10
00124     };
00125 
00150   enum SocketError
00151     {
00152       NoError = 0,
00153       LookupFailure,
00154       AddressInUse,
00155       AlreadyCreated,
00156       AlreadyBound,
00157       AlreadyConnected,
00158       NotConnected,
00159       NotBound,
00160       NotCreated,
00161       WouldBlock,
00162       ConnectionRefused,
00163       ConnectionTimedOut,
00164       InProgress,
00165       NetFailure,
00166       NotSupported,
00167       Timeout,
00168       UnknownError
00169     };
00170 
00171 public:
00175   KSocketBase();
00176 
00180   virtual ~KSocketBase();
00181 
00182   /*
00183    * The following functions are shared by all descended classes and will have
00184    * to be reimplemented.
00185    */
00186 
00187 protected:
00201   virtual bool setSocketOptions(int opts);
00202 
00212   virtual int socketOptions() const;
00213 
00214 public:
00230   virtual bool setBlocking(bool enable);
00231 
00238   bool blocking() const;
00239 
00254   virtual bool setAddressReuseable(bool enable);
00255 
00262   bool addressReuseable() const;
00263 
00279   virtual bool setIPv6Only(bool enable);
00280 
00287   bool isIPv6Only() const;
00288 
00300   virtual bool setBroadcast(bool enable);
00301 
00308   bool broadcast() const;
00309 
00316   KSocketDevice* socketDevice() const;
00317 
00331   virtual void setSocketDevice(KSocketDevice* device);
00332 
00354   int setRequestedCapabilities(int add, int remove = 0);
00355 
00356 protected:
00361   bool hasDevice() const;
00362 
00368   void setError(SocketError error);
00369 
00370 public:
00375   SocketError error() const;
00376 
00380   inline QString errorString() const
00381   { return errorString(error()); }
00382 
00398   QMutex* mutex() const;
00399 
00400 public:
00406   static QString errorString(SocketError code);
00407 
00416   static bool isFatalError(int code);
00417 
00418 private:
00421   void unsetSocketDevice();
00422 
00423   KSocketBase(const KSocketBase&);
00424   KSocketBase& operator =(const KSocketBase&);
00425 
00426   KSocketBasePrivate *d;
00427 
00428   friend class KSocketDevice;
00429 };
00430 
00440 class KActiveSocketBase: public QIODevice, virtual public KSocketBase
00441 {
00442 public:
00446   KActiveSocketBase();
00447 
00451   virtual ~KActiveSocketBase();
00452 
00463   virtual bool bind(const KResolverEntry& address) = 0;
00464 
00481   virtual bool connect(const KResolverEntry& address) = 0;
00482 
00498   virtual bool disconnect() = 0;
00499 
00504   virtual Offset size() const
00505   { return 0; }
00506 
00511   virtual Offset at() const
00512   { return 0; }
00513 
00518   virtual bool at(Offset)
00519   { return false; }
00520 
00525   virtual bool atEnd() const
00526   { return true; }
00527 
00532   virtual Q_LONG bytesAvailable() const = 0;
00533 
00545   virtual Q_LONG waitForMore(int msecs, bool *timeout = 0L) = 0;
00546 
00553   virtual Q_LONG readBlock(char *data, Q_ULONG len) = 0;
00554 
00566   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0;
00567 
00579   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen) = 0;
00580 
00593   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0;
00594 
00601   virtual Q_LONG writeBlock(const char *data, Q_ULONG len) = 0;
00602 
00614   virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to) = 0;
00615 
00620   virtual int getch();
00621 
00626   virtual int putch(int ch);
00627 
00632   virtual int ungetch(int)
00633   { return -1; }
00634 
00638   virtual KSocketAddress localAddress() const = 0;
00639 
00645   virtual KSocketAddress peerAddress() const = 0;
00646 
00647 protected:
00654   void setError(int status, SocketError error);
00655 
00659   void resetError();
00660 };
00661 
00671 class KPassiveSocketBase: virtual public KSocketBase
00672 {
00673 public:
00677   KPassiveSocketBase();
00678 
00682   virtual ~KPassiveSocketBase();
00683 
00694   virtual bool bind(const KResolverEntry& address) = 0;
00695 
00710   virtual bool listen(int backlog) = 0;
00711 
00716   virtual void close() = 0;
00717 
00731   virtual KActiveSocketBase* accept() = 0;
00732 
00736   virtual KSocketAddress localAddress() const = 0;
00737 
00741   virtual KSocketAddress externalAddress() const = 0;
00742 
00743 private:
00744   KPassiveSocketBase(const KPassiveSocketBase&);
00745   KPassiveSocketBase& operator = (const KPassiveSocketBase&);
00746 };
00747 
00748 }               // namespace KNetwork
00749 
00750 #endif
KDE Logo
This file is part of the documentation for kdecore Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:32:30 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003