OysterByte

Added Byte class instead of handling byte messages manually.
Converted all functions to use OysterByte.
This commit is contained in:
Pontus Fransson 2013-11-27 11:01:22 +01:00
parent 45c145439a
commit 214e29e906
20 changed files with 352 additions and 171 deletions

View File

@ -81,13 +81,11 @@ void Connection::Disconnect()
closesocket(this->socket); closesocket(this->socket);
} }
bool Connection::Send(const unsigned char message[]) bool Connection::Send(OysterByte& bytes)
{ {
int nBytes; int nBytes;
unsigned long messageSize = strlen((char*)message);
messageSize = 1600; nBytes = send(this->socket, bytes, bytes.GetSize(), 0);
nBytes = send(this->socket, (char*)message , messageSize, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
//Send failed! //Send failed!
@ -99,29 +97,31 @@ bool Connection::Send(const unsigned char message[])
return true; return true;
} }
int Connection::Recieve(unsigned char message[]) int Connection::Recieve(OysterByte& bytes)
{ {
int nBytes; int nBytes;
nBytes = recv(this->socket, (char*)message, 1600, 0); bytes.Clear(1000);
nBytes = recv(this->socket, bytes, 500, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
//Recv failed //Recv failed
return -1; return -1;
} }
else
{
bytes.SetSize(nBytes);
}
message[nBytes] = '\0'; std::cout << "Size of the recieved data: " << nBytes << " bytes" << std::endl;
//bytes.byteArray[nBytes] = '\0';
return 1; return 1;
} }
int Connection::Listen() int Connection::Listen()
{ {
int optlen = sizeof(int);
int optval;
int Return = getsockopt(this->socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen);
std::cout << optval << std::endl;
int clientSocket; int clientSocket;
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET) if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
{ {

View File

@ -6,6 +6,7 @@
////////////////////////////////// //////////////////////////////////
#include "IConnection.h" #include "IConnection.h"
#include "../NetworkDependencies/OysterByte.h"
namespace Oyster namespace Oyster
{ {
@ -22,8 +23,8 @@ namespace Oyster
virtual bool InitiateServer( unsigned short port ); virtual bool InitiateServer( unsigned short port );
virtual bool InitiateClient(); virtual bool InitiateClient();
virtual bool Send(const unsigned char message[]); virtual bool Send( OysterByte& bytes );
virtual int Recieve(unsigned char message[]); virtual int Recieve( OysterByte& bytes );
virtual void Disconnect(); virtual void Disconnect();
virtual bool Connect( unsigned short port , const char serverName[] ); virtual bool Connect( unsigned short port , const char serverName[] );

View File

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

View File

@ -9,12 +9,13 @@ namespace Oyster
{ {
namespace Network namespace Network
{ {
class OysterByte;
class ITranslate class ITranslate
{ {
public: public:
virtual unsigned char* Pack (Protocols::ProtocolHeader &header ) = 0; virtual void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes) = 0;
virtual Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, unsigned char message[] ) = 0; virtual Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ) = 0;
}; };
} }

View File

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

View File

@ -7,6 +7,7 @@
#include <string> #include <string>
#include "../Protocols.h" #include "../Protocols.h"
#include "../OysterByte.h"
namespace Oyster namespace Oyster
{ {
@ -20,64 +21,64 @@ namespace Oyster
MessageHeader(); MessageHeader();
virtual ~MessageHeader(); virtual ~MessageHeader();
virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[] ); virtual void Pack(Protocols::ProtocolHeader& header, OysterByte& bytes );
virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header); virtual void Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header);
protected: protected:
//Pack variables to messages //Pack variables to messages
void PackBool(bool i, unsigned char msg[]); void PackBool(bool i, OysterByte& bytes);
void PackChar(char i, unsigned char msg[]); void PackChar(char i, OysterByte& bytes);
void PackUnsignedChar(unsigned char i, unsigned char msg[]); void PackUnsignedChar(unsigned char i, OysterByte& bytes);
void PackShort(short i, unsigned char msg[]); void PackShort(short i, OysterByte& bytes);
void PackUnsignedShort(unsigned short i, unsigned char msg[]); void PackUnsignedShort(unsigned short i, OysterByte& bytes);
void PackInt(int i, unsigned char msg[]); void PackInt(int i, OysterByte& bytes);
void PackUnsignedInt(unsigned int i, unsigned char msg[]); void PackUnsignedInt(unsigned int i, OysterByte& bytes);
void PackInt64(__int64 i, unsigned char msg[]); void PackInt64(__int64 i, OysterByte& bytes);
void PackUnsignedInt64(unsigned __int64 i, unsigned char msg[]); void PackUnsignedInt64(unsigned __int64 i, OysterByte& bytes);
void PackFloat(float i, unsigned char msg[]); void PackFloat(float i, OysterByte& bytes);
void PackFloat(float i[], unsigned int elementCount, unsigned char msg[]); void PackFloat(float i[], unsigned int elementCount, OysterByte& bytes);
void PackDouble(double i, unsigned char msg[]); void PackDouble(double i, OysterByte& bytes);
void PackStr(char str[], unsigned char msg[]); void PackStr(char str[], OysterByte& bytes);
void PackStr(std::string str, unsigned char msg[]); void PackStr(std::string str, OysterByte& bytes);
//TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc. //TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc.
//Unpack variables from message //Unpack variables from message
bool UnpackBool(unsigned char msg[]); bool UnpackBool(OysterByte& bytes);
char UnpackChar(unsigned char msg[]); char UnpackChar(OysterByte& bytes);
unsigned char UnpackUnsignedChar(unsigned char msg[]); unsigned char UnpackUnsignedChar(OysterByte& bytes);
short UnpackShort(unsigned char msg[]); short UnpackShort(OysterByte& bytes);
unsigned short UnpackUnsignedShort(unsigned char msg[]); unsigned short UnpackUnsignedShort(OysterByte& bytes);
int UnpackInt(unsigned char msg[]); int UnpackInt(OysterByte& bytes);
unsigned int UnpackUnsignedInt(unsigned char msg[]); unsigned int UnpackUnsignedInt(OysterByte& bytes);
__int64 UnpackInt64(unsigned char msg[]); __int64 UnpackInt64(OysterByte& bytes);
unsigned __int64 UnpackUnsignedInt64(unsigned char msg[]); unsigned __int64 UnpackUnsignedInt64(OysterByte& bytes);
float UnpackFloat(unsigned char msg[]); float UnpackFloat(OysterByte& bytes);
float* UnpackFloat(unsigned int& elementCount, unsigned char msg[]); float* UnpackFloat(unsigned int& elementCount, OysterByte& bytes);
double UnpackDouble(unsigned char msg[]); double UnpackDouble(OysterByte& bytes);
std::string UnpackStr(unsigned char msg[]); std::string UnpackStr(OysterByte& bytes);
//TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc. //TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc.
//Sets the this->size to first position in msg //Sets the this->size to the first position in msg
void SetSize(unsigned char msg[]); void SetSize(OysterByte& bytes);
private: private:
int size; unsigned int size;
}; };
} }

