Fixed code standard, Added packing functions

This commit is contained in:
Pontus Fransson 2013-11-25 19:00:33 +01:00
parent ad7091e520
commit caabb6702e
25 changed files with 423 additions and 154 deletions

View File

@ -96,8 +96,11 @@ bool Connection::Send(const unsigned char message[])
{ {
int nBytes; int nBytes;
unsigned long messageSize = strlen((char*)message); unsigned long messageSize = strlen((char*)message);
int optlen = sizeof(int);
int optval = 0;
getsockopt(mySocket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
messageSize = 255; messageSize = 1000;
nBytes = send(mySocket, (char*)message , messageSize, 0); nBytes = send(mySocket, (char*)message , messageSize, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
@ -111,7 +114,7 @@ bool Connection::Send(const unsigned char message[])
int Connection::Recieve(unsigned char message[]) int Connection::Recieve(unsigned char message[])
{ {
int nBytes; int nBytes;
nBytes = recv(mySocket, (char*)message , 255, 0); nBytes = recv(mySocket, (char*)message , 10000, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
//Recv failed //Recv failed

View File

@ -0,0 +1,25 @@
#ifndef NETWORK_SERVER_ILISTENER_H
#define NETWORK_SERVER_ILISTENER_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
namespace Oyster
{
namespace Network
{
namespace Server
{
class IListener
{
public:
virtual bool Init(unsigned int port) = 0;
virtual int Accept() = 0;
};
}
}
}
#endif

View File

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

View File

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

View File

@ -0,0 +1,34 @@
#ifndef NETWORK_SERVER_LISTENER_H
#define NETWORK_SERVER_LISTENER_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "IListener.h"
#include "../NetworkDependencies/Connection.h"
namespace Oyster
{
namespace Network
{
namespace Server
{
class Listener
{
public:
Listener();
~Listener();
bool Init(unsigned int port);
int Accept();
private:
::Oyster::Network::Connection* connection;
};
}
}
}
#endif

View File

@ -2,7 +2,6 @@
#include "../Packing.h" #include "../Packing.h"
using namespace Oyster::Network::Messages; using namespace Oyster::Network::Messages;
using namespace Oyster::Network::Packing;
using namespace Oyster::Network::Protocols; using namespace Oyster::Network::Protocols;
MessageHeader::MessageHeader() MessageHeader::MessageHeader()
@ -14,52 +13,223 @@ MessageHeader::~MessageHeader()
{ {
} }
void MessageHeader::Translate(ProtocolHeader& header, unsigned char msg[] ) void MessageHeader::Pack(ProtocolHeader& header, unsigned char msg[] )
{ {
size = 0; size = 0;
AddInt(header.size, msg); PackInt(header.size, msg);
AddInt(header.packageType, msg); PackInt(header.packageType, msg);
AddInt(header.clientID, msg); PackInt(header.clientID, msg);
SetSize(msg); SetSize(msg);
} }
void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header) void MessageHeader::Unpack(unsigned char msg[], ProtocolHeader& header)
{ {
size = 0; size = 0;
header.clientID = GetInt(message); header.clientID = UnpackInt(msg);
header.packageType = GetInt(message); header.packageType = UnpackInt(msg);
header.size = GetInt(message); header.size = UnpackInt(msg);
} }
void MessageHeader::AddInt(int i, unsigned char msg[]) /**************************
Pack
**************************/
void MessageHeader::PackBool(bool i, unsigned char msg[])
{ {
Pack(&msg[size], i); Packing::Pack(&msg[size], i);
size += 1;
}
void MessageHeader::PackChar(char i, unsigned char msg[])
{
Packing::Pack(&msg[size], i);
size += 1;
}
void MessageHeader::PackUnsignedChar(unsigned char i, unsigned char msg[])
{
Packing::Pack(&msg[size], i);
size += 1;
}
void MessageHeader::PackShort(short i, unsigned char msg[])
{
Packing::Pack(&msg[size], i);
size += 2;
}
void MessageHeader::PackUnsignedShort(unsigned short i, unsigned char msg[])
{
Packing::Pack(&msg[size], i);
size += 2;
}
void MessageHeader::PackInt(int i, unsigned char msg[])
{
Packing::Pack(&msg[size], i);
size += 4; size += 4;
} }
void MessageHeader::AddStr(std::string str, unsigned char msg[]) void MessageHeader::PackUnsignedInt(unsigned int i, unsigned char msg[])
{ {
Pack(&msg[size], str); Packing::Pack(&msg[size], i);
size += 4;
}
void MessageHeader::PackInt64(__int64 i, unsigned char msg[])
{
Packing::Pack(&msg[size], i);
size += 8;
}
void MessageHeader::PackUnsignedInt64(unsigned __int64 i, unsigned char msg[])
{
Packing::Pack(&msg[size], i);
size += 8;
}
void MessageHeader::PackFloat(float i, unsigned char msg[])
{
Packing::Pack(&msg[size], i);
size += 4;
}
void MessageHeader::PackFloat(float i[], unsigned int elementCount, unsigned char msg[])
{
//Pack number of elements
PackUnsignedInt(elementCount, &msg[size]);
//Pack all elements
for(int j = 0; j < elementCount; j++)
{
PackFloat(i[j], &msg[size]);
}
}
void MessageHeader::PackDouble(double i, unsigned char msg[])
{
Packing::Pack(&msg[size], i);
size += 8;
}
void MessageHeader::PackStr(char str[], unsigned char msg[])
{
Packing::Pack(&msg[size], str);
size += 2 + strlen(str);
}
void MessageHeader::PackStr(std::string str, unsigned char msg[])
{
Packing::Pack(&msg[size], str);
size += 2 + str.length(); size += 2 + str.length();
} }
int MessageHeader::GetInt(unsigned char message[]) /**************************
Unpack
**************************/
bool MessageHeader::UnpackBool(unsigned char msg[])
{ {
int i = Unpacki(&message[size]); bool i = Packing::Unpackb(&msg[size]);
size += 1;
return i;
}
char MessageHeader::UnpackChar(unsigned char msg[])
{
char i = Packing::Unpackc(&msg[size]);
size += 1;
return i;
}
unsigned char MessageHeader::UnpackUnsignedChar(unsigned char msg[])
{
unsigned char i = Packing::UnpackC(&msg[size]);
size += 1;
return i;
}
short MessageHeader::UnpackShort(unsigned char msg[])
{
short i = Packing::Unpacks(&msg[size]);
size += 2;
return i;
}
unsigned short MessageHeader::UnpackUnsignedShort(unsigned char msg[])
{
unsigned short i = Packing::UnpackS(&msg[size]);
size += 2;
return i;
}
int MessageHeader::UnpackInt(unsigned char msg[])
{
int i = Packing::Unpacki(&msg[size]);
size += 4; size += 4;
return i; return i;
} }
std::string MessageHeader::GetStr(unsigned char message[]) unsigned int MessageHeader::UnpackUnsignedInt(unsigned char msg[])
{ {
std::string std = UnpackStr(&message[size]); unsigned int i = Packing::UnpackI(&msg[size]);
size += 2 + std.length(); size += 4;
return std; return i;
}
__int64 MessageHeader::UnpackInt64(unsigned char msg[])
{
__int64 i = Packing::Unpacki64(&msg[size]);
size += 8;
return i;
}
unsigned __int64 MessageHeader::UnpackUnsignedInt64(unsigned char msg[])
{
unsigned __int64 i = Packing::UnpackI64(&msg[size]);
size += 8;
return i;
}
float MessageHeader::UnpackFloat(unsigned char msg[])
{
float i = Packing::Unpackf(&msg[size]);
size += 4;
return i;
}
float* MessageHeader::UnpackFloat(unsigned int& elementCount, unsigned char msg[])
{
float* i;
elementCount = UnpackUnsignedInt(&msg[size]);
i = new float[elementCount];
for(int j = 0; j < elementCount; j++)
{
i[j] = UnpackFloat(&msg[size]);
}
return i;
}
double MessageHeader::UnpackDouble(unsigned char msg[])
{
double i = Packing::Unpackd(&msg[size]);
size += 8;
return i;
}
std::string MessageHeader::UnpackStr(unsigned char msg[])
{
std::string str = Packing::UnpackStr(&msg[size]);
size += 2 + str.length();
return str;
} }
void MessageHeader::SetSize(unsigned char msg[]) void MessageHeader::SetSize(unsigned char msg[])
{ {
Pack(&msg[0], size); Packing::Pack(&msg[0], size);
} }

View File

@ -1,9 +1,9 @@
#ifndef NETWORK_DEPENDENCIES_MESSAGE_HEADER_H #ifndef NETWORK_DEPENDENCIES_MESSAGE_HEADER_H
#define NETWORK_DEPENDENCIES_MESSAGE_HEADER_H #define NETWORK_DEPENDENCIES_MESSAGE_HEADER_H
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
// Created by Pontus Fransson 2013 // Created by Pontus Fransson 2013 //
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
#include <string> #include <string>
#include "../Protocols.h" #include "../Protocols.h"
@ -20,19 +20,60 @@ namespace Oyster
MessageHeader(); MessageHeader();
virtual ~MessageHeader(); virtual ~MessageHeader();
virtual void Translate(Protocols::ProtocolHeader& header, unsigned char msg[] ); virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[] );
virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header); virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header);
protected: protected:
//Add variables to messages //Pack variables to messages
void AddInt(int i, unsigned char msg[]); void PackBool(bool i, unsigned char msg[]);
void AddStr(std::string str, unsigned char msg[]);
//Get variables from message void PackChar(char i, unsigned char msg[]);
int GetInt(unsigned char message[]); void PackUnsignedChar(unsigned char i, unsigned char msg[]);
std::string GetStr(unsigned char message[]);
void PackShort(short i, unsigned char msg[]);
void PackUnsignedShort(unsigned short i, unsigned char msg[]);
void PackInt(int i, unsigned char msg[]);
void PackUnsignedInt(unsigned int i, unsigned char msg[]);
void PackInt64(__int64 i, unsigned char msg[]);
void PackUnsignedInt64(unsigned __int64 i, unsigned char msg[]);
void PackFloat(float i, unsigned char msg[]);
void PackFloat(float i[], unsigned int elementCount, unsigned char msg[]);
void PackDouble(double i, unsigned char msg[]);
void PackStr(char str[], unsigned char msg[]);
void PackStr(std::string str, unsigned char msg[]);
//TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc.
//Unpack variables from message
bool UnpackBool(unsigned char msg[]);
char UnpackChar(unsigned char msg[]);
unsigned char UnpackUnsignedChar(unsigned char msg[]);
short UnpackShort(unsigned char msg[]);
unsigned short UnpackUnsignedShort(unsigned char msg[]);
int UnpackInt(unsigned char msg[]);
unsigned int UnpackUnsignedInt(unsigned char msg[]);
__int64 UnpackInt64(unsigned char msg[]);
unsigned __int64 UnpackUnsignedInt64(unsigned char msg[]);
float UnpackFloat(unsigned char msg[]);
float* UnpackFloat(unsigned int& elementCount, unsigned char msg[]);
double UnpackDouble(unsigned char msg[]);
std::string UnpackStr(unsigned char msg[]);
//TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc.
//Sets the this->size to first position in msg
void SetSize(unsigned char msg[]); void SetSize(unsigned char msg[]);
private: private:

View File

@ -11,17 +11,19 @@ MessageTest::~MessageTest()
{ {
} }
void MessageTest::Translate(ProtocolHeader& header, unsigned char msg[]) void MessageTest::Pack(ProtocolHeader& header, unsigned char msg[])
{ {
MessageHeader::Translate(header, msg); MessageHeader::Pack(header, msg);
AddStr(static_cast<ProtocolTest*>(&header)->textMessage, msg); PackStr(static_cast<ProtocolTest*>(&header)->textMessage, msg);
PackFloat(static_cast<ProtocolTest*>(&header)->f, static_cast<ProtocolTest*>(&header)->numOfFloats, msg);
SetSize(msg); SetSize(msg);
} }
void MessageTest::Translate(unsigned char message[], ProtocolHeader& header) void MessageTest::Unpack(unsigned char msg[], ProtocolHeader& header)
{ {
MessageHeader::Translate(message, header); MessageHeader::Unpack(msg, header);
static_cast<ProtocolTest*>(&header)->textMessage = GetStr(message); static_cast<ProtocolTest*>(&header)->textMessage = UnpackStr(msg);
static_cast<ProtocolTest*>(&header)->f = UnpackFloat(static_cast<ProtocolTest*>(&header)->numOfFloats, msg);
} }

View File

@ -1,9 +1,9 @@
#ifndef NETWORK_DEPENDENCIES_MESSAGE_TEST_H #ifndef NETWORK_DEPENDENCIES_MESSAGE_TEST_H
#define NETWORK_DEPENDENCIES_MESSAGE_TEST_H #define NETWORK_DEPENDENCIES_MESSAGE_TEST_H
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
// Created by Pontus Fransson 2013 // Created by Pontus Fransson 2013 //
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
#include "MessageHeader.h" #include "MessageHeader.h"
@ -19,11 +19,10 @@ namespace Oyster
MessageTest(); MessageTest();
virtual ~MessageTest(); virtual ~MessageTest();
virtual void Translate(Protocols::ProtocolHeader& header, unsigned char msg[]); virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[]);
virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header); virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header);
private: private:
}; };
} }
} }

