GL - merge with network - fixed dependencies in launcher project

This commit is contained in:
Linda Andersson 2013-12-11 09:00:36 +01:00
commit bb789faa67
52 changed files with 1490 additions and 477 deletions

View File

@ -5,6 +5,9 @@
#include "GameClientState/GameClientState.h"
#include "GameClientState\GameState.h"
#include "GameClientState\LobbyState.h"
#include "L_inputClass.h"
#include "vld.h"
namespace DanBias
@ -12,7 +15,6 @@ namespace DanBias
__int64 DanBiasGame::cntsPerSec = 0;
__int64 DanBiasGame::prevTimeStamp = 0;
float DanBiasGame::secsPerCnt = 0;
InputClass* DanBiasGame::inputObj = NULL;
HINSTANCE DanBiasGame::g_hInst = NULL;
HWND DanBiasGame::g_hWnd = NULL;
@ -32,6 +34,7 @@ namespace DanBias
public:
Client::GameClientState* gameClientState;
InputClass* inputObj;
// gameClient;
} data;
@ -163,8 +166,8 @@ namespace DanBias
//-------------------------------------------------------------------------------------
HRESULT DanBiasGame::InitInput()
{
inputObj = new InputClass;
if(!inputObj->Initialize(g_hInst, g_hWnd, 1024, 768))
m_data->inputObj = new InputClass;
if(!m_data->inputObj->Initialize(g_hInst, g_hWnd, 1024, 768))
{
MessageBox(0, L"Could not initialize the input object.", L"Error", MB_OK);
return E_FAIL;
@ -174,10 +177,10 @@ namespace DanBias
HRESULT DanBiasGame::Update(float deltaTime)
{
inputObj->Update();
m_data->inputObj->Update();
DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same;
state = m_data->gameClientState->Update(deltaTime, inputObj);
state = m_data->gameClientState->Update(deltaTime, m_data->inputObj);
if(state != Client::GameClientState::ClientState_Same)
{
@ -206,7 +209,7 @@ namespace DanBias
HRESULT DanBiasGame::Render(float deltaTime)
{
int isPressed = 0;
if(inputObj->IsKeyPressed(DIK_A))
if(m_data->inputObj->IsKeyPressed(DIK_A))
{
isPressed = 1;
}
@ -224,8 +227,9 @@ namespace DanBias
{
m_data->gameClientState->Release();
delete m_data->gameClientState;
delete m_data->inputObj;
delete m_data;
delete inputObj;
Oyster::Graphics::API::Clean();
return S_OK;

View File

@ -11,8 +11,6 @@
#include <Windows.h>
#include "L_inputClass.h"
namespace DanBias
{
@ -60,7 +58,7 @@ namespace DanBias
static __int64 cntsPerSec;
static __int64 prevTimeStamp;
static float secsPerCnt;
static InputClass* inputObj;
static HINSTANCE g_hInst;
static HWND g_hWnd;
static DanBiasGamePrivateData* m_data;

View File

@ -71,32 +71,32 @@
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL;</LibraryPath>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL;</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL;</LibraryPath>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL;</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL;</LibraryPath>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL;</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL;</LibraryPath>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL;</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@ -105,13 +105,13 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)Input;$(SolutionDir)Include;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterGraphics\;$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<DelayLoadDLLs>DanBiasServer_$(PlatformShortName)D.dll;DanBiasGame_$(PlatformShortName)D.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
<AdditionalDependencies>Input_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>DanBiasServer_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -121,13 +121,13 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)Input;$(SolutionDir)Include;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterGraphics\;$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<DelayLoadDLLs>DanBiasServer_$(PlatformShortName)D.dll;DanBiasGame_$(PlatformShortName)D.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
<AdditionalDependencies>Input_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>DanBiasServer_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -139,7 +139,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)Input;$(SolutionDir)Include;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterGraphics\;$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@ -147,7 +147,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<DelayLoadDLLs>DanBiasServer_$(PlatformShortName).dll;DanBiasGame_$(PlatformShortName).dll;%(DelayLoadDLLs)</DelayLoadDLLs>
<AdditionalDependencies>Input_$(PlatformShortName).lib;DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -159,7 +159,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)Input;$(SolutionDir)Include;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterGraphics\;$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@ -167,28 +167,13 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<DelayLoadDLLs>DanBiasServer_$(PlatformShortName).dll;DanBiasGame_$(PlatformShortName).dll;%(DelayLoadDLLs)</DelayLoadDLLs>
<AdditionalDependencies>Input_$(PlatformShortName).lib;DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Launcher.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\GamePhysics\GamePhysics.vcxproj">
<Project>{104fa3e9-94d9-4e1d-a941-28a03bc8a095}</Project>
</ProjectReference>
<ProjectReference Include="..\..\Input\Input.vcxproj">
<Project>{7e3990d2-3d94-465c-b58d-64a74b3ecf9b}</Project>
</ProjectReference>
<ProjectReference Include="..\..\Misc\Misc.vcxproj">
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
</ProjectReference>
<ProjectReference Include="..\..\OysterGraphics\OysterGraphics.vcxproj">
<Project>{0ec83e64-230e-48ef-b08c-6ac9651b4f82}</Project>
</ProjectReference>
<ProjectReference Include="..\..\OysterMath\OysterMath.vcxproj">
<Project>{f10cbc03-9809-4cba-95d8-327c287b18ee}</Project>
</ProjectReference>
<ProjectReference Include="..\DanBiasGame\DanBiasGame.vcxproj">
<Project>{2a1bc987-af42-4500-802d-89cd32fc1309}</Project>
</ProjectReference>

View File

@ -4,8 +4,8 @@
#define NOMINMAX
#include <Windows.h>
//#define DANBIAS_SERVER
#define DANBIAS_CLIENT
#define DANBIAS_SERVER
//#define DANBIAS_CLIENT
#if defined(DANBIAS_SERVER)

View File

@ -46,7 +46,7 @@ namespace Oyster
Node *front;
Node *back;
int nrOfNodes;
OysterMutex mutex;
std::mutex stdMutex;
};
@ -68,7 +68,7 @@ namespace Oyster
template < typename Type >
ThreadSafeQueue<Type>::~ThreadSafeQueue()
{
this->mutex.LockMutex();
stdMutex.lock();
if(this->front != NULL)
{
@ -87,16 +87,16 @@ namespace Oyster
this->back = NULL;
}
this->mutex.UnlockMutex();
stdMutex.unlock();
}
template < typename Type >
void ThreadSafeQueue<Type>::Push(Type item)
{
stdMutex.lock();
Node *e = new Node(item);
mutex.LockMutex();
if(this->front != NULL)
{
this->back->next = e;
@ -111,91 +111,86 @@ namespace Oyster
this->nrOfNodes++;
mutex.UnlockMutex();
stdMutex.unlock();
}
template < typename Type >
Type ThreadSafeQueue<Type>::Pop()
{
mutex.LockMutex();
if(this->front != NULL)
stdMutex.lock();
Type item = this->front->item;
Node *destroyer = this->front;
this->front = front->next;
delete destroyer;
this->nrOfNodes--;
if(nrOfNodes == 0)
{
Type item = this->front->item;
Node *destroyer = this->front;
this->front = front->next;
delete destroyer;
this->nrOfNodes--;
if(nrOfNodes == 0)
{
this->front = NULL;
this->back = NULL;
}
return item;
this->front = NULL;
this->back = NULL;
}
mutex.UnlockMutex();
return NULL;
stdMutex.unlock();
return item;
}
template < typename Type >
Type ThreadSafeQueue<Type>::Front()
{
mutex.LockMutex();
if(front != NULL)
{
return this->front->item;
}
mutex.UnlockMutex();
stdMutex.lock();
Type temp = this->front->item;
stdMutex.unlock();
return temp;
return NULL;
}
template < typename Type >
Type ThreadSafeQueue<Type>::Back()
{
mutex.LockMutex();
if(back != NULL)
{
return this->back->item;
}
mutex.UnlockMutex();
stdMutex.lock();
Type temp = this->back->item;
stdMutex.unlock();
return temp;
return NULL;
}
template < typename Type >
int ThreadSafeQueue<Type>::Size()
{
//? behövs denna låsas?
mutex.LockMutex();
return this->nrOfNodes;
mutex.UnlockMutex();
stdMutex.lock();
int size = this->nrOfNodes;
stdMutex.unlock();
return size;
}
template < typename Type >
bool ThreadSafeQueue<Type>::IsEmpty()
{
mutex.LockMutex();
if(nrOfNodes == 0 && this->front == NULL)
stdMutex.lock();
if(nrOfNodes == 0 || this->front == NULL)
{
mutex.UnlockMutex();
stdMutex.unlock();
return true;
}
else
{
mutex.UnlockMutex();
stdMutex.unlock();
}
return false;
}
template < typename Type >
void ThreadSafeQueue<Type>::Swap(IQueue<Type> &queue )
{
mutex.LockMutex();
stdMutex.lock();
int prevNrOfNodes = this->nrOfNodes;
int size = queue.Size();
@ -208,7 +203,7 @@ namespace Oyster
{
queue.Push(this->Pop());
}
mutex.UnlockMutex();
stdMutex.unlock();
}

View File

@ -0,0 +1,58 @@
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#include "CustomNetProtocol.h"
#include <map>
using namespace Network;
struct CustomNetProtocol::PrivateData
{
std::map<int, NetAttributeContainer> attributes;
PrivateData()
{ }
~PrivateData()
{
for (auto i = attributes.begin(); i != attributes.end(); i++)
{
RemoveAttribute(i->first);
}
}
void RemoveAttribute(int ID)
{
auto i = attributes.find(ID);
if(i == attributes.end()) return;
switch (i->second.type)
{
case NetAttributeType_CharArray:
delete [] i->second.value.netCharPtr;
break;
}
}
//Do network stuff
};
CustomNetProtocol::CustomNetProtocol()
{
this->privateData = new PrivateData();
}
CustomNetProtocol::~CustomNetProtocol()
{
delete this->privateData;
}
NetAttributeContainer& CustomNetProtocol::operator[](int ID)
{
if(this->privateData->attributes.find(ID) == this->privateData->attributes.end())
{
this->privateData->attributes[ID];
this->privateData->attributes[ID].type = NetAttributeType_UNKNOWN;
memset(&this->privateData->attributes[ID].value, 0, sizeof(NetAttributeValue));
}
return this->privateData->attributes[ID];
}

View File

@ -0,0 +1,103 @@
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#ifndef NETWORK_CUSTOM_NETWORK_PROTOCOL_H
#define NETWORK_CUSTOM_NETWORK_PROTOCOL_H
#include <string>
#ifdef CUSTOM_NET_PROTOCOL_EXPORT
#define NET_PROTOCOL_EXPORT __declspec(dllexport)
#else
#define NET_PROTOCOL_EXPORT __declspec(dllimport)
#endif
namespace Network
{
extern "C"
{
enum NetAttributeType
{
NetAttributeType_Bool,
NetAttributeType_Char,
NetAttributeType_UnsignedChar,
NetAttributeType_Short,
NetAttributeType_UnsignedShort,
NetAttributeType_Int,
NetAttributeType_UnsignedInt,
NetAttributeType_Int64,
NetAttributeType_UnsignedInt64,
NetAttributeType_Float,
NetAttributeType_Double,
NetAttributeType_CharArray,
NetAttributeType_UNKNOWN,
};
union NetAttributeValue
{
bool netBool;
char netChar;
unsigned char netUChar;
short netShort;
unsigned short netUShort;
int netInt;
unsigned int netUInt;
__int64 netInt64;
unsigned __int64 netUInt64;
float netFloat;
double netDouble;
char* netCharPtr;
NetAttributeValue(){ memset(this, 0, sizeof(NetAttributeValue)); }
NetAttributeValue(bool v) : netBool (v) {}
NetAttributeValue(char v) : netChar (v) {}
NetAttributeValue(unsigned char v) : netUChar (v) {}
NetAttributeValue(short v) : netShort (v) {}
NetAttributeValue(unsigned short v) : netUShort (v) {}
NetAttributeValue(int v) : netInt (v) {}
NetAttributeValue(unsigned int v) : netUInt (v) {}
NetAttributeValue(__int64 v) : netInt64 (v) {}
NetAttributeValue(unsigned __int64 v) : netUInt64 (v) {}
NetAttributeValue(float v) : netFloat (v) {}
NetAttributeValue(double v) : netDouble (v) {}
NetAttributeValue(char* v) : netCharPtr(v) {}
};
struct NetAttributeContainer
{
NetAttributeType type;
NetAttributeValue value;
NetAttributeContainer() { type = NetAttributeType_UNKNOWN; }
};
class CustomNetProtocol;
struct CustomProtocolObject
{
virtual CustomNetProtocol* GetProtocol() = 0;
};
class NET_PROTOCOL_EXPORT CustomNetProtocol
{
public:
CustomNetProtocol();
~CustomNetProtocol();
NetAttributeContainer& operator[](int ID);
private:
struct PrivateData;
PrivateData* privateData;
};
}//End extern "C"
} //End namespace Network
#endif // !NETWORK_CUSTOM_NETWORK_PROTOCOL_H

View File

@ -0,0 +1,180 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{460D625F-2AC9-4559-B809-0BA89CEAEDF4}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>NetworkAPI</RootNamespace>
<ProjectName>NetworkAPI</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath)</LibraryPath>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)..\Bin\DLL\</OutDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath)</LibraryPath>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)..\Bin\DLL\</OutDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)..\Bin\DLL\</OutDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)..\Bin\DLL\</OutDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>CUSTOM_NET_PROTOCOL_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>CUSTOM_NET_PROTOCOL_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>CUSTOM_NET_PROTOCOL_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>CUSTOM_NET_PROTOCOL_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="CustomNetProtocol.cpp" />
<ClCompile Include="NetworkServer.cpp" />
<ClCompile Include="NetworkClient.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CustomNetProtocol.h" />
<ClInclude Include="NetworkServer.h" />
<ClInclude Include="NetworkClient.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj">
<Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,41 @@
#include "NetworkClient.h"
using namespace Oyster::Network;
/*************************************
PrivateData
*************************************/
struct PrivateData
{
};
/*************************************
NetworkClient
*************************************/
NetworkClient::NetworkClient()
{
}
NetworkClient::~NetworkClient()
{
}
void NetworkClient::Disconnect()
{
}
bool NetworkClient::IsConnected()
{
return false;
}
void NetworkClient::Send()
{
}