View File

@ -1,5 +1,6 @@
#include "MessageTest.h" #include "MessageTest.h"
using namespace Oyster::Network;
using namespace Oyster::Network::Messages; using namespace Oyster::Network::Messages;
using namespace Oyster::Network::Protocols; using namespace Oyster::Network::Protocols;
@ -11,19 +12,21 @@ MessageTest::~MessageTest()
{ {
} }
void MessageTest::Pack(ProtocolHeader& header, unsigned char msg[]) void MessageTest::Pack(ProtocolHeader& header, OysterByte& bytes)
{ {
MessageHeader::Pack(header, msg); MessageHeader::Pack(header, bytes);
unsigned char asd[1000];
//strcpy_s(asd, bytes.GetSize(), bytes);
PackStr(static_cast<ProtocolTest*>(&header)->textMessage, msg); PackStr(static_cast<ProtocolTest*>(&header)->textMessage, bytes);
PackFloat(static_cast<ProtocolTest*>(&header)->f, static_cast<ProtocolTest*>(&header)->numOfFloats, msg); PackFloat(static_cast<ProtocolTest*>(&header)->f, static_cast<ProtocolTest*>(&header)->numOfFloats, bytes);
SetSize(msg); SetSize(bytes);
} }
void MessageTest::Unpack(unsigned char msg[], ProtocolHeader& header) void MessageTest::Unpack(OysterByte& bytes, ProtocolHeader& header)
{ {
MessageHeader::Unpack(msg, header); MessageHeader::Unpack(bytes, header);
static_cast<ProtocolTest*>(&header)->textMessage = UnpackStr(msg); static_cast<ProtocolTest*>(&header)->textMessage = UnpackStr(bytes);
static_cast<ProtocolTest*>(&header)->f = UnpackFloat(static_cast<ProtocolTest*>(&header)->numOfFloats, msg); static_cast<ProtocolTest*>(&header)->f = UnpackFloat(static_cast<ProtocolTest*>(&header)->numOfFloats, bytes);
} }

View File

@ -19,8 +19,8 @@ namespace Oyster
MessageTest(); MessageTest();
virtual ~MessageTest(); virtual ~MessageTest();
virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[]); virtual void Pack(Protocols::ProtocolHeader& header, OysterByte& bytes);
virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header); virtual void Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header);
private: private:
}; };

View File

@ -164,6 +164,7 @@
<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" />
<ClCompile Include="OysterByte.cpp" />
<ClCompile Include="Packing.cpp" /> <ClCompile Include="Packing.cpp" />
<ClCompile Include="Translator.cpp" /> <ClCompile Include="Translator.cpp" />
<ClCompile Include="WinsockFunctions.cpp" /> <ClCompile Include="WinsockFunctions.cpp" />
@ -176,6 +177,7 @@
<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" />
<ClInclude Include="OysterByte.h" />
<ClInclude Include="Packing.h" /> <ClInclude Include="Packing.h" />
<ClInclude Include="ITranslate.h" /> <ClInclude Include="ITranslate.h" />
<ClInclude Include="Protocols.h" /> <ClInclude Include="Protocols.h" />

View File

@ -9,6 +9,7 @@
<ClCompile Include="Translator.cpp" /> <ClCompile Include="Translator.cpp" />
<ClCompile Include="Listener.cpp" /> <ClCompile Include="Listener.cpp" />
<ClCompile Include="WinsockFunctions.cpp" /> <ClCompile Include="WinsockFunctions.cpp" />
<ClCompile Include="OysterByte.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Connection.h" /> <ClInclude Include="Connection.h" />
@ -23,5 +24,6 @@
<ClInclude Include="Listener.h" /> <ClInclude Include="Listener.h" />
<ClInclude Include="IListener.h" /> <ClInclude Include="IListener.h" />
<ClInclude Include="WinsockFunctions.h" /> <ClInclude Include="WinsockFunctions.h" />
<ClInclude Include="OysterByte.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,94 @@
#include "OysterByte.h"
using namespace Oyster::Network;
OysterByte::OysterByte()
{
size = 0;
capacity = 10;
byteArray = new unsigned char[capacity];
}
OysterByte::OysterByte(int cap)
{
size = 0;
capacity = cap;
byteArray = new unsigned char[capacity];
}
OysterByte::~OysterByte()
{
delete[] byteArray;
}
void OysterByte::Clear(unsigned int cap)
{
delete[] byteArray;
byteArray = new unsigned char[cap];
size = 0;
}
int OysterByte::GetSize()
{
return size;
}
unsigned char* OysterByte::GetByteArray()
{
return byteArray;
}
void OysterByte::AddSize(unsigned int size)
{
int oldSize = this->size;
this->size += size;
if(this->size >= capacity)
{
IncreaseCapacity(oldSize);
}
}
void OysterByte::SetBytes(unsigned char* bytes)
{
delete[] byteArray;
byteArray = bytes;
}
void OysterByte::SetSize(unsigned int size)
{
this->size = size;
}
OysterByte::operator char*()
{
return (char*)byteArray;
}
OysterByte::operator const char*()
{
return (const char*)byteArray;
}
OysterByte::operator unsigned char*()
{
return byteArray;
}
/////////////
// Private //
/////////////
void OysterByte::IncreaseCapacity(unsigned int oldSize)
{
capacity = size * 2;
unsigned char* temp = new unsigned char[capacity];
for(int i = 0; i < oldSize; i++)
{
temp[i] = byteArray[i];
}
delete[] byteArray;
byteArray = temp;
}

View File

