KDECore
k3socketbase.h
Go to the documentation of this file.
00001 /* -*- C++ -*- 00002 * Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org> 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 <QtCore/QIODevice> 00060 #include <QtCore/QString> 00061 00062 #include <kdecore_export.h> 00063 #include "k3socketaddress.h" 00064 00065 class QMutex; 00066 00067 namespace KNetwork { 00068 00069 class KResolverEntry; 00070 class KSocketDevice; 00071 00072 class KSocketBasePrivate; 00085 class KDECORE_EXPORT KSocketBase 00086 { 00087 public: 00108 enum SocketOptions 00109 { 00110 Blocking = 0x01, 00111 AddressReuseable = 0x02, 00112 IPv6Only = 0x04, 00113 Keepalive = 0x08, 00114 Broadcast = 0x10, 00115 NoDelay = 0x20 00116 }; 00117 00143 enum SocketError 00144 { 00145 NoError = 0, 00146 LookupFailure, 00147 AddressInUse, 00148 AlreadyCreated, 00149 AlreadyBound, 00150 AlreadyConnected, 00151 NotConnected, 00152 NotBound, 00153 NotCreated, 00154 WouldBlock, 00155 ConnectionRefused, 00156 ConnectionTimedOut, 00157 InProgress, 00158 NetFailure, 00159 NotSupported, 00160 Timeout, 00161 UnknownError, 00162 RemotelyDisconnected 00163 }; 00164 00165 public: 00169 KSocketBase(); 00170 00174 virtual ~KSocketBase(); 00175 00176 /* 00177 * The following functions are shared by all descended classes and will have 00178 * to be reimplemented. 00179 */ 00180 00181 protected: 00195 virtual bool setSocketOptions(int opts); 00196 00206 virtual int socketOptions() const; 00207 00208 public: 00224 virtual bool setBlocking(bool enable); 00225 00232 bool blocking() const; 00233 00248 virtual bool setAddressReuseable(bool enable); 00249 00256 bool addressReuseable() const; 00257 00273 virtual bool setIPv6Only(bool enable); 00274 00281 bool isIPv6Only() const; 00282 00294 virtual bool setBroadcast(bool enable); 00295 00302 bool broadcast() const; 00303 00317 virtual bool setNoDelay(bool enable); 00318 00324 bool noDelay() const; 00325 00332 KSocketDevice* socketDevice() const; 00333 00347 virtual void setSocketDevice(KSocketDevice* device); 00348 00370 int setRequestedCapabilities(int add, int remove = 0); 00371 00372 protected: 00377 bool hasDevice() const; 00378 00384 void setError(SocketError error); 00385 00389 void resetError(); 00390 00391 public: 00396 SocketError error() const; 00397 00401 QString errorString() const; 00402 00418 QMutex* mutex() const; 00419 00420 public: 00426 static QString errorString(SocketError code); 00427 00436 static bool isFatalError(int code); 00437 00438 private: 00441 void unsetSocketDevice(); 00442 00443 KSocketBase(const KSocketBase&); 00444 KSocketBase& operator =(const KSocketBase&); 00445 00446 KSocketBasePrivate* const d; 00447 00448 friend class KSocketDevice; 00449 }; 00450 00461 class KDECORE_EXPORT KActiveSocketBase: public QIODevice, virtual public KSocketBase 00462 { 00463 Q_OBJECT 00464 public: 00468 KActiveSocketBase(QObject* parent); 00469 00473 virtual ~KActiveSocketBase(); 00474 00478 QString errorString() const; 00479 00483 virtual void setSocketDevice(KSocketDevice* device); 00484 00488 virtual bool open(OpenMode mode); 00489 00500 virtual bool bind(const KResolverEntry& address) = 0; 00501 00520 virtual bool connect(const KResolverEntry& address, 00521 OpenMode mode = ReadWrite) = 0; 00522 00538 virtual bool disconnect() = 0; 00539 00543 virtual bool isSequential() const; 00544 00549 virtual qint64 size() const; 00550 00555 virtual qint64 pos() const; 00556 00561 virtual bool seek(qint64); 00562 00567 virtual bool atEnd() const; 00568 00575 qint64 read(char *data, qint64 maxlen); 00576 00583 QByteArray read(qint64 len); 00584 00596 qint64 read(char *data, qint64 maxlen, KSocketAddress& from); 00597 00609 qint64 peek(char *data, qint64 maxlen); 00610 00624 qint64 peek(char *data, qint64 maxlen, KSocketAddress& from); 00625 00632 qint64 write(const char *data, qint64 len); 00633 00640 qint64 write(const QByteArray& data); 00641 00653 qint64 write(const char *data, qint64 len, const KSocketAddress& to); 00654 00666 virtual qint64 waitForMore(int msecs, bool *timeout = 0L) = 0; 00667 00671 void ungetChar(char); 00672 00676 virtual KSocketAddress localAddress() const = 0; 00677 00683 virtual KSocketAddress peerAddress() const = 0; 00684 00685 // FIXME KDE 4.0: 00686 // enable this function 00687 #if 0 00688 00691 virtual KSocketAddress externalAddress() const = 0; 00692 #endif 00693 00694 protected: 00701 virtual qint64 readData(char *data, qint64 len); 00702 00714 virtual qint64 readData(char *data, qint64 maxlen, KSocketAddress* from) = 0; 00715 00728 virtual qint64 peekData(char *data, qint64 maxlen, KSocketAddress* from) = 0; 00729 00736 virtual qint64 writeData(const char *data, qint64 len); 00737 00749 virtual qint64 writeData(const char *data, qint64 len, const KSocketAddress* to) = 0; 00750 00756 void setError(SocketError error); 00757 00761 void resetError(); 00762 }; 00763 00774 class KDECORE_EXPORT KPassiveSocketBase: virtual public KSocketBase 00775 { 00776 public: 00780 KPassiveSocketBase(); 00781 00785 virtual ~KPassiveSocketBase(); 00786 00797 virtual bool bind(const KResolverEntry& address) = 0; 00798 00813 virtual bool listen(int backlog) = 0; 00814 00819 virtual void close() = 0; 00820 00834 virtual KActiveSocketBase* accept() = 0; 00835 00839 virtual KSocketAddress localAddress() const = 0; 00840 00844 virtual KSocketAddress externalAddress() const = 0; 00845 00846 private: 00847 KPassiveSocketBase(const KPassiveSocketBase&); 00848 KPassiveSocketBase& operator = (const KPassiveSocketBase&); 00849 }; 00850 00851 } // namespace KNetwork 00852 00853 #endif
KDE 4.6 API Reference