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

View File

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

View File

@ -71,32 +71,32 @@
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir> <OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath)</IncludePath> <IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL;</LibraryPath> <LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL;</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir> <OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath)</IncludePath> <IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL;</LibraryPath> <LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL;</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir> <OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath)</IncludePath> <IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL;</LibraryPath> <LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL;</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)..\Bin\Executable\</OutDir> <OutDir>$(SolutionDir)..\Bin\Executable\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath)</IncludePath> <IncludePath>$(SolutionDir)..\External\Include\;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL;</LibraryPath> <LibraryPath>$(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL;</LibraryPath>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
@ -105,13 +105,13 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <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> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<DelayLoadDLLs>DanBiasServer_$(PlatformShortName)D.dll;DanBiasGame_$(PlatformShortName)D.dll;%(DelayLoadDLLs)</DelayLoadDLLs> <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> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -121,13 +121,13 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <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> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<DelayLoadDLLs>DanBiasServer_$(PlatformShortName)D.dll;DanBiasGame_$(PlatformShortName)D.dll;%(DelayLoadDLLs)</DelayLoadDLLs> <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> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -139,7 +139,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <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> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@ -147,7 +147,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<DelayLoadDLLs>DanBiasServer_$(PlatformShortName).dll;DanBiasGame_$(PlatformShortName).dll;%(DelayLoadDLLs)</DelayLoadDLLs> <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> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -159,7 +159,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <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> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@ -167,28 +167,13 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<DelayLoadDLLs>DanBiasServer_$(PlatformShortName).dll;DanBiasGame_$(PlatformShortName).dll;%(DelayLoadDLLs)</DelayLoadDLLs> <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> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Launcher.cpp" /> <ClCompile Include="Launcher.cpp" />
</ItemGroup> </ItemGroup>
<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"> <ProjectReference Include="..\DanBiasGame\DanBiasGame.vcxproj">
<Project>{2a1bc987-af42-4500-802d-89cd32fc1309}</Project> <Project>{2a1bc987-af42-4500-802d-89cd32fc1309}</Project>
</ProjectReference> </ProjectReference>

View File

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

View File

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

View File

@ -216,7 +216,7 @@ namespace Utility
T* operator-> (); T* operator-> ();
operator T* (); operator T* ();
operator bool(); operator bool();
/** /**
* Returns the connected pointer * Returns the connected pointer
*/ */

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,10 +6,11 @@
///////////////////////////////////// /////////////////////////////////////
#include "IListener.h" #include "IListener.h"
#include "../NetworkDependencies/Connection.h" #include "Connection.h"
#include "IPostBox.h"
#include "../../Misc/Thread/OysterThread.h" #include "../../Misc/Thread/OysterThread.h"
#include "../../Misc/Thread/OysterMutex.h" #include "../../Misc/Thread/OysterMutex.h"
#include "IPostBox.h" #include "../../Misc/Utilities.h"
namespace Oyster namespace Oyster
{ {
@ -17,32 +18,37 @@ namespace Oyster
{ {
namespace Server namespace Server
{ {
class Listener : public ::Oyster::Thread::IThreadObject class Listener : public IListener, public ::Oyster::Thread::IThreadObject
{ {
public: public:
Listener(); Listener();
Listener(Oyster::Network::IPostBox<int>* postBox);
~Listener(); ~Listener();
bool Init(unsigned int port); bool Init(unsigned int port);
bool Init(unsigned int port, bool start);
void Start();
void Stop();
void Shutdown(); void Shutdown();
void SetPostBox(IPostBox<int>* postBox); void SetPostBox(IPostBox<int>* postBox);
private:
//Thread functions //Thread functions
bool DoWork(); bool DoWork();
void ThreadEntry(); void ThreadEntry();
void ThreadExit(); void ThreadExit();
private:
//Function that runs in the thread. //Function that runs in the thread.
int Accept(); int Accept();
private: private:
::Oyster::Network::Connection* connection; ::Oyster::Network::Connection* connection;
::Oyster::Thread::OysterThread thread; ::Oyster::Thread::OysterThread thread;
OysterMutex mutex; OysterMutex mutex;
std::mutex stdMutex;
IPostBox<int>* postBox; 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) void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes)
{ {
bytes.AddSize(4);
//Pack number of elements //Pack number of elements
PackUnsignedInt(elementCount, bytes); PackUnsignedInt(elementCount, bytes);
@ -131,7 +130,7 @@ void MessageHeader::PackDouble(double i, OysterByte& bytes)
void MessageHeader::PackStr(char str[], OysterByte& bytes) void MessageHeader::PackStr(char str[], OysterByte& bytes)
{ {
int totalSize = 2 + strlen(str); int totalSize = 2 + (int)strlen(str);
bytes.AddSize(totalSize); bytes.AddSize(totalSize);
Packing::Pack(&bytes.GetByteArray()[size], str); Packing::Pack(&bytes.GetByteArray()[size], str);
size += totalSize; size += totalSize;
@ -139,7 +138,7 @@ void MessageHeader::PackStr(char str[], OysterByte& bytes)
void MessageHeader::PackStr(std::string 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); bytes.AddSize(totalSize);
Packing::Pack(&bytes.GetByteArray()[size], str); Packing::Pack(&bytes.GetByteArray()[size], str);
size += totalSize; size += totalSize;
@ -244,7 +243,7 @@ double MessageHeader::UnpackDouble(OysterByte& bytes)
std::string MessageHeader::UnpackStr(OysterByte& bytes) std::string MessageHeader::UnpackStr(OysterByte& bytes)
{ {
std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]); std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]);
size += 2 + str.length(); size += 2 + (int)str.length();
return str; return str;
} }

View File

@ -47,8 +47,9 @@ namespace Oyster
void PackStr(char str[], OysterByte& bytes); void PackStr(char str[], OysterByte& bytes);
void PackStr(std::string str, OysterByte& bytes); void PackStr(std::string str, OysterByte& bytes);
//Maybe
//TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc. //TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc.
//Unpack variables from message //Unpack variables from message
bool UnpackBool(OysterByte& bytes); bool UnpackBool(OysterByte& bytes);
@ -71,6 +72,7 @@ namespace Oyster
std::string UnpackStr(OysterByte& bytes); std::string UnpackStr(OysterByte& bytes);
//Maybe
//TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc. //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 "MessageHeader.h"
#include "MessageTest.h" #include "MessageTest.h"
#include "MessagePlayerPos.h"
#endif #endif

View File

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

View File

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

View File

@ -16,18 +16,38 @@ OysterByte::OysterByte(int cap)
byteArray = new unsigned char[capacity]; 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() OysterByte::~OysterByte()
{ {
delete[] byteArray; delete[] byteArray;
} }
void OysterByte::Clear(unsigned int cap) void OysterByte::Clear()
{ {
delete[] byteArray;
byteArray = new unsigned char[cap];
size = 0; size = 0;
} }
void OysterByte::Resize(unsigned int cap)
{
if(capacity < cap)
{
delete[] byteArray;
byteArray = new unsigned char[cap];
}
}
int OysterByte::GetSize() int OysterByte::GetSize()
{ {
return size; return size;
@ -60,6 +80,21 @@ void OysterByte::SetSize(unsigned int size)
this->size = 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*() OysterByte::operator char*()
{ {
return (char*)byteArray; return (char*)byteArray;

View File

@ -16,9 +16,11 @@ namespace Oyster
public: public:
OysterByte(); OysterByte();
OysterByte(int cap); OysterByte(int cap);
OysterByte(const OysterByte& obj);
virtual ~OysterByte(); 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(); int GetSize();
unsigned char* GetByteArray(); unsigned char* GetByteArray();
@ -27,6 +29,8 @@ namespace Oyster
void SetBytes(unsigned char* bytes); void SetBytes(unsigned char* bytes);
void SetSize(unsigned int size); //Only sets the private variable 'size' void SetSize(unsigned int size); //Only sets the private variable 'size'
OysterByte& operator =(const OysterByte& obj);
operator char*(); operator char*();
operator const char*(); operator const char*();
operator unsigned char*(); operator unsigned char*();

View File

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

View File

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

View File

@ -22,7 +22,7 @@ namespace Oyster
PackageType_header, PackageType_header,
PackageType_test, PackageType_test,
PackageType_input, PackageType_input,
PackageType_update_position PackageType_player_pos,
}; };
struct ProtocolHeader struct ProtocolHeader
@ -45,6 +45,16 @@ namespace Oyster
virtual ~ProtocolTest() { delete[] f; } 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. //Holding every protocol in an union.
//Used because we now don't have to type case our protocol when we recieve them. //Used because we now don't have to type case our protocol when we recieve them.
@ -56,6 +66,7 @@ namespace Oyster
{ {
ProtocolHeader* pHeader; ProtocolHeader* pHeader;
ProtocolTest *pTest; ProtocolTest *pTest;
ProtocolPlayerPos *pPlayerPos;
}Protocol; }Protocol;
@ -75,6 +86,12 @@ namespace Oyster
delete Protocol.pTest; delete Protocol.pTest;
} }
break; 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 ::Protocols;
using namespace ::Messages; using namespace ::Messages;
void Translator::Pack( ProtocolHeader &header, OysterByte& bytes ) void Translator::Pack( ProtocolHeader &header, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes )
{ {
MessageHeader *message = NULL; MessageHeader *message = NULL;
@ -17,23 +17,27 @@ void Translator::Pack( ProtocolHeader &header, OysterByte& bytes )
case PackageType_test: case PackageType_test:
message = new MessageTest(); message = new MessageTest();
break; break;
case PackageType_player_pos:
message = new MessagePlayerPos();
break;
} }
if(message != NULL) if(message != NULL)
{ {
message->Pack(header, bytes); message->Pack(header, *bytes);
delete message; delete message;
message = NULL; message = NULL;
} }
} }
void Translator::Unpack(ProtocolSet* set, OysterByte& bytes ) void Translator::Unpack(ProtocolSet* set, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
{ {
ProtocolHeader *header = new ProtocolHeader(); ProtocolHeader *header = new ProtocolHeader();
MessageHeader *message = new MessageHeader(); MessageHeader *message = new MessageHeader();
message->Unpack(bytes, *header); message->Unpack(*bytes, *header);
delete message; delete message;
message = NULL; message = NULL;
@ -44,13 +48,19 @@ void Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
case PackageType_header: case PackageType_header:
message = new MessageHeader(); message = new MessageHeader();
set->Protocol.pHeader = new ProtocolHeader; set->Protocol.pHeader = new ProtocolHeader;
message->Unpack(bytes, *set->Protocol.pHeader); message->Unpack(*bytes, *set->Protocol.pHeader);
break; break;
case PackageType_test: case PackageType_test:
message = new MessageTest(); message = new MessageTest();
set->Protocol.pTest = new ProtocolTest; 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; break;
} }

View File

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

View File

@ -24,7 +24,7 @@ std::wstring GetErrorMessage(int errorCode)
(LPWSTR)&lpMessage, (LPWSTR)&lpMessage,
0 , 0 ,
NULL ); NULL );
if(bufLen) if(bufLen)
{ {
retVal = lpMessage; retVal = lpMessage;

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

View File

@ -18,13 +18,5 @@
<ClCompile Include="ClientMain.cpp"> <ClCompile Include="ClientMain.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Client.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </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"> <ProjectReference Include="..\..\Misc\Misc.vcxproj">
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project> <Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\NetworkAPI\NetworkAPI.vcxproj">
<Project>{460d625f-2ac9-4559-b809-0ba89ceaedf4}</Project>
</ProjectReference>
<ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj"> <ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj">
<Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project> <Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Client.cpp" />
<ClCompile Include="ServerMain.cpp" /> <ClCompile Include="ServerMain.cpp" />
<ClCompile Include="TestClass.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Client.h" /> <ClInclude Include="RecieverObject.h" />
<ClInclude Include="TestClass.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -18,12 +18,15 @@
<ClCompile Include="ServerMain.cpp"> <ClCompile Include="ServerMain.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Client.cpp"> <ClCompile Include="TestClass.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Client.h"> <ClInclude Include="RecieverObject.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="TestClass.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </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 <vector>
#include <vld.h> #include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h" #include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkDependencies/Listener.h" #include "TestClass.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")
using namespace std; using namespace std;
using namespace Oyster::Network::Server;
using namespace Oyster::Network; void clientProc(Oyster::Network::NetworkClient* client);
using namespace ::Protocols;
using namespace Utility;
int main() int main()
{ {
OysterByte recvBuffer;
IPostBox<int>* postBox = new PostBox<int>();
cout << "Server" << endl;
Translator t;
int errorCode;
if(!InitWinSock()) if(!InitWinSock())
{ {
cout << "errorMessage: unable to start winsock" << endl; cout << "errorMessage: unable to start winsock" << endl;
} }
//Create socket Test test;
Listener listener;
listener.Init(9876);
listener.SetPostBox(postBox);
Sleep(1000);
//Start listening cout << "Server" << endl;
//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--;
}
t.Pack(test, recvBuffer); test.mainLoop();
WinTimer timer;
vector<Client*> clients;
int client = -1;
while(1) while(1)
{ {
client = -1; //Fetch new clients from the postbox
postBox->FetchMessage(client); /*
if(client != -1) //Send a message every 1 seconds to all clients.
{
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.
if(timer.getElapsedSeconds() > 1) if(timer.getElapsedSeconds() > 1)
{ {
cout << "Sending to " << clients.size() << " clients." << endl; cout << "Sending to " << clients.size() << " clients." << endl;
timer.reset(); timer.reset();
for(int i = 0; i < (int)clients.size(); i++) for(int i = 0; i < (int)clients.size(); i++)
{ {
clients.at(i)->Send(recvBuffer); clients.at(i)->Send(sendBuffer);
} }
} }*/
Sleep(100);
}
listener.Shutdown();
/* /*//Fetch messages
ProtocolSet* set = new ProtocolSet; if(recvPostBox->FetchMessage(recvBuffer))
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++)
{ {
cout << set->Protocol.pTest->f[i] << ' '; t.Unpack(set, recvBuffer);
}
cout << endl;
set->Release();
client2.Send(recvBuffer);
client2.Recv(recvBuffer); //PrintOutMessage(set);
set->Release();
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);
} }
Sleep(1000);
ShutdownWinSock();
delete set;
*/
system("pause"); system("pause");
return 0; 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