@ -0,0 +1,49 @@
#ifndef NETWORK_DEPENDENCIES_OYSTER_BYTE_H
#define NETWORK_DEPENDENCIES_OYSTER_BYTE_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include <string.h>
namespace Oyster
{
namespace Network
{
class OysterByte
{
public:
OysterByte();
OysterByte(int cap);
virtual ~OysterByte();
void Clear(unsigned int cap);
int GetSize();
unsigned char* GetByteArray();
void AddSize(unsigned int size);
void SetBytes(unsigned char* bytes);
void SetSize(unsigned int size); //Only sets the private variable 'size'
operator char*();
operator const char*();
operator unsigned char*();
//unsigned char& operator[](unsigned int id);
private:
void IncreaseCapacity(unsigned int cap); //Expands the byteArray
private:
unsigned char* byteArray;
unsigned int size;
unsigned int capacity;
};
}
}
#endif

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::Pack( ProtocolHeader &header ) void Translator::Pack( ProtocolHeader &header, OysterByte& bytes )
{ {
MessageHeader *message = NULL; MessageHeader *message = NULL;
@ -21,21 +21,19 @@ unsigned char* Translator::Pack( ProtocolHeader &header )
if(message != NULL) if(message != NULL)
{ {
message->Pack(header, this->msg); message->Pack(header, bytes);
delete message; delete message;
message = NULL; message = NULL;
} }
return msg;
} }
ProtocolSet* Translator::Unpack(ProtocolSet* set, unsigned char msg[] ) ProtocolSet* Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
{ {
ProtocolHeader *header = new ProtocolHeader(); ProtocolHeader *header = new ProtocolHeader();
MessageHeader *message = new MessageHeader(); MessageHeader *message = new MessageHeader();
message->Unpack(msg, *header); message->Unpack(bytes, *header);
delete message; delete message;
message = NULL; message = NULL;
@ -46,13 +44,13 @@ ProtocolSet* Translator::Unpack(ProtocolSet* set, unsigned char msg[] )
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->Unpack(msg, *set->Protocol.pHeader); message->Unpack(bytes, *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->Unpack(msg, *set->Protocol.pTest); message->Unpack(bytes, *set->Protocol.pTest);
break; break;
} }

View File

@ -8,6 +8,7 @@
#include "Messages/MessagesInclude.h" #include "Messages/MessagesInclude.h"
#include "Protocols.h" #include "Protocols.h"
#include "ITranslate.h" #include "ITranslate.h"
#include "OysterByte.h"
namespace Oyster namespace Oyster
{ {
@ -16,14 +17,15 @@ namespace Oyster
class Translator : public ITranslate class Translator : public ITranslate
{ {
public: public:
Translator () { msg = new unsigned char[1601]; }; Translator () { /*msg = new unsigned char[1601];*/ };
~Translator() { if(msg != NULL) { delete [] this->msg; }}; ~Translator() { /*if(msg != NULL) { delete [] this->msg; }*/ };
unsigned char* Pack (Protocols::ProtocolHeader &header ); void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes );
Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, unsigned char msg[] ); Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, OysterByte& bytes );
private: private:
unsigned char* msg; //unsigned char* msg;
//OysterByte bytes;
}; };
} }

View File

@ -21,12 +21,12 @@ bool Client::Connect(unsigned int port, char filename[])
return true; return true;
} }
void Client::Send(unsigned char msg[]) void Client::Send(Oyster::Network::OysterByte& bytes)
{ {
connection->Send(msg); connection->Send(bytes);
} }
void Client::Recv(unsigned char msg[]) void Client::Recv(Oyster::Network::OysterByte& bytes)
{ {
connection->Recieve(msg); connection->Recieve(bytes);
} }

View File