View File

@ -0,0 +1,42 @@
#ifndef NETWORK_API_NETWORK_CLIENT_H
#define NETWORK_API_NETWORK_CLIENT_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#ifdef CUSTOM_NET_PROTOCOL_EXPORT
#define NET_PROTOCOL_EXPORT __declspec(dllexport)
#else
#define NET_PROTOCOL_EXPORT __declspec(dllimport)
#endif
class RecieverObject;
namespace Oyster
{
namespace Network
{
extern "C"
{
class NET_PROTOCOL_EXPORT NetworkClient
{
public:
NetworkClient();
virtual ~NetworkClient();
virtual void Disconnect();
virtual bool IsConnected();
virtual void Send();
private:
struct PrivateData;
PrivateData* privateData;
};
}
}
}
#endif

View File

@ -0,0 +1,202 @@
#include "NetworkServer.h"
#include "NetworkClient.h"
#include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/Utilities.h"
#include "../../Misc/Thread/OysterThread.h"
using namespace Oyster::Network;
using namespace ::Server;
using namespace Utility::DynamicMemory;
using namespace Oyster::Thread;
/*************************************
PrivateData
*************************************/
struct NetworkServer::PrivateData : public IThreadObject
{
PrivateData();
~PrivateData();
bool Init(INIT_DESC& initDesc);
bool Start();
bool Stop();
bool Shutdown();
void CheckForNewClient();
virtual bool DoWork();
//
IListener* listener;
INIT_DESC initDesc;
bool started;
//Postbox for new clients
IPostBox<int> *postBox;
//Server thread
OysterThread thread;
};
NetworkServer::PrivateData::PrivateData()
{
listener = 0;
started = false;
postBox = new PostBox<int>;
}
NetworkServer::PrivateData::~PrivateData()
{
Shutdown();
}
bool NetworkServer::PrivateData::Init(INIT_DESC& initDesc)
{
//Check if it's a valid port
if(initDesc.port == 0)
{
return false;
}
this->initDesc = initDesc;
//Initiate listener
listener = new Listener(postBox);
((Listener*)listener)->Init(this->initDesc.port, false);
thread.Create(this, false);
return true;
}
bool NetworkServer::PrivateData::Start()
{
//Start listener
((Listener*)listener)->Start();
started = true;
thread.Start();
return true;
}
bool NetworkServer::PrivateData::Stop()
{
if(listener)
{
((Listener*)listener)->Stop();
}
started = false;
thread.Stop();
return true;
}
bool NetworkServer::PrivateData::Shutdown()
{
//Stop server main thread
thread.Stop();
if(listener)
{
delete listener;
listener = NULL;
}
if(postBox)
{
delete postBox;
postBox = NULL;
}
started = false;
return true;
}
//Checks for new clients and sends them to the proc function.
void NetworkServer::PrivateData::CheckForNewClient()
{
if(postBox->IsFull())
{
int clientSocketNum;
postBox->FetchMessage(clientSocketNum);
//Safety check that is probably not needed.
if(clientSocketNum == -1)
{
return;
}
//Create client and Proc function if the pointer is not NULL
if(initDesc.proc)
{
Oyster::Network::NetworkClient* client = new Oyster::Network::NetworkClient();
initDesc.proc((NetworkClient*)client);
}
}
}
bool NetworkServer::PrivateData::DoWork()
{
CheckForNewClient();
return true;
}
/*************************************
NetworkServer
*************************************/
NetworkServer::NetworkServer()
{
privateData = new PrivateData();
}
NetworkServer::~NetworkServer()
{
if(privateData)
{
delete privateData;
}
}
bool NetworkServer::Init(INIT_DESC& initDesc)
{
privateData->Init(initDesc);
return true;
}
bool NetworkServer::Start()
{
privateData->Start();
return true;
}
bool NetworkServer::Stop()
{
privateData->Stop();
return true;
}
bool NetworkServer::Shutdown()
{
privateData->Shutdown();
return true;
}
bool NetworkServer::IsStarted() const
{
return privateData->started;
}

