Logo
~Sockets~
~Examples~
~Contact~


MasterSocket Class Reference

#include <MasterSocket.h>

Inheritance diagram for MasterSocket:

Inheritance graph
[legend]
Collaboration diagram for MasterSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 MasterSocket (ISocketHandler &)
 ~MasterSocket ()
void Init ()
void InitSSLServer ()
void OnAccept ()
void OnSSLAccept ()
void OnDelete ()
void OnRead ()
void SendToExt ()
void CloseExtConn ()

Private Member Functions

 MasterSocket (const MasterSocket &s)
MasterSocketoperator= (const MasterSocket &)

Private Attributes

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

Detailed Description

Definition at line 9 of file MasterSocket.h.


Constructor & Destructor Documentation

MasterSocket::MasterSocket ( ISocketHandler &   ) 

Definition at line 9 of file MasterSocket.cpp.

00010 :BaseSocket(h)
00011 ,m_state(0)
00012 {
00013 }

MasterSocket::~MasterSocket (  ) 

Definition at line 16 of file MasterSocket.cpp.

00017 {
00018 }

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

Definition at line 27 of file MasterSocket.h.

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


Member Function Documentation

void MasterSocket::Init (  ) 

Definition at line 21 of file MasterSocket.cpp.

00022 {
00023         EnableSSL();
00024 }

void MasterSocket::InitSSLServer (  ) 

Definition at line 27 of file MasterSocket.cpp.

00028 {
00029         InitializeContext("master", "comb.pem", "", SSLv23_method());
00030 }

void MasterSocket::OnAccept (  ) 

Definition at line 203 of file MasterSocket.cpp.

00204 {
00205         TcpSocket::OnAccept();
00206         printf("Incoming connection from: %s:%u\n", GetRemoteAddress().c_str(), GetRemotePort());
00207         // check valid ip
00208         if (GetRemoteAddress() != static_cast<MHandler&>(Handler()).GetValidIP())
00209         {
00210                 printf("Refusing IP: %s\n", GetRemoteAddress().c_str());
00211                 SetCloseAndDelete();
00212         }
00213 }

void MasterSocket::OnSSLAccept (  ) 

Definition at line 197 of file MasterSocket.cpp.

00198 {
00199         TcpSocket::OnSSLAccept();
00200 }

void MasterSocket::OnDelete (  ) 

Definition at line 216 of file MasterSocket.cpp.

00217 {
00218         printf("Lost connection to: %s:%u\n", GetRemoteAddress().c_str(), GetRemotePort());
00219 }

void MasterSocket::OnRead (  ) 

Definition at line 33 of file MasterSocket.cpp.

References CloseExtConn(), m_command, m_ip, m_length, m_packet, m_packet_ptr, m_state, PACKETSIZE, BaseSocket::printmsg(), S2M_CONNECT, S2M_DATA, S2M_DISCONNECTED, and SendToExt().

