ksocketdevice.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 #ifndef KSOCKETDEVICE_H 00026 #define KSOCKETDEVICE_H 00027 00028 #include <tqsocketnotifier.h> 00029 #include "ksocketbase.h" 00030 00031 namespace KNetwork { 00032 00033 class KSocketDevice; 00034 class KSocketDeviceFactoryBase; 00035 00036 class KSocketDevicePrivate; 00050 class KDECORE_EXPORT KSocketDevice: public KActiveSocketBase, public KPassiveSocketBase 00051 { 00052 public: 00063 enum Capabilities 00064 { 00067 CanConnectString = 0x01, 00068 00071 CanBindString = 0x02, 00072 00075 CanNotBind = 0x04, 00076 00079 CanNotListen = 0x08, 00080 00084 CanMulticast = 0x10, 00085 00090 CanNotUseDatagrams = 0x20 00091 }; 00092 protected: 00095 int m_sockfd; 00096 00097 public: 00104 explicit KSocketDevice(const KSocketBase* = 0L); 00105 00112 explicit KSocketDevice(int fd); 00113 00117 virtual ~KSocketDevice(); 00118 00122 inline int socket() const 00123 { return m_sockfd; } 00124 00134 virtual int capabilities() const 00135 { return 0; } 00136 00140 virtual bool setSocketOptions(int opts); 00141 00145 virtual bool open(TQ_OpenMode mode); 00146 00152 virtual void close(); 00153 00157 virtual void flush() 00158 { } 00159 00164 virtual bool create(int family, int type, int protocol); 00165 00170 bool create(const KResolverEntry& address); 00171 00175 virtual bool bind(const KResolverEntry& address); 00176 00180 virtual bool listen(int backlog = 5); // 5 is arbitrary 00181 00185 virtual bool connect(const KResolverEntry& address); 00186 00191 virtual KSocketDevice* accept(); 00192 00196 virtual bool disconnect(); 00197 00201 #ifdef USE_QT3 00202 virtual TQ_LONG bytesAvailable() const; 00203 #endif 00204 #ifdef USE_QT4 00205 virtual qint64 bytesAvailable() const; 00206 #endif 00207 00214 virtual TQ_LONG waitForMore(int msecs, bool *timeout = 0L); 00215 00219 virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen); 00220 00224 virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen, KSocketAddress& from); 00225 00229 virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen); 00230 00234 virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen, KSocketAddress& from); 00235 00239 virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len); 00240 00244 virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len, const KSocketAddress& to); 00245 00249 virtual KSocketAddress localAddress() const; 00250 00255 virtual KSocketAddress peerAddress() const; 00256 00271 virtual KSocketAddress externalAddress() const; 00272 00280 TQSocketNotifier* readNotifier() const; 00281 00288 TQSocketNotifier* writeNotifier() const; 00289 00296 TQSocketNotifier* exceptionNotifier() const; 00297 00316 virtual bool poll(bool* input, bool* output, bool* exception = 0L, 00317 int timeout = -1, bool* timedout = 0L); 00318 00330 bool poll(int timeout = -1, bool* timedout = 0L); 00331 00332 protected: 00340 KSocketDevice(bool, const KSocketBase* parent = 0L); 00341 00357 virtual TQSocketNotifier* createNotifier(TQSocketNotifier::Type type) const; 00358 00359 public: 00370 static KSocketDevice* createDefault(KSocketBase* parent); 00371 00380 static KSocketDevice* createDefault(KSocketBase* parent, int capabilities); 00381 00388 static KSocketDeviceFactoryBase* setDefaultImpl(KSocketDeviceFactoryBase* factory); 00389 00394 static void addNewImpl(KSocketDeviceFactoryBase* factory, int capabilities); 00395 00396 private: 00397 KSocketDevice(const KSocketDevice&); 00398 KSocketDevice& operator=(const KSocketDevice&); 00399 00400 KSocketDevicePrivate *d; 00401 }; 00402 00407 class KSocketDeviceFactoryBase 00408 { 00409 public: 00410 KSocketDeviceFactoryBase() {} 00411 virtual ~KSocketDeviceFactoryBase() {} 00412 00413 virtual KSocketDevice* create(KSocketBase*) const = 0; 00414 }; 00415 00420 template<class Impl> 00421 class KSocketDeviceFactory: public KSocketDeviceFactoryBase 00422 { 00423 public: 00424 KSocketDeviceFactory() {} 00425 virtual ~KSocketDeviceFactory() {} 00426 00427 virtual KSocketDevice* create(KSocketBase* parent) const 00428 { return new Impl(parent); } 00429 }; 00430 00431 } // namespaces 00432 00433 #endif