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 <qobject.h> 00029 #include <qstring.h> 00030 00031 #include "ksocketbase.h" 00032 #include "kresolver.h" 00033 #include <kdelibs_export.h> 00034 00035 namespace KNetwork { 00036 00037 class KClientSocketBasePrivate; 00049 class KDECORE_EXPORT KClientSocketBase : public QObject, public KActiveSocketBase 00050 { 00051 Q_OBJECT 00052 00053 public: 00071 enum SocketState 00072 { 00073 Idle, 00074 HostLookup, 00075 HostFound, 00076 Bound, 00077 Connecting, 00078 Open, 00079 Closing, 00080 00081 Unconnected = Bound, 00082 Connected = Open, 00083 Connection = Open 00084 }; 00085 00086 public: 00093 KClientSocketBase(QObject* parent, const char *name); 00094 00098 virtual ~KClientSocketBase(); 00099 00104 SocketState state() const; 00105 00106 protected: 00110 virtual bool setSocketOptions(int opts); 00111 00112 public: 00121 KResolver& peerResolver() const; 00122 00126 const KResolverResults& peerResults() const; 00127 00136 KResolver& localResolver() const; 00137 00141 const KResolverResults& localResults() const; 00142 00157 void setResolutionEnabled(bool enable); 00158 00165 void setFamily(int families); 00166 00184 virtual bool lookup(); 00185 00205 virtual bool bind(const QString& node = QString::null, 00206 const QString& service = QString::null) = 0; 00207 00216 virtual bool bind(const KResolverEntry& address); 00217 00246 virtual bool connect(const QString& node = QString::null, 00247 const QString& service = QString::null) = 0; 00248 00253 virtual bool connect(const KResolverEntry& address); 00254 00260 inline void connectToHost(const QString& host, Q_UINT16 port) 00261 { connect(host, QString::number(port)); } 00262 00267 virtual bool disconnect(); 00268 00274 virtual inline bool open(int) 00275 { return connect(); } 00276 00283 virtual void close(); 00284 00288 virtual void flush() 00289 { } 00290 00295 virtual Q_LONG bytesAvailable() const; 00296 00300 virtual Q_LONG waitForMore(int msecs, bool *timeout = 0L); 00301 00305 virtual Q_LONG readBlock(char *data, Q_ULONG maxlen); 00306 00311 virtual Q_LONG readBlock(char *data, Q_ULONG maxlen, KSocketAddress& from); 00312 00316 virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen); 00317 00322 virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen, KSocketAddress &from); 00323 00327 virtual Q_LONG writeBlock(const char *data, Q_ULONG len); 00328 00333 virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to); 00334 00338 virtual KSocketAddress localAddress() const; 00339 00343 virtual KSocketAddress peerAddress() const; 00344 00348 bool emitsReadyRead() const; 00349 00356 virtual void enableRead(bool enable); 00357 00361 bool emitsReadyWrite() const; 00362 00369 virtual void enableWrite(bool enable); 00370 00371 protected slots: 00372 // protected slots 00373 00383 virtual void slotReadActivity(); 00384 00394 virtual void slotWriteActivity(); 00395 00396 private slots: 00397 void lookupFinishedSlot(); 00398 00399 signals: 00408 void stateChanged(int newstate); 00409 00415 void gotError(int code); 00416 00420 void hostFound(); 00421 00428 void bound(const KResolverEntry& local); 00429 00445 void aboutToConnect(const KResolverEntry& remote, bool& skip); 00446 00453 void connected(const KResolverEntry& remote); 00454 00459 void closed(); 00460 00469 void readyRead(); 00470 00482 void readyWrite(); 00483 00484 protected: 00489 void setState(SocketState state); 00490 00500 virtual void stateChanging(SocketState newState); 00501 00506 void copyError(); 00507 00508 private: 00509 KClientSocketBase(const KClientSocketBase&); 00510 KClientSocketBase& operator=(const KClientSocketBase&); 00511 00512 KClientSocketBasePrivate *d; 00513 }; 00514 00515 } // namespace KNetwork 00516 00517 #endif