Logo
~Sockets~
~Examples~
~Contact~


SlaveSocket Class Reference

#include <SlaveSocket.h>

Inheritance diagram for SlaveSocket:

Inheritance graph
[legend]
Collaboration diagram for SlaveSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 SlaveSocket (ISocketHandler &)
 ~SlaveSocket ()
void OnConnect ()
void OnRead ()
void OpenConnection ()
void CloseConnection ()
void SendPacket ()
InternalSocketGetSock (unsigned short id)
port_t GetHostPort (const std::string &header, std::string &host)

Private Member Functions

 SlaveSocket (const SlaveSocket &s)
SlaveSocketoperator= (const SlaveSocket &)

Private Attributes

unsigned short m_ip
short m_command
short m_length
char m_packet [32000]
size_t m_packet_ptr
int m_state

Detailed Description

Definition at line 9 of file SlaveSocket.h.


Constructor & Destructor Documentation

SlaveSocket::SlaveSocket ( ISocketHandler &   ) 

Definition at line 13 of file SlaveSocket.cpp.

00014 :BaseSocket(h)
00015 ,m_state(0)
00016 {
00017 }

SlaveSocket::~SlaveSocket (  ) 

Definition at line 20 of file SlaveSocket.cpp.

00021 {
00022 }

SlaveSocket::SlaveSocket ( const SlaveSocket s  )  [inline, private]

Definition at line 30 of file SlaveSocket.h.

00030 : BaseSocket(s) {} // copy constructor


Member Function Documentation

void SlaveSocket::OnConnect (  )  [inline]

Definition at line 15 of file SlaveSocket.h.

00015                          {
00016 printf("Connected to %s:%u\n", GetRemoteAddress().c_str(), GetRemotePort());
00017                 TcpSocket::OnConnect();
00018         }

void SlaveSocket::OnRead (  ) 

Definition at line 25 of file SlaveSocket.cpp.

References CloseConnection(), M2S_CLOSE, M2S_DATA, M2S_OPEN, m_command, m_ip, m_length, m_packet, m_packet_ptr, m_state, OpenConnection(), BaseSocket::printmsg(), and SendPacket().

00026 {
00027         TcpSocket::OnRead();
00028         bool need_more = false;
00029         while (!need_more && !CloseAndDelete())
00030         {
00031                 size_t l = ibuf.GetLength();
00032                 switch (m_state)
00033                 {
00034                 case 0: // to id (2 bytes)
00035                         if (l >= 2)
00036                         {
00037                                 ibuf.Read( (char *)&m_ip, 2);
00038                                 m_ip = ntohs(m_ip);
00039                                 m_state = 2;
00040                         }
00041                         else
00042                                 need_more = true;
00043                         break;
00044                 case 2: // command (open, close)
00045                         if (l >= 2)
00046                         {
00047                                 ibuf.Read( (char *)&m_command, 2);
00048                                 m_command = ntohs(m_command);
00049                                 m_state = 3;
00050                         }
00051                         else
00052                                 need_more = true;
00053                         break;
00054                 case 3: // length (2 bytes)
00055                         if (l >= 2)
00056                         {
00057                                 ibuf.Read( (char *)&m_length, 2);
00058                                 m_length = ntohs(m_length);
00059                                 m_packet_ptr = 0;
00060                                 m_state = 4;
00061                         }
00062                         else
00063                                 need_more = true;
00064                         break;
00065                 case 4: // read data (length bytes)
00066                         if (m_length)
00067                         {
00068                                 if (l < m_length - m_packet_ptr) // not enough
00069                                 {
00070                                         ibuf.Read(m_packet + m_packet_ptr, l);
00071                                         m_packet_ptr += l;
00072                                         need_more = true;
00073                                 }
00074                                 else
00075                                 {
00076                                         ibuf.Read(m_packet + m_packet_ptr, m_length - m_packet_ptr);
00077                                         m_packet_ptr += m_length - m_packet_ptr;
00078                                 }
00079                         }
00080                         if (m_packet_ptr == (size_t)m_length)
00081                         {
00082                                 mastercmd_t cmd = static_cast<mastercmd_t>(m_command);
00083 printmsg(m_ip, cmd, m_length);
00084                                 switch (cmd)
00085                                 {
00086                                 case M2S_OPEN: // open
00087                                         OpenConnection();
00088                                         break;
00089                                 case M2S_CLOSE: // close
00090                                         CloseConnection();
00091                                         break;
00092                                 case M2S_DATA: // data
00093                                         SendPacket();
00094                                         break;
00095 /*
00096                                 case M2S_PROXY_HEADER: // http proxy header
00097                                         // m_ip - proxy session id#
00098                                         // m_port - 0
00099                                         m_packet[m_length] = 0;
00100                                         {
00101                                                 std::string header = m_packet;
00102                                                 std::string host;
00103                                                 port_t port = GetHostPort(header, host);
00104                                                 unsigned short id;
00105                                                 memcpy(&id, &m_ip, 2);
00106                                                 ProxyOutSocket *p = new ProxyOutSocket(Handler(), id, false); // false - no connect
00107                                                 p -> SetDeleteByHandler();
00108                                                 p -> Open(host, port);
00109                                                 Handler().Add(p);
00110                                                 p -> Send(header);
00111                                         }
00112                                         break;
00113                                 case M2S_PROXY_BODY: // http proxy body
00114                                         // m_ip - proxy session id#
00115                                         // m_port - 0
00116                                         {
00117                                                 unsigned short id;
00118                                                 memcpy(&id, &m_ip, 2);
00119                                                 TcpSocket *p = static_cast<SlaveHandler&>(Handler()).GetProxySocket(id);
00120                                                 if (p)
00121                                                 {
00122                                                         p -> SendBuf(m_packet, m_length);
00123                                                 }
00124                                         }
00125                                         break;
00126                                 case M2S_PROXY_CONNECT: // proxy connect
00127                                         // m_ip - proxy session id#
00128                                         // m_port - 0
00129                                         m_packet[m_length] = 0;
00130                                         {
00131                                                 std::string header = m_packet;
00132                                                 std::string host;
00133                                                 port_t port = GetHostPort(header, host);
00134                                                 unsigned short id;
00135                                                 memcpy(&id, &m_ip, 2);
00136                                                 ProxyBinSocket *p = new ProxyBinSocket(Handler(), id);
00137                                                 p -> SetDeleteByHandler();
00138                                                 p -> Open(host, port);
00139                                                 Handler().Add(p);
00140                                         }
00141                                         break;
00142 */
00143                                 } // switch (command)
00144                                 m_state = 0;
00145                         } // length == packet_ptr
00146                         break;
00147                 default:
00148                         printf("SlaveSocket: Bad state (%d)\n", m_state);
00149                         assert(0);
00150                         SetCloseAndDelete();
00151                         break;
00152                 }
00153         } // while (!need_more)
00154 }

