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 <winsock2.h>
#include <iostream> #include <iostream>
Connection::Connection() using namespace Oyster::Network;
{
mySocket = NULL;
}
Connection::Connection(int socket)
{
mySocket = socket;
}
Connection::~Connection() Connection::~Connection()
{ {

View File

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

View File

@ -11,13 +11,14 @@ namespace Oyster
{ {
class IConnection class IConnection
{ {
public:
virtual void Disconnect() = 0; public:
virtual bool Send( const char message[] ) = 0; virtual void Disconnect() = 0;
virtual int Recieve(char message[]) = 0; virtual bool Send( const char message[] ) = 0;
virtual bool InitiateServer( unsigned short port ) { return false; }; virtual int Recieve(char message[]) = 0;
virtual int Listen() { return -1; }; virtual bool InitiateServer( unsigned short port ) { return false; };
virtual bool Connect( unsigned short port, const char serverName[] ) { 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> <ItemGroup>
<ClCompile Include="Connection.cpp" /> <ClCompile Include="Connection.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="Translator.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Connection.h" /> <ClInclude Include="Connection.h" />
<ClInclude Include="IConnection.h" /> <ClInclude Include="IConnection.h" />
<ClInclude Include="ITranslate.h" />
<ClInclude Include="Protocols.h" />
<ClInclude Include="Translator.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -21,6 +21,9 @@
<ClCompile Include="Connection.cpp"> <ClCompile Include="Connection.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Translator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="IConnection.h"> <ClInclude Include="IConnection.h">
@ -29,5 +32,14 @@
<ClInclude Include="Connection.h"> <ClInclude Include="Connection.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </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> </ItemGroup>
</Project> </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" #include "Client.h"
using namespace Oyster::Network::Client;
Client::Client() Client::Client()
{ {
connection = new Connection(); connection = new Connection();

View File

@ -7,20 +7,28 @@
#include "../NetworkDependencies/Connection.h" #include "../NetworkDependencies/Connection.h"
class Client namespace Oyster
{ {
public: namespace Network
Client(); {
~Client(); 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 Send(char msg[]);
void Recv(char msg[]); void Recv(char msg[]);
private: private:
Connection* connection; ::Oyster::Network::Connection* connection;
};
}; }
}
}
#endif #endif

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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