![]() |
MasterSocket Class Reference#include <MasterSocket.h>
Inheritance diagram for MasterSocket: ![]() ![]()
Detailed Description
Definition at line 9 of file MasterSocket.h. Constructor & Destructor Documentation
Definition at line 9 of file MasterSocket.cpp. 00010 :BaseSocket(h) 00011 ,m_state(0) 00012 { 00013 }
Member Function Documentation
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 }
Definition at line 216 of file MasterSocket.cpp. 00217 { 00218 printf("Lost connection to: %s:%u\n", GetRemoteAddress().c_str(), GetRemotePort()); 00219 }
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 }
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 }
Member Data Documentation
Definition at line 29 of file MasterSocket.h. Referenced by CloseExtConn(), OnRead(), and SendToExt().
The documentation for this class was generated from the following files: |