@ -6,6 +6,7 @@
///////////////////////////////////// /////////////////////////////////////
#include "../NetworkDependencies/Connection.h" #include "../NetworkDependencies/Connection.h"
#include "../NetworkDependencies/OysterByte.h"
namespace Oyster namespace Oyster
{ {
@ -21,8 +22,8 @@ namespace Oyster
bool Connect(unsigned int port, char filename[]); bool Connect(unsigned int port, char filename[]);
void Send(unsigned char msg[]); void Send(OysterByte& bytes);
void Recv(unsigned char msg[]); void Recv(OysterByte& bytes);
private: private:
::Oyster::Network::Connection* connection; ::Oyster::Network::Connection* connection;

View File

@ -4,12 +4,13 @@
#include "../NetworkDependencies/WinsockFunctions.h" #include "../NetworkDependencies/WinsockFunctions.h"
#include "..\NetworkDependencies\Translator.h" #include "..\NetworkDependencies\Translator.h"
#include "..\NetworkDependencies\Protocols.h" #include "..\NetworkDependencies\Protocols.h"
#include "../NetworkDependencies/OysterByte.h"
#include "Client.h" #include "Client.h"
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
using namespace std; using namespace std;
using namespace Oyster::Network::Protocols;; using namespace Oyster::Network::Protocols;
using namespace Oyster::Network::Client; using namespace Oyster::Network::Client;
void chat(Client &client); void chat(Client &client);
@ -40,7 +41,7 @@ void chat(Client &client)
{ {
Oyster::Network::Translator *t = new Oyster::Network::Translator(); Oyster::Network::Translator *t = new Oyster::Network::Translator();
unsigned char msgRecv[1601] = "\0"; Oyster::Network::OysterByte msgRecv;
string msgSend = ""; string msgSend = "";
ProtocolSet* set = new ProtocolSet; ProtocolSet* set = new ProtocolSet;
@ -70,13 +71,14 @@ void chat(Client &client)
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++) for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++)
{ {
//cout << set->Protocol.pTest->f[i] << ' ' ; cout << set->Protocol.pTest->f[i] << ' ' ;
} }
cout << endl; cout << endl;
break; break;
} }
set->Release(); set->Release();
msgRecv.Clear(1000);
std::getline(std::cin, msgSend); std::getline(std::cin, msgSend);
@ -91,9 +93,9 @@ void chat(Client &client)
test.textMessage = msgSend; test.textMessage = msgSend;
unsigned char *message = t->Pack(test); t->Pack(test, msgRecv);
client.Send(message); client.Send(msgRecv);
} }
else else

View File

@ -1,5 +1,6 @@
#include "Client.h" #include "Client.h"
using namespace Oyster::Network;
using namespace Oyster::Network::Server; using namespace Oyster::Network::Server;
Client::Client(unsigned int socket) Client::Client(unsigned int socket)
@ -12,12 +13,12 @@ Client::~Client()
delete connection; delete connection;
} }
void Client::Send(unsigned char buffer[]) void Client::Send(OysterByte& bytes)
{ {
connection->Send(buffer); connection->Send(bytes);
} }
void Client::Recv(unsigned char buffer[]) void Client::Recv(OysterByte& bytes)
{ {
connection->Recieve(buffer); connection->Recieve(bytes);
} }

View File

@ -6,6 +6,7 @@
///////////////////////////////////// /////////////////////////////////////
#include "../NetworkDependencies/Connection.h" #include "../NetworkDependencies/Connection.h"
#include "../NetworkDependencies/OysterByte.h"
namespace Oyster namespace Oyster
{ {
@ -19,8 +20,8 @@ namespace Oyster
Client(unsigned int socket); Client(unsigned int socket);
~Client(); ~Client();
void Send(unsigned char buffer[]); void Send(OysterByte& bytes);
void Recv(unsigned char buffer[]); void Recv(OysterByte& bytes);
private: private:
::Oyster::Network::Connection* connection; ::Oyster::Network::Connection* connection;

View File

@ -5,6 +5,7 @@
#include "../NetworkDependencies/Listener.h" #include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/Translator.h" #include "../NetworkDependencies/Translator.h"
#include "Client.h" #include "Client.h"
#include "../NetworkDependencies/OysterByte.h"
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
@ -15,7 +16,8 @@ using namespace ::Protocols;
int main() int main()
{ {
unsigned char* recvBuffer = new unsigned char[1601]; OysterByte recvBuffer;
cout << "Server" << endl; cout << "Server" << endl;
Translator t; Translator t;
@ -42,8 +44,9 @@ int main()
ProtocolSet* set = new ProtocolSet; ProtocolSet* set = new ProtocolSet;
ProtocolTest test; ProtocolTest test;
test.clientID = 0; test.clientID = 0;
test.size = 2;
test.textMessage = "hej"; test.textMessage = "hej";
test.numOfFloats = 350; test.numOfFloats = 35;
test.f = new float[test.numOfFloats]; test.f = new float[test.numOfFloats];
float temp = 395.456f; float temp = 395.456f;
for(int i = 0; i < test.numOfFloats; i++) for(int i = 0; i < test.numOfFloats; i++)
@ -51,7 +54,8 @@ int main()
test.f[i] = temp; test.f[i] = temp;
temp--; temp--;
} }
recvBuffer = t.Pack(test);
t.Pack(test, recvBuffer);
client1.Send(recvBuffer); client1.Send(recvBuffer);
@ -85,7 +89,6 @@ int main()
} }
ShutdownSockets(); ShutdownSockets();
delete[] recvBuffer;
delete set; delete set;
system("pause"); system("pause");