GameLogic - Merge with network, resolved 2 merging errors

This commit is contained in:
dean11 2013-12-14 22:54:24 +01:00
commit 17aa38d865
13 changed files with 87 additions and 270 deletions

View File

@ -38,7 +38,12 @@ struct ClientDataContainer
Translator translator;
//ID
static unsigned int currID;
const unsigned int ID;
ClientDataContainer(IThreadObject* o)
: ID(currID++)
{
InitWinSock();
callbackType = NetworkProtocolCallbackType_Unknown;
@ -46,7 +51,7 @@ struct ClientDataContainer
connection.SetBlockingMode(false);
}
ClientDataContainer(IThreadObject* o, unsigned int socket )
:connection(socket)
:connection(socket), ID(currID++)
{
InitWinSock();
callbackType = NetworkProtocolCallbackType_Unknown;
@ -64,6 +69,8 @@ struct ClientDataContainer
}
};
unsigned int ClientDataContainer::currID = 0;
struct NetworkClient::PrivateData : public IThreadObject
{
Utility::DynamicMemory::SmartPointer<ClientDataContainer> data;
@ -233,4 +240,9 @@ void NetworkClient::SetRecieverObject(RecieverObject recvObj, NetworkProtocolCal
privateData->data->recvObj = recvObj;
privateData->data->callbackType = type;
privateData->data->recvObjMutex.unlock();
}
bool NetworkClient::operator ==(const NetworkClient& obj)
{
return (this->privateData->data->ID == obj.privateData->data->ID);
}

View File

@ -38,10 +38,14 @@ namespace Oyster
bool IsConnected();
//Adds the protocol to the queue of protocols to be sent.
void Send(CustomProtocolObject& protocol);
void SetRecieverObject(RecieverObject recvObj, NetworkProtocolCallbackType type);
//Compares the internal ID.
bool operator ==(const NetworkClient& obj);
private:
struct PrivateData;
PrivateData* privateData;

View File

@ -35,9 +35,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++)
@ -46,6 +44,7 @@ struct Translator::PrivateData
}
message.PackShort(size, bytes);
size += 2;
for(int i = 0; i < (int)headerString.size(); i++)
{

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,24 +19,30 @@ 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);
operator char*();
operator const char*();
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,11 +37,12 @@ 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, "127.0.0.1");
client.SetRecieverObject(proc, NetworkProtocolCallbackType_Function);
if(errorCode != 0)
{
@ -43,94 +50,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

@ -66,28 +66,28 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<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\Win32;$(LibraryPath)</LibraryPath>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<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>
@ -102,6 +102,9 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<DelayLoadDLLs>
</DelayLoadDLLs>
<AdditionalDependencies>NetworkAPI_$(PlatformShortName)D.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -149,6 +152,9 @@
<ProjectReference Include="..\..\Misc\Misc.vcxproj">
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
</ProjectReference>
<ProjectReference Include="..\NetworkAPI\NetworkAPI.vcxproj">
<Project>{460d625f-2ac9-4559-b809-0ba89ceaedf4}</Project>
</ProjectReference>
<ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj">
<Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project>
</ProjectReference>

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