00034 {
00035         TcpSocket::OnRead();
00036         bool need_more = false;
00037         while (!need_more && !CloseAndDelete())
00038         {
00039                 size_t l = ibuf.GetLength();
00040                 switch (m_state)
00041                 {
00042                 case 0: // to id (2 bytes)
00043                         if (l >= 2)
00044                         {
00045                                 ibuf.Read( (char *)&m_ip, 2);
00046                                 m_ip = ntohs(m_ip);
00047                                 m_state = 2;
00048                         }
00049                         else
00050                                 need_more = true;
00051                         break;
00052                 case 2: // command (open, close)
00053                         if (l >= 2)
00054                         {
00055                                 ibuf.Read( (char *)&m_command, 2);
00056                                 m_command = ntohs(m_command);
00057                                 m_state = 3;
00058                         }
00059                         else
00060                                 need_more = true;
00061                         break;
00062                 case 3: // length (2 bytes)
00063                         if (l >= 2)
00064                         {
00065                                 ibuf.Read( (char *)&m_length, 2);
00066                                 m_length = ntohs(m_length);
00067                                 if (m_length > PACKETSIZE)
00068                                 {
00069                                         printf("Incoming length (%d) larger than local buffer (%d) - terminating connection\n",
00070                                                 m_length,
00071                                                 PACKETSIZE);
00072                                         SetCloseAndDelete();
00073                                 }
00074                                 m_packet_ptr = 0;
00075                                 m_state = 4;
00076                         }
00077                         else
00078                                 need_more = true;
00079                         break;
00080                 case 4: // read data (length bytes)
00081                         if (m_length)
00082                         {
00083                                 if (m_packet_ptr + l > PACKETSIZE)
00084                                 {
00085                                         printf("Trying to read beyond local buffer size (%d + %d > %d) - terminating connection\n",
00086                                                 m_packet_ptr, l, PACKETSIZE);
00087                                         SetCloseAndDelete();
00088                                         break;
00089                                 }
00090                                 if (l < m_length - m_packet_ptr) // not enough
00091                                 {
00092                                         ibuf.Read(m_packet + m_packet_ptr, l);
00093                                         m_packet_ptr += l;
00094                                         need_more = true;
00095                                 }
00096                                 else
00097                                 {
00098                                         ibuf.Read(m_packet + m_packet_ptr, m_length - m_packet_ptr);
00099                                         m_packet_ptr += m_length - m_packet_ptr;
00100                                 }
00101                         }
00102                         if (m_length == (short)m_packet_ptr)
00103                         {
00104                                 slavecmd_t cmd = static_cast<slavecmd_t>(m_command);
00105 printmsg(m_ip, cmd, m_length);
00106                                 switch (cmd)
00107                                 {
00108                                 case S2M_CONNECT: // connect
00109                                         break;
00110                                 case S2M_DISCONNECTED: // disconnected
00111                                         CloseExtConn();
00112                                         break;
00113                                 case S2M_DATA: // data
00114                                         SendToExt();
00115                                         break;
00116 /*
00117                                 case S2M_PROXY_HEADER: // proxy header
00118                                         {
00119                                                 unsigned short id;
00120                                                 memcpy(&id, &m_ip, 2);
00121                                                 ProxyInSocket *p = static_cast<MHandler&>(Handler()).GetProxySocket(id);
00122                                                 if (p)
00123                                                         p -> SendBuf(m_packet, m_packet_ptr);
00124                                         }
00125                                         break;
00126                                 case S2M_PROXY_DATA: // proxy data
00127                                         {
00128                                                 unsigned short id;
00129                                                 memcpy(&id, &m_ip, 2);
00130                                                 ProxyInSocket *p = static_cast<MHandler&>(Handler()).GetProxySocket(id);
00131                                                 if (p)
00132                                                         p -> SendBuf(m_packet, m_packet_ptr);
00133                                         }
00134                                         break;
00135                                 case S2M_PROXY_CONNECT: // proxy connect
00136                                         {
00137                                                 unsigned short id;
00138                                                 memcpy(&id, &m_ip, 2);
00139                                                 ProxyInSocket *p = static_cast<MHandler&>(Handler()).GetProxySocket(id);
00140                                                 if (p)
00141                                                 {
00142                                                         p -> Send("HTTP/1.0 200 Connection Established\r\n\r\n");
00143                                                 }
00144                                         }
00145                                         break;
00146                                 case S2M_PROXY_CONNECT_FAILED: // proxy connect failed
00147                                         {
00148                                                 unsigned short id;
00149                                                 memcpy(&id, &m_ip, 2);
00150                                                 ProxyInSocket *p = static_cast<MHandler&>(Handler()).GetProxySocket(id);
00151                                                 if (p)
00152                                                 {
00153                                                         p -> Send("HTTP/1.0 404 Failed\r\n\r\n");
00154                                                 }
00155                                         }
00156                                         break;
00157                                 case S2M_PROXY_CLOSE: // close
00158                                         {
00159                                                 unsigned short id;
00160                                                 memcpy(&id, &m_ip, 2);
00161                                                 ProxyInSocket *p = static_cast<MHandler&>(Handler()).GetProxySocket(id);
00162                                                 if (p)
00163                                                         p -> SetCloseAndDelete();
00164                                         }
00165                                         break;
00166 */
00167                                 }
00168                                 m_state = 0;
00169                         }
00170                         break;
00171                 default:
00172                         printf("MasterSocket: Bad state (%d)\n", m_state);
00173                         assert(0);
00174                         SetCloseAndDelete();
00175                         break;
00176                 }
00177         } // while (!need_more)
00178 }

void MasterSocket::SendToExt (  ) 

Definition at line 181 of file MasterSocket.cpp.

References m_ip, m_length, and m_packet.

Referenced by OnRead().

00182 {
00183         unsigned short id;
00184         memcpy(&id, &m_ip, 2);
00185         static_cast<MHandler&>(Handler()).SendToExt(id, m_packet, m_length);
00186 }

void MasterSocket::CloseExtConn (  ) 

Definition at line 189 of file MasterSocket.cpp.

References m_ip.

Referenced by OnRead().

00190 {
00191         unsigned short id;
00192         memcpy(&id, &m_ip, 2);
00193         static_cast<MHandler&>(Handler()).CloseExtConn(id);
00194 }

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

Definition at line 28 of file MasterSocket.h.

00028 { return *this; } // assignment operator


Member Data Documentation

unsigned short MasterSocket::m_ip [private]

Definition at line 29 of file MasterSocket.h.

Referenced by CloseExtConn(), OnRead(), and SendToExt().

short MasterSocket::m_command [private]

Definition at line 30 of file MasterSocket.h.

Referenced by OnRead().

short MasterSocket::m_length [private]

Definition at line 31 of file MasterSocket.h.

Referenced by OnRead(), and SendToExt().

char MasterSocket::m_packet[PACKETSIZE] [private]

Definition at line 32 of file MasterSocket.h.

Referenced by OnRead(), and SendToExt().

size_t MasterSocket::m_packet_ptr [private]

Definition at line 33 of file MasterSocket.h.

Referenced by OnRead().

int MasterSocket::m_state [private]

Definition at line 34 of file MasterSocket.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