View File

@ -0,0 +1,56 @@
#ifndef NETWORK_API_NETWORK_SERVER_H
#define NETWORK_API_NETWORK_SERVER_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#ifdef CUSTOM_NET_PROTOCOL_EXPORT
#define NET_PROTOCOL_EXPORT __declspec(dllexport)
#else
#define NET_PROTOCOL_EXPORT __declspec(dllimport)
#endif
#pragma comment(lib, "ws2_32.lib")
//#include "NetworkClient.h"
namespace Oyster
{
namespace Network
{
namespace Server
{
extern "C"
{
class NET_PROTOCOL_EXPORT NetworkServer
{
public:
class NetworkClient;
struct INIT_DESC
{
unsigned short port; //Port the server should be accepting clients on.
void (*proc)(NetworkClient*);
};
NetworkServer();
virtual ~NetworkServer();
virtual bool Init(INIT_DESC& initDesc);
virtual bool Start();
virtual bool Stop();
virtual bool Shutdown();
virtual bool IsStarted() const;
private:
struct PrivateData;
PrivateData* privateData;
};
}
}
}
}
#endif

View File

@ -7,6 +7,20 @@
using namespace Oyster::Network;
Connection::Connection()
{
this->socket = 0;
bool stillSending = false;
bool closed = true;
}
Connection::Connection(int socket)
{
this->socket = socket;
bool stillSending = false;
bool closed = true;
}
Connection::~Connection()
{
closesocket( this->socket );
@ -30,6 +44,9 @@ int Connection::Connect(unsigned short port , const char serverName[])
return WSAGetLastError();
}
closed = false;
stillSending = true;
//connection succesfull!
return 0;
}
@ -63,6 +80,9 @@ int Connection::InitiateServer(unsigned short port)
return errorCode;
}
closed = false;
stillSending = true;
//Server started!
return 0;
}
@ -79,11 +99,11 @@ int Connection::Disconnect()
return WSAGetLastError();
}
int Connection::Send(OysterByte& bytes)
int Connection::Send(Utility::DynamicMemory::SmartPointer<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();
@ -92,34 +112,32 @@ int Connection::Send(OysterByte& bytes)
return 0;
}
int Connection::Recieve(OysterByte& bytes)
int Connection::Recieve(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
{
int nBytes;
bytes.Clear(1000);
nBytes = recv(this->socket, bytes, 500, 0);
bytes->Resize(1000);
nBytes = recv(this->socket, *bytes , 500, 0);
if(nBytes == SOCKET_ERROR)
{
return WSAGetLastError();
}
else
{
bytes.SetSize(nBytes);
bytes->SetSize(nBytes);
}
std::cout << "Size of the recieved data: " << nBytes << " bytes" << std::endl;
//bytes.byteArray[nBytes] = '\0';
return 0;
}
int Connection::Listen()
{
int clientSocket;
if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
if((clientSocket = (int)accept(this->socket, NULL, NULL)) == INVALID_SOCKET)
{
return WSAGetLastError();
return (int)INVALID_SOCKET;//WSAGetLastError();
}
return clientSocket;
@ -130,7 +148,7 @@ int Connection::Listen()
///////////////////////////////////////
int Connection::InitiateSocket()
{
this->socket = ::socket(AF_INET, SOCK_STREAM, 0);
this->socket = (int)::socket(AF_INET, SOCK_STREAM, 0);
if(this->socket == SOCKET_ERROR)
{
return WSAGetLastError();
@ -139,15 +157,24 @@ int Connection::InitiateSocket()
return 0;
}
void Connection::SetBlockingMode(bool blocking)
int Connection::SetBlockingMode(bool blocking)
{
//TODO: Implement this function. Setting the socket to blocking or non-blocking.
DWORD nonBlocking;
if(blocking)
{
//fcntl(this->socket, F_SETFL, O_NONBLOCK);
nonBlocking = 0;
}
else
{
nonBlocking = 1;
}
int result = ioctlsocket(this->socket, FIONBIO, &nonBlocking);
if(result != 0)
{
return WSAGetLastError();
}
return 0;
}

View File

@ -16,28 +16,32 @@ namespace Oyster
{
public:
Connection() { this->socket = 0; };
Connection( int socket ) { this->socket = socket; };
Connection();
Connection( int socket );
virtual ~Connection();
virtual int InitiateServer( unsigned short port );
virtual int InitiateClient();
virtual int Send( OysterByte& bytes );
virtual int Recieve( OysterByte& bytes );
virtual int Send( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
virtual int Recieve( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
virtual int Disconnect();
virtual int Connect( unsigned short port , const char serverName[] );
virtual int Listen();
//Setting the socket to blocking/non-blocking mode.
int SetBlockingMode( bool blocking );
private:
int InitiateSocket();
void SetBlockingMode( bool blocking );
int socket;
bool stillSending;
bool closed;
};
}
}

View File

@ -5,6 +5,8 @@
// Created by Sam Svensson 2013 //
//////////////////////////////////
#include "../../Misc/Utilities.h"
namespace Oyster
{
namespace Network
@ -17,8 +19,8 @@ namespace Oyster
//sends and recieve functions with bytearrays,
//will send to the users connection via socket
virtual int Send( OysterByte& bytes ) = 0;
virtual int Recieve( OysterByte& bytes) = 0;
virtual int Send( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes ) = 0;
virtual int Recieve( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes) = 0;
//initiates sockets and address for server and client
virtual int InitiateServer( unsigned short port ) { return false; };

View File

@ -14,6 +14,7 @@ namespace Oyster
class IListener
{
public:
virtual ~IListener() {}
virtual bool Init(unsigned int port) = 0;
virtual int Accept() = 0;

View File

@ -15,7 +15,7 @@ namespace Oyster
public:
virtual ~IPostBox() {}
virtual void PostMessage(T& message) = 0;
virtual void FetchMessage(T& message) = 0;
virtual bool FetchMessage(T& message) = 0;
virtual bool IsFull() = 0;
};

View File

@ -5,6 +5,8 @@
// Created by Sam Svensson 2013 //
//////////////////////////////////
#include "../../Misc/Utilities.h"
namespace Oyster
{
namespace Network
@ -15,8 +17,8 @@ namespace Oyster
public:
//packs and unpacks packages for sending or recieving over the connection
virtual void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes) = 0;
virtual void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ) = 0;
virtual void Pack (Protocols::ProtocolHeader &header, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes) = 0;
virtual void Unpack (Protocols::ProtocolSet* set, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes ) = 0;
};
}

View File

@ -1,12 +1,19 @@
#include "Listener.h"
using namespace Oyster::Network::Server;
using namespace Utility::DynamicMemory;
Listener::Listener()
{
connection = NULL;
}
Listener::Listener(Oyster::Network::IPostBox<int>* postBox)
{
connection = NULL;
this->postBox = postBox;
}
Listener::~Listener()
{
if(connection)
@ -15,10 +22,10 @@ Listener::~Listener()
}
}
//Starts the thread immediate
bool Listener::Init(unsigned int port)
{
connection = new Connection();
connection->InitiateServer(port);
thread.Create(this, true);
@ -26,26 +33,53 @@ bool Listener::Init(unsigned int port)
return true;
}
bool Listener::Init(unsigned int port, bool start)
{
connection = new Connection();
connection->InitiateServer(port);
thread.Create(this, start);
return true;
}
void Listener::Start()
{
thread.Start();
}
void Listener::Stop()
{
thread.Stop();
}
void Listener::Shutdown()
{
thread.Terminate();
thread.Stop();
}
void Listener::SetPostBox(Oyster::Network::IPostBox<int>* postBox)
{
mutex.LockMutex();
stdMutex.lock();
//mutex.LockMutex();
this->postBox = postBox;
mutex.UnlockMutex();
//mutex.UnlockMutex();
stdMutex.unlock();
}
int Listener::Accept()
{
int clientSocket = 0;
int clientSocket = -1;
clientSocket = connection->Listen();
mutex.LockMutex();
postBox->PostMessage(clientSocket);
mutex.UnlockMutex();
if(clientSocket != -1)
{
stdMutex.lock();
//mutex.LockMutex();
postBox->PostMessage(clientSocket);
//mutex.UnlockMutex();
stdMutex.unlock();
}
return clientSocket;
}
@ -57,13 +91,10 @@ bool Listener::DoWork()
return true;
}
#include <iostream>
void Listener::ThreadEntry()
{
std::cout << "Thread started" << std::endl;
}
void Listener::ThreadExit()
{
std::cout << "Thread stopped" << std::endl;
}

