Fixed implementation of Translator
This commit is contained in:
parent
7c417dd223
commit
dcf456ce6d
|
@ -9,8 +9,8 @@ namespace Oyster
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual char* Translate (const Protocols::ProtocolHeader &header ) = 0;
|
virtual unsigned char* Translate (Protocols::ProtocolHeader &header ) = 0;
|
||||||
virtual Protocols::ProtocolHeader& Translate ( char message[] ) = 0;
|
virtual Protocols::ProtocolHeader& Translate ( unsigned char message[] ) = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using namespace Oyster::Network::Messages;
|
using namespace Oyster::Network::Messages;
|
||||||
using namespace Oyster::Network::Packing;
|
using namespace Oyster::Network::Packing;
|
||||||
|
using namespace Oyster::Network::Protocols;
|
||||||
|
|
||||||
MessageHeader::MessageHeader()
|
MessageHeader::MessageHeader()
|
||||||
{
|
{
|
||||||
|
@ -15,22 +16,22 @@ MessageHeader::~MessageHeader()
|
||||||
delete[] msg;
|
delete[] msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MessageHeader::Translate(/*Message& msg*/)
|
void MessageHeader::Translate(ProtocolHeader& header)
|
||||||
{
|
{
|
||||||
size = 0;
|
size = 0;
|
||||||
|
|
||||||
AddInt(4);
|
AddInt(header.clientID);
|
||||||
AddInt(5);
|
AddInt(header.packageType);
|
||||||
AddInt(6);
|
AddInt(header.size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MessageHeader::Translate(unsigned char message[])
|
void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header)
|
||||||
{
|
{
|
||||||
size = 0;
|
size = 0;
|
||||||
|
|
||||||
int i = GetInt(message);
|
header.clientID = GetInt(message);
|
||||||
int j = GetInt(message);
|
header.packageType = GetInt(message);
|
||||||
int k = GetInt(message);
|
header.size = GetInt(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* MessageHeader::GetMsg()
|
unsigned char* MessageHeader::GetMsg()
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
/////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include "../Protocols.h"
|
||||||
|
|
||||||
namespace Oyster
|
namespace Oyster
|
||||||
{
|
{
|
||||||
|
@ -19,8 +20,8 @@ namespace Oyster
|
||||||
MessageHeader();
|
MessageHeader();
|
||||||
virtual ~MessageHeader();
|
virtual ~MessageHeader();
|
||||||
|
|
||||||
virtual void Translate(/*Message& msg*/);
|
virtual void Translate(Protocols::ProtocolHeader& header);
|
||||||
virtual void Translate(unsigned char message[]);
|
virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header);
|
||||||
|
|
||||||
unsigned char* GetMsg();
|
unsigned char* GetMsg();
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "MessageTest.h"
|
#include "MessageTest.h"
|
||||||
|
|
||||||
using namespace Oyster::Network::Messages;
|
using namespace Oyster::Network::Messages;
|
||||||
|
using namespace Oyster::Network::Protocols;
|
||||||
|
|
||||||
MessageTest::MessageTest()
|
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);
|
||||||
}
|
}
|
|
@ -19,8 +19,8 @@ namespace Oyster
|
||||||
MessageTest();
|
MessageTest();
|
||||||
virtual ~MessageTest();
|
virtual ~MessageTest();
|
||||||
|
|
||||||
virtual void Translate();
|
virtual void Translate(Protocols::ProtocolHeader& header);
|
||||||
virtual void Translate(unsigned char message[]);
|
virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H
|
||||||
|
#define NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H
|
||||||
|
|
||||||
|
#include "MessageHeader.h"
|
||||||
|
#include "MessageTest.h"
|
||||||
|
|
||||||
|
#endif
|
|
@ -162,6 +162,7 @@
|
||||||
<ClInclude Include="Connection.h" />
|
<ClInclude Include="Connection.h" />
|
||||||
<ClInclude Include="IConnection.h" />
|
<ClInclude Include="IConnection.h" />
|
||||||
<ClInclude Include="Messages\MessageHeader.h" />
|
<ClInclude Include="Messages\MessageHeader.h" />
|
||||||
|
<ClInclude Include="Messages\MessagesInclude.h" />
|
||||||
<ClInclude Include="Messages\MessageTest.h" />
|
<ClInclude Include="Messages\MessageTest.h" />
|
||||||
<ClInclude Include="Packing.h" />
|
<ClInclude Include="Packing.h" />
|
||||||
<ClInclude Include="ITranslate.h" />
|
<ClInclude Include="ITranslate.h" />
|
||||||
|
|
|
@ -1,59 +1,22 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Filter Include="Source Files">
|
<ClCompile Include="Connection.cpp" />
|
||||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
<ClCompile Include="main.cpp" />
|
||||||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
<ClCompile Include="Messages\MessageHeader.cpp" />
|
||||||
</Filter>
|
<ClCompile Include="Messages\MessageTest.cpp" />
|
||||||
<Filter Include="Header Files">
|
<ClCompile Include="Packing.cpp" />
|
||||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
<ClCompile Include="Translator.cpp" />
|
||||||
<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>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="main.cpp">
|
<ClInclude Include="Connection.h" />
|
||||||
<Filter>Source Files</Filter>
|
<ClInclude Include="IConnection.h" />
|
||||||
</ClCompile>
|
<ClInclude Include="Messages\MessageHeader.h" />
|
||||||
<ClCompile Include="Connection.cpp">
|
<ClInclude Include="Messages\MessageTest.h" />
|
||||||
<Filter>Source Files</Filter>
|
<ClInclude Include="Packing.h" />
|
||||||
</ClCompile>
|
<ClInclude Include="ITranslate.h" />
|
||||||
<ClCompile Include="Packing.cpp">
|
<ClInclude Include="Protocols.h" />
|
||||||
<Filter>Source Files</Filter>
|
<ClInclude Include="Translator.h" />
|
||||||
</ClCompile>
|
<ClInclude Include="Messages\MessagesInclude.h" />
|
||||||
<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>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -1,4 +1,7 @@
|
||||||
#include "string";
|
#ifndef NETWORK_DEPENDENCIES_PROTOCOLS_H
|
||||||
|
#define NETWORK_DEPENDENCIES_PROTOCOLS_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace Oyster
|
namespace Oyster
|
||||||
{
|
{
|
||||||
|
@ -12,7 +15,6 @@ namespace Oyster
|
||||||
package_type_test,
|
package_type_test,
|
||||||
package_type_input,
|
package_type_input,
|
||||||
package_type_update_position
|
package_type_update_position
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,3 +35,5 @@ namespace Oyster
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,50 +1,52 @@
|
||||||
//#include "Translator.h"
|
#include "Translator.h"
|
||||||
//
|
|
||||||
//using namespace Oyster::Network;
|
using namespace Oyster::Network;
|
||||||
//using namespace ::Protocols;
|
using namespace ::Protocols;
|
||||||
//using namespace ::Messages;
|
using namespace ::Messages;
|
||||||
//
|
|
||||||
//char* Translate ( const ProtocolHeader &header )
|
unsigned char* Translator::Translate( ProtocolHeader &header )
|
||||||
//{
|
{
|
||||||
// MessageHeader *message;
|
MessageHeader *message = NULL;
|
||||||
//
|
|
||||||
// switch(header.packageType)
|
switch(header.packageType)
|
||||||
// {
|
{
|
||||||
//
|
|
||||||
// case package_type_header:
|
case package_type_header:
|
||||||
// message = new MessageHeader();
|
message = new MessageHeader();
|
||||||
// break;
|
break;
|
||||||
//
|
|
||||||
// case package_type_test:
|
case package_type_test:
|
||||||
// message = new MessageTest();
|
message = new MessageTest();
|
||||||
// break;
|
break;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// message->Translate(header);
|
message->Translate(header);
|
||||||
// return message->GetMsg();
|
|
||||||
//}
|
return message->GetMsg();
|
||||||
//
|
}
|
||||||
//ProtocolHeader& Translator::Translate( char msg[] )
|
|
||||||
//{
|
ProtocolHeader& Translator::Translate(unsigned char msg[] )
|
||||||
// ProtocolHeader header;
|
{
|
||||||
// MessageHeader *message = new MessageHeader();
|
ProtocolHeader* header = new ProtocolHeader();
|
||||||
//
|
MessageHeader *message = new MessageHeader();
|
||||||
// header = message->translate(message);
|
|
||||||
//
|
message->Translate(msg, *header);
|
||||||
// switch(header.packageType)
|
|
||||||
// {
|
switch(header->packageType)
|
||||||
//
|
{
|
||||||
// case package_type_header:
|
case package_type_header:
|
||||||
// message = new MessageHeader();
|
message = new MessageHeader();
|
||||||
// break;
|
header = new ProtocolHeader();
|
||||||
//
|
break;
|
||||||
// case package_type_test:
|
|
||||||
// message = new MessageTest();
|
case package_type_test:
|
||||||
// break;
|
message = new MessageTest();
|
||||||
// }
|
header = new ProtocolTest();
|
||||||
//
|
break;
|
||||||
// message->Translate(msg);
|
}
|
||||||
// return message->GetProtocol();
|
|
||||||
//}
|
message->Translate(msg, *header);
|
||||||
|
return *header;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,27 +1,24 @@
|
||||||
//#ifndef NETWORK_DEPENDENCIES_TRANSLATOR_H
|
#ifndef NETWORK_DEPENDENCIES_TRANSLATOR_H
|
||||||
//#define NETWORK_DEPENDENCIES_TRANSLATOR_H
|
#define NETWORK_DEPENDENCIES_TRANSLATOR_H
|
||||||
//
|
|
||||||
////#include "MessageHeader.h"
|
#include "Messages/MessagesInclude.h"
|
||||||
//#include "Protocols.h"
|
#include "Protocols.h"
|
||||||
//#include "ITranslate.h"
|
#include "ITranslate.h"
|
||||||
//
|
|
||||||
//namespace Oyster
|
namespace Oyster
|
||||||
//{
|
{
|
||||||
// namespace Network
|
namespace Network
|
||||||
// {
|
{
|
||||||
// namespace Messages
|
class Translator : public ITranslate
|
||||||
// {
|
{
|
||||||
// class Translator
|
public:
|
||||||
// {
|
Translator (){};
|
||||||
// public:
|
~Translator(){};
|
||||||
// Translator (){};
|
|
||||||
// ~Translator(){};
|
unsigned char* Translate (Protocols::ProtocolHeader &header );
|
||||||
//
|
Protocols::ProtocolHeader& Translate (unsigned char msg[] );
|
||||||
// char* Translate (const Protocols::ProtocolHeader &header );
|
};
|
||||||
// Protocols::ProtocolHeader& Translate ( char msg[] );
|
}
|
||||||
// };
|
}
|
||||||
// }
|
|
||||||
// }
|
#endif
|
||||||
//}
|
|
||||||
//
|
|
||||||
//#endif
|
|
|
@ -10,19 +10,34 @@ using namespace Oyster::Network::Server;
|
||||||
void ShutdownSockets();
|
void ShutdownSockets();
|
||||||
bool InitSockets();
|
bool InitSockets();
|
||||||
|
|
||||||
#include "../NetworkDependencies/Messages/MessageTest.h"
|
#include "../NetworkDependencies/Translator.h"
|
||||||
using namespace Oyster::Network::Messages;
|
using namespace Oyster::Network;
|
||||||
|
using namespace ::Protocols;
|
||||||
|
|
||||||
int main()
|
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;
|
cout << "Server" << endl;
|
||||||
|
|
||||||
unsigned char* recvBuffer = new unsigned char[255];
|
|
||||||
|
|
||||||
MessageTest msg;
|
|
||||||
msg.Translate();
|
|
||||||
recvBuffer = msg.GetMsg();
|
|
||||||
msg.Translate(recvBuffer);
|
|
||||||
|
|
||||||
if(!InitSockets())
|
if(!InitSockets())
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue