Network -

This commit is contained in:
Pontus Fransson 2013-12-16 08:59:38 +01:00
parent 94ccd91828
commit 7acb3a3613
10 changed files with 103 additions and 74 deletions

View File

@ -51,13 +51,13 @@ struct Translator::PrivateData
}
//Packages a header with a size(int) and a string of characters(char)
void PackHeader(SmartPointer<OysterByte> &bytes, CustomNetProtocol& protocol)
void PackHeader(OysterByte &bytes, CustomNetProtocol& protocol)
{
auto it = ((MyCastingStruct*)protocol.privateData)->attributes.begin();
auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end();
size = 4; //size(int)
message.PackInt(size, *bytes);
message.PackInt(size, bytes);
//Find all the data types
for(; it != end; it++)
@ -65,19 +65,19 @@ struct Translator::PrivateData
headerString.push_back(it->second.type);
}
message.PackShort(size, *bytes);
message.PackShort(size, bytes);
size += 2;
for(int i = 0; i < (int)headerString.size(); i++)
{
message.PackChar(headerString.at(i), *bytes);
message.PackChar(headerString.at(i), bytes);
size++;
}
message.SetSize(bytes);
}
void PackMessage(SmartPointer<OysterByte> &bytes, CustomNetProtocol& protocol)
void PackMessage(OysterByte &bytes, CustomNetProtocol& protocol)
{
auto it = ((MyCastingStruct*)protocol.privateData)->attributes.begin();
auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end();
@ -87,40 +87,40 @@ struct Translator::PrivateData
switch((int)headerString.at(i))
{
case NetAttributeType_Bool:
message.PackBool(it->second.value.netBool, *bytes);
message.PackBool(it->second.value.netBool, bytes);
break;
case NetAttributeType_Char:
message.PackChar(it->second.value.netChar, *bytes);
message.PackChar(it->second.value.netChar, bytes);
break;
case NetAttributeType_UnsignedChar:
message.PackUnsignedChar(it->second.value.netUChar, *bytes);
message.PackUnsignedChar(it->second.value.netUChar, bytes);
break;
case NetAttributeType_Short:
message.PackShort(it->second.value.netShort, *bytes);
message.PackShort(it->second.value.netShort, bytes);
break;
case NetAttributeType_UnsignedShort:
message.PackUnsignedShort(it->second.value.netUShort, *bytes);
message.PackUnsignedShort(it->second.value.netUShort, bytes);
break;
case NetAttributeType_Int:
message.PackInt(it->second.value.netInt, *bytes);
message.PackInt(it->second.value.netInt, bytes);
break;
case NetAttributeType_UnsignedInt:
message.PackUnsignedInt(it->second.value.netUInt, *bytes);
message.PackUnsignedInt(it->second.value.netUInt, bytes);
break;
case NetAttributeType_Int64:
message.PackInt64(it->second.value.netInt64, *bytes);
message.PackInt64(it->second.value.netInt64, bytes);
break;
case NetAttributeType_UnsignedInt64:
message.PackUnsignedInt64(it->second.value.netUInt64, *bytes);
message.PackUnsignedInt64(it->second.value.netUInt64, bytes);
break;
case NetAttributeType_Float:
message.PackFloat(it->second.value.netFloat, *bytes);
message.PackFloat(it->second.value.netFloat, bytes);
break;
case NetAttributeType_Double:
message.PackDouble(it->second.value.netDouble, *bytes);
message.PackDouble(it->second.value.netDouble, bytes);
break;
case NetAttributeType_CharArray:
message.PackStr(it->second.value.netCharPtr, *bytes);
message.PackStr(it->second.value.netCharPtr, bytes);
break;
default:
numberOfUnknownTypes++;
@ -131,27 +131,27 @@ struct Translator::PrivateData
message.SetSize(bytes);
}
bool UnpackHeader(CustomNetProtocol& protocol, SmartPointer<OysterByte> &bytes)
bool UnpackHeader(CustomNetProtocol& protocol, OysterByte &bytes)
{
message.SetSize(0);
int packageSize = message.UnpackInt(*bytes);
if(packageSize != bytes->GetSize())
int packageSize = message.UnpackInt(bytes);
if(packageSize != bytes.GetSize())
{
return false;
}
short numberOfTypes = message.UnpackShort(*bytes);
short numberOfTypes = message.UnpackShort(bytes);
for(int i = 0; i < numberOfTypes; i++)
{
char temp = message.UnpackChar(*bytes);
char temp = message.UnpackChar(bytes);
headerString.push_back(temp);
}
return true;
}
void UnpackMessage(CustomNetProtocol& protocol, SmartPointer<OysterByte> &bytes)
void UnpackMessage(CustomNetProtocol& protocol, OysterByte &bytes)
{
for(int i = 0; i < (int)headerString.size(); i++)
{
@ -159,40 +159,40 @@ struct Translator::PrivateData
switch(protocol[i].type)
{
case NetAttributeType_Bool:
protocol[i].value.netBool = message.UnpackBool(*bytes);
protocol[i].value.netBool = message.UnpackBool(bytes);
break;
case NetAttributeType_Char:
protocol[i].value.netChar = message.UnpackChar(*bytes);
protocol[i].value.netChar = message.UnpackChar(bytes);
break;
case NetAttributeType_UnsignedChar:
protocol[i].value.netUChar = message.UnpackUnsignedChar(*bytes);
protocol[i].value.netUChar = message.UnpackUnsignedChar(bytes);
break;
case NetAttributeType_Short:
protocol[i].value.netShort = message.UnpackShort(*bytes);
protocol[i].value.netShort = message.UnpackShort(bytes);
break;
case NetAttributeType_UnsignedShort:
protocol[i].value.netUShort = message.UnpackUnsignedShort(*bytes);
protocol[i].value.netUShort = message.UnpackUnsignedShort(bytes);
break;
case NetAttributeType_Int:
protocol[i].value.netInt = message.UnpackInt(*bytes);
protocol[i].value.netInt = message.UnpackInt(bytes);
break;
case NetAttributeType_UnsignedInt:
protocol[i].value.netUInt = message.UnpackUnsignedInt(*bytes);
protocol[i].value.netUInt = message.UnpackUnsignedInt(bytes);
break;
case NetAttributeType_Int64:
protocol[i].value.netInt64 = message.UnpackInt64(*bytes);
protocol[i].value.netInt64 = message.UnpackInt64(bytes);
break;
case NetAttributeType_UnsignedInt64:
protocol[i].value.netUInt64 = message.UnpackUnsignedInt64(*bytes);
protocol[i].value.netUInt64 = message.UnpackUnsignedInt64(bytes);
break;
case NetAttributeType_Float:
protocol[i].value.netFloat = message.UnpackFloat(*bytes);
protocol[i].value.netFloat = message.UnpackFloat(bytes);
break;
case NetAttributeType_Double:
protocol[i].value.netDouble = message.UnpackDouble(*bytes);
protocol[i].value.netDouble = message.UnpackDouble(bytes);
break;
case NetAttributeType_CharArray:
protocol[i].value.netCharPtr = message.UnpackCStr(*bytes);
protocol[i].value.netCharPtr = message.UnpackCStr(bytes);
break;
default:
numberOfUnknownTypes++;
@ -222,7 +222,7 @@ Translator::~Translator()
}
}
void Translator::Pack(SmartPointer<OysterByte> &bytes, CustomNetProtocol& protocol)
void Translator::Pack(OysterByte &bytes, CustomNetProtocol& protocol)
{
privateData->headerString.clear();
@ -230,7 +230,7 @@ void Translator::Pack(SmartPointer<OysterByte> &bytes, CustomNetProtocol& protoc
privateData->PackMessage(bytes, protocol);
}
bool Translator::Unpack(CustomNetProtocol& protocol, SmartPointer<OysterByte> &bytes)
bool Translator::Unpack(CustomNetProtocol& protocol, OysterByte &bytes)
{
if(!privateData->UnpackHeader(protocol, bytes))
{

View File

@ -49,10 +49,10 @@ namespace Oyster
Translator ();
~Translator();
void Pack(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes, CustomNetProtocol& protocol);
void Pack(OysterByte &bytes, CustomNetProtocol& protocol);
//Returns false if it discovers any faulty stuff with the package.
bool Unpack(CustomNetProtocol& protocol, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes);
bool Unpack(CustomNetProtocol& protocol, OysterByte &bytes);
private:
struct PrivateData;

View File

@ -104,11 +104,11 @@ int Connection::Disconnect()
return 0;
}
int Connection::Send(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
int Connection::Send(OysterByte &bytes)
{
int nBytes;
nBytes = send(this->socket, *bytes, bytes->GetSize(), 0);
nBytes = send(this->socket, bytes, bytes.GetSize(), 0);
if(nBytes == SOCKET_ERROR)
{
return WSAGetLastError();
@ -117,20 +117,20 @@ int Connection::Send(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
return 0;
}
int Connection::Recieve(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
int Connection::Recieve(OysterByte &bytes)
{
int nBytes;
bytes->Resize(1000);
nBytes = recv(this->socket, *bytes, 1000, 0);
bytes.Resize(1000);
nBytes = recv(this->socket, bytes, 1000, 0);
if(nBytes == SOCKET_ERROR)
{
bytes->SetSize(0);
bytes.SetSize(0);
return WSAGetLastError();
}
else
{
bytes->SetSize(nBytes);
bytes.SetSize(nBytes);
}
return 0;
@ -177,6 +177,7 @@ int Connection::SetBlockingMode(bool blocking)
return WSAGetLastError();
}
//Success
return 0;
}

View File

@ -23,8 +23,8 @@ namespace Oyster
virtual int InitiateServer( unsigned short port );
virtual int InitiateClient();
virtual int Send( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
virtual int Recieve( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
virtual int Send( OysterByte &bytes );
virtual int Recieve( OysterByte &bytes );
virtual int Disconnect();
virtual int Connect( unsigned short port , const char serverName[] );

View File

@ -19,8 +19,8 @@ namespace Oyster
//sends and recieve functions with bytearrays,
//will send to the users connection via socket
virtual int Send( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes ) = 0;
virtual int Recieve( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes) = 0;
virtual int Send( OysterByte &bytes ) = 0;
virtual int Recieve( OysterByte &bytes) = 0;
//initiates sockets and address for server and client
virtual int InitiateServer( unsigned short port ) { return false; };

View File

@ -84,7 +84,7 @@ int ThreadedClient::Recv()
{
int errorCode = -1;
SmartPointer<OysterByte> temp = new OysterByte;
OysterByte temp;
errorCode = this->connection->Recieve(temp);
if(errorCode == 0)

View File

@ -18,9 +18,6 @@ using namespace Oyster::Network;
using namespace Utility;
using namespace Utility::DynamicMemory;
void chat(ThreadedClient &client);
void PrintOutMessage(ProtocolSet* set);
void proc(CustomNetProtocol& protocol)
{
@ -40,14 +37,13 @@ int main()
NetworkClient client;
//Connect to server
errorCode = client.Connect(15151, "193.11.186.101");
if(client.Connect(15151, "localhost"))
{
cout << "FAILED" << endl;
}
client.SetRecieverObject(proc, NetworkProtocolCallbackType_Function);
if(errorCode != 0)
{
wstring errorTest = GetErrorMessage(errorCode);
wcout << "errorMessage: " << errorTest << endl;
}
cout << "Done" << endl;
while(1)
{

View File

@ -70,28 +70,28 @@
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<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>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\NetworkAPI;$(SolutionDir)..\Bin\DLL;C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(OutDir)..\DLL\</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<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>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath);$(OutDir)..\DLL\</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<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>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath);$(OutDir)..\DLL\</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<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\Win64;$(LibraryPath)</LibraryPath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath);$(OutDir)..\DLL\</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@ -116,6 +116,7 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<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)'=='Release|Win32'">
@ -131,6 +132,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<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)'=='Release|x64'">
@ -146,6 +148,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<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>
<ItemGroup>

View File

@ -66,32 +66,32 @@
</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>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath);$(OutDir)..\DLL\</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>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath);$(OutDir)..\DLL\</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>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath);$(OutDir)..\DLL\</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>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath)</LibraryPath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath);$(OutDir)..\DLL\</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@ -104,6 +104,7 @@
<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'">

View File

@ -2,16 +2,44 @@
#include <vector>
#include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkAPI/NetworkServer.h"
using namespace Oyster::Network;
using namespace std;
void proc(NetworkClient client)
{
cout << "Hej" << endl;
}
int main()
{
if(!InitWinSock())
NetworkServer server;
Oyster::Network::NetworkServer::INIT_DESC desc;
desc.port = 15151;
desc.callbackType = NetworkClientCallbackType_Function;
desc.recvObj = proc;
if(!server.Init(desc))
{
cout << "errorMessage: unable to start winsock" << endl;
cout << "Init failed" << endl;
return 0;
}
if(!server.Start())
{
cout << "Start failed" << endl;
return 0;
}
cout << "Server started" << endl;
while(1)
{
}
system("pause");
return 0;