View File

@ -6,10 +6,11 @@
/////////////////////////////////////
#include "IListener.h"
#include "../NetworkDependencies/Connection.h"
#include "Connection.h"
#include "IPostBox.h"
#include "../../Misc/Thread/OysterThread.h"
#include "../../Misc/Thread/OysterMutex.h"
#include "IPostBox.h"
#include "../../Misc/Utilities.h"
namespace Oyster
{
@ -17,32 +18,37 @@ namespace Oyster
{
namespace Server
{
class Listener : public ::Oyster::Thread::IThreadObject
class Listener : public IListener, public ::Oyster::Thread::IThreadObject
{
public:
Listener();
Listener(Oyster::Network::IPostBox<int>* postBox);
~Listener();
bool Init(unsigned int port);
bool Init(unsigned int port, bool start);
void Start();
void Stop();
void Shutdown();
void SetPostBox(IPostBox<int>* postBox);
private:
//Thread functions
bool DoWork();
void ThreadEntry();
void ThreadExit();
private:
//Function that runs in the thread.
int Accept();
private:
::Oyster::Network::Connection* connection;
::Oyster::Thread::OysterThread thread;
OysterMutex mutex;
std::mutex stdMutex;
IPostBox<int>* postBox;

View File

@ -111,7 +111,6 @@ void MessageHeader::PackFloat(float i, OysterByte& bytes)
void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes)
{
bytes.AddSize(4);
//Pack number of elements
PackUnsignedInt(elementCount, bytes);
@ -131,7 +130,7 @@ void MessageHeader::PackDouble(double i, OysterByte& bytes)
void MessageHeader::PackStr(char str[], OysterByte& bytes)
{
int totalSize = 2 + strlen(str);
int totalSize = 2 + (int)strlen(str);
bytes.AddSize(totalSize);
Packing::Pack(&bytes.GetByteArray()[size], str);
size += totalSize;
@ -139,7 +138,7 @@ void MessageHeader::PackStr(char str[], OysterByte& bytes)
void MessageHeader::PackStr(std::string str, OysterByte& bytes)
{
int totalSize = 2 + str.length();
int totalSize = 2 + (int)str.length();
bytes.AddSize(totalSize);
Packing::Pack(&bytes.GetByteArray()[size], str);
size += totalSize;
@ -244,7 +243,7 @@ double MessageHeader::UnpackDouble(OysterByte& bytes)
std::string MessageHeader::UnpackStr(OysterByte& bytes)
{
std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]);
size += 2 + str.length();
size += 2 + (int)str.length();
return str;
}

View File

@ -47,6 +47,7 @@ namespace Oyster
void PackStr(char str[], OysterByte& bytes);
void PackStr(std::string str, OysterByte& bytes);
//Maybe
//TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc.
@ -71,6 +72,7 @@ namespace Oyster
std::string UnpackStr(OysterByte& bytes);
//Maybe
//TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc.

View File

@ -0,0 +1,30 @@
#include "MessagePlayerPos.h"
using namespace Oyster::Network;
using namespace Oyster::Network::Messages;
using namespace Oyster::Network::Protocols;
MessagePlayerPos::MessagePlayerPos()
{
}
MessagePlayerPos::~MessagePlayerPos()
{
}
void MessagePlayerPos::Pack(Protocols::ProtocolHeader& header, OysterByte& bytes)
{
MessageHeader::Pack(header, bytes);
PackInt(static_cast<ProtocolPlayerPos*>(&header)->ID, bytes);
PackFloat(static_cast<ProtocolPlayerPos*>(&header)->matrix, static_cast<ProtocolPlayerPos*>(&header)->nrOfFloats, bytes);
SetSize(bytes);
}
void MessagePlayerPos::Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header)
{
MessageHeader::Unpack(bytes, header);
static_cast<ProtocolPlayerPos*>(&header)->ID = UnpackInt(bytes);
static_cast<ProtocolPlayerPos*>(&header)->matrix = UnpackFloat(static_cast<ProtocolPlayerPos*>(&header)->nrOfFloats, bytes);
}

View File

@ -0,0 +1,34 @@
#ifndef NETWORK_DEPENDENCIES_MESSAGE_PLAYER_POS_H
#define NETWORK_DEPENDENCIES_MESSAGE_PLAYER_POS_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "MessageHeader.h"
namespace Oyster
{
namespace Network
{
namespace Messages
{
class MessagePlayerPos : public MessageHeader
{
public:
MessagePlayerPos();
virtual ~MessagePlayerPos();
virtual void Pack(Protocols::ProtocolHeader& header, OysterByte& bytes);
virtual void Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header);
private:
};
}
}
}
#endif

View File

@ -7,5 +7,6 @@
#include "MessageHeader.h"
#include "MessageTest.h"
#include "MessagePlayerPos.h"
#endif

View File

@ -153,11 +153,12 @@
<ItemGroup>
<ClCompile Include="Connection.cpp" />
<ClCompile Include="Listener.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Messages\MessageHeader.cpp" />
<ClCompile Include="Messages\MessagePlayerPos.cpp" />
<ClCompile Include="Messages\MessageTest.cpp" />
<ClCompile Include="OysterByte.cpp" />
<ClCompile Include="Packing.cpp" />
<ClCompile Include="ThreadedClient.cpp" />
<ClCompile Include="Translator.cpp" />
<ClCompile Include="WinsockFunctions.cpp" />
</ItemGroup>
@ -168,6 +169,7 @@
<ClInclude Include="IPostBox.h" />
<ClInclude Include="Listener.h" />
<ClInclude Include="Messages\MessageHeader.h" />
<ClInclude Include="Messages\MessagePlayerPos.h" />
<ClInclude Include="Messages\MessagesInclude.h" />
<ClInclude Include="Messages\MessageTest.h" />
<ClInclude Include="OysterByte.h" />
@ -175,6 +177,7 @@
<ClInclude Include="ITranslate.h" />
<ClInclude Include="PostBox.h" />
<ClInclude Include="Protocols.h" />
<ClInclude Include="ThreadedClient.h" />
<ClInclude Include="Translator.h" />
<ClInclude Include="WinsockFunctions.h" />
</ItemGroup>

View File

@ -2,30 +2,33 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="Connection.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Messages\MessageHeader.cpp" />
<ClCompile Include="Messages\MessageTest.cpp" />
<ClCompile Include="Packing.cpp" />
<ClCompile Include="Translator.cpp" />
<ClCompile Include="Listener.cpp" />
<ClCompile Include="WinsockFunctions.cpp" />
<ClCompile Include="Messages\MessageHeader.cpp" />
<ClCompile Include="Messages\MessagePlayerPos.cpp" />
<ClCompile Include="Messages\MessageTest.cpp" />
<ClCompile Include="OysterByte.cpp" />
<ClCompile Include="Packing.cpp" />
<ClCompile Include="ThreadedClient.cpp" />
<ClCompile Include="Translator.cpp" />
<ClCompile Include="WinsockFunctions.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Connection.h" />
<ClInclude Include="IConnection.h" />
<ClInclude Include="IListener.h" />
<ClInclude Include="IPostBox.h" />
<ClInclude Include="Listener.h" />
<ClInclude Include="Messages\MessageHeader.h" />
<ClInclude Include="Messages\MessagePlayerPos.h" />
<ClInclude Include="Messages\MessagesInclude.h" />
<ClInclude Include="Messages\MessageTest.h" />
<ClInclude Include="OysterByte.h" />
<ClInclude Include="Packing.h" />
<ClInclude Include="ITranslate.h" />
<ClInclude Include="Protocols.h" />
<ClInclude Include="Translator.h" />
<ClInclude Include="Messages\MessagesInclude.h" />
<ClInclude Include="Listener.h" />
<ClInclude Include="IListener.h" />
<ClInclude Include="WinsockFunctions.h" />
<ClInclude Include="OysterByte.h" />
<ClInclude Include="IPostBox.h" />
<ClInclude Include="PostBox.h" />
<ClInclude Include="Protocols.h" />
<ClInclude Include="ThreadedClient.h" />
<ClInclude Include="Translator.h" />
<ClInclude Include="WinsockFunctions.h" />
</ItemGroup>
</Project>

View File

@ -16,18 +16,38 @@ OysterByte::OysterByte(int cap)
byteArray = new unsigned char[capacity];
}
OysterByte::OysterByte(const OysterByte& obj)
{
//delete[] this->byteArray;
this->byteArray = new unsigned char[obj.capacity];
for(int i = 0; i < (int)obj.size; i++)
{
this->byteArray[i] = obj.byteArray[i];
}
this->size = obj.size;
this->capacity = obj.capacity;
}
OysterByte::~OysterByte()
{
delete[] byteArray;
}
void OysterByte::Clear(unsigned int cap)
void OysterByte::Clear()
{
delete[] byteArray;
byteArray = new unsigned char[cap];
size = 0;
}
void OysterByte::Resize(unsigned int cap)
{
if(capacity < cap)
{
delete[] byteArray;
byteArray = new unsigned char[cap];
}
}
int OysterByte::GetSize()
{
return size;
@ -60,6 +80,21 @@ void OysterByte::SetSize(unsigned int size)
this->size = size;
}
OysterByte& OysterByte::operator =(const OysterByte& obj)
{
delete[] this->byteArray;
this->byteArray = new unsigned char[obj.capacity];
for(int i = 0; i < (int)obj.size; i++)
{
this->byteArray[i] = obj.byteArray[i];
}
this->size = obj.size;
this->capacity = obj.capacity;
return *this;
}
OysterByte::operator char*()
{
return (char*)byteArray;

View File

@ -16,9 +16,11 @@ namespace Oyster
public:
OysterByte();
OysterByte(int cap);
OysterByte(const OysterByte& obj);
virtual ~OysterByte();
void Clear(unsigned int cap);
void Clear(); //Resets size to 0
void Resize(unsigned int cap); //Resizes the array with, it does not keep anything in it.
int GetSize();
unsigned char* GetByteArray();
@ -27,6 +29,8 @@ namespace Oyster
void SetBytes(unsigned char* bytes);
void SetSize(unsigned int size); //Only sets the private variable 'size'
OysterByte& operator =(const OysterByte& obj);
operator char*();
operator const char*();
operator unsigned char*();

View File

@ -87,7 +87,7 @@ namespace Oyster
//floating point (32, 64-bit)
void Pack(unsigned char buffer[], float i)
{
int tempFloat = Pack754(i, 32, 8);
int tempFloat = (int)Pack754(i, 32, 8);
Pack(buffer, tempFloat);
}
@ -100,7 +100,7 @@ namespace Oyster
//string
void Pack(unsigned char buffer[], char str[])
{
short len = strlen(str);
short len = (short)strlen(str);
Pack(buffer, len);
buffer += 2;
memcpy(buffer, str, len);
@ -108,7 +108,7 @@ namespace Oyster
void Pack(unsigned char buffer[], std::string& str)
{
short len = str.length();
short len = (short)str.length();
Pack(buffer, len);
buffer += 2;
memcpy(buffer, str.c_str(), len);
@ -153,7 +153,7 @@ namespace Oyster
fnorm = fnorm - 1.0;
// calculate the binary form (non-float) of the significand data
significand = fnorm * ((1LL << significandbits) + 0.5f);
significand = (long long)(fnorm * ((1LL << significandbits) + 0.5f));
// get the biased exponent
exp = shift + ((1 << (expbits - 1)) - 1); // shift + bias
@ -169,7 +169,7 @@ namespace Oyster
//bool (1-bit)
bool Unpackb(unsigned char buffer[])
{
return (bool)buffer;
return buffer;
}
//char (8-bit)
@ -305,7 +305,7 @@ namespace Oyster
return 0.0;
// pull the significand
result = (i&((1LL << significandbits) - 1)); // mask
result = (long double)(i&((1LL << significandbits) - 1)); // mask
result /= (1LL << significandbits); // convert back to float
result += 1.0f; // add the one back on

View File

@ -23,7 +23,7 @@ namespace Oyster
virtual ~PostBox();
virtual void PostMessage(T& message);
virtual void FetchMessage(T& message);
virtual bool FetchMessage(T& message);
virtual bool IsFull();
private:
@ -49,13 +49,14 @@ namespace Oyster
}
template <class T>
void PostBox<T>::FetchMessage(T& message)
bool PostBox<T>::FetchMessage(T& message)
{
if(IsFull())
{
message = messages.Front();
messages.Pop();
message = messages.Pop();
return true;
}
return false;
}
template <class T>

View File

@ -22,7 +22,7 @@ namespace Oyster
PackageType_header,
PackageType_test,
PackageType_input,
PackageType_update_position
PackageType_player_pos,
};
struct ProtocolHeader
@ -45,6 +45,16 @@ namespace Oyster
virtual ~ProtocolTest() { delete[] f; }
};
struct ProtocolPlayerPos : public ProtocolHeader
{
int ID;
unsigned int nrOfFloats;
float *matrix;
ProtocolPlayerPos() { this->packageType = PackageType_player_pos; }
virtual ~ProtocolPlayerPos() { delete[] matrix; }
};
//Holding every protocol in an union.
//Used because we now don't have to type case our protocol when we recieve them.
@ -56,6 +66,7 @@ namespace Oyster
{
ProtocolHeader* pHeader;
ProtocolTest *pTest;
ProtocolPlayerPos *pPlayerPos;
}Protocol;
@ -75,6 +86,12 @@ namespace Oyster
delete Protocol.pTest;
}
break;
case PackageType_player_pos:
if(Protocol.pPlayerPos)
{
delete Protocol.pPlayerPos;
}
break;
}
}
};

