Implemented Translator

also fixed some coding standards with namespaces in client, connection,
listener, server.
added protocols with enums for packagetype.
This commit is contained in:
Sam Mario Svensson 2013-11-21 13:40:52 +01:00
parent 5bcc285141
commit f77efb107d
17 changed files with 222 additions and 50 deletions

View File

@ -3,15 +3,7 @@
#include <winsock2.h>
#include <iostream>
Connection::Connection()
{
mySocket = NULL;
}
Connection::Connection(int socket)
{
mySocket = socket;
}
using namespace Oyster::Network;
Connection::~Connection()
{

View File

@ -7,23 +7,30 @@
#include "IConnection.h"
class Connection : public ::Oyster::Network::IConnection
namespace Oyster
{
private:
int mySocket;
namespace Network
{
class Connection : public IConnection
{
public:
Connection();
Connection(int socket);
~Connection();
public:
Connection() { mySocket = 0; };
Connection(int socket) { mySocket = socket; };
~Connection();
virtual bool Connect( unsigned short port , const char serverName[] );
virtual bool InitiateServer( unsigned short port );
virtual void Disconnect();
virtual bool Send(const char message[]);
virtual int Recieve(char message[]);
virtual int Listen();
virtual bool Connect( unsigned short port , const char serverName[] );
virtual bool InitiateServer( unsigned short port );
virtual void Disconnect();
virtual bool Send(const char message[]);
virtual int Recieve(char message[]);
virtual int Listen();
};
private:
int mySocket;
};
}
}
#endif

View File

@ -11,13 +11,14 @@ namespace Oyster
{
class IConnection
{
public:
virtual void Disconnect() = 0;
virtual bool Send( const char message[] ) = 0;
virtual int Recieve(char message[]) = 0;
virtual bool InitiateServer( unsigned short port ) { return false; };
virtual int Listen() { return -1; };
virtual bool Connect( unsigned short port, const char serverName[] ) { return false; };
public:
virtual void Disconnect() = 0;
virtual bool Send( const char message[] ) = 0;
virtual int Recieve(char message[]) = 0;
virtual bool InitiateServer( unsigned short port ) { return false; };
virtual int Listen() { return -1; };
virtual bool Connect( unsigned short port, const char serverName[] ) { return false; };
};
}
}

View File

@ -0,0 +1,18 @@
#ifndef NETWORK_DEPENDENCIES_I_TRANSLATE
#define NETWORK_DEPENDENCIES_I_TRANSLATE
namespace Oyster
{
namespace Network
{
class ITranslate
{
public:
virtual char* Translate (const Protocols::ProtocolHeader &header ) = 0;
virtual Protocols::ProtocolHeader& Translate ( char message[] ) = 0;
};
}
}
#endif

View File

@ -153,10 +153,14 @@
<ItemGroup>
<ClCompile Include="Connection.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Translator.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Connection.h" />
<ClInclude Include="IConnection.h" />
<ClInclude Include="ITranslate.h" />
<ClInclude Include="Protocols.h" />
<ClInclude Include="Translator.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -21,6 +21,9 @@
<ClCompile Include="Connection.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Translator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="IConnection.h">
@ -29,5 +32,14 @@
<ClInclude Include="Connection.h">
<Filter>Header Files</Filter>
</ClInclude>
<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>
</Project>

View File

@ -0,0 +1,35 @@
#include "string";
namespace Oyster
{
namespace Network
{
namespace Protocols
{
enum PackageType
{
package_type_header,
package_type_test,
package_type_input,
package_type_update_position
};
struct ProtocolHeader
{
int size;
int packageType;
int clientID;
ProtocolHeader() { this->packageType = package_type_header; }
};
struct ProtocolTest : public ProtocolHeader
{
std::string textMessage;
ProtocolTest() { this->packageType = package_type_test; }
};
}
}
}

View File

@ -0,0 +1,50 @@
//#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();
//}

View File

@ -0,0 +1,27 @@
//#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

View File

@ -1,5 +1,7 @@
#include "Client.h"
using namespace Oyster::Network::Client;
Client::Client()
{
connection = new Connection();

View File

@ -7,20 +7,28 @@
#include "../NetworkDependencies/Connection.h"
class Client
namespace Oyster
{
public:
Client();
~Client();
namespace Network
{
namespace Client
{
class Client
{
public:
Client();
~Client();
bool Connect(unsigned int port, char filename[]);
bool Connect(unsigned int port, char filename[]);
void Send(char msg[]);
void Recv(char msg[]);
void Send(char msg[]);
void Recv(char msg[]);
private:
Connection* connection;
};
private:
::Oyster::Network::Connection* connection;
};
}
}
}
#endif

View File

@ -2,6 +2,7 @@
#include "Client.h"
#include <WinSock2.h>
using namespace std;
using namespace Oyster::Network::Client;
#pragma comment(lib, "ws2_32.lib")
void ShutdownSockets();

View File

@ -1,5 +1,7 @@
#include "Client.h"
using namespace Oyster::Network::Server;
Client::Client(unsigned int socket)
{
connection = new Connection(socket);

View File

@ -7,19 +7,29 @@
#include "../NetworkDependencies/Connection.h"
class Client
namespace Oyster
{
public:
Client(unsigned int socket);
~Client();
namespace Network
{
namespace Server
{
class Client
{
void Send(char buffer[]);
void Recv(char buffer[]);
public:
Client(unsigned int socket);
~Client();
void Send(char buffer[]);
void Recv(char buffer[]);
private:
Connection* connection;
private:
::Oyster::Network::Connection* connection;
};
}
}
};
#endif

View File

@ -1,5 +1,7 @@
#include "Listener.h"
using namespace Oyster::Network;
Listener::Listener()
{

View File

@ -18,7 +18,7 @@ public:
int Accept();
private:
Connection* connection;
::Oyster::Network::Connection* connection;
};

View File

@ -3,6 +3,7 @@
#include "Listener.h"
#include "Client.h"
using namespace std;
using namespace Oyster::Network::Server;
#pragma comment(lib, "ws2_32.lib")