Fixed automatic size on Messages

This commit is contained in:
Pontus Fransson 2013-11-22 15:48:49 +01:00
parent 6eff55e763
commit ad7091e520
4 changed files with 18 additions and 10 deletions

View File

@ -8,23 +8,20 @@ using namespace Oyster::Network::Protocols;
MessageHeader::MessageHeader() MessageHeader::MessageHeader()
{ {
size = 0; size = 0;
} }
MessageHeader::~MessageHeader() MessageHeader::~MessageHeader()
{ {
} }
void MessageHeader::Translate(ProtocolHeader& header, unsigned char msg[] ) void MessageHeader::Translate(ProtocolHeader& header, unsigned char msg[] )
{ {
size = 0; size = 0;
AddInt(header.clientID, msg);
AddInt(header.packageType, msg);
AddInt(header.size, msg); AddInt(header.size, msg);
AddInt(header.packageType, msg);
AddInt(header.clientID, msg);
SetSize(msg);
} }
void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header) void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header)
@ -36,7 +33,6 @@ void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header)
header.size = GetInt(message); header.size = GetInt(message);
} }
void MessageHeader::AddInt(int i, unsigned char msg[]) void MessageHeader::AddInt(int i, unsigned char msg[])
{ {
Pack(&msg[size], i); Pack(&msg[size], i);
@ -62,3 +58,8 @@ std::string MessageHeader::GetStr(unsigned char message[])
size += 2 + std.length(); size += 2 + std.length();
return std; return std;
} }
void MessageHeader::SetSize(unsigned char msg[])
{
Pack(&msg[0], size);
}

View File

@ -33,6 +33,8 @@ namespace Oyster
int GetInt(unsigned char message[]); int GetInt(unsigned char message[]);
std::string GetStr(unsigned char message[]); std::string GetStr(unsigned char message[]);
void SetSize(unsigned char msg[]);
private: private:
int size; int size;

View File

@ -5,7 +5,6 @@ using namespace Oyster::Network::Protocols;
MessageTest::MessageTest() MessageTest::MessageTest()
{ {
} }
MessageTest::~MessageTest() MessageTest::~MessageTest()
@ -17,6 +16,7 @@ void MessageTest::Translate(ProtocolHeader& header, unsigned char msg[])
MessageHeader::Translate(header, msg); MessageHeader::Translate(header, msg);
AddStr(static_cast<ProtocolTest*>(&header)->textMessage, msg); AddStr(static_cast<ProtocolTest*>(&header)->textMessage, msg);
SetSize(msg);
} }
void MessageTest::Translate(unsigned char message[], ProtocolHeader& header) void MessageTest::Translate(unsigned char message[], ProtocolHeader& header)

View File

@ -41,8 +41,13 @@ int main()
Client client2(clientSocket); Client client2(clientSocket);
cout << "Second client connected." << endl; cout << "Second client connected." << endl;
client1.Send((unsigned char*)"Hej");
ProtocolSet* set = new ProtocolSet; ProtocolSet* set = new ProtocolSet;
ProtocolTest test;
test.clientID = 0;
test.textMessage = "hej";
recvBuffer = t.Translate(test);
client1.Send(recvBuffer);
while(1) while(1)
{ {