View File

@ -0,0 +1,143 @@
#include "ThreadedClient.h"
#include "OysterByte.h"
#include <iostream>
using namespace Oyster::Network;
using namespace Oyster::Thread;
using namespace Utility::DynamicMemory;
ThreadedClient::ThreadedClient()
{
this->connection = new Connection();
this->sendPostBox = new PostBox<SmartPointer<OysterByte>>();
this->recvPostBox = NULL;
connection->SetBlockingMode(false);
}
ThreadedClient::ThreadedClient(unsigned int socket)
{
this->connection = new Connection(socket);
this->sendPostBox = new PostBox<SmartPointer<OysterByte>>();
this->recvPostBox = NULL;
connection->SetBlockingMode(false);
thread.Create(this, true);
}
ThreadedClient::ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>>* postBox, unsigned int socket)
{
this->connection = new Connection(socket);
this->sendPostBox = new PostBox<SmartPointer<OysterByte>>;
this->recvPostBox = postBox;
connection->SetBlockingMode(false);
thread.Create(this, true);
}
ThreadedClient::~ThreadedClient()
{
thread.Terminate();
delete this->connection;
this->connection = NULL;
this->recvPostBox = NULL;
if(sendPostBox != NULL)
{
delete sendPostBox;
this->sendPostBox = NULL;
}
}
void ThreadedClient::Send(SmartPointer<OysterByte>& byte)
{
this->sendPostBox->PostMessage(byte);
}
int ThreadedClient::Send()
{
int errorCode = 0;
if(sendPostBox->IsFull())
{
SmartPointer<OysterByte> temp = new OysterByte;
sendPostBox->FetchMessage(temp);
errorCode = this->connection->Send(temp);
}
return errorCode;
}
int ThreadedClient::Recv()
{
int errorCode = -1;
SmartPointer<OysterByte> temp = new OysterByte;
errorCode = this->connection->Recieve(temp);
if(errorCode == 0)
{
stdMutex.lock();
recvPostBox->PostMessage(temp);
stdMutex.unlock();
}
return errorCode;
}
void ThreadedClient::ThreadEntry()
{
std::cout<< "Client Thread started" << std::endl;
}
void ThreadedClient::ThreadExit()
{
std::cout << "Client Thread exit" << std::endl;
}
#include <Windows.h>
bool ThreadedClient::DoWork()
{
int errorCode;
errorCode = Send();
/*if(errorCode != 0)
{
return false;
}*/
errorCode = Recv();
/*if(errorCode != 0)
{
return false;
}*/
Sleep(1);
return true;
}
int ThreadedClient::Connect(unsigned short port, const char serverName[])
{
int errorCode;
if((errorCode = connection->InitiateClient()) != 0)
{
return errorCode;
}
else if((errorCode = connection->Connect(port, serverName)) != 0)
{
return errorCode;
}
thread.Create(this, true);
return 0;
}
void ThreadedClient::setRecvPostBox(IPostBox<SmartPointer<OysterByte>> *postBox)
{
stdMutex.lock();
this->recvPostBox = postBox;
stdMutex.unlock();
}

View File

@ -0,0 +1,55 @@
#ifndef NETWORK_DEPENDENCIES_THREADED_CLIENT_H
#define NETWORK_DEPENDENCIES_THREADED_CLIENT_H
//////////////////////////////////
// Created by Sam Svensson 2013 //
//////////////////////////////////
#include "../../Misc/Thread/IThreadObject.h"
#include "PostBox.h"
#include "Connection.h"
#include "../../Misc/Thread/OysterThread.h"
#include "../../Misc/Thread/OysterMutex.h"
#include "../../Misc/Utilities.h"
#include <mutex>
namespace Oyster
{
namespace Network
{
class OysterByte;
class ThreadedClient : public Thread::IThreadObject
{
public:
ThreadedClient();
ThreadedClient(unsigned int socket);
ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *postBox, unsigned int socket);
virtual ~ThreadedClient();
void Send(Utility::DynamicMemory::SmartPointer<OysterByte>& byte);
int Connect(unsigned short port, const char serverName[]);
void setRecvPostBox(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *postBox);
private:
virtual int Send();
virtual int Recv();
virtual void ThreadEntry();
virtual void ThreadExit();
virtual bool DoWork();
private:
Connection* connection;
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *sendPostBox;
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *recvPostBox;
Oyster::Thread::OysterThread thread;
std::mutex stdMutex;
};
}
}
#endif

