Logo
~Sockets~
~Examples~
~Contact~


BaseSocket.cpp

Go to the documentation of this file.
00001 #include <Utility.h>
00002 
00003 #include "BaseSocket.h"
00004 
00005 
00006 
00007 
00008 BaseSocket::BaseSocket(ISocketHandler& h)
00009 :TcpSocket(h)
00010 {
00011 }
00012 
00013 
00014 BaseSocket::~BaseSocket()
00015 {
00016 }
00017 
00018 
00019 void BaseSocket::SendCmd(unsigned short id,mastercmd_t cmd,short len)
00020 {
00021         char buffer[6];
00022 printf(">Send Id %5u  Command %s  Length %d\n", id, GetText(cmd).c_str(), len);
00023         unsigned short l = htons(id);
00024         memcpy(buffer, &l, 2);
00025         short s = htons(cmd);
00026         memcpy(buffer + 2, &s, 2);
00027         s = htons(len);
00028         memcpy(buffer + 4, &s, 2);
00029         SendBuf(buffer, 6);
00030 }
00031 
00032 
00033 void BaseSocket::SendCmd(unsigned short id,slavecmd_t cmd,short len)
00034 {
00035         char buffer[6];
00036 printf(">Send Id %5u  Command %s  Length %d\n", id, GetText(cmd).c_str(), len);
00037         unsigned short l = htons(id);
00038         memcpy(buffer, &l, 2);
00039         short s = htons(cmd);
00040         memcpy(buffer + 2, &s, 2);
00041         s = htons(len);
00042         memcpy(buffer + 4, &s, 2);
00043         SendBuf(buffer, 6);
00044 }
00045 
00046 
00047 void BaseSocket::printmsg(unsigned short id,mastercmd_t cmd,short len)
00048 {
00049         printf("<Recv Id %5u  Command %s  Length %d\n", id, GetText(cmd).c_str(), len);
00050 }
00051 
00052 
00053 void BaseSocket::printmsg(unsigned short id,slavecmd_t cmd,short len)
00054 {
00055         printf("<Recv Id %5u  Command %s  Length %d\n", id, GetText(cmd).c_str(), len);
00056 }
00057 
00058 
00059 std::string BaseSocket::GetText(mastercmd_t cmd)
00060 {
00061         switch (cmd)
00062         {
00063         case M2S_OPEN: // payload: host:port
00064                 return "M2S_OPEN         " + Utility::l2string(cmd);
00065         case M2S_CLOSE:
00066                 return "M2S_CLOSE        " + Utility::l2string(cmd);
00067         case M2S_DATA: // payload: data
00068                 return "M2S_DATA         " + Utility::l2string(cmd);
00069 /*
00070         case M2S_PROXY_HEADER: // payload: http header to remote server
00071                 return "M2S_PROXY_HEADER " + Utility::l2string(cmd);
00072         case M2S_PROXY_BODY: // payload: data
00073                 return "M2S_PROXY_BODY   " + Utility::l2string(cmd);
00074         case M2S_PROXY_CONNECT: // payload: http proxy header w/ connection info
00075                 return "M2S_PROXY_CONNECT" + Utility::l2string(cmd);
00076 */
00077         }
00078         return         "M2S_BAD COMMAND  " + Utility::l2string(cmd);
00079 }
00080 
00081 
00082 std::string BaseSocket::GetText(slavecmd_t cmd)
00083 {
00084         switch (cmd)
00085         {
00086         case S2M_CONNECT:
00087                 return "S2M_CONNECT      " + Utility::l2string(cmd);
00088         case S2M_DISCONNECTED:
00089                 return "S2M_DISCONNECTED " + Utility::l2string(cmd);
00090         case S2M_DATA:
00091                 return "S2M_DATA         " + Utility::l2string(cmd);
00092 /*
00093         case S2M_PROXY_HEADER:
00094                 return "S2M_PROXY_HEADER " + Utility::l2string(cmd);
00095         case S2M_PROXY_DATA:
00096                 return "S2M_PROXY_DATA   " + Utility::l2string(cmd);
00097         case S2M_PROXY_CONNECT:
00098                 return "S2M_PROXY_CONNECT" + Utility::l2string(cmd);
00099         case S2M_PROXY_CONNECT_FAILED:
00100                 return "S2M_PROXY_CONNECT_FAILED" + Utility::l2string(cmd);
00101         case S2M_PROXY_CLOSE:
00102                 return "S2M_PROXY_CLOSE  " + Utility::l2string(cmd);
00103 */
00104         }
00105         return         "S2M_BAD COMMAND  " + Utility::l2string(cmd);
00106 }
00107 
00108 
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