kclientsocketbase.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 KCLIENTSOCKETBASE_H 00026 #define KCLIENTSOCKETBASE_H 00027 00028 #include <tqobject.h> 00029 #include <tqstring.h> 00030 00031 #include "ksocketbase.h" 00032 #include "kresolver.h" 00033 #include <kdelibs_export.h> 00034 00035 #ifdef Q_MOC_RUN 00036 #define USE_QT4 00037 #endif // Q_MOC_RUN 00038 00039 namespace KNetwork { 00040 00041 class KClientSocketBasePrivate; 00053 class KDECORE_EXPORT KClientSocketBase : 00054 #ifdef USE_QT4 00055 #else // USE_QT4 00056 public TQObject, 00057 #endif // USE_QT4 00058 public KActiveSocketBase 00059 { 00060 Q_OBJECT 00061 TQ_OBJECT 00062 00063 public: 00081 enum SocketState 00082 { 00083 Idle, 00084 HostLookup, 00085 HostFound, 00086 Bound, 00087 Connecting, 00088 Open, 00089 Closing, 00090 00091 Unconnected = Bound, 00092 Connected = Open, 00093 Connection = Open 00094 }; 00095 00096 public: 00103 KClientSocketBase(TQObject* parent, const char *name); 00104 00108 virtual ~KClientSocketBase(); 00109 00114 SocketState state() const; 00115 00116 protected: 00120 virtual bool setSocketOptions(int opts); 00121 00122 public: 00131 KResolver& peerResolver() const; 00132 00136 const KResolverResults& peerResults() const; 00137 00146 KResolver& localResolver() const; 00147 00151 const KResolverResults& localResults() const; 00152 00167 void setResolutionEnabled(bool enable); 00168 00175 void setFamily(int families); 00176 00194 virtual bool lookup(); 00195 00215 virtual bool bind(const TQString& node = TQString::null, 00216 const TQString& service = TQString::null) = 0; 00217 00226 virtual bool bind(const KResolverEntry& address); 00227 00256 virtual bool connect(const TQString& node = TQString::null, 00257 const TQString& service = TQString::null) = 0; 00258 00263 virtual bool connect(const KResolverEntry& address); 00264 00270 inline void connectToHost(const TQString& host, TQ_UINT16 port) 00271 { connect(host, TQString::number(port)); } 00272 00277 virtual bool disconnect(); 00278 00284 virtual inline bool open(TQ_OpenMode) 00285 { return connect(); } 00286 00293 virtual void close(); 00294 00298 virtual void flush() 00299 { } 00300 00305 #ifdef USE_QT3 00306 virtual TQ_LONG bytesAvailable() const; 00307 #endif 00308 #ifdef USE_QT4 00309 virtual qint64 bytesAvailable() const; 00310 #endif 00311 00315 virtual TQ_LONG waitForMore(int msecs, bool *timeout = 0L); 00316 00320 virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen); 00321 00326 virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen, KSocketAddress& from); 00327 00331 virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen); 00332 00337 virtual TQ_LONG peekBlock(char *data, TQ_ULONG maxlen, KSocketAddress &from); 00338 00342 virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len); 00343 00348 virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len, const KSocketAddress& to); 00349 00353 virtual KSocketAddress localAddress() const; 00354 00358 virtual KSocketAddress peerAddress() const; 00359 00363 bool emitsReadyRead() const; 00364 00371 virtual void enableRead(bool enable); 00372 00376 bool emitsReadyWrite() const; 00377 00384 virtual void enableWrite(bool enable); 00385 00386 protected slots: 00387 // protected slots 00388 00398 virtual void slotReadActivity(); 00399 00409 virtual void slotWriteActivity(); 00410 00411 private slots: 00412 void lookupFinishedSlot(); 00413 00414 signals: 00423 void stateChanged(int newstate); 00424 00430 void gotError(int code); 00431 00435 void hostFound(); 00436 00443 void bound(const KResolverEntry& local); 00444 00460 void aboutToConnect(const KResolverEntry& remote, bool& skip); 00461 00468 void connected(const KResolverEntry& remote); 00469 00474 void closed(); 00475 00484 void readyRead(); 00485 00497 void readyWrite(); 00498 00499 protected: 00504 void setState(SocketState state); 00505 00515 virtual void stateChanging(SocketState newState); 00516 00521 void copyError(); 00522 00523 private: 00524 KClientSocketBase(const KClientSocketBase&); 00525 KClientSocketBase& operator=(const KClientSocketBase&); 00526 00527 KClientSocketBasePrivate *d; 00528 }; 00529 00530 } // namespace KNetwork 00531 00532 #endif