View File

@ -4,7 +4,7 @@ using namespace Oyster::Network;
using namespace ::Protocols;
using namespace ::Messages;
void Translator::Pack( ProtocolHeader &header, OysterByte& bytes )
void Translator::Pack( ProtocolHeader &header, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes )
{
MessageHeader *message = NULL;
@ -17,23 +17,27 @@ void Translator::Pack( ProtocolHeader &header, OysterByte& bytes )
case PackageType_test:
message = new MessageTest();
break;
case PackageType_player_pos:
message = new MessagePlayerPos();
break;
}
if(message != NULL)
{
message->Pack(header, bytes);
message->Pack(header, *bytes);
delete message;
message = NULL;
}
}
void Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
void Translator::Unpack(ProtocolSet* set, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
{
ProtocolHeader *header = new ProtocolHeader();
MessageHeader *message = new MessageHeader();
message->Unpack(bytes, *header);
message->Unpack(*bytes, *header);
delete message;
message = NULL;
@ -44,13 +48,19 @@ void Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
case PackageType_header:
message = new MessageHeader();
set->Protocol.pHeader = new ProtocolHeader;
message->Unpack(bytes, *set->Protocol.pHeader);
message->Unpack(*bytes, *set->Protocol.pHeader);
break;
case PackageType_test:
message = new MessageTest();
set->Protocol.pTest = new ProtocolTest;
message->Unpack(bytes, *set->Protocol.pTest);
message->Unpack(*bytes, *set->Protocol.pTest);
break;
case PackageType_player_pos:
message = new MessagePlayerPos();
set->Protocol.pPlayerPos = new ProtocolPlayerPos;
message->Unpack(*bytes, *set->Protocol.pPlayerPos);
break;
}

View File

@ -20,8 +20,8 @@ namespace Oyster
Translator () { };
~Translator() { };
void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes );
void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes );
void Pack (Protocols::ProtocolHeader &header, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
void Unpack (Protocols::ProtocolSet* set, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
private:

View File

@ -1,41 +0,0 @@
#include "Client.h"
using namespace Oyster::Network::Client;
Client::Client()
{
connection = new Connection();
}
Client::~Client()
{
delete this->connection;
connection = 0;
}
int Client::Connect(unsigned int port, char filename[])
{
int errorCode;
if((errorCode = connection->InitiateClient()) != 0)
{
return errorCode;
}
if((errorCode = connection->Connect(port, filename)) != 0)
{
return errorCode;
}
return 0;
}
void Client::Send(Oyster::Network::OysterByte& bytes)
{
connection->Send(bytes);
}
void Client::Recv(Oyster::Network::OysterByte& bytes)
{
connection->Recieve(bytes);
}

View File

@ -1,35 +0,0 @@
#ifndef NETWORK_CLIENT_CLIENT_H
#define NETWORK_CLIENT_CLIENT_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "../NetworkDependencies/Connection.h"
#include "../NetworkDependencies/OysterByte.h"
namespace Oyster
{
namespace Network
{
namespace Client
{
class Client
{
public:
Client();
~Client();
int Connect(unsigned int port, char filename[]);
void Send(OysterByte& bytes);
void Recv(OysterByte& bytes);
private:
::Oyster::Network::Connection* connection;
};
}
}
}
#endif

View File

@ -6,15 +6,20 @@
#include "..\NetworkDependencies\Protocols.h"
#include "../NetworkDependencies/OysterByte.h"
#include "../../Misc/ThreadSafeQueue.h"
#include "Client.h"
#include "../NetworkDependencies/ThreadedClient.h"
#include "../../Misc/WinTimer.h"
#include "../../Misc/Utilities.h"
#pragma comment(lib, "ws2_32.lib")
using namespace std;
using namespace Oyster::Network::Protocols;
using namespace Oyster::Network::Client;
using namespace Oyster::Network;
using namespace Utility;
using namespace Utility::DynamicMemory;
void chat(Client &client);
void chat(ThreadedClient &client);
void PrintOutMessage(ProtocolSet* set);
int main()
{
@ -27,10 +32,10 @@ int main()
cout << "Client" << endl;
//Create Client
Client client;
ThreadedClient* client = new ThreadedClient;
//Connect to server
errorCode = client.Connect(9876, "localhost");
errorCode = client->Connect(9876, "localhost");
if(errorCode != 0)
{
@ -38,7 +43,8 @@ int main()
wcout << "errorMessage: " << errorTest << endl;
}
chat(client);
chat(*client);
delete client;
ShutdownWinSock();
@ -46,76 +52,79 @@ int main()
return 0;
}
void chat(Client &client)
void chat(ThreadedClient &client)
{
Oyster::Network::Translator *t = new Oyster::Network::Translator();
IPostBox< SmartPointer<OysterByte >> *postBox = new PostBox< SmartPointer<OysterByte >>;
Oyster::Network::OysterByte msgRecv;
string msgSend = "";
client.setRecvPostBox(postBox);
SmartPointer<OysterByte> msgRecv = new OysterByte();
SmartPointer<OysterByte> msgSend = new OysterByte();
ProtocolSet* set = new ProtocolSet;
ProtocolTest test;
test.numOfFloats = 5;
test.f = new float[test.numOfFloats];
float temp = 12345.5654f;
for(int i = 0; i < 5; i++)
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.f[i] = temp;
test.matrix[i] = temp;
temp++;
}
t->Pack(test, msgSend);
bool chatDone = false;
WinTimer timer;
while(!chatDone)
while(1)
{
client.Recv(msgRecv);
t->Unpack(set, msgRecv);
switch(set->type)
//Fetch new messages from the postbox
if(postBox->FetchMessage(msgRecv))
{
case PackageType_header:
break;
case PackageType_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] << ' ' ;
}
cout << endl;
break;
t->Unpack(set, msgRecv);
//PrintOutMessage(set);
set->Release();
}
set->Release();
msgRecv.Clear(1000);
/*std::getline(std::cin, msgSend);
if( msgSend != "exit")
//Send message to server each second
if(timer.getElapsedSeconds() > 1)
{
if(msgSend.length() < 1)
{
msgSend = "ERROR!";
}
test.textMessage = msgSend;
t->Pack(test, msgRecv);
client.Send(msgRecv);
cout << "Sending to server." << endl;
timer.reset();
client.Send(msgSend);
}
else
{
chatDone = true;
}
cin.clear();*/
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

@ -154,12 +154,8 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Client.cpp" />
<ClCompile Include="ClientMain.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

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

View File

@ -1,24 +0,0 @@
#include "Client.h"
using namespace Oyster::Network;
using namespace Oyster::Network::Server;
Client::Client(unsigned int socket)
{
connection = new Connection(socket);
}
Client::~Client()
{
delete connection;
}
void Client::Send(OysterByte& bytes)
{
connection->Send(bytes);
}
void Client::Recv(OysterByte& bytes)
{
connection->Recieve(bytes);
}

View File

@ -1,34 +0,0 @@
#ifndef NETWORK_SERVER_CLIENT_H
#define NETWORK_SERVER_CLIENT_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "../NetworkDependencies/Connection.h"
#include "../NetworkDependencies/OysterByte.h"
namespace Oyster
{
namespace Network
{
namespace Server
{
class Client
{
public:
Client(unsigned int socket);
~Client();
void Send(OysterByte& bytes);
void Recv(OysterByte& bytes);
private:
::Oyster::Network::Connection* connection;
};
}
}
};
#endif

View File

@ -149,16 +149,20 @@
<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>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Client.cpp" />
<ClCompile Include="ServerMain.cpp" />
<ClCompile Include="TestClass.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h" />
<ClInclude Include="RecieverObject.h" />
<ClInclude Include="TestClass.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -18,12 +18,15 @@
<ClCompile Include="ServerMain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Client.cpp">
<ClCompile Include="TestClass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h">
<ClInclude Include="RecieverObject.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="TestClass.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>

View File

@ -0,0 +1,19 @@
#ifndef OYSTER_NETWORK_SERVER_RECIEVER_OBJECT_H
#define OYSTER_NETWORK_SERVER_RECIEVER_OBJECT_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "../NetworkDependencies/Protocols.h"
#include "../NetworkDependencies/OysterByte.h"
#include "../../Misc/Utilities.h"
class RecieverObject
{
public:
virtual void ProcFunc(Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> msg) = 0;
};
#endif

View File

@ -3,126 +3,53 @@
#include <vector>
#include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/Translator.h"
#include "Client.h"
#include "../NetworkDependencies/OysterByte.h"
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/WinTimer.h"
#pragma comment(lib, "ws2_32.lib")
#include "TestClass.h"
using namespace std;
using namespace Oyster::Network::Server;
using namespace Oyster::Network;
using namespace ::Protocols;
using namespace Utility;
void clientProc(Oyster::Network::NetworkClient* client);
int main()
{
OysterByte recvBuffer;
IPostBox<int>* postBox = new PostBox<int>();
cout << "Server" << endl;
Translator t;
int errorCode;
if(!InitWinSock())
{
cout << "errorMessage: unable to start winsock" << endl;
}
//Create socket
Listener listener;
listener.Init(9876);
listener.SetPostBox(postBox);
Sleep(1000);
Test test;
//Start listening
//Accept a client
ProtocolTest test;
test.clientID = 0;
test.size = 2;
test.textMessage = "hej";
test.numOfFloats = 0;
test.f = new float[test.numOfFloats];
float temp = 395.456f;
for(int i = 0; i < (int)test.numOfFloats; i++)
{
test.f[i] = temp;
temp--;
}
cout << "Server" << endl;
t.Pack(test, recvBuffer);
test.mainLoop();
WinTimer timer;
vector<Client*> clients;
int client = -1;
while(1)
{
client = -1;
postBox->FetchMessage(client);
if(client != -1)
{
cout << "Client connected: " << client << endl;
clients.push_back(new Client(client));
clients.at(clients.size()-1)->Send(recvBuffer);
}
//Send a message every 1 secounds to all clients.
//Fetch new clients from the postbox
/*
//Send a message every 1 seconds to all clients.
if(timer.getElapsedSeconds() > 1)
{
cout << "Sending to " << clients.size() << " clients." << endl;
timer.reset();
for(int i = 0; i < (int)clients.size(); i++)
{
clients.at(i)->Send(recvBuffer);
clients.at(i)->Send(sendBuffer);
}
}
Sleep(100);
}
listener.Shutdown();
}*/
/*
ProtocolSet* set = new ProtocolSet;
client1.Send(recvBuffer);
while(1)
{
client1.Recv(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 < (int)set->Protocol.pTest->numOfFloats; i++)
/*//Fetch messages
if(recvPostBox->FetchMessage(recvBuffer))
{
cout << set->Protocol.pTest->f[i] << ' ';
}
cout << endl;
set->Release();
client2.Send(recvBuffer);
t.Unpack(set, recvBuffer);
client2.Recv(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 < (int)set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ';
}
cout << endl;
set->Release();
client1.Send(recvBuffer);
//PrintOutMessage(set);
set->Release();
}*/
}
Sleep(1000);
ShutdownWinSock();
delete set;
*/
system("pause");
return 0;
}

View File

@ -0,0 +1,86 @@
#include "TestClass.h"
#include "../../Misc/WinTimer.h"
#include <iostream>
using namespace Oyster::Network;
using namespace ::Server;
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;
initDesc.proc = NULL;
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::ProcFunc(Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> msg)
{
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

@ -0,0 +1,37 @@
#ifndef TEST_CLASS_H
#define TEST_CLASS_H
#include "RecieverObject.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 <vector>
class Test : public RecieverObject
{
public:
Test();
~Test();
void mainLoop();
virtual void ProcFunc(Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> msg);
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::Server::NetworkServer server;
};
#endif