void SlaveSocket::OpenConnection (  ) 

Definition at line 157 of file SlaveSocket.cpp.

References m_ip, m_length, and m_packet.

Referenced by OnRead().

00158 {
00159         unsigned short id;
00160         memcpy(&id, &m_ip, 2);
00161         m_packet[m_length] = 0;
00162 printf(" payload: %s\n", m_packet);
00163         Parse pa(m_packet, ":");
00164         std::string host = pa.getword();
00165         port_t port = pa.getvalue();
00166 printf(" host: %s  port: %u\n", host.c_str(), port);
00167         InternalSocket *p = new InternalSocket(Handler(), this, id);
00168         p -> SetDeleteByHandler();
00169         p -> Open(host, port);
00170         Handler().Add(p);
00171 }

void SlaveSocket::CloseConnection (  ) 

Definition at line 174 of file SlaveSocket.cpp.

References GetSock(), and m_ip.

Referenced by OnRead().

00175 {
00176         unsigned short id;
00177         memcpy(&id, &m_ip, 2);
00178         InternalSocket *p = GetSock(id);
00179         if (p)
00180                 p -> SetCloseAndDelete();
00181 }

void SlaveSocket::SendPacket (  ) 

Definition at line 184 of file SlaveSocket.cpp.

References GetSock(), m_ip, m_length, and m_packet.

Referenced by OnRead().

00185 {
00186         unsigned short id;
00187         memcpy(&id, &m_ip, 2);
00188         InternalSocket *p = GetSock(id);
00189         if (p)
00190                 p -> SendBuf(m_packet, m_length);
00191 }

InternalSocket * SlaveSocket::GetSock ( unsigned short  id  ) 

Definition at line 194 of file SlaveSocket.cpp.

Referenced by CloseConnection(), and SendPacket().

00195 {
00196         return static_cast<SlaveHandler&>(Handler()).GetSock(id);
00197 }

port_t SlaveSocket::GetHostPort ( const std::string &  header,
std::string &  host 
)

Definition at line 200 of file SlaveSocket.cpp.

00201 {
00202         size_t i = 0;
00203         port_t port = 0;
00204         while (i < header.size())
00205         {
00206                 size_t x = i;
00207                 while (header[i] != 13 && header[i] != 10 && i < header.size())
00208                 {
00209                         i++;
00210                 }
00211                 std::string line = header.substr(x, i - x);
00212                 while (i < header.size() && (header[i] == 13 || header[i] == 10))
00213                 {
00214                         i++;
00215                 }
00216                 Parse pa(line, ":");
00217                 std::string key = pa.getword();
00218                 if (!strcasecmp(key.c_str(), "host"))
00219                 {
00220                         Parse pa2(pa.getrest(), ":");
00221                         host = pa2.getword();
00222                         port = pa2.getvalue();
00223                         if (!port)
00224                                 port = 80;
00225                         return port;
00226                 }
00227         }
00228         return 0;
00229 }

SlaveSocket& SlaveSocket::operator= ( const SlaveSocket  )  [inline, private]

Definition at line 31 of file SlaveSocket.h.

00031 { return *this; } // assignment operator


Member Data Documentation

unsigned short SlaveSocket::m_ip [private]

Definition at line 32 of file SlaveSocket.h.

Referenced by CloseConnection(), OnRead(), OpenConnection(), and SendPacket().

short SlaveSocket::m_command [private]

Definition at line 33 of file SlaveSocket.h.

Referenced by OnRead().

short SlaveSocket::m_length [private]

Definition at line 34 of file SlaveSocket.h.

Referenced by OnRead(), OpenConnection(), and SendPacket().

char SlaveSocket::m_packet[32000] [private]

Definition at line 35 of file SlaveSocket.h.

Referenced by OnRead(), OpenConnection(), and SendPacket().

size_t SlaveSocket::m_packet_ptr [private]

Definition at line 36 of file SlaveSocket.h.

Referenced by OnRead().

int SlaveSocket::m_state [private]

Definition at line 37 of file SlaveSocket.h.

Referenced by OnRead().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4