Fixed code standard, Added packing functions
This commit is contained in:
parent
ad7091e520
commit
caabb6702e
|
@ -96,8 +96,11 @@ bool Connection::Send(const unsigned char message[])
|
|||
{
|
||||
int nBytes;
|
||||
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);
|
||||
if(nBytes == SOCKET_ERROR)
|
||||
{
|
||||
|
@ -111,7 +114,7 @@ bool Connection::Send(const unsigned char message[])
|
|||
int Connection::Recieve(unsigned char message[])
|
||||
{
|
||||
int nBytes;
|
||||
nBytes = recv(mySocket, (char*)message , 255, 0);
|
||||
nBytes = recv(mySocket, (char*)message , 10000, 0);
|
||||
if(nBytes == SOCKET_ERROR)
|
||||
{
|
||||
//Recv failed
|
||||
|
|
|
@ -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
|
|
@ -9,8 +9,8 @@ namespace Oyster
|
|||
{
|
||||
|
||||
public:
|
||||
virtual unsigned char* Translate (Protocols::ProtocolHeader &header ) = 0;
|
||||
virtual Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char message[] ) = 0;
|
||||
virtual unsigned char* Pack (Protocols::ProtocolHeader &header ) = 0;
|
||||
virtual Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, unsigned char message[] ) = 0;
|
||||
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "Listener.h"
|
||||
|
||||
using namespace Oyster::Network;
|
||||
using namespace Oyster::Network::Server;
|
||||
|
||||
Listener::Listener()
|
||||
{
|
|
@ -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
|
|
@ -2,7 +2,6 @@
|
|||
#include "../Packing.h"
|
||||
|
||||
using namespace Oyster::Network::Messages;
|
||||
using namespace Oyster::Network::Packing;
|
||||
using namespace Oyster::Network::Protocols;
|
||||
|
||||
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;
|
||||
|
||||
AddInt(header.size, msg);
|
||||
AddInt(header.packageType, msg);
|
||||
AddInt(header.clientID, msg);
|
||||
PackInt(header.size, msg);
|
||||
PackInt(header.packageType, msg);
|
||||
PackInt(header.clientID, msg);
|
||||
SetSize(msg);
|
||||
}
|
||||
|
||||
void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header)
|
||||
void MessageHeader::Unpack(unsigned char msg[], ProtocolHeader& header)
|
||||
{
|
||||
size = 0;
|
||||
|
||||
header.clientID = GetInt(message);
|
||||
header.packageType = GetInt(message);
|
||||
header.size = GetInt(message);
|
||||
header.clientID = UnpackInt(msg);
|
||||
header.packageType = UnpackInt(msg);
|
||||
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;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
return i;
|
||||
}
|
||||
|
||||
std::string MessageHeader::GetStr(unsigned char message[])
|
||||
unsigned int MessageHeader::UnpackUnsignedInt(unsigned char msg[])
|
||||
{
|
||||
std::string std = UnpackStr(&message[size]);
|
||||
size += 2 + std.length();
|
||||
return std;
|
||||
unsigned int i = Packing::UnpackI(&msg[size]);
|
||||
size += 4;
|
||||
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[])
|
||||
{
|
||||
Pack(&msg[0], size);
|
||||
Packing::Pack(&msg[0], size);
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
#ifndef NETWORK_DEPENDENCIES_MESSAGE_HEADER_H
|
||||
#define NETWORK_DEPENDENCIES_MESSAGE_HEADER_H
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013 //
|
||||
/////////////////////////////////////
|
||||
|
||||
#include <string>
|
||||
#include "../Protocols.h"
|
||||
|
@ -20,19 +20,60 @@ namespace Oyster
|
|||
MessageHeader();
|
||||
virtual ~MessageHeader();
|
||||
|
||||
virtual void Translate(Protocols::ProtocolHeader& header, unsigned char msg[] );
|
||||
virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header);
|
||||
|
||||
virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[] );
|
||||
virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header);
|
||||
|
||||
protected:
|
||||
//Add variables to messages
|
||||
void AddInt(int i, unsigned char msg[]);
|
||||
void AddStr(std::string str, unsigned char msg[]);
|
||||
//Pack variables to messages
|
||||
void PackBool(bool i, unsigned char msg[]);
|
||||
|
||||
//Get variables from message
|
||||
int GetInt(unsigned char message[]);
|
||||
std::string GetStr(unsigned char message[]);
|
||||
void PackChar(char i, unsigned char msg[]);
|
||||
void PackUnsignedChar(unsigned char i, unsigned char msg[]);
|
||||
|
||||
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[]);
|
||||
|
||||
private:
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
#ifndef NETWORK_DEPENDENCIES_MESSAGE_TEST_H
|
||||
#define NETWORK_DEPENDENCIES_MESSAGE_TEST_H
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013 //
|
||||
/////////////////////////////////////
|
||||
|
||||
#include "MessageHeader.h"
|
||||
|
||||
|
@ -19,11 +19,10 @@ namespace Oyster
|
|||
MessageTest();
|
||||
virtual ~MessageTest();
|
||||
|
||||
virtual void Translate(Protocols::ProtocolHeader& header, unsigned char msg[]);
|
||||
virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header);
|
||||
virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[]);
|
||||
virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header);
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
#ifndef NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H
|
||||
#define NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H
|
||||
|
||||
/////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013 //
|
||||
/////////////////////////////////////
|
||||
|
||||
#include "MessageHeader.h"
|
||||
#include "MessageTest.h"
|
||||
|
||||
|
|
|
@ -160,6 +160,7 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Connection.cpp" />
|
||||
<ClCompile Include="Listener.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="Messages\MessageHeader.cpp" />
|
||||
<ClCompile Include="Messages\MessageTest.cpp" />
|
||||
|
@ -169,6 +170,8 @@
|
|||
<ItemGroup>
|
||||
<ClInclude Include="Connection.h" />
|
||||
<ClInclude Include="IConnection.h" />
|
||||
<ClInclude Include="IListener.h" />
|
||||
<ClInclude Include="Listener.h" />
|
||||
<ClInclude Include="Messages\MessageHeader.h" />
|
||||
<ClInclude Include="Messages\MessagesInclude.h" />
|
||||
<ClInclude Include="Messages\MessageTest.h" />
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
<ClCompile Include="Messages\MessageTest.cpp" />
|
||||
<ClCompile Include="Packing.cpp" />
|
||||
<ClCompile Include="Translator.cpp" />
|
||||
<ClCompile Include="Listener.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Connection.h" />
|
||||
|
@ -18,5 +19,7 @@
|
|||
<ClInclude Include="Protocols.h" />
|
||||
<ClInclude Include="Translator.h" />
|
||||
<ClInclude Include="Messages\MessagesInclude.h" />
|
||||
<ClInclude Include="Listener.h" />
|
||||
<ClInclude Include="IListener.h" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -33,13 +33,13 @@ namespace Oyster
|
|||
void Pack(unsigned char buffer[], short i)
|
||||
{
|
||||
*buffer++ = i >> 8;
|
||||
*buffer++ = i;
|
||||
*buffer++ = (char)i;
|
||||
}
|
||||
|
||||
void Pack(unsigned char buffer[], unsigned short i)
|
||||
{
|
||||
*buffer++ = i >> 8;
|
||||
*buffer++ = i;
|
||||
*buffer++ = (char)i;
|
||||
}
|
||||
|
||||
//int (32-bit)
|
||||
|
@ -62,26 +62,26 @@ namespace Oyster
|
|||
//__int64 (64-bit)
|
||||
void Pack(unsigned char buffer[], __int64 i)
|
||||
{
|
||||
*buffer++ = i >> 56;
|
||||
*buffer++ = i >> 48;
|
||||
*buffer++ = i >> 40;
|
||||
*buffer++ = i >> 32;
|
||||
*buffer++ = i >> 24;
|
||||
*buffer++ = i >> 16;
|
||||
*buffer++ = i >> 8;
|
||||
*buffer++ = i;
|
||||
*buffer++ = (char)(i >> 56);
|
||||
*buffer++ = (char)(i >> 48);
|
||||
*buffer++ = (char)(i >> 40);
|
||||
*buffer++ = (char)(i >> 32);
|
||||
*buffer++ = (char)(i >> 24);
|
||||
*buffer++ = (char)(i >> 16);
|
||||
*buffer++ = (char)(i >> 8);
|
||||
*buffer++ = (char)i;
|
||||
}
|
||||
|
||||
void Pack(unsigned char buffer[], unsigned __int64 i)
|
||||
{
|
||||
*buffer++ = i >> 56;
|
||||
*buffer++ = i >> 48;
|
||||
*buffer++ = i >> 40;
|
||||
*buffer++ = i >> 32;
|
||||
*buffer++ = i >> 24;
|
||||
*buffer++ = i >> 16;
|
||||
*buffer++ = i >> 8;
|
||||
*buffer++ = i;
|
||||
*buffer++ = (char)(i >> 56);
|
||||
*buffer++ = (char)(i >> 48);
|
||||
*buffer++ = (char)(i >> 40);
|
||||
*buffer++ = (char)(i >> 32);
|
||||
*buffer++ = (char)(i >> 24);
|
||||
*buffer++ = (char)(i >> 16);
|
||||
*buffer++ = (char)(i >> 8);
|
||||
*buffer++ = (char)i;
|
||||
}
|
||||
|
||||
//floating point (32, 64-bit)
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#ifndef PACKING_H
|
||||
#define PACKING_H
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013 //
|
||||
/////////////////////////////////////
|
||||
|
||||
#include <string>
|
||||
|
||||
|
|
|
@ -23,19 +23,24 @@ namespace Oyster
|
|||
int packageType;
|
||||
int clientID;
|
||||
|
||||
ProtocolHeader() { this->packageType = package_type_header; }
|
||||
ProtocolHeader() { this->packageType = package_type_header; }
|
||||
virtual ~ProtocolHeader() { }
|
||||
};
|
||||
|
||||
struct ProtocolTest : public ProtocolHeader
|
||||
{
|
||||
std::string textMessage;
|
||||
unsigned int numOfFloats;
|
||||
float *f;
|
||||
|
||||
ProtocolTest() { this->packageType = package_type_test; }
|
||||
virtual ~ProtocolTest() { delete[] f; }
|
||||
};
|
||||
|
||||
class ProtocolSet
|
||||
{
|
||||
public:
|
||||
PackageType t;
|
||||
PackageType type;
|
||||
union
|
||||
{
|
||||
ProtocolHeader* pHeader;
|
||||
|
@ -45,7 +50,7 @@ namespace Oyster
|
|||
|
||||
void Release()
|
||||
{
|
||||
switch(t)
|
||||
switch(type)
|
||||
{
|
||||
case package_type_header:
|
||||
if(Protocol.pHeader)
|
||||
|
|
|
@ -4,7 +4,7 @@ using namespace Oyster::Network;
|
|||
using namespace ::Protocols;
|
||||
using namespace ::Messages;
|
||||
|
||||
unsigned char* Translator::Translate( ProtocolHeader &header )
|
||||
unsigned char* Translator::Pack( ProtocolHeader &header )
|
||||
{
|
||||
MessageHeader *message = NULL;
|
||||
|
||||
|
@ -21,7 +21,7 @@ unsigned char* Translator::Translate( ProtocolHeader &header )
|
|||
|
||||
if(message != NULL)
|
||||
{
|
||||
message->Translate(header, this->msg);
|
||||
message->Pack(header, this->msg);
|
||||
|
||||
delete message;
|
||||
message = NULL;
|
||||
|
@ -30,34 +30,36 @@ unsigned char* Translator::Translate( ProtocolHeader &header )
|
|||
return msg;
|
||||
}
|
||||
|
||||
ProtocolSet* Translator::Translate(ProtocolSet* set, unsigned char msg[] )
|
||||
ProtocolSet* Translator::Unpack(ProtocolSet* set, unsigned char msg[] )
|
||||
{
|
||||
ProtocolHeader *header = new ProtocolHeader();
|
||||
MessageHeader *message = new MessageHeader();
|
||||
|
||||
message->Translate(msg, *header);
|
||||
message->Unpack(msg, *header);
|
||||
delete message;
|
||||
message = NULL;
|
||||
|
||||
//Switch to the correct package.
|
||||
set->t = (PackageType)header->packageType;
|
||||
switch(set->t)
|
||||
set->type = (PackageType)header->packageType;
|
||||
switch(set->type)
|
||||
{
|
||||
case package_type_header:
|
||||
message = new MessageHeader();
|
||||
set->Protocol.pHeader = new ProtocolHeader;
|
||||
message->Translate(msg, *set->Protocol.pHeader);
|
||||
message->Unpack(msg, *set->Protocol.pHeader);
|
||||
break;
|
||||
|
||||
case package_type_test:
|
||||
message = new MessageTest();
|
||||
set->Protocol.pTest = new ProtocolTest;
|
||||
message->Translate(msg, *set->Protocol.pTest);
|
||||
message->Unpack(msg, *set->Protocol.pTest);
|
||||
break;
|
||||
}
|
||||
|
||||
if(message)
|
||||
{
|
||||
delete message;
|
||||
}
|
||||
delete header;
|
||||
|
||||
return set;
|
||||
|
|
|
@ -15,8 +15,8 @@ namespace Oyster
|
|||
Translator () { msg = new unsigned char[256]; };
|
||||
~Translator() { if(msg != NULL) { delete [] this->msg; }};
|
||||
|
||||
unsigned char* Translate (Protocols::ProtocolHeader &header );
|
||||
Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char msg[] );
|
||||
unsigned char* Pack (Protocols::ProtocolHeader &header );
|
||||
Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, unsigned char msg[] );
|
||||
|
||||
private:
|
||||
unsigned char* msg;
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#ifndef NETWORK_CLIENT_CLIENT_H
|
||||
#define NETWORK_CLIENT_CLIENT_H
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013 //
|
||||
/////////////////////////////////////
|
||||
|
||||
#include "../NetworkDependencies/Connection.h"
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ int main()
|
|||
Client client;
|
||||
|
||||
//Connect to server
|
||||
client.Connect(9876, "10.0.0.3");
|
||||
client.Connect(9876, "localhost");
|
||||
|
||||
chat(client);
|
||||
|
||||
|
@ -52,12 +52,19 @@ void chat(Client &client)
|
|||
{
|
||||
Oyster::Network::Translator *t = new Oyster::Network::Translator();
|
||||
|
||||
unsigned char msgRecv[256] = "\0";
|
||||
unsigned char msgRecv[2560] = "\0";
|
||||
string msgSend = "";
|
||||
|
||||
ProtocolSet* set = new ProtocolSet;
|
||||
ProtocolHeader* header;
|
||||
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;
|
||||
|
||||
|
@ -65,20 +72,26 @@ void chat(Client &client)
|
|||
{
|
||||
client.Recv(msgRecv);
|
||||
|
||||
t->Translate(set, msgRecv);
|
||||
t->Unpack(set, msgRecv);
|
||||
|
||||
switch(set->t)
|
||||
switch(set->type)
|
||||
{
|
||||
case package_type_header:
|
||||
break;
|
||||
case package_type_test:
|
||||
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;
|
||||
}
|
||||
|
||||
set->Release();
|
||||
|
||||
std::getline(std::cin, msgSend);
|
||||
|
||||
|
||||
|
||||
if( msgSend != "exit")
|
||||
{
|
||||
|
@ -91,7 +104,7 @@ void chat(Client &client)
|
|||
test.size = msgSend.length();
|
||||
test.textMessage = msgSend;
|
||||
|
||||
unsigned char *message = t->Translate(test);
|
||||
unsigned char *message = t->Pack(test);
|
||||
|
||||
client.Send(message);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#ifndef NETWORK_SERVER_CLIENT_H
|
||||
#define NETWORK_SERVER_CLIENT_H
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////
|
||||
// Created by Pontus Fransson 2013 //
|
||||
/////////////////////////////////////
|
||||
|
||||
#include "../NetworkDependencies/Connection.h"
|
||||
|
||||
|
@ -15,7 +15,6 @@ namespace Oyster
|
|||
{
|
||||
class Client
|
||||
{
|
||||
|
||||
public:
|
||||
Client(unsigned int socket);
|
||||
~Client();
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -158,13 +158,10 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Client.cpp" />
|
||||
<ClCompile Include="Listener.cpp" />
|
||||
<ClCompile Include="ServerMain.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Client.h" />
|
||||
<ClInclude Include="IListener.h" />
|
||||
<ClInclude Include="Listener.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
|
|
@ -21,19 +21,10 @@
|
|||
<ClCompile Include="Client.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Listener.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Client.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Listener.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="IListener.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -1,8 +1,9 @@
|
|||
#include <iostream>
|
||||
#include <WinSock2.h>
|
||||
#include <vld.h>
|
||||
#include "Listener.h"
|
||||
#include "../NetworkDependencies/Listener.h"
|
||||
#include "Client.h"
|
||||
#include "../NetworkDependencies/Packing.h"
|
||||
using namespace std;
|
||||
using namespace Oyster::Network::Server;
|
||||
|
||||
|
@ -17,7 +18,7 @@ using namespace ::Protocols;
|
|||
|
||||
int main()
|
||||
{
|
||||
unsigned char* recvBuffer = new unsigned char[256];
|
||||
unsigned char* recvBuffer = new unsigned char[5000];
|
||||
cout << "Server" << endl;
|
||||
Translator t;
|
||||
|
||||
|
@ -45,7 +46,15 @@ int main()
|
|||
ProtocolTest test;
|
||||
test.clientID = 0;
|
||||
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);
|
||||
|
||||
|
@ -53,17 +62,27 @@ int main()
|
|||
{
|
||||
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 << "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();
|
||||
client2.Send(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 << "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();
|
||||
client1.Send(recvBuffer);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue