00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef CABAL_TCPSOCKET_H
00015 #define CABAL_TCPSOCKET_H
00016
00017 #include <cabal_socket.h>
00018
00019 namespace Cabal
00020 {
00021
00027 class TCPSocket: public Socket
00028 {
00029 void buildSocket( const Address *local, const Address *remote ) throw( InitError );
00030
00031 protected:
00032 OpType m_opType;
00033 const void *m_buffer;
00034 int m_buflen;
00035 int m_bufdone;
00036
00037 virtual int rawRecv( void *data, const int len )
00038 {
00039 return Net::recv( m_skCom, data, len );
00040 }
00041
00042 virtual int rawSend( const void *data, const int len )
00043 {
00044 return Net::send( m_skCom, data, len );
00045 }
00046
00047 public:
00048
00049 TCPSocket( const long timeout=-1, const Address *local=0, const Address *remote=0 ) throw( InitError )
00050 : Socket( ST_TCP )
00051 {
00052 m_timeout = timeout;
00053 m_opType = OT_NONE;
00054 buildSocket( local, remote );
00055 }
00056
00057 TCPSocket( const Address *local, const Address *remote=0 ) throw( InitError )
00058 : Socket( ST_TCP )
00059 {
00060 m_timeout = -1;
00061 m_opType = OT_NONE;
00062 buildSocket( local, remote );
00063 }
00064
00065 TCPSocket( const long timeout, const char *local_ip, const int port ) throw( InitError )
00066 : Socket( ST_TCP )
00067 {
00068 m_timeout = -1;
00069 m_opType = OT_NONE;
00070 Address addr( local_ip, port );
00071 buildSocket( &addr, 0 );
00072 }
00073
00079 virtual bool close()
00080 {
00081 Net::shutdown( m_skCom );
00082 return Socket::close();
00083 }
00084
00094 bool connect();
00095
00102 bool connect( const Address &remote )
00103 {
00104 m_remote = remote;
00105 return connect();
00106 }
00107
00114 bool connect( const char *ip_addr, const int port ) {
00115 m_remote.set( ip_addr, port );
00116 return connect();
00117 }
00118
00131 int recvAll( void *buffer, const int len );
00132
00143 int sendAll( const void *buffer, const int len );
00144
00146 std::string recvLine( const int maxCount=0 );
00147
00148 int sendLine( const std::string &val )
00149 {
00150 std::string sendstr = val + "\r\n";
00151 return sendAll( sendstr.c_str(), sendstr.length() );
00152 }
00153
00154 int send( const std::string &val )
00155 {
00156 return sendAll( val.c_str(), val.length() );
00157 }
00158
00159 int sendLine( const char *data ) {
00160 return sendLine( std::string( data ) );
00161 }
00162
00163 int sendString( const char *data ) {
00164 return send( std::string( data ) );
00165 }
00166
00170 bool continueOp();
00171 };
00172
00173 }
00174
00175 #endif
00176
00177