ksocketbase.h
00001 /* -*- C++ -*- 00002 * Copyright (C) 2003,2005 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 00152 enum SocketError 00153 { 00154 NoError = 0, 00155 LookupFailure, 00156 AddressInUse, 00157 AlreadyCreated, 00158 AlreadyBound, 00159 AlreadyConnected, 00160 NotConnected, 00161 NotBound, 00162 NotCreated, 00163 WouldBlock, 00164 ConnectionRefused, 00165 ConnectionTimedOut, 00166 InProgress, 00167 NetFailure, 00168 NotSupported, 00169 Timeout, 00170 UnknownError, 00171 RemotelyDisconnected 00172 }; 00173 00174 public: 00178 KSocketBase(); 00179 00183 virtual ~KSocketBase(); 00184 00185 /* 00186 * The following functions are shared by all descended classes and will have 00187 * to be reimplemented. 00188 */ 00189 00190 protected: 00204 virtual bool setSocketOptions(int opts); 00205 00215 virtual int socketOptions() const; 00216 00217 public: 00233 virtual bool setBlocking(bool enable); 00234 00241 bool blocking() const; 00242 00257 virtual bool setAddressReuseable(bool enable); 00258 00265 bool addressReuseable() const; 00266 00282 virtual bool setIPv6Only(bool enable); 00283 00290 bool isIPv6Only() const; 00291 00303 virtual bool setBroadcast(bool enable); 00304 00311 bool broadcast() const; 00312 00319 KSocketDevice* socketDevice() const; 00320 00334 virtual void setSocketDevice(KSocketDevice* device); 00335 00357 int setRequestedCapabilities(int add, int remove = 0); 00358 00359 protected: 00364 bool hasDevice() const; 00365 00371 void setError(SocketError error); 00372 00373 public: 00378 SocketError error() const; 00379 00383 inline QString errorString() const 00384 { return errorString(error()); } 00385 00401 QMutex* mutex() const; 00402 00403 public: 00409 static QString errorString(SocketError code); 00410 00419 static bool isFatalError(int code); 00420 00421 private: 00424 void unsetSocketDevice(); 00425 00426 KSocketBase(const KSocketBase&); 00427 KSocketBase& operator =(const KSocketBase&); 00428 00429 KSocketBasePrivate *d; 00430 00431 friend class KSocketDevice; 00432 }; 00433 00443 class KDECORE_EXPORT KActiveSocketBase: public QIODevice, virtual public KSocketBase 00444 { 00445 public: 00449 KActiveSocketBase(); 00450 00454 virtual ~KActiveSocketBase(); 00455 00466 virtual bool bind(const KResolverEntry& address) = 0; 00467 00484 virtual bool connect(const KResolverEntry& address) = 0; 00485 00501 virtual bool disconnect() = 0; 00502 00507 virtual Offset size() const 00508 { return 0; } 00509 00514 virtual Offset at() const 00515 { return 0; } 00516 00521 virtual bool at(Offset) 00522 { return false; } 00523 00528 virtual bool atEnd() const 00529 { return true; } 00530 00535 virtual Q_LONG bytesAvailable() const = 0; 00536 00548 virtual Q_LONG waitForMore(int msecs, bool *timeout = 0L) = 0; 00549 00556 virtual Q_LONG readBlock(char *data, Q_ULONG len) = 0; 00557 00569 virtual Q_LONG readBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0; 00570 00582 virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen) = 0; 00583 00596 virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0; 00597 00604 virtual Q_LONG writeBlock(const char *data, Q_ULONG len) = 0; 00605 00617 virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to) = 0; 00618 00623 virtual int getch(); 00624 00629 virtual int putch(int ch); 00630 00635 virtual int ungetch(int) 00636 { return -1; } 00637 00641 virtual KSocketAddress localAddress() const = 0; 00642 00648 virtual KSocketAddress peerAddress() const = 0; 00649 00650 // FIXME KDE 4.0: 00651 // enable this function 00652 #if 0 00653 00656 virtual KSocketAddress externalAddress() const = 0; 00657 #endif 00658 00659 protected: 00666 void setError(int status, SocketError error); 00667 00671 void resetError(); 00672 }; 00673 00683 class KDECORE_EXPORT KPassiveSocketBase: virtual public KSocketBase 00684 { 00685 public: 00689 KPassiveSocketBase(); 00690 00694 virtual ~KPassiveSocketBase(); 00695 00706 virtual bool bind(const KResolverEntry& address) = 0; 00707 00722 virtual bool listen(int backlog) = 0; 00723 00728 virtual void close() = 0; 00729 00743 virtual KActiveSocketBase* accept() = 0; 00744 00748 virtual KSocketAddress localAddress() const = 0; 00749 00753 virtual KSocketAddress externalAddress() const = 0; 00754 00755 private: 00756 KPassiveSocketBase(const KPassiveSocketBase&); 00757 KPassiveSocketBase& operator = (const KPassiveSocketBase&); 00758 }; 00759 00760 } // namespace KNetwork 00761 00762 #endif