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:
00064 return "M2S_OPEN " + Utility::l2string(cmd);
00065 case M2S_CLOSE:
00066 return "M2S_CLOSE " + Utility::l2string(cmd);
00067 case M2S_DATA:
00068 return "M2S_DATA " + Utility::l2string(cmd);
00069
00070
00071
00072
00073
00074
00075
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
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 }
00105 return "S2M_BAD COMMAND " + Utility::l2string(cmd);
00106 }
00107
00108