tdesocketbase.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 TDESOCKETBASE_H 00057 #define TDESOCKETBASE_H 00058 00059 #include <tqiodevice.h> 00060 #include <tqstring.h> 00061 00062 #include "tdesocketaddress.h" 00063 #include <tdelibs_export.h> 00064 00065 /* 00066 * This is extending QIODevice's error codes 00067 * 00068 * According to tqiodevice.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 TQMutex; 00079 00080 namespace KNetwork { 00081 00082 class KResolverEntry; 00083 class TDESocketDevice; 00084 00085 class TDESocketBasePrivate; 00097 class TDECORE_EXPORT TDESocketBase 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 TDESocketBase(); 00179 00183 virtual ~TDESocketBase(); 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 TDESocketDevice* socketDevice() const; 00320 00334 virtual void setSocketDevice(TDESocketDevice* 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 TQString errorString() const 00384 { return errorString(error()); } 00385 00401 TQMutex* mutex() const; 00402 00403 public: 00409 static TQString errorString(SocketError code); 00410 00419 static bool isFatalError(int code); 00420 00421 private: 00424 void unsetSocketDevice(); 00425 00426 TDESocketBase(const TDESocketBase&); 00427 TDESocketBase& operator =(const TDESocketBase&); 00428 00429 TDESocketBasePrivate *d; 00430 00431 friend class TDESocketDevice; 00432 }; 00433 00443 class TDECORE_EXPORT KActiveSocketBase: public TQIODevice, virtual public TDESocketBase 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 #ifdef USE_QT4 00508 virtual qint64 size() const 00509 #else // USE_QT4 00510 virtual Offset size() const 00511 #endif // USE_QT4 00512 { return 0; } 00513 00518 virtual Offset at() const 00519 { return 0; } 00520 00525 virtual bool at(Offset) 00526 { return false; } 00527 00532 virtual bool atEnd() const 00533 { return true; } 00534 00539 #ifdef USE_QT3 00540 virtual TQ_LONG bytesAvailable() const = 0; 00541 #endif 00542 #ifdef USE_QT4 00543 virtual qint64 bytesAvailable() const = 0; 00544 #endif 00545 00557 virtual TQ_LONG waitForMore(int msecs, bool *timeout = 0L) = 0; 00558 00565 virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG len) = 0; 00566 00578 virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen, TDESocketAddress& from) = 0; 00579 00591 virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen) = 0; 00592 00605 virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen, TDESocketAddress& from) = 0; 00606 00613 virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len) = 0; 00614 00626 virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len, const TDESocketAddress& to) = 0; 00627 00632 virtual int getch(); 00633 00638 virtual int putch(int ch); 00639 00644 virtual int ungetch(int) 00645 { return -1; } 00646 00650 virtual TDESocketAddress localAddress() const = 0; 00651 00657 virtual TDESocketAddress peerAddress() const = 0; 00658 00659 // FIXME KDE 4.0: 00660 // enable this function 00661 #if 0 00662 00665 virtual TDESocketAddress externalAddress() const = 0; 00666 #endif 00667 00668 protected: 00675 void setError(int status, SocketError error); 00676 00680 void resetError(); 00681 }; 00682 00692 class TDECORE_EXPORT KPassiveSocketBase: virtual public TDESocketBase 00693 { 00694 public: 00698 KPassiveSocketBase(); 00699 00703 virtual ~KPassiveSocketBase(); 00704 00715 virtual bool bind(const KResolverEntry& address) = 0; 00716 00731 virtual bool listen(int backlog) = 0; 00732 00737 virtual void close() = 0; 00738 00752 virtual KActiveSocketBase* accept() = 0; 00753 00757 virtual TDESocketAddress localAddress() const = 0; 00758 00762 virtual TDESocketAddress externalAddress() const = 0; 00763 00764 private: 00765 KPassiveSocketBase(const KPassiveSocketBase&); 00766 KPassiveSocketBase& operator = (const KPassiveSocketBase&); 00767 }; 00768 00769 } // namespace KNetwork 00770 00771 #endif