KDECore
k3socketbase.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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;
00084 class KDECORE_EXPORT KSocketBase
00085 {
00086 public:
00107 enum SocketOptions
00108 {
00109 Blocking = 0x01,
00110 AddressReuseable = 0x02,
00111 IPv6Only = 0x04,
00112 Keepalive = 0x08,
00113 Broadcast = 0x10,
00114 NoDelay = 0x20
00115 };
00116
00142 enum SocketError
00143 {
00144 NoError = 0,
00145 LookupFailure,
00146 AddressInUse,
00147 AlreadyCreated,
00148 AlreadyBound,
00149 AlreadyConnected,
00150 NotConnected,
00151 NotBound,
00152 NotCreated,
00153 WouldBlock,
00154 ConnectionRefused,
00155 ConnectionTimedOut,
00156 InProgress,
00157 NetFailure,
00158 NotSupported,
00159 Timeout,
00160 UnknownError,
00161 RemotelyDisconnected
00162 };
00163
00164 public:
00168 KSocketBase();
00169
00173 virtual ~KSocketBase();
00174
00175
00176
00177
00178
00179
00180 protected:
00194 virtual bool setSocketOptions(int opts);
00195
00205 virtual int socketOptions() const;
00206
00207 public:
00223 virtual bool setBlocking(bool enable);
00224
00231 bool blocking() const;
00232
00247 virtual bool setAddressReuseable(bool enable);
00248
00255 bool addressReuseable() const;
00256
00272 virtual bool setIPv6Only(bool enable);
00273
00280 bool isIPv6Only() const;
00281
00293 virtual bool setBroadcast(bool enable);
00294
00301 bool broadcast() const;
00302
00316 virtual bool setNoDelay(bool enable);
00317
00323 bool noDelay() const;
00324
00331 KSocketDevice* socketDevice() const;
00332
00346 virtual void setSocketDevice(KSocketDevice* device);
00347
00369 int setRequestedCapabilities(int add, int remove = 0);
00370
00371 protected:
00376 bool hasDevice() const;
00377
00383 void setError(SocketError error);
00384
00388 void resetError();
00389
00390 public:
00395 SocketError error() const;
00396
00400 QString errorString() const;
00401
00417 QMutex* mutex() const;
00418
00419 public:
00425 static QString errorString(SocketError code);
00426
00435 static bool isFatalError(int code);
00436
00437 private:
00440 void unsetSocketDevice();
00441
00442 KSocketBase(const KSocketBase&);
00443 KSocketBase& operator =(const KSocketBase&);
00444
00445 KSocketBasePrivate* const d;
00446
00447 friend class KSocketDevice;
00448 };
00449
00459 class KDECORE_EXPORT KActiveSocketBase: public QIODevice, virtual public KSocketBase
00460 {
00461 Q_OBJECT
00462 public:
00466 KActiveSocketBase(QObject* parent);
00467
00471 virtual ~KActiveSocketBase();
00472
00476 QString errorString() const;
00477
00481 virtual void setSocketDevice(KSocketDevice* device);
00482
00486 virtual bool open(OpenMode mode);
00487
00498 virtual bool bind(const KResolverEntry& address) = 0;
00499
00518 virtual bool connect(const KResolverEntry& address,
00519 OpenMode mode = ReadWrite) = 0;
00520
00536 virtual bool disconnect() = 0;
00537
00541 virtual bool isSequential() const;
00542
00547 virtual qint64 size() const;
00548
00553 virtual qint64 pos() const;
00554
00559 virtual bool seek(qint64);
00560
00565 virtual bool atEnd() const;
00566
00573 qint64 read(char *data, qint64 maxlen);
00574
00581 QByteArray read(qint64 len);
00582
00594 qint64 read(char *data, qint64 maxlen, KSocketAddress& from);
00595
00607 qint64 peek(char *data, qint64 maxlen);
00608
00622 qint64 peek(char *data, qint64 maxlen, KSocketAddress& from);
00623
00630 qint64 write(const char *data, qint64 len);
00631
00638 qint64 write(const QByteArray& data);
00639
00651 qint64 write(const char *data, qint64 len, const KSocketAddress& to);
00652
00664 virtual qint64 waitForMore(int msecs, bool *timeout = 0L) = 0;
00665
00669 void ungetChar(char);
00670
00674 virtual KSocketAddress localAddress() const = 0;
00675
00681 virtual KSocketAddress peerAddress() const = 0;
00682
00683
00684
00685 #if 0
00686
00689 virtual KSocketAddress externalAddress() const = 0;
00690 #endif
00691
00692 protected:
00699 virtual qint64 readData(char *data, qint64 len);
00700
00712 virtual qint64 readData(char *data, qint64 maxlen, KSocketAddress* from) = 0;
00713
00726 virtual qint64 peekData(char *data, qint64 maxlen, KSocketAddress* from) = 0;
00727
00734 virtual qint64 writeData(const char *data, qint64 len);
00735
00747 virtual qint64 writeData(const char *data, qint64 len, const KSocketAddress* to) = 0;
00748
00754 void setError(SocketError error);
00755
00759 void resetError();
00760 };
00761
00771 class KDECORE_EXPORT KPassiveSocketBase: virtual public KSocketBase
00772 {
00773 public:
00777 KPassiveSocketBase();
00778
00782 virtual ~KPassiveSocketBase();
00783
00794 virtual bool bind(const KResolverEntry& address) = 0;
00795
00810 virtual bool listen(int backlog) = 0;
00811
00816 virtual void close() = 0;
00817
00831 virtual KActiveSocketBase* accept() = 0;
00832
00836 virtual KSocketAddress localAddress() const = 0;
00837
00841 virtual KSocketAddress externalAddress() const = 0;
00842
00843 private:
00844 KPassiveSocketBase(const KPassiveSocketBase&);
00845 KPassiveSocketBase& operator = (const KPassiveSocketBase&);
00846 };
00847
00848 }
00849
00850 #endif