Network - Fixed bugg, and small fixed

Using sizeof in MessageHeader class.
Fixed bugg in Translator.
This commit is contained in:
Pontus Fransson 2013-12-13 08:17:05 +01:00
parent 092f83a084
commit 2ddb984a2b
12 changed files with 61 additions and 267 deletions

View File

@ -57,8 +57,7 @@ struct Translator::PrivateData
auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end();
size = 4; //size(int)
bytes->AddSize(4);
message.SetSize(size);
message.PackInt(size, *bytes);
//Find all the data types
for(; it != end; it++)

View File

@ -17,7 +17,6 @@ Listener::Listener(Oyster::Network::IPostBox<int>* postBox)
Listener::~Listener()
{
Stop();
if(connection)
{
delete connection;

View File

@ -41,72 +41,72 @@ void MessageHeader::Unpack(OysterByte& bytes, ProtocolHeader& header)
void MessageHeader::PackBool(bool i, OysterByte& bytes)
{
bytes.AddSize(1);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 1;
size += sizeof(i);
}
void MessageHeader::PackChar(char i, OysterByte& bytes)
{
bytes.AddSize(1);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 1;
size += sizeof(i);
}
void MessageHeader::PackUnsignedChar(unsigned char i, OysterByte& bytes)
{
bytes.AddSize(1);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 1;
size += sizeof(i);
}
void MessageHeader::PackShort(short i, OysterByte& bytes)
{
bytes.AddSize(2);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 2;
size += sizeof(i);
}
void MessageHeader::PackUnsignedShort(unsigned short i, OysterByte& bytes)
{
bytes.AddSize(2);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 2;
size += sizeof(i);
}
void MessageHeader::PackInt(int i, OysterByte& bytes)
{
bytes.AddSize(4);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 4;
size += sizeof(i);
}
void MessageHeader::PackUnsignedInt(unsigned int i, OysterByte& bytes)
{
bytes.AddSize(4);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 4;
size += sizeof(i);
}
void MessageHeader::PackInt64(__int64 i, OysterByte& bytes)
{
bytes.AddSize(8);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 8;
size += sizeof(i);
}
void MessageHeader::PackUnsignedInt64(unsigned __int64 i, OysterByte& bytes)
{
bytes.AddSize(8);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 8;
size += sizeof(i);
}
void MessageHeader::PackFloat(float i, OysterByte& bytes)
{
bytes.AddSize(4);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 4;
size += sizeof(i);
}
void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes)
@ -123,14 +123,14 @@ void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte&
void MessageHeader::PackDouble(double i, OysterByte& bytes)
{
bytes.AddSize(8);
bytes.AddSize(sizeof(i));
Packing::Pack(&bytes.GetByteArray()[size], i);
size += 8;
size += sizeof(i);
}
void MessageHeader::PackStr(char str[], OysterByte& bytes)
{
int totalSize = 2 + (int)strlen(str);
int totalSize = sizeof(short) + (int)strlen(str);
bytes.AddSize(totalSize);
Packing::Pack(&bytes.GetByteArray()[size], str);
size += totalSize;
@ -138,7 +138,7 @@ void MessageHeader::PackStr(char str[], OysterByte& bytes)
void MessageHeader::PackStr(std::string str, OysterByte& bytes)
{
int totalSize = 2 + (int)str.length();
int totalSize = sizeof(short) + (int)str.length();
bytes.AddSize(totalSize);
Packing::Pack(&bytes.GetByteArray()[size], str);
size += totalSize;
@ -151,70 +151,70 @@ void MessageHeader::PackStr(std::string str, OysterByte& bytes)
bool MessageHeader::UnpackBool(OysterByte& bytes)
{
bool i = Packing::Unpackb(&bytes.GetByteArray()[size]);
size += 1;
size += sizeof(i);
return i;
}
char MessageHeader::UnpackChar(OysterByte& bytes)
{
char i = Packing::Unpackc(&bytes.GetByteArray()[size]);
size += 1;
size += sizeof(i);
return i;
}
unsigned char MessageHeader::UnpackUnsignedChar(OysterByte& bytes)
{
unsigned char i = Packing::UnpackC(&bytes.GetByteArray()[size]);
size += 1;
size += sizeof(i);
return i;
}
short MessageHeader::UnpackShort(OysterByte& bytes)
{
short i = Packing::Unpacks(&bytes.GetByteArray()[size]);
size += 2;
size += sizeof(i);
return i;
}
unsigned short MessageHeader::UnpackUnsignedShort(OysterByte& bytes)
{
unsigned short i = Packing::UnpackS(&bytes.GetByteArray()[size]);
size += 2;
size += sizeof(i);
return i;
}
int MessageHeader::UnpackInt(OysterByte& bytes)
{
int i = Packing::Unpacki(&bytes.GetByteArray()[size]);
size += 4;
size += sizeof(i);
return i;
}
unsigned int MessageHeader::UnpackUnsignedInt(OysterByte& bytes)
{
unsigned int i = Packing::UnpackI(&bytes.GetByteArray()[size]);
size += 4;
size += sizeof(i);
return i;
}
__int64 MessageHeader::UnpackInt64(OysterByte& bytes)
{
__int64 i = Packing::Unpacki64(&bytes.GetByteArray()[size]);
size += 8;
size += sizeof(i);
return i;
}
unsigned __int64 MessageHeader::UnpackUnsignedInt64(OysterByte& bytes)
{
unsigned __int64 i = Packing::UnpackI64(&bytes.GetByteArray()[size]);
size += 8;
size += sizeof(i);
return i;
}
float MessageHeader::UnpackFloat(OysterByte& bytes)
{
float i = Packing::Unpackf(&bytes.GetByteArray()[size]);
size += 4;
size += sizeof(i);
return i;
}
@ -236,21 +236,21 @@ float* MessageHeader::UnpackFloat(unsigned int& elementCount, OysterByte& bytes)
double MessageHeader::UnpackDouble(OysterByte& bytes)
{
double i = Packing::Unpackd(&bytes.GetByteArray()[size]);
size += 8;
size += sizeof(i);
return i;
}
char* MessageHeader::UnpackCStr(OysterByte& bytes)
{
char* str = Packing::UnpackCStr(&bytes.GetByteArray()[size]);
size += 2 + (int)strlen(str);
size += sizeof(short) + (int)strlen(str);
return str;
}
std::string MessageHeader::UnpackStr(OysterByte& bytes)
{
std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]);
size += 2 + (int)str.length();
size += sizeof(short) + (int)str.length();
return str;
}

View File

@ -18,7 +18,6 @@ OysterByte::OysterByte(int cap)
OysterByte::OysterByte(const OysterByte& obj)
{
//delete[] this->byteArray;
this->byteArray = new unsigned char[obj.capacity];
for(int i = 0; i < (int)obj.size; i++)

View File

@ -19,15 +19,20 @@ namespace Oyster
OysterByte(const OysterByte& obj);
virtual ~OysterByte();
void Clear(); //Resets size to 0
void Resize(unsigned int cap); //Resizes the array with, it does not keep anything in it.
//Resets size to 0
void Clear();
//Resizes the array with, it does not keep anything in it.
void Resize(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'
//Only sets the private variable 'size'
void SetSize(unsigned int size);
OysterByte& operator =(const OysterByte& obj);
@ -36,7 +41,8 @@ namespace Oyster
operator unsigned char*();
private:
void IncreaseCapacity(unsigned int cap); //Expands the byteArray
//Expands the byteArray
void IncreaseCapacity(unsigned int cap);
private:

View File

@ -8,6 +8,7 @@
#include "../NetworkDependencies/ThreadedClient.h"
#include "../../Misc/WinTimer.h"
#include "../../Misc/Utilities.h"
#include "../NetworkAPI/NetworkClient.h"
#pragma comment(lib, "ws2_32.lib")
@ -20,6 +21,11 @@ using namespace Utility::DynamicMemory;
void chat(ThreadedClient &client);
void PrintOutMessage(ProtocolSet* set);
void proc(CustomNetProtocol& protocol)
{
}
int main()
{
int errorCode;
@ -31,10 +37,11 @@ int main()
cout << "Client" << endl;
//Create Client
ThreadedClient* client = new ThreadedClient;
NetworkClient client;
//Connect to server
errorCode = client->Connect(15151, "193.11.186.101");
errorCode = client.Connect(15151, "193.11.186.101");
client.SetRecieverObject(proc, NetworkProtocolCallbackType_Function);
if(errorCode != 0)
{
@ -42,94 +49,13 @@ int main()
wcout << "errorMessage: " << errorTest << endl;
}
chat(*client);
while(1)
{
}
delete client;
ShutdownWinSock();
system("pause");
return 0;
}
void chat(ThreadedClient &client)
{
/*Oyster::Network::Translator *t = new Oyster::Network::Translator();
IPostBox< SmartPointer<OysterByte >> *postBox = new PostBox< SmartPointer<OysterByte >>;
//client.setRecvPostBox(postBox);
SmartPointer<OysterByte> msgRecv = new OysterByte();
SmartPointer<OysterByte> msgSend = new OysterByte();
ProtocolSet* set = new ProtocolSet;
ProtocolPlayerPos test;
test.ID = 5;
test.nrOfFloats = 5;
test.matrix = new float[test.nrOfFloats];
float temp = 10;
for(int i = 0; i < (int)test.nrOfFloats; i++)
{
test.matrix[i] = temp;
temp++;
}
t->Pack(test, msgSend);
WinTimer timer;
while(1)
{
//Fetch new messages from the postbox
//if(postBox->FetchMessage(msgRecv))
{
t->Unpack(set, msgRecv);
//PrintOutMessage(set);
set->Release();
}
//Send message to server each second
if(timer.getElapsedSeconds() > 1)
{
cout << "Sending to server." << endl;
timer.reset();
//client.Send(msgSend);
}
Sleep(1);
}
delete postBox;
delete t;
delete set;*/
}
void PrintOutMessage(ProtocolSet* set)
{
switch(set->type)
{
case PackageType_header:
break;
case PackageType_test:
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
for(int i = 0; i < (int)set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ' ;
}
cout << endl;
break;
case PackageType_player_pos:
cout << "ID " << set->Protocol.pPlayerPos->ID << endl;
for(int i = 0; i < (int)set->Protocol.pPlayerPos->nrOfFloats; i++)
{
cout << set->Protocol.pPlayerPos->matrix[i] << ' ';
}
cout << endl;
break;
}
}

View File

@ -73,21 +73,21 @@
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\NetworkAPI;$(SolutionDir)..\Bin\DLL;C:\Program Files (x86)\Visual Leak Detector\lib\Win32</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>

View File

@ -160,10 +160,6 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="ServerMain.cpp" />
<ClCompile Include="TestClass.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="TestClass.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -18,13 +18,5 @@
<ClCompile Include="ServerMain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="TestClass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="TestClass.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -2,7 +2,6 @@
#include <vector>
#include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkAPI/Translator.h"
using namespace std;

View File

@ -1,84 +0,0 @@
#include "TestClass.h"
#include "../../Misc/WinTimer.h"
#include <iostream>
/*
using namespace Oyster::Network;
using namespace ::Protocols;
using namespace Utility;
using namespace ::DynamicMemory;
using namespace std;
Test::Test()
{
recvPostBox = new PostBox<SmartPointer<OysterByte>>;
sendBuffer = new OysterByte;
recvBuffer = new OysterByte;
NetworkServer::INIT_DESC initDesc;
initDesc.port = 9876;
server.Init(initDesc);
server.Start();
test.clientID = 0;
test.ID = 5;
test.nrOfFloats = 10;
test.matrix = new float[test.nrOfFloats];
for(int i = 0; i < (int)test.nrOfFloats; i++)
{
test.matrix[i] = (float)i;
}
//t.Pack(test, sendBuffer);
}
Test::~Test()
{
for(int i = 0; i < (int)clients.size(); i++)
delete clients.at(i);
server.Stop();
}
void Test::ProcFunction(CustomNetProtocol& protocol)
{
return;
}
void Test::mainLoop()
{
WinTimer timer;
while(1)
{
}
}
void Test::PrintOutMessage(ProtocolSet* set)
{
switch(set->type)
{
case PackageType_header:
break;
case PackageType_test:
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
for(int i = 0; i < (int)set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ' ;
}
cout << endl;
break;
case PackageType_player_pos:
//cout << "ID " << set->Protocol.pPlayerPos->ID << endl;
for(int i = 0; i < (int)set->Protocol.pPlayerPos->nrOfFloats; i++)
{
cout << set->Protocol.pPlayerPos->matrix[i] << ' ';
}
cout << endl;
break;
}
}*/

View File

@ -1,38 +0,0 @@
#ifndef TEST_CLASS_H
#define TEST_CLASS_H
/*
#include "../../Misc/Utilities.h"
#include "../NetworkDependencies/OysterByte.h"
#include "../NetworkDependencies/PostBox.h"
#include "../NetworkAPI/NetworkClient.h"
#include "../NetworkAPI/NetworkServer.h"
//#include "../NetworkDependencies/Translator.h"
#include "../NetworkAPI/CustomNetProtocol.h"
#include "../NetworkDependencies/Protocols.h"
#include <vector>
class Test
{
public:
Test();
~Test();
void mainLoop();
virtual void ProcFunction(Oyster::Network::CustomNetProtocol& protocol);
void PrintOutMessage(Oyster::Network::Protocols::ProtocolSet* set);
private:
std::vector<Oyster::Network::NetworkClient*> clients;
Oyster::Network::IPostBox<Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte>> *recvPostBox;
//Oyster::Network::Translator t;
Oyster::Network::Protocols::ProtocolPlayerPos test;
Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> sendBuffer;
Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> recvBuffer;
Oyster::Network::NetworkServer server;
};
*/
#endif