Fixed implementation of Translator

This commit is contained in:
Pontus Fransson 2013-11-21 14:49:30 +01:00
parent 7c417dd223
commit dcf456ce6d
12 changed files with 150 additions and 158 deletions

View File

@ -9,8 +9,8 @@ namespace Oyster
{
public:
virtual char* Translate (const Protocols::ProtocolHeader &header ) = 0;
virtual Protocols::ProtocolHeader& Translate ( char message[] ) = 0;
virtual unsigned char* Translate (Protocols::ProtocolHeader &header ) = 0;
virtual Protocols::ProtocolHeader& Translate ( unsigned char message[] ) = 0;
};
}

View File

@ -3,6 +3,7 @@
using namespace Oyster::Network::Messages;
using namespace Oyster::Network::Packing;
using namespace Oyster::Network::Protocols;
MessageHeader::MessageHeader()
{
@ -15,22 +16,22 @@ MessageHeader::~MessageHeader()
delete[] msg;
}
void MessageHeader::Translate(/*Message& msg*/)
void MessageHeader::Translate(ProtocolHeader& header)
{
size = 0;
AddInt(4);
AddInt(5);
AddInt(6);
AddInt(header.clientID);
AddInt(header.packageType);
AddInt(header.size);
}
void MessageHeader::Translate(unsigned char message[])
void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header)
{
size = 0;
int i = GetInt(message);
int j = GetInt(message);
int k = GetInt(message);
header.clientID = GetInt(message);
header.packageType = GetInt(message);
header.size = GetInt(message);
}
unsigned char* MessageHeader::GetMsg()

View File

@ -6,6 +6,7 @@
/////////////////////////////////////////////////////////////////////
#include <string>
#include "../Protocols.h"
namespace Oyster
{
@ -19,8 +20,8 @@ namespace Oyster
MessageHeader();
virtual ~MessageHeader();
virtual void Translate(/*Message& msg*/);
virtual void Translate(unsigned char message[]);
virtual void Translate(Protocols::ProtocolHeader& header);
virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header);
unsigned char* GetMsg();

View File

@ -1,6 +1,7 @@
#include "MessageTest.h"
using namespace Oyster::Network::Messages;
using namespace Oyster::Network::Protocols;
MessageTest::MessageTest()
{
@ -11,16 +12,16 @@ MessageTest::~MessageTest()
{
}
void MessageTest::Translate()
void MessageTest::Translate(ProtocolHeader& header)
{
MessageHeader::Translate();
MessageHeader::Translate(header);
AddStr("Hej hur mår du idag?");
AddStr(static_cast<ProtocolTest*>(&header)->textMessage);
}
void MessageTest::Translate(unsigned char message[])
void MessageTest::Translate(unsigned char message[], ProtocolHeader& header)
{
MessageHeader::Translate(message);
MessageHeader::Translate(message, header);
std::string str = GetStr(message);
static_cast<ProtocolTest*>(&header)->textMessage = GetStr(message);
}

View File

@ -19,8 +19,8 @@ namespace Oyster
MessageTest();
virtual ~MessageTest();
virtual void Translate();
virtual void Translate(unsigned char message[]);
virtual void Translate(Protocols::ProtocolHeader& header);
virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header);
private:

View File

@ -0,0 +1,7 @@
#ifndef NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H
#define NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H
#include "MessageHeader.h"
#include "MessageTest.h"
#endif

View File

@ -162,6 +162,7 @@
<ClInclude Include="Connection.h" />
<ClInclude Include="IConnection.h" />
<ClInclude Include="Messages\MessageHeader.h" />
<ClInclude Include="Messages\MessagesInclude.h" />
<ClInclude Include="Messages\MessageTest.h" />
<ClInclude Include="Packing.h" />
<ClInclude Include="ITranslate.h" />

View File

@ -1,59 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<ClCompile Include="Connection.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Messages\MessageHeader.cpp" />
<ClCompile Include="Messages\MessageTest.cpp" />
<ClCompile Include="Packing.cpp" />
<ClCompile Include="Translator.cpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Connection.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Packing.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Messages\MessageHeader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Messages\MessageTest.cpp">
<ClCompile Include="Translator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="IConnection.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Connection.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Packing.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Messages\MessageTest.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Messages\MessageHeader.h">
<ClInclude Include="Protocols.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ITranslate.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Translator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Connection.h" />
<ClInclude Include="IConnection.h" />
<ClInclude Include="Messages\MessageHeader.h" />
<ClInclude Include="Messages\MessageTest.h" />
<ClInclude Include="Packing.h" />
<ClInclude Include="ITranslate.h" />
<ClInclude Include="Protocols.h" />
<ClInclude Include="Translator.h" />
<ClInclude Include="Messages\MessagesInclude.h" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,7 @@
#include "string";
#ifndef NETWORK_DEPENDENCIES_PROTOCOLS_H
#define NETWORK_DEPENDENCIES_PROTOCOLS_H
#include <string>
namespace Oyster
{
@ -12,7 +15,6 @@ namespace Oyster
package_type_test,
package_type_input,
package_type_update_position
};
@ -33,3 +35,5 @@ namespace Oyster
}
}
}
#endif