View File

@ -1,6 +1,10 @@
#ifndef NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H #ifndef NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H
#define NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H #define NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "MessageHeader.h" #include "MessageHeader.h"
#include "MessageTest.h" #include "MessageTest.h"

View File

@ -160,6 +160,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Connection.cpp" /> <ClCompile Include="Connection.cpp" />
<ClCompile Include="Listener.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="Messages\MessageHeader.cpp" /> <ClCompile Include="Messages\MessageHeader.cpp" />
<ClCompile Include="Messages\MessageTest.cpp" /> <ClCompile Include="Messages\MessageTest.cpp" />
@ -169,6 +170,8 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="Connection.h" /> <ClInclude Include="Connection.h" />
<ClInclude Include="IConnection.h" /> <ClInclude Include="IConnection.h" />
<ClInclude Include="IListener.h" />
<ClInclude Include="Listener.h" />
<ClInclude Include="Messages\MessageHeader.h" /> <ClInclude Include="Messages\MessageHeader.h" />
<ClInclude Include="Messages\MessagesInclude.h" /> <ClInclude Include="Messages\MessagesInclude.h" />
<ClInclude Include="Messages\MessageTest.h" /> <ClInclude Include="Messages\MessageTest.h" />

View File

@ -7,6 +7,7 @@
<ClCompile Include="Messages\MessageTest.cpp" /> <ClCompile Include="Messages\MessageTest.cpp" />
<ClCompile Include="Packing.cpp" /> <ClCompile Include="Packing.cpp" />
<ClCompile Include="Translator.cpp" /> <ClCompile Include="Translator.cpp" />
<ClCompile Include="Listener.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Connection.h" /> <ClInclude Include="Connection.h" />
@ -18,5 +19,7 @@
<ClInclude Include="Protocols.h" /> <ClInclude Include="Protocols.h" />
<ClInclude Include="Translator.h" /> <ClInclude Include="Translator.h" />
<ClInclude Include="Messages\MessagesInclude.h" /> <ClInclude Include="Messages\MessagesInclude.h" />
<ClInclude Include="Listener.h" />
<ClInclude Include="IListener.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -33,13 +33,13 @@ namespace Oyster
void Pack(unsigned char buffer[], short i) void Pack(unsigned char buffer[], short i)
{ {
*buffer++ = i >> 8; *buffer++ = i >> 8;
*buffer++ = i; *buffer++ = (char)i;
} }
void Pack(unsigned char buffer[], unsigned short i) void Pack(unsigned char buffer[], unsigned short i)
{ {
*buffer++ = i >> 8; *buffer++ = i >> 8;
*buffer++ = i; *buffer++ = (char)i;
} }
//int (32-bit) //int (32-bit)
@ -62,26 +62,26 @@ namespace Oyster
//__int64 (64-bit) //__int64 (64-bit)
void Pack(unsigned char buffer[], __int64 i) void Pack(unsigned char buffer[], __int64 i)
{ {
*buffer++ = i >> 56; *buffer++ = (char)(i >> 56);
*buffer++ = i >> 48; *buffer++ = (char)(i >> 48);
*buffer++ = i >> 40; *buffer++ = (char)(i >> 40);
*buffer++ = i >> 32; *buffer++ = (char)(i >> 32);
*buffer++ = i >> 24; *buffer++ = (char)(i >> 24);
*buffer++ = i >> 16; *buffer++ = (char)(i >> 16);
*buffer++ = i >> 8; *buffer++ = (char)(i >> 8);
*buffer++ = i; *buffer++ = (char)i;
} }
void Pack(unsigned char buffer[], unsigned __int64 i) void Pack(unsigned char buffer[], unsigned __int64 i)
{ {
*buffer++ = i >> 56; *buffer++ = (char)(i >> 56);
*buffer++ = i >> 48; *buffer++ = (char)(i >> 48);
*buffer++ = i >> 40; *buffer++ = (char)(i >> 40);
*buffer++ = i >> 32; *buffer++ = (char)(i >> 32);
*buffer++ = i >> 24; *buffer++ = (char)(i >> 24);
*buffer++ = i >> 16; *buffer++ = (char)(i >> 16);
*buffer++ = i >> 8; *buffer++ = (char)(i >> 8);
*buffer++ = i; *buffer++ = (char)i;
} }
//floating point (32, 64-bit) //floating point (32, 64-bit)

