• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/net/TcpSocket.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _tcpsocket_h
00018 #define _tcpsocket_h
00019 
00020 #include <spl/Debug.h>
00021 #include <spl/RefCountPtr.h>
00022 #include <spl/net/Socket.h>
00023 
00030 class TcpSocket;
00031 typedef RefCountPtr<TcpSocket> TcpSocketPtr;
00032 typedef WeakReference<TcpSocket, TcpSocketPtr> TcpSocketRef;
00033 
00034 REGISTER_TYPEOF(68, TcpSocketPtr);
00035 REGISTER_TYPEOF( 476, TcpSocketRef );
00036 
00040 class TcpSocket : public IMemoryValidate
00041 {
00042 private:
00043         // Copy constructor doesn't make sense for this class
00044         inline TcpSocket(const TcpSocket& sock) {}
00045         inline TcpSocket& operator =(const TcpSocket& pmp) {}
00046 
00047 protected:
00048         String m_address;
00049         SocketPtr m_sock;
00050         spl::IStreamPtr m_strm;
00051 
00052         TcpSocket( SOCKET fd );
00053 
00054 public:
00055         TcpSocket( const String& address, int port );
00056         virtual ~TcpSocket();
00057 
00058         void Connect ();   // INADDR_ANY...
00059 
00061         spl::IStreamPtr GetStream();
00062 
00063         inline bool IsClosed() { return m_sock->IsClosed(); }
00064         inline void Close() { m_sock->Close(); }
00065         inline void Shutdown( int i=SD_BOTH ) { m_sock->Shutdown(i); }
00066         
00067         inline void SetNonBlocking() { m_sock->SetNonBlocking(); }
00068         inline void SetBlocking() { m_sock->SetBlocking(); }
00069         inline void SetLingerOn() { m_sock->SetLingerOn(); }
00070         inline void SetLingerOff() { m_sock->SetLingerOff(); }
00071         inline void SetNoDelay() { m_sock->SetNoDelay(); }
00072 
00073         inline void SetSendTimeout(int toMS) { m_sock->SetSendTimeout(toMS); }
00074         inline void SetRecvTimeout(int toMS) { m_sock->SetRecvTimeout(toMS); }
00075         inline int GetSendTimeout() { return m_sock->GetSendTimeout(); }
00076         inline int GetRecvTimeout() { return m_sock->GetRecvTimeout(); }
00077 
00078         inline int GetErrorCode() { return m_sock->m_errorStatus; }
00079         inline int GetBytesAvail() { return m_sock->GetBytesAvail(); }
00080 
00081         inline String GetRemoteIp(  ) { return m_sock->GetRemoteIp(); }
00082 
00083         inline bool operator ==(const TcpSocket& s) const
00084         {
00085                 return m_sock->GetFD() == s.m_sock->GetFD();
00086         }
00087 
00088         friend class ServerSocket;
00089         friend class SocketSet;
00090 
00091 #if defined(DEBUG) || defined(_DEBUG)
00092         virtual void CheckMem() const;
00093         virtual void ValidateMem() const;
00094 #endif
00095 };
00096 
00097 REGISTER_TYPEOF(70, TcpSocket);
00098 
00101 #endif