Added protocols to an enum

Now we don't have to type cast
This commit is contained in:
Pontus Fransson 2013-11-22 11:40:55 +01:00
parent 9e6593abd1
commit 623cffced6
6 changed files with 66 additions and 26 deletions

View File

@ -10,7 +10,7 @@ namespace Oyster
public:
virtual unsigned char* Translate (Protocols::ProtocolHeader &header ) = 0;
virtual Protocols::ProtocolHeader* Translate ( unsigned char message[] ) = 0;
virtual Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char message[] ) = 0;
};
}

View File

@ -17,7 +17,6 @@ namespace Oyster
package_type_update_position
};
struct ProtocolHeader
{
int size;
@ -32,14 +31,37 @@ namespace Oyster
std::string textMessage;
ProtocolTest() { this->packageType = package_type_test; }
};
/*struct Prutt
class ProtocolSet
{
public:
PackageType t;
union PRUTT
union
{
ProtocolTest *ptest,
ProtocolHeader* pHeader;
ProtocolTest *pTest;
}Protocol;
void Release()
{
switch(t)
{
case package_type_header:
if(Protocol.pHeader)
{
delete Protocol.pHeader;
}
break;
case package_type_test:
if(Protocol.pTest)
{
delete Protocol.pTest;
}
break;
}
}
};
};*/
}
}
}

View File

@ -25,26 +25,35 @@ unsigned char* Translator::Translate( ProtocolHeader &header )
return message->GetMsg();
}
ProtocolHeader* Translator::Translate(unsigned char msg[] )
ProtocolSet* Translator::Translate(ProtocolSet* set, unsigned char msg[] )
{
ProtocolHeader *header = new ProtocolHeader();
MessageHeader *message = new MessageHeader();
message->Translate(msg, *header);
delete message;
message = NULL;
switch(header->packageType)
//Switch to the correct package.
set->t = (PackageType)header->packageType;
switch(set->t)
{
case package_type_header:
message = new MessageHeader();
header = new ProtocolHeader();
set->Protocol.pHeader = new ProtocolHeader;
message->Translate(msg, *set->Protocol.pHeader);
break;
case package_type_test:
message = new MessageTest();
header = new ProtocolTest();
set->Protocol.pTest = new ProtocolTest;
message->Translate(msg, *set->Protocol.pTest);
break;
}
message->Translate(msg, *header);
return header;
if(message)
delete message;
delete header;
return set;
}

View File

@ -12,11 +12,14 @@ namespace Oyster
class Translator : public ITranslate
{
public:
Translator (){};
~Translator(){};
Translator (){ };
~Translator(){ };
unsigned char* Translate (Protocols::ProtocolHeader &header );
Protocols::ProtocolHeader* Translate (unsigned char msg[] );
Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char msg[] );
private:
};
}
}

View File

@ -63,6 +63,7 @@ void chat(Client client)
unsigned char msgRecv[255] = "\0";
string msgSend = "";
ProtocolSet* set = new ProtocolSet;
ProtocolHeader* header;
ProtocolTest test;
@ -72,14 +73,14 @@ void chat(Client client)
{
client.Recv(msgRecv);
header = t->Translate(msgRecv);
set = t->Translate(set, msgRecv);
switch(header->packageType)
switch(set->t)
{
case package_type_header:
break;
case package_type_test:
cout <<"Client 2: " <<((ProtocolTest*)header)->textMessage <<endl;
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
break;
}
@ -113,4 +114,5 @@ void chat(Client client)
}
delete set;
}

View File

@ -41,28 +41,32 @@ int main()
cout << "Second client connected." << endl;
client1.Send((unsigned char*)"Hej");
ProtocolHeader* header = NULL;
ProtocolSet* set = new ProtocolSet;
while(1)
{
client1.Recv(recvBuffer);
header = t.Translate(recvBuffer);
cout << header->clientID << ' ' << header->packageType << ' ' << header->size << endl;
cout << "Client1: " << ((ProtocolTest*)header)->textMessage << endl;
set = t.Translate(set, recvBuffer);
cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
cout << "Client1: " << set->Protocol.pTest->textMessage << endl;
set->Release();
client2.Send(recvBuffer);
client2.Recv(recvBuffer);
header = t.Translate(recvBuffer);
cout << header->clientID << ' ' << header->packageType << ' ' << header->size << endl;
cout << "Client1: " << ((ProtocolTest*)header)->textMessage << endl;
set = t.Translate(set, recvBuffer);
cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
cout << "Client2: " << set->Protocol.pTest->textMessage << endl;
set->Release();
client1.Send(recvBuffer);
}
ShutdownSockets();
delete[] recvBuffer;
delete set;
system("pause");
return 0;