View File

@ -1,50 +1,52 @@
//#include "Translator.h"
//
//using namespace Oyster::Network;
//using namespace ::Protocols;
//using namespace ::Messages;
//
//char* Translate ( const ProtocolHeader &header )
//{
// MessageHeader *message;
//
// switch(header.packageType)
// {
//
// case package_type_header:
// message = new MessageHeader();
// break;
//
// case package_type_test:
// message = new MessageTest();
// break;
// }
//
// message->Translate(header);
// return message->GetMsg();
//}
//
//ProtocolHeader& Translator::Translate( char msg[] )
//{
// ProtocolHeader header;
// MessageHeader *message = new MessageHeader();
//
// header = message->translate(message);
//
// switch(header.packageType)
// {
//
// case package_type_header:
// message = new MessageHeader();
// break;
//
// case package_type_test:
// message = new MessageTest();
// break;
// }
//
// message->Translate(msg);
// return message->GetProtocol();
//}
#include "Translator.h"
using namespace Oyster::Network;
using namespace ::Protocols;
using namespace ::Messages;
unsigned char* Translator::Translate( ProtocolHeader &header )
{
MessageHeader *message = NULL;
switch(header.packageType)
{
case package_type_header:
message = new MessageHeader();
break;
case package_type_test:
message = new MessageTest();
break;
}
message->Translate(header);
return message->GetMsg();
}
ProtocolHeader& Translator::Translate(unsigned char msg[] )
{
ProtocolHeader* header = new ProtocolHeader();
MessageHeader *message = new MessageHeader();
message->Translate(msg, *header);
switch(header->packageType)
{
case package_type_header:
message = new MessageHeader();
header = new ProtocolHeader();
break;
case package_type_test:
message = new MessageTest();
header = new ProtocolTest();
break;
}
message->Translate(msg, *header);
return *header;
}

View File

@ -1,27 +1,24 @@
//#ifndef NETWORK_DEPENDENCIES_TRANSLATOR_H
//#define NETWORK_DEPENDENCIES_TRANSLATOR_H
//
////#include "MessageHeader.h"
//#include "Protocols.h"
//#include "ITranslate.h"
//
//namespace Oyster
//{
// namespace Network
// {
// namespace Messages
// {
// class Translator
// {
// public:
// Translator (){};
// ~Translator(){};
//
// char* Translate (const Protocols::ProtocolHeader &header );
// Protocols::ProtocolHeader& Translate ( char msg[] );
// };
// }
// }
//}
//
//#endif
#ifndef NETWORK_DEPENDENCIES_TRANSLATOR_H
#define NETWORK_DEPENDENCIES_TRANSLATOR_H
#include "Messages/MessagesInclude.h"
#include "Protocols.h"
#include "ITranslate.h"
namespace Oyster
{
namespace Network
{
class Translator : public ITranslate
{
public:
Translator (){};
~Translator(){};
unsigned char* Translate (Protocols::ProtocolHeader &header );
Protocols::ProtocolHeader& Translate (unsigned char msg[] );
};
}
}
#endif

View File

@ -10,19 +10,34 @@ using namespace Oyster::Network::Server;
void ShutdownSockets();
bool InitSockets();
#include "../NetworkDependencies/Messages/MessageTest.h"
using namespace Oyster::Network::Messages;
#include "../NetworkDependencies/Translator.h"
using namespace Oyster::Network;
using namespace ::Protocols;
int main()
{
unsigned char* recvBuffer;
Translator t;
ProtocolTest header;
header.clientID = 1;
header.packageType = package_type_test;
header.size = 12;
header.textMessage = "Hej";
recvBuffer = t.Translate(header);
ProtocolHeader& asd = t.Translate(recvBuffer);
switch(asd.packageType)
{
case package_type_test:
break;
}
cout << static_cast<ProtocolTest*>(&asd)->textMessage << endl;
cout << "Server" << endl;
unsigned char* recvBuffer = new unsigned char[255];
MessageTest msg;
msg.Translate();
recvBuffer = msg.GetMsg();
msg.Translate(recvBuffer);
if(!InitSockets())
{