View File

@ -1,9 +1,9 @@
#ifndef PACKING_H #ifndef PACKING_H
#define PACKING_H #define PACKING_H
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
// Created by Pontus Fransson 2013 // Created by Pontus Fransson 2013 //
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
#include <string> #include <string>

View File

@ -24,18 +24,23 @@ namespace Oyster
int clientID; int clientID;
ProtocolHeader() { this->packageType = package_type_header; } ProtocolHeader() { this->packageType = package_type_header; }
virtual ~ProtocolHeader() { }
}; };
struct ProtocolTest : public ProtocolHeader struct ProtocolTest : public ProtocolHeader
{ {
std::string textMessage; std::string textMessage;
unsigned int numOfFloats;
float *f;
ProtocolTest() { this->packageType = package_type_test; } ProtocolTest() { this->packageType = package_type_test; }
virtual ~ProtocolTest() { delete[] f; }
}; };
class ProtocolSet class ProtocolSet
{ {
public: public:
PackageType t; PackageType type;
union union
{ {
ProtocolHeader* pHeader; ProtocolHeader* pHeader;
@ -45,7 +50,7 @@ namespace Oyster
void Release() void Release()
{ {
switch(t) switch(type)
{ {
case package_type_header: case package_type_header:
if(Protocol.pHeader) if(Protocol.pHeader)

View File

@ -4,7 +4,7 @@ using namespace Oyster::Network;
using namespace ::Protocols; using namespace ::Protocols;
using namespace ::Messages; using namespace ::Messages;
unsigned char* Translator::Translate( ProtocolHeader &header ) unsigned char* Translator::Pack( ProtocolHeader &header )
{ {
MessageHeader *message = NULL; MessageHeader *message = NULL;
@ -21,7 +21,7 @@ unsigned char* Translator::Translate( ProtocolHeader &header )
if(message != NULL) if(message != NULL)
{ {
message->Translate(header, this->msg); message->Pack(header, this->msg);
delete message; delete message;
message = NULL; message = NULL;
@ -30,34 +30,36 @@ unsigned char* Translator::Translate( ProtocolHeader &header )
return msg; return msg;
} }
ProtocolSet* Translator::Translate(ProtocolSet* set, unsigned char msg[] ) ProtocolSet* Translator::Unpack(ProtocolSet* set, unsigned char msg[] )
{ {
ProtocolHeader *header = new ProtocolHeader(); ProtocolHeader *header = new ProtocolHeader();
MessageHeader *message = new MessageHeader(); MessageHeader *message = new MessageHeader();
message->Translate(msg, *header); message->Unpack(msg, *header);
delete message; delete message;
message = NULL; message = NULL;
//Switch to the correct package. //Switch to the correct package.
set->t = (PackageType)header->packageType; set->type = (PackageType)header->packageType;
switch(set->t) switch(set->type)
{ {
case package_type_header: case package_type_header:
message = new MessageHeader(); message = new MessageHeader();
set->Protocol.pHeader = new ProtocolHeader; set->Protocol.pHeader = new ProtocolHeader;
message->Translate(msg, *set->Protocol.pHeader); message->Unpack(msg, *set->Protocol.pHeader);
break; break;
case package_type_test: case package_type_test:
message = new MessageTest(); message = new MessageTest();
set->Protocol.pTest = new ProtocolTest; set->Protocol.pTest = new ProtocolTest;
message->Translate(msg, *set->Protocol.pTest); message->Unpack(msg, *set->Protocol.pTest);
break; break;
} }
if(message) if(message)
{
delete message; delete message;
}
delete header; delete header;
return set; return set;

View File

@ -15,8 +15,8 @@ namespace Oyster
Translator () { msg = new unsigned char[256]; }; Translator () { msg = new unsigned char[256]; };
~Translator() { if(msg != NULL) { delete [] this->msg; }}; ~Translator() { if(msg != NULL) { delete [] this->msg; }};
unsigned char* Translate (Protocols::ProtocolHeader &header ); unsigned char* Pack (Protocols::ProtocolHeader &header );
Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char msg[] ); Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, unsigned char msg[] );
private: private:
unsigned char* msg; unsigned char* msg;

View File

@ -1,9 +1,9 @@
#ifndef NETWORK_CLIENT_CLIENT_H #ifndef NETWORK_CLIENT_CLIENT_H
#define NETWORK_CLIENT_CLIENT_H #define NETWORK_CLIENT_CLIENT_H
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
// Created by Pontus Fransson 2013 // Created by Pontus Fransson 2013 //
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
#include "../NetworkDependencies/Connection.h" #include "../NetworkDependencies/Connection.h"

View File

@ -27,7 +27,7 @@ int main()
Client client; Client client;
//Connect to server //Connect to server
client.Connect(9876, "10.0.0.3"); client.Connect(9876, "localhost");
chat(client); chat(client);
@ -52,12 +52,19 @@ void chat(Client &client)
{ {
Oyster::Network::Translator *t = new Oyster::Network::Translator(); Oyster::Network::Translator *t = new Oyster::Network::Translator();
unsigned char msgRecv[256] = "\0"; unsigned char msgRecv[2560] = "\0";
string msgSend = ""; string msgSend = "";
ProtocolSet* set = new ProtocolSet; ProtocolSet* set = new ProtocolSet;
ProtocolHeader* header;
ProtocolTest test; ProtocolTest test;
test.numOfFloats = 5;
test.f = new float[test.numOfFloats];
float temp = 12345.5654f;
for(int i = 0; i < 5; i++)
{
test.f[i] = temp;
temp++;
}
bool chatDone = false; bool chatDone = false;
@ -65,14 +72,18 @@ void chat(Client &client)
{ {
client.Recv(msgRecv); client.Recv(msgRecv);
t->Translate(set, msgRecv); t->Unpack(set, msgRecv);
switch(set->t) switch(set->type)
{ {
case package_type_header: case package_type_header:
break; break;
case package_type_test: case package_type_test:
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl; cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ' ;
}
break; break;
} }
@ -80,6 +91,8 @@ void chat(Client &client)
std::getline(std::cin, msgSend); std::getline(std::cin, msgSend);
if( msgSend != "exit") if( msgSend != "exit")
{ {
if(msgSend.length() < 1) if(msgSend.length() < 1)
@ -91,7 +104,7 @@ void chat(Client &client)
test.size = msgSend.length(); test.size = msgSend.length();
test.textMessage = msgSend; test.textMessage = msgSend;
unsigned char *message = t->Translate(test); unsigned char *message = t->Pack(test);
client.Send(message); client.Send(message);
} }

View File

@ -1,9 +1,9 @@
#ifndef NETWORK_SERVER_CLIENT_H #ifndef NETWORK_SERVER_CLIENT_H
#define NETWORK_SERVER_CLIENT_H #define NETWORK_SERVER_CLIENT_H
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
// Created by Pontus Fransson 2013 // Created by Pontus Fransson 2013 //
///////////////////////////////////////////////////////////////////// /////////////////////////////////////
#include "../NetworkDependencies/Connection.h" #include "../NetworkDependencies/Connection.h"
@ -15,7 +15,6 @@ namespace Oyster
{ {
class Client class Client
{ {
public: public:
Client(unsigned int socket); Client(unsigned int socket);
~Client(); ~Client();

View File

@ -1,16 +0,0 @@
#ifndef NETWORK_SERVER_ILISTENER_H
#define NETWORK_SERVER_ILISTENER_H
/////////////////////////////////////////////////////////////////////
// Created by Pontus Fransson 2013
/////////////////////////////////////////////////////////////////////
class IListener
{
public:
virtual bool Init(unsigned int port) = 0;
virtual int Accept() = 0;
};
#endif

View File

@ -1,25 +0,0 @@
#ifndef NETWORK_SERVER_LISTENER_H
#define NETWORK_SERVER_LISTENER_H
/////////////////////////////////////////////////////////////////////
// Created by Pontus Fransson 2013
/////////////////////////////////////////////////////////////////////
#include "IListener.h"
#include "../NetworkDependencies/Connection.h"
class Listener
{
public:
Listener();
~Listener();
bool Init(unsigned int port);
int Accept();
private:
::Oyster::Network::Connection* connection;
};
#endif

View File

@ -158,13 +158,10 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Client.cpp" /> <ClCompile Include="Client.cpp" />
<ClCompile Include="Listener.cpp" />
<ClCompile Include="ServerMain.cpp" /> <ClCompile Include="ServerMain.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Client.h" /> <ClInclude Include="Client.h" />
<ClInclude Include="IListener.h" />
<ClInclude Include="Listener.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -21,19 +21,10 @@
<ClCompile Include="Client.cpp"> <ClCompile Include="Client.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Listener.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Client.h"> <ClInclude Include="Client.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Listener.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IListener.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,8 +1,9 @@
#include <iostream> #include <iostream>
#include <WinSock2.h> #include <WinSock2.h>
#include <vld.h> #include <vld.h>
#include "Listener.h" #include "../NetworkDependencies/Listener.h"
#include "Client.h" #include "Client.h"
#include "../NetworkDependencies/Packing.h"
using namespace std; using namespace std;
using namespace Oyster::Network::Server; using namespace Oyster::Network::Server;
@ -17,7 +18,7 @@ using namespace ::Protocols;
int main() int main()
{ {
unsigned char* recvBuffer = new unsigned char[256]; unsigned char* recvBuffer = new unsigned char[5000];
cout << "Server" << endl; cout << "Server" << endl;
Translator t; Translator t;
@ -45,7 +46,15 @@ int main()
ProtocolTest test; ProtocolTest test;
test.clientID = 0; test.clientID = 0;
test.textMessage = "hej"; test.textMessage = "hej";
recvBuffer = t.Translate(test); test.numOfFloats = 500;
test.f = new float[test.numOfFloats];
float temp = 500.456f;
for(int i = 0; i < test.numOfFloats; i++)
{
test.f[i] = temp;
temp--;
}
recvBuffer = t.Pack(test);
client1.Send(recvBuffer); client1.Send(recvBuffer);
@ -53,17 +62,27 @@ int main()
{ {
client1.Recv(recvBuffer); client1.Recv(recvBuffer);
t.Translate(set, recvBuffer); t.Unpack(set, recvBuffer);
cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl; cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
cout << "Client1: " << set->Protocol.pTest->textMessage << endl; cout << "Client1: " << set->Protocol.pTest->textMessage << endl;
for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ';
}
cout << endl;
set->Release(); set->Release();
client2.Send(recvBuffer); client2.Send(recvBuffer);
client2.Recv(recvBuffer); client2.Recv(recvBuffer);
t.Translate(set, recvBuffer); t.Unpack(set, recvBuffer);
cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl; cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
cout << "Client2: " << set->Protocol.pTest->textMessage << endl; cout << "Client2: " << set->Protocol.pTest->textMessage << endl;
for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ';
}
cout << endl;
set->Release(); set->Release();
client1.Send(recvBuffer); client1.Send(recvBuffer);
} }