Merge branch 'Network' of https://github.com/dean11/Danbias into Network
This commit is contained in:
commit
9d264b0d89
|
@ -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,13 +111,13 @@ 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();
|
||||||
|
|
||||||
Type item = this->front->item;
|
Type item = this->front->item;
|
||||||
Node *destroyer = this->front;
|
Node *destroyer = this->front;
|
||||||
|
@ -132,55 +132,56 @@ namespace Oyster
|
||||||
this->back = NULL;
|
this->back = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
Type ThreadSafeQueue<Type>::Front()
|
Type ThreadSafeQueue<Type>::Front()
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
|
Type temp = this->front->item;
|
||||||
|
stdMutex.unlock();
|
||||||
|
|
||||||
return this->front->item;
|
return temp;
|
||||||
|
|
||||||
mutex.UnlockMutex();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
Type ThreadSafeQueue<Type>::Back()
|
Type ThreadSafeQueue<Type>::Back()
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
|
Type temp = this->back->item;
|
||||||
|
stdMutex.unlock();
|
||||||
|
|
||||||
return this->back->item;
|
return temp;
|
||||||
|
|
||||||
mutex.UnlockMutex();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
int ThreadSafeQueue<Type>::Size()
|
int ThreadSafeQueue<Type>::Size()
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
|
int size = this->nrOfNodes;
|
||||||
|
stdMutex.unlock();
|
||||||
|
|
||||||
return this->nrOfNodes;
|
return size;
|
||||||
|
|
||||||
mutex.UnlockMutex();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
@ -189,7 +190,7 @@ namespace Oyster
|
||||||
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();
|
||||||
|
|
||||||
|
@ -202,7 +203,7 @@ namespace Oyster
|
||||||
{
|
{
|
||||||
queue.Push(this->Pop());
|
queue.Push(this->Pop());
|
||||||
}
|
}
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -201,7 +201,6 @@ namespace Utility
|
||||||
T *_ptr;
|
T *_ptr;
|
||||||
|
|
||||||
/** Destroys the pointer and returns the memory allocated. */
|
/** Destroys the pointer and returns the memory allocated. */
|
||||||
void Destroy();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SmartPointer();
|
SmartPointer();
|
||||||
|
@ -217,6 +216,7 @@ namespace Utility
|
||||||
operator T* ();
|
operator T* ();
|
||||||
operator bool();
|
operator bool();
|
||||||
|
|
||||||
|
void Destroy();
|
||||||
/**
|
/**
|
||||||
* Returns the connected pointer
|
* Returns the connected pointer
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
#ifndef NETWORK_DEPENDENCIES_I_CLIENT_H
|
|
||||||
#define NETWORK_DEPENDENCIES_I_CLIENT_H
|
|
||||||
|
|
||||||
//////////////////////////////////
|
|
||||||
// Created by Sam Svensson 2013 //
|
|
||||||
//////////////////////////////////
|
|
||||||
|
|
||||||
#include "../NetworkDependencies/Connection.h"
|
|
||||||
#include "../NetworkDependencies/OysterByte.h"
|
|
||||||
|
|
||||||
namespace Oyster
|
|
||||||
{
|
|
||||||
namespace Network
|
|
||||||
{
|
|
||||||
class IClient
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
virtual ~IClient() {};
|
|
||||||
virtual int Send() = 0;
|
|
||||||
virtual int Recv() = 0;
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,12 @@ Listener::Listener()
|
||||||
connection = NULL;
|
connection = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Listener::Listener(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
|
||||||
|
{
|
||||||
|
connection = NULL;
|
||||||
|
this->postBox = postBox;
|
||||||
|
}
|
||||||
|
|
||||||
Listener::~Listener()
|
Listener::~Listener()
|
||||||
{
|
{
|
||||||
if(connection)
|
if(connection)
|
||||||
|
@ -16,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);
|
||||||
|
@ -27,6 +33,26 @@ 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.Stop();
|
thread.Stop();
|
||||||
|
@ -34,9 +60,11 @@ void Listener::Shutdown()
|
||||||
|
|
||||||
void Listener::SetPostBox(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
|
void Listener::SetPostBox(Oyster::Network::IPostBox<SmartPointer<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()
|
||||||
|
@ -46,9 +74,11 @@ int Listener::Accept()
|
||||||
|
|
||||||
if(*clientSocket != -1)
|
if(*clientSocket != -1)
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
|
//mutex.LockMutex();
|
||||||
postBox->PostMessage(clientSocket);
|
postBox->PostMessage(clientSocket);
|
||||||
mutex.UnlockMutex();
|
//mutex.UnlockMutex();
|
||||||
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientSocket;
|
return clientSocket;
|
||||||
|
@ -61,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;
|
|
||||||
}
|
}
|
|
@ -6,11 +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 "../../Misc/Utilities.h"
|
#include "../../Misc/Utilities.h"
|
||||||
#include "IPostBox.h"
|
|
||||||
|
|
||||||
namespace Oyster
|
namespace Oyster
|
||||||
{
|
{
|
||||||
|
@ -18,13 +18,18 @@ 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<Utility::DynamicMemory::SmartPointer<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<Utility::DynamicMemory::SmartPointer<int>>* postBox);
|
void SetPostBox(IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox);
|
||||||
|
@ -43,6 +48,7 @@ namespace Oyster
|
||||||
|
|
||||||
::Oyster::Thread::OysterThread thread;
|
::Oyster::Thread::OysterThread thread;
|
||||||
OysterMutex mutex;
|
OysterMutex mutex;
|
||||||
|
std::mutex stdMutex;
|
||||||
|
|
||||||
IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox;
|
IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox;
|
||||||
|
|
||||||
|
|
|
@ -165,7 +165,6 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Connection.h" />
|
<ClInclude Include="Connection.h" />
|
||||||
<ClInclude Include="IClient.h" />
|
|
||||||
<ClInclude Include="IConnection.h" />
|
<ClInclude Include="IConnection.h" />
|
||||||
<ClInclude Include="IListener.h" />
|
<ClInclude Include="IListener.h" />
|
||||||
<ClInclude Include="IPostBox.h" />
|
<ClInclude Include="IPostBox.h" />
|
||||||
|
|
|
@ -2,38 +2,34 @@
|
||||||
<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="Listener.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="Messages\MessageHeader.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="OysterByte.cpp" />
|
|
||||||
<ClCompile Include="ThreadedClient.cpp" /
|
|
||||||
<ClCompile Include="Messages\MessagePlayerPos.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="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="IClient.h" />
|
<ClInclude Include="Protocols.h" />
|
||||||
<ClInclude Include="ThreadedClient.h" />
|
|
||||||
<ClInclude Include="Messages\MessagePlayerPos.h" />
|
|
||||||
<ClInclude Include="IClient.h" />
|
|
||||||
<ClInclude Include="ThreadedClient.h" />
|
<ClInclude Include="ThreadedClient.h" />
|
||||||
|
<ClInclude Include="Translator.h" />
|
||||||
|
<ClInclude Include="WinsockFunctions.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -18,7 +18,7 @@ OysterByte::OysterByte(int cap)
|
||||||
|
|
||||||
OysterByte::OysterByte(const OysterByte& obj)
|
OysterByte::OysterByte(const OysterByte& obj)
|
||||||
{
|
{
|
||||||
delete[] this->byteArray;
|
//delete[] this->byteArray;
|
||||||
this->byteArray = new unsigned char[obj.capacity];
|
this->byteArray = new unsigned char[obj.capacity];
|
||||||
|
|
||||||
for(int i = 0; i < obj.size; i++)
|
for(int i = 0; i < obj.size; i++)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "ThreadedClient.h"
|
#include "ThreadedClient.h"
|
||||||
|
#include "OysterByte.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace Oyster::Network;
|
using namespace Oyster::Network;
|
||||||
|
@ -25,7 +26,7 @@ ThreadedClient::ThreadedClient(unsigned int socket)
|
||||||
thread.Create(this, true);
|
thread.Create(this, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadedClient::ThreadedClient(IPostBox<SmartPointer<OysterByte>>* postBox, unsigned int socket)
|
ThreadedClient::ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>>* postBox, unsigned int socket)
|
||||||
{
|
{
|
||||||
this->connection = new Connection(socket);
|
this->connection = new Connection(socket);
|
||||||
this->sendPostBox = new PostBox<SmartPointer<OysterByte>>;
|
this->sendPostBox = new PostBox<SmartPointer<OysterByte>>;
|
||||||
|
@ -50,41 +51,37 @@ ThreadedClient::~ThreadedClient()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ThreadedClient::Send(SmartPointer<OysterByte> &byte)
|
void ThreadedClient::Send(SmartPointer<OysterByte>& byte)
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
|
||||||
this->sendPostBox->PostMessage(byte);
|
this->sendPostBox->PostMessage(byte);
|
||||||
mutex.UnlockMutex();
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ThreadedClient::Send()
|
int ThreadedClient::Send()
|
||||||
{
|
{
|
||||||
int errorCode = 0;
|
int errorCode = 0;
|
||||||
mutex.LockMutex();
|
|
||||||
if(sendPostBox->IsFull())
|
if(sendPostBox->IsFull())
|
||||||
{
|
{
|
||||||
SmartPointer<OysterByte> temp = NULL;
|
SmartPointer<OysterByte> temp = new OysterByte;
|
||||||
sendPostBox->FetchMessage(temp);
|
sendPostBox->FetchMessage(temp);
|
||||||
errorCode = this->connection->Send(temp);
|
errorCode = this->connection->Send(temp);
|
||||||
}
|
}
|
||||||
mutex.UnlockMutex();
|
|
||||||
|
|
||||||
return errorCode;
|
return errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ThreadedClient::Recv()
|
int ThreadedClient::Recv()
|
||||||
{
|
{
|
||||||
int errorCode = 0;
|
int errorCode = -1;
|
||||||
|
|
||||||
SmartPointer<OysterByte> temp = new OysterByte();
|
SmartPointer<OysterByte> temp = new OysterByte;
|
||||||
errorCode = this->connection->Recieve(temp);
|
errorCode = this->connection->Recieve(temp);
|
||||||
|
|
||||||
if(errorCode == 0)
|
if(errorCode == 0)
|
||||||
{
|
{
|
||||||
mutex.LockMutex();
|
stdMutex.lock();
|
||||||
recvPostBox->PostMessage(temp);
|
recvPostBox->PostMessage(temp);
|
||||||
mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return errorCode;
|
return errorCode;
|
||||||
|
@ -100,6 +97,8 @@ void ThreadedClient::ThreadExit()
|
||||||
std::cout << "Client Thread exit" << std::endl;
|
std::cout << "Client Thread exit" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
bool ThreadedClient::DoWork()
|
bool ThreadedClient::DoWork()
|
||||||
{
|
{
|
||||||
int errorCode;
|
int errorCode;
|
||||||
|
@ -114,7 +113,7 @@ bool ThreadedClient::DoWork()
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}*/
|
}*/
|
||||||
|
Sleep(1);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +137,7 @@ int ThreadedClient::Connect(unsigned short port, const char serverName[])
|
||||||
|
|
||||||
void ThreadedClient::setRecvPostBox(IPostBox<SmartPointer<OysterByte>> *postBox)
|
void ThreadedClient::setRecvPostBox(IPostBox<SmartPointer<OysterByte>> *postBox)
|
||||||
{
|
{
|
||||||
this->mutex.LockMutex();
|
stdMutex.lock();
|
||||||
this->recvPostBox = postBox;
|
this->recvPostBox = postBox;
|
||||||
this->mutex.UnlockMutex();
|
stdMutex.unlock();
|
||||||
}
|
}
|
|
@ -5,18 +5,21 @@
|
||||||
// Created by Sam Svensson 2013 //
|
// Created by Sam Svensson 2013 //
|
||||||
//////////////////////////////////
|
//////////////////////////////////
|
||||||
|
|
||||||
#include "../NetworkDependencies/IClient.h"
|
|
||||||
#include "../../Misc/Thread/IThreadObject.h"
|
#include "../../Misc/Thread/IThreadObject.h"
|
||||||
#include "../NetworkDependencies/PostBox.h"
|
#include "PostBox.h"
|
||||||
|
#include "Connection.h"
|
||||||
#include "../../Misc/Thread/OysterThread.h"
|
#include "../../Misc/Thread/OysterThread.h"
|
||||||
#include "../../Misc/Thread/OysterMutex.h"
|
#include "../../Misc/Thread/OysterMutex.h"
|
||||||
#include "../../Misc/Utilities.h"
|
#include "../../Misc/Utilities.h"
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace Oyster
|
namespace Oyster
|
||||||
{
|
{
|
||||||
namespace Network
|
namespace Network
|
||||||
{
|
{
|
||||||
class ThreadedClient : public IClient, public Thread::IThreadObject
|
class OysterByte;
|
||||||
|
class ThreadedClient : public Thread::IThreadObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ThreadedClient();
|
ThreadedClient();
|
||||||
|
@ -24,7 +27,7 @@ namespace Oyster
|
||||||
ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *postBox, unsigned int socket);
|
ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *postBox, unsigned int socket);
|
||||||
virtual ~ThreadedClient();
|
virtual ~ThreadedClient();
|
||||||
|
|
||||||
int Send(Utility::DynamicMemory::SmartPointer< OysterByte > &byte);
|
void Send(Utility::DynamicMemory::SmartPointer<OysterByte>& byte);
|
||||||
|
|
||||||
int Connect(unsigned short port, const char serverName[]);
|
int Connect(unsigned short port, const char serverName[]);
|
||||||
|
|
||||||
|
@ -43,7 +46,7 @@ namespace Oyster
|
||||||
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *sendPostBox;
|
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *sendPostBox;
|
||||||
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *recvPostBox;
|
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *recvPostBox;
|
||||||
Oyster::Thread::OysterThread thread;
|
Oyster::Thread::OysterThread thread;
|
||||||
OysterMutex mutex;
|
std::mutex stdMutex;
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,8 +59,8 @@ void chat(ThreadedClient &client)
|
||||||
|
|
||||||
client.setRecvPostBox(postBox);
|
client.setRecvPostBox(postBox);
|
||||||
|
|
||||||
SmartPointer<OysterByte> msgRecv = SmartPointer<OysterByte>(new OysterByte());
|
SmartPointer<OysterByte> msgRecv = new OysterByte();
|
||||||
SmartPointer<OysterByte> msgSend = SmartPointer<OysterByte>(new OysterByte());
|
SmartPointer<OysterByte> msgSend = new OysterByte();
|
||||||
|
|
||||||
ProtocolSet* set = new ProtocolSet;
|
ProtocolSet* set = new ProtocolSet;
|
||||||
ProtocolPlayerPos test;
|
ProtocolPlayerPos test;
|
||||||
|
@ -84,7 +84,7 @@ void chat(ThreadedClient &client)
|
||||||
{
|
{
|
||||||
t->Unpack(set, msgRecv);
|
t->Unpack(set, msgRecv);
|
||||||
|
|
||||||
PrintOutMessage(set);
|
//PrintOutMessage(set);
|
||||||
set->Release();
|
set->Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,6 +95,7 @@ void chat(ThreadedClient &client)
|
||||||
timer.reset();
|
timer.reset();
|
||||||
client.Send(msgSend);
|
client.Send(msgSend);
|
||||||
}
|
}
|
||||||
|
Sleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete postBox;
|
delete postBox;
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef OYSTER_NETWORK_SERVER_I_CLIENT_H
|
||||||
|
#define OYSTER_NETWORK_SERVER_I_CLIENT_H
|
||||||
|
|
||||||
|
/////////////////////////////////////
|
||||||
|
// Created by Pontus Fransson 2013 //
|
||||||
|
/////////////////////////////////////
|
||||||
|
|
||||||
|
class IClient
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
IClient() {}
|
||||||
|
virtual ~IClient() {}
|
||||||
|
|
||||||
|
virtual void Disconnect() {}
|
||||||
|
virtual bool IsConnected() {return true;}
|
||||||
|
|
||||||
|
virtual void Send() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,154 @@
|
||||||
|
#include "IServer.h"
|
||||||
|
#include "../NetworkDependencies/Listener.h"
|
||||||
|
#include "IClient.h"
|
||||||
|
#include "../NetworkDependencies/PostBox.h"
|
||||||
|
#include "../../Misc/Utilities.h"
|
||||||
|
|
||||||
|
using namespace Oyster::Network;
|
||||||
|
using namespace ::Server;
|
||||||
|
using namespace Utility::DynamicMemory;
|
||||||
|
|
||||||
|
/*************************************
|
||||||
|
PrivateData
|
||||||
|
*************************************/
|
||||||
|
|
||||||
|
struct IServer::PrivateData
|
||||||
|
{
|
||||||
|
PrivateData();
|
||||||
|
~PrivateData();
|
||||||
|
|
||||||
|
bool Init(INIT_DESC& initDesc);
|
||||||
|
bool Start();
|
||||||
|
bool Stop();
|
||||||
|
bool Shutdown();
|
||||||
|
|
||||||
|
//
|
||||||
|
IListener* listener;
|
||||||
|
INIT_DESC initDesc;
|
||||||
|
bool started;
|
||||||
|
|
||||||
|
IPostBox<SmartPointer<int>> *postBox;
|
||||||
|
};
|
||||||
|
|
||||||
|
IServer::PrivateData::PrivateData()
|
||||||
|
{
|
||||||
|
listener = 0;
|
||||||
|
started = false;
|
||||||
|
postBox = new PostBox<SmartPointer<int>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
IServer::PrivateData::~PrivateData()
|
||||||
|
{
|
||||||
|
Shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IServer::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();
|
||||||
|
((Listener*)listener)->Init(this->initDesc.port, false);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IServer::PrivateData::Start()
|
||||||
|
{
|
||||||
|
//Start listener
|
||||||
|
((Listener*)listener)->Start();
|
||||||
|
started = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IServer::PrivateData::Stop()
|
||||||
|
{
|
||||||
|
if(listener)
|
||||||
|
{
|
||||||
|
((Listener*)listener)->Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
started = false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IServer::PrivateData::Shutdown()
|
||||||
|
{
|
||||||
|
if(listener)
|
||||||
|
{
|
||||||
|
delete listener;
|
||||||
|
listener = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
started = false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************
|
||||||
|
IServer
|
||||||
|
*************************************/
|
||||||
|
|
||||||
|
IServer::IServer()
|
||||||
|
{
|
||||||
|
privateData = new PrivateData();
|
||||||
|
}
|
||||||
|
|
||||||
|
IServer::~IServer()
|
||||||
|
{
|
||||||
|
if(privateData)
|
||||||
|
{
|
||||||
|
delete privateData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IServer::Init(INIT_DESC& initDesc)
|
||||||
|
{
|
||||||
|
privateData->Init(initDesc);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IServer::Start()
|
||||||
|
{
|
||||||
|
privateData->Start();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IServer::Stop()
|
||||||
|
{
|
||||||
|
privateData->Stop();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IServer::Shutdown()
|
||||||
|
{
|
||||||
|
privateData->Shutdown();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IServer::AddSession(ISession* session)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void IServer::RemoveSession(ISession* session)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IServer::IsStarted() const
|
||||||
|
{
|
||||||
|
return privateData->started;
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
#ifndef OYSTER_NETWORK_SERVER_I_SERVER_H
|
||||||
|
#define OYSTER_NETWORK_SERVER_I_SERVER_H
|
||||||
|
|
||||||
|
/////////////////////////////////////
|
||||||
|
// Created by Pontus Fransson 2013 //
|
||||||
|
/////////////////////////////////////
|
||||||
|
|
||||||
|
#include "IClient.h"
|
||||||
|
|
||||||
|
namespace Oyster
|
||||||
|
{
|
||||||
|
namespace Network
|
||||||
|
{
|
||||||
|
namespace Server
|
||||||
|
{
|
||||||
|
class IServer
|
||||||
|
{
|
||||||
|
class ISession;
|
||||||
|
public:
|
||||||
|
struct INIT_DESC
|
||||||
|
{
|
||||||
|
unsigned short port; //Port the server should be accepting clients on.
|
||||||
|
void (*proc)(IClient*);
|
||||||
|
};
|
||||||
|
|
||||||
|
IServer();
|
||||||
|
virtual ~IServer();
|
||||||
|
|
||||||
|
virtual bool Init(INIT_DESC& initDesc);
|
||||||
|
virtual bool Start();
|
||||||
|
virtual bool Stop();
|
||||||
|
virtual bool Shutdown();
|
||||||
|
|
||||||
|
virtual void AddSession(ISession* session);
|
||||||
|
virtual void RemoveSession(ISession* session);
|
||||||
|
|
||||||
|
virtual bool IsStarted() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct PrivateData;
|
||||||
|
PrivateData* privateData;
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,34 @@
|
||||||
|
#ifndef OYSTER_NETWORK_SERVER_I_SESSION_H
|
||||||
|
#define OYSTER_NETWORK_SERVER_I_SESSION_H
|
||||||
|
|
||||||
|
/////////////////////////////////////
|
||||||
|
// Created by Pontus Fransson 2013 //
|
||||||
|
/////////////////////////////////////
|
||||||
|
|
||||||
|
class ISession
|
||||||
|
{
|
||||||
|
class IClient;
|
||||||
|
public:
|
||||||
|
struct INIT_DESC
|
||||||
|
{
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
ISession();
|
||||||
|
virtual ~ISession();
|
||||||
|
|
||||||
|
virtual bool Init();
|
||||||
|
virtual bool Start();
|
||||||
|
virtual bool Stop();
|
||||||
|
virtual bool Shutdown();
|
||||||
|
|
||||||
|
virtual void SendToAll();
|
||||||
|
|
||||||
|
virtual void AddClient(IClient* client);
|
||||||
|
virtual void RemoveClient(IClient* client);
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
|
@ -154,8 +154,14 @@
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="IServer.cpp" />
|
||||||
<ClCompile Include="ServerMain.cpp" />
|
<ClCompile Include="ServerMain.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="IClient.h" />
|
||||||
|
<ClInclude Include="IServer.h" />
|
||||||
|
<ClInclude Include="ISession.h" />
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
</ImportGroup>
|
</ImportGroup>
|
||||||
|
|
|
@ -18,5 +18,19 @@
|
||||||
<ClCompile Include="ServerMain.cpp">
|
<ClCompile Include="ServerMain.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="IServer.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="IServer.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="ISession.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="IClient.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -12,6 +12,10 @@
|
||||||
#include "../../Misc/Utilities.h"
|
#include "../../Misc/Utilities.h"
|
||||||
#include "../../Misc/Utilities-Impl.h"
|
#include "../../Misc/Utilities-Impl.h"
|
||||||
|
|
||||||
|
#include "IServer.h"
|
||||||
|
#include "IClient.h"
|
||||||
|
#include "ISession.h"
|
||||||
|
|
||||||
#pragma comment(lib, "ws2_32.lib")
|
#pragma comment(lib, "ws2_32.lib")
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
@ -22,12 +26,15 @@ using namespace Utility;
|
||||||
using namespace ::Utility::DynamicMemory;
|
using namespace ::Utility::DynamicMemory;
|
||||||
|
|
||||||
void PrintOutMessage(ProtocolSet* set);
|
void PrintOutMessage(ProtocolSet* set);
|
||||||
|
void clientProc(IClient* client);
|
||||||
|
vector<ThreadedClient*> clients;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
SmartPointer<OysterByte> sendBuffer = SmartPointer<OysterByte>(new OysterByte);
|
SmartPointer<OysterByte> sendBuffer = new OysterByte;
|
||||||
SmartPointer<OysterByte> recvBuffer = SmartPointer<OysterByte>(new OysterByte());
|
SmartPointer<OysterByte> recvBuffer = new OysterByte;
|
||||||
ProtocolSet* set = new ProtocolSet;
|
ProtocolSet* set = new ProtocolSet;
|
||||||
|
|
||||||
IPostBox<SmartPointer<int>> *postBox = new PostBox<SmartPointer<int>>();
|
IPostBox<SmartPointer<int>> *postBox = new PostBox<SmartPointer<int>>();
|
||||||
IPostBox<SmartPointer<OysterByte>> *recvPostBox = new PostBox<SmartPointer<OysterByte>>();
|
IPostBox<SmartPointer<OysterByte>> *recvPostBox = new PostBox<SmartPointer<OysterByte>>();
|
||||||
|
|
||||||
|
@ -39,11 +46,18 @@ int main()
|
||||||
{
|
{
|
||||||
cout << "errorMessage: unable to start winsock" << endl;
|
cout << "errorMessage: unable to start winsock" << endl;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
IServer server;
|
||||||
|
IServer::INIT_DESC initDesc;
|
||||||
|
initDesc.port = 9876;
|
||||||
|
initDesc.proc = clientProc;
|
||||||
|
server.Init(initDesc);
|
||||||
|
*/
|
||||||
//Create socket
|
//Create socket
|
||||||
Listener listener;
|
Listener listener;
|
||||||
listener.Init(9876);
|
listener.Init(9876);
|
||||||
listener.SetPostBox(postBox);
|
listener.SetPostBox(postBox);
|
||||||
|
listener.Start();
|
||||||
Sleep(1000);
|
Sleep(1000);
|
||||||
|
|
||||||
//Start listening
|
//Start listening
|
||||||
|
@ -63,8 +77,13 @@ int main()
|
||||||
|
|
||||||
WinTimer timer;
|
WinTimer timer;
|
||||||
|
|
||||||
vector<ThreadedClient*> clients;
|
/* DEBUGGING: Connect 25 clients
|
||||||
SmartPointer<int> client = SmartPointer<int>();
|
for(int i = 0; i < 25; i++)
|
||||||
|
{
|
||||||
|
clients.push_back(new ThreadedClient(recvPostBox, 1));
|
||||||
|
}*/
|
||||||
|
|
||||||
|
SmartPointer<int> client = int();
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
//Fetch new clients from the postbox
|
//Fetch new clients from the postbox
|
||||||
|
@ -92,21 +111,22 @@ int main()
|
||||||
{
|
{
|
||||||
t.Unpack(set, recvBuffer);
|
t.Unpack(set, recvBuffer);
|
||||||
|
|
||||||
PrintOutMessage(set);
|
//PrintOutMessage(set);
|
||||||
set->Release();
|
set->Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
Sleep(1);
|
Sleep(1);
|
||||||
}
|
}
|
||||||
|
//server.Stop();
|
||||||
|
//server.Shutdown();
|
||||||
listener.Shutdown();
|
listener.Shutdown();
|
||||||
Sleep(1000);
|
Sleep(1000);
|
||||||
|
|
||||||
system("pause");
|
system("pause");
|
||||||
|
|
||||||
for(int i = 0; i < clients.size(); i++)
|
for(int i = 0; i < (int)clients.size(); i++)
|
||||||
delete clients.at(i);
|
delete clients.at(i);
|
||||||
|
|
||||||
delete postBox;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,3 +155,9 @@ void PrintOutMessage(ProtocolSet* set)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clientProc(IClient* client)
|
||||||
|
{
|
||||||
|
cout << "Proc" << endl;
|
||||||
|
//clients.push_back(client);
|
||||||
|
}
|
Loading…
Reference in New Issue