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 *back;
|
||||
int nrOfNodes;
|
||||
OysterMutex mutex;
|
||||
std::mutex stdMutex;
|
||||
};
|
||||
|
||||
|
||||
|
@ -68,7 +68,7 @@ namespace Oyster
|
|||
template < typename Type >
|
||||
ThreadSafeQueue<Type>::~ThreadSafeQueue()
|
||||
{
|
||||
this->mutex.LockMutex();
|
||||
stdMutex.lock();
|
||||
|
||||
if(this->front != NULL)
|
||||
{
|
||||
|
@ -87,16 +87,16 @@ namespace Oyster
|
|||
this->back = NULL;
|
||||
}
|
||||
|
||||
this->mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
template < typename Type >
|
||||
void ThreadSafeQueue<Type>::Push(Type item)
|
||||
{
|
||||
stdMutex.lock();
|
||||
Node *e = new Node(item);
|
||||
|
||||
mutex.LockMutex();
|
||||
if(this->front != NULL)
|
||||
{
|
||||
this->back->next = e;
|
||||
|
@ -111,13 +111,13 @@ namespace Oyster
|
|||
|
||||
this->nrOfNodes++;
|
||||
|
||||
mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
Type ThreadSafeQueue<Type>::Pop()
|
||||
{
|
||||
mutex.LockMutex();
|
||||
stdMutex.lock();
|
||||
|
||||
Type item = this->front->item;
|
||||
Node *destroyer = this->front;
|
||||
|
@ -132,55 +132,56 @@ namespace Oyster
|
|||
this->back = NULL;
|
||||
}
|
||||
|
||||
mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
return item;
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
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 >
|
||||
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 >
|
||||
int ThreadSafeQueue<Type>::Size()
|
||||
{
|
||||
mutex.LockMutex();
|
||||
stdMutex.lock();
|
||||
int size = this->nrOfNodes;
|
||||
stdMutex.unlock();
|
||||
|
||||
return this->nrOfNodes;
|
||||
return size;
|
||||
|
||||
mutex.UnlockMutex();
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
bool ThreadSafeQueue<Type>::IsEmpty()
|
||||
{
|
||||
mutex.LockMutex();
|
||||
stdMutex.lock();
|
||||
if(nrOfNodes == 0 || this->front == NULL)
|
||||
{
|
||||
mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -189,7 +190,7 @@ namespace Oyster
|
|||
template < typename Type >
|
||||
void ThreadSafeQueue<Type>::Swap(IQueue<Type> &queue )
|
||||
{
|
||||
mutex.LockMutex();
|
||||
stdMutex.lock();
|
||||
int prevNrOfNodes = this->nrOfNodes;
|
||||
int size = queue.Size();
|
||||
|
||||
|
@ -202,7 +203,7 @@ namespace Oyster
|
|||
{
|
||||
queue.Push(this->Pop());
|
||||
}
|
||||
mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -201,7 +201,6 @@ namespace Utility
|
|||
T *_ptr;
|
||||
|
||||
/** Destroys the pointer and returns the memory allocated. */
|
||||
void Destroy();
|
||||
|
||||
public:
|
||||
SmartPointer();
|
||||
|
@ -216,7 +215,8 @@ namespace Utility
|
|||
T* operator-> ();
|
||||
operator T* ();
|
||||
operator bool();
|
||||
|
||||
|
||||
void Destroy();
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
public:
|
||||
virtual ~IListener() {}
|
||||
virtual bool Init(unsigned int port) = 0;
|
||||
virtual int Accept() = 0;
|
||||
|
||||
|
|
|
@ -8,6 +8,12 @@ Listener::Listener()
|
|||
connection = NULL;
|
||||
}
|
||||
|
||||
Listener::Listener(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
|
||||
{
|
||||
connection = NULL;
|
||||
this->postBox = postBox;
|
||||
}
|
||||
|
||||
Listener::~Listener()
|
||||
{
|
||||
if(connection)
|
||||
|
@ -16,10 +22,10 @@ Listener::~Listener()
|
|||
}
|
||||
}
|
||||
|
||||
//Starts the thread immediate
|
||||
bool Listener::Init(unsigned int port)
|
||||
{
|
||||
connection = new Connection();
|
||||
|
||||
connection->InitiateServer(port);
|
||||
|
||||
thread.Create(this, true);
|
||||
|
@ -27,6 +33,26 @@ bool Listener::Init(unsigned int port)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool Listener::Init(unsigned int port, bool start)
|
||||
{
|
||||
connection = new Connection();
|
||||
connection->InitiateServer(port);
|
||||
|
||||
thread.Create(this, start);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Listener::Start()
|
||||
{
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
void Listener::Stop()
|
||||
{
|
||||
thread.Stop();
|
||||
}
|
||||
|
||||
void Listener::Shutdown()
|
||||
{
|
||||
thread.Stop();
|
||||
|
@ -34,9 +60,11 @@ void Listener::Shutdown()
|
|||
|
||||
void Listener::SetPostBox(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
|
||||
{
|
||||
mutex.LockMutex();
|
||||
stdMutex.lock();
|
||||
//mutex.LockMutex();
|
||||
this->postBox = postBox;
|
||||
mutex.UnlockMutex();
|
||||
//mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
}
|
||||
|
||||
int Listener::Accept()
|
||||
|
@ -46,9 +74,11 @@ int Listener::Accept()
|
|||
|
||||
if(*clientSocket != -1)
|
||||
{
|
||||
mutex.LockMutex();
|
||||
stdMutex.lock();
|
||||
//mutex.LockMutex();
|
||||
postBox->PostMessage(clientSocket);
|
||||
mutex.UnlockMutex();
|
||||
//mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
}
|
||||
|
||||
return clientSocket;
|
||||
|
@ -61,13 +91,10 @@ bool Listener::DoWork()
|
|||
return true;
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
void Listener::ThreadEntry()
|
||||
{
|
||||
std::cout << "Thread started" << std::endl;
|
||||
}
|
||||
|
||||
void Listener::ThreadExit()
|
||||
{
|
||||
std::cout << "Thread stopped" << std::endl;
|
||||
}
|
|
@ -6,11 +6,11 @@
|
|||
/////////////////////////////////////
|
||||
|
||||
#include "IListener.h"
|
||||
#include "../NetworkDependencies/Connection.h"
|
||||
#include "Connection.h"
|
||||
#include "IPostBox.h"
|
||||
#include "../../Misc/Thread/OysterThread.h"
|
||||
#include "../../Misc/Thread/OysterMutex.h"
|
||||
#include "../../Misc/Utilities.h"
|
||||
#include "IPostBox.h"
|
||||
|
||||
namespace Oyster
|
||||
{
|
||||
|
@ -18,13 +18,18 @@ namespace Oyster
|
|||
{
|
||||
namespace Server
|
||||
{
|
||||
class Listener : public ::Oyster::Thread::IThreadObject
|
||||
class Listener : public IListener, public ::Oyster::Thread::IThreadObject
|
||||
{
|
||||
public:
|
||||
Listener();
|
||||
Listener(Oyster::Network::IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox);
|
||||
~Listener();
|
||||
|
||||
bool Init(unsigned int port);
|
||||
bool Init(unsigned int port, bool start);
|
||||
void Start();
|
||||
void Stop();
|
||||
|
||||
void Shutdown();
|
||||
|
||||
void SetPostBox(IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox);
|
||||
|
@ -43,6 +48,7 @@ namespace Oyster
|
|||
|
||||
::Oyster::Thread::OysterThread thread;
|
||||
OysterMutex mutex;
|
||||
std::mutex stdMutex;
|
||||
|
||||
IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox;
|
||||
|
||||
|
|
|
@ -165,7 +165,6 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Connection.h" />
|
||||
<ClInclude Include="IClient.h" />
|
||||
<ClInclude Include="IConnection.h" />
|
||||
<ClInclude Include="IListener.h" />
|
||||
<ClInclude Include="IPostBox.h" />
|
||||
|
|
|
@ -2,38 +2,34 @@
|
|||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Connection.cpp" />
|
||||
<ClCompile Include="Listener.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="Messages\MessageHeader.cpp" />
|
||||
<ClCompile Include="Messages\MessageTest.cpp" />
|
||||
<ClCompile Include="Packing.cpp" />
|
||||
<ClCompile Include="Translator.cpp" />
|
||||
<ClCompile Include="Listener.cpp" />
|
||||
<ClCompile Include="WinsockFunctions.cpp" />
|
||||
<ClCompile Include="OysterByte.cpp" />
|
||||
<ClCompile Include="ThreadedClient.cpp" /
|
||||
<ClCompile Include="Messages\MessagePlayerPos.cpp" />
|
||||
<ClCompile Include="Messages\MessageTest.cpp" />
|
||||
<ClCompile Include="OysterByte.cpp" />
|
||||
<ClCompile Include="Packing.cpp" />
|
||||
<ClCompile Include="ThreadedClient.cpp" />
|
||||
<ClCompile Include="Translator.cpp" />
|
||||
<ClCompile Include="WinsockFunctions.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Connection.h" />
|
||||
<ClInclude Include="IConnection.h" />
|
||||
<ClInclude Include="IListener.h" />
|
||||
<ClInclude Include="IPostBox.h" />
|
||||
<ClInclude Include="Listener.h" />
|
||||
<ClInclude Include="Messages\MessageHeader.h" />
|
||||
<ClInclude Include="Messages\MessagePlayerPos.h" />
|
||||
<ClInclude Include="Messages\MessagesInclude.h" />
|
||||
<ClInclude Include="Messages\MessageTest.h" />
|
||||
<ClInclude Include="OysterByte.h" />
|
||||
<ClInclude Include="Packing.h" />
|
||||
<ClInclude Include="ITranslate.h" />
|
||||
<ClInclude Include="Protocols.h" />
|
||||
<ClInclude Include="Translator.h" />
|
||||
<ClInclude Include="Messages\MessagesInclude.h" />
|
||||
<ClInclude Include="Listener.h" />
|
||||
<ClInclude Include="IListener.h" />
|
||||
<ClInclude Include="WinsockFunctions.h" />
|
||||
<ClInclude Include="OysterByte.h" />
|
||||
<ClInclude Include="IPostBox.h" />
|
||||
<ClInclude Include="PostBox.h" />
|
||||
<ClInclude Include="IClient.h" />
|
||||
<ClInclude Include="ThreadedClient.h" />
|
||||
<ClInclude Include="Messages\MessagePlayerPos.h" />
|
||||
<ClInclude Include="IClient.h" />
|
||||
<ClInclude Include="Protocols.h" />
|
||||
<ClInclude Include="ThreadedClient.h" />
|
||||
<ClInclude Include="Translator.h" />
|
||||
<ClInclude Include="WinsockFunctions.h" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -18,7 +18,7 @@ OysterByte::OysterByte(int cap)
|
|||
|
||||
OysterByte::OysterByte(const OysterByte& obj)
|
||||
{
|
||||
delete[] this->byteArray;
|
||||
//delete[] this->byteArray;
|
||||
this->byteArray = new unsigned char[obj.capacity];
|
||||
|
||||
for(int i = 0; i < obj.size; i++)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "ThreadedClient.h"
|
||||
#include "OysterByte.h"
|
||||
|
||||
#include <iostream>
|
||||
using namespace Oyster::Network;
|
||||
|
@ -25,7 +26,7 @@ ThreadedClient::ThreadedClient(unsigned int socket)
|
|||
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->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);
|
||||
mutex.UnlockMutex();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ThreadedClient::Send()
|
||||
{
|
||||
int errorCode = 0;
|
||||
mutex.LockMutex();
|
||||
|
||||
if(sendPostBox->IsFull())
|
||||
{
|
||||
SmartPointer<OysterByte> temp = NULL;
|
||||
SmartPointer<OysterByte> temp = new OysterByte;
|
||||
sendPostBox->FetchMessage(temp);
|
||||
errorCode = this->connection->Send(temp);
|
||||
}
|
||||
mutex.UnlockMutex();
|
||||
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
int ThreadedClient::Recv()
|
||||
{
|
||||
int errorCode = 0;
|
||||
int errorCode = -1;
|
||||
|
||||
SmartPointer<OysterByte> temp = new OysterByte();
|
||||
SmartPointer<OysterByte> temp = new OysterByte;
|
||||
errorCode = this->connection->Recieve(temp);
|
||||
|
||||
if(errorCode == 0)
|
||||
{
|
||||
mutex.LockMutex();
|
||||
stdMutex.lock();
|
||||
recvPostBox->PostMessage(temp);
|
||||
mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
}
|
||||
|
||||
return errorCode;
|
||||
|
@ -100,6 +97,8 @@ void ThreadedClient::ThreadExit()
|
|||
std::cout << "Client Thread exit" << std::endl;
|
||||
}
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
bool ThreadedClient::DoWork()
|
||||
{
|
||||
int errorCode;
|
||||
|
@ -114,7 +113,7 @@ bool ThreadedClient::DoWork()
|
|||
{
|
||||
return false;
|
||||
}*/
|
||||
|
||||
Sleep(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -138,7 +137,7 @@ int ThreadedClient::Connect(unsigned short port, const char serverName[])
|
|||
|
||||
void ThreadedClient::setRecvPostBox(IPostBox<SmartPointer<OysterByte>> *postBox)
|
||||
{
|
||||
this->mutex.LockMutex();
|
||||
stdMutex.lock();
|
||||
this->recvPostBox = postBox;
|
||||
this->mutex.UnlockMutex();
|
||||
stdMutex.unlock();
|
||||
}
|
|
@ -5,30 +5,33 @@
|
|||
// Created by Sam Svensson 2013 //
|
||||
//////////////////////////////////
|
||||
|
||||
#include "../NetworkDependencies/IClient.h"
|
||||
#include "../../Misc/Thread/IThreadObject.h"
|
||||
#include "../NetworkDependencies/PostBox.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 ThreadedClient : public IClient, public Thread::IThreadObject
|
||||
class OysterByte;
|
||||
class ThreadedClient : public Thread::IThreadObject
|
||||
{
|
||||
public:
|
||||
ThreadedClient();
|
||||
ThreadedClient(unsigned int socket);
|
||||
ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer< OysterByte >> *postBox, unsigned int socket);
|
||||
ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *postBox, unsigned int socket);
|
||||
virtual ~ThreadedClient();
|
||||
|
||||
int Send(Utility::DynamicMemory::SmartPointer< OysterByte > &byte);
|
||||
void Send(Utility::DynamicMemory::SmartPointer<OysterByte>& byte);
|
||||
|
||||
int Connect(unsigned short port, const char serverName[]);
|
||||
|
||||
void setRecvPostBox(IPostBox< Utility::DynamicMemory::SmartPointer< OysterByte >> *postBox);
|
||||
void setRecvPostBox(IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *postBox);
|
||||
|
||||
private:
|
||||
virtual int Send();
|
||||
|
@ -43,7 +46,7 @@ namespace Oyster
|
|||
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *sendPostBox;
|
||||
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *recvPostBox;
|
||||
Oyster::Thread::OysterThread thread;
|
||||
OysterMutex mutex;
|
||||
std::mutex stdMutex;
|
||||
|
||||
};
|
||||
}
|
||||
|
|
|
@ -59,8 +59,8 @@ void chat(ThreadedClient &client)
|
|||
|
||||
client.setRecvPostBox(postBox);
|
||||
|
||||
SmartPointer<OysterByte> msgRecv = SmartPointer<OysterByte>(new OysterByte());
|
||||
SmartPointer<OysterByte> msgSend = SmartPointer<OysterByte>(new OysterByte());
|
||||
SmartPointer<OysterByte> msgRecv = new OysterByte();
|
||||
SmartPointer<OysterByte> msgSend = new OysterByte();
|
||||
|
||||
ProtocolSet* set = new ProtocolSet;
|
||||
ProtocolPlayerPos test;
|
||||
|
@ -84,7 +84,7 @@ void chat(ThreadedClient &client)
|
|||
{
|
||||
t->Unpack(set, msgRecv);
|
||||
|
||||
PrintOutMessage(set);
|
||||
//PrintOutMessage(set);
|
||||
set->Release();
|
||||
}
|
||||
|
||||
|
@ -95,6 +95,7 @@ void chat(ThreadedClient &client)
|
|||
timer.reset();
|
||||
client.Send(msgSend);
|
||||
}
|
||||
Sleep(1);
|
||||
}
|
||||
|
||||
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>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="IServer.cpp" />
|
||||
<ClCompile Include="ServerMain.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="IClient.h" />
|
||||
<ClInclude Include="IServer.h" />
|
||||
<ClInclude Include="ISession.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
|
|
@ -18,5 +18,19 @@
|
|||
<ClCompile Include="ServerMain.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</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>
|
||||
</Project>
|
|
@ -12,6 +12,10 @@
|
|||
#include "../../Misc/Utilities.h"
|
||||
#include "../../Misc/Utilities-Impl.h"
|
||||
|
||||
#include "IServer.h"
|
||||
#include "IClient.h"
|
||||
#include "ISession.h"
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
using namespace std;
|
||||
|
@ -22,12 +26,15 @@ using namespace Utility;
|
|||
using namespace ::Utility::DynamicMemory;
|
||||
|
||||
void PrintOutMessage(ProtocolSet* set);
|
||||
void clientProc(IClient* client);
|
||||
vector<ThreadedClient*> clients;
|
||||
|
||||
int main()
|
||||
{
|
||||
SmartPointer<OysterByte> sendBuffer = SmartPointer<OysterByte>(new OysterByte);
|
||||
SmartPointer<OysterByte> recvBuffer = SmartPointer<OysterByte>(new OysterByte());
|
||||
{
|
||||
SmartPointer<OysterByte> sendBuffer = new OysterByte;
|
||||
SmartPointer<OysterByte> recvBuffer = new OysterByte;
|
||||
ProtocolSet* set = new ProtocolSet;
|
||||
|
||||
IPostBox<SmartPointer<int>> *postBox = new PostBox<SmartPointer<int>>();
|
||||
IPostBox<SmartPointer<OysterByte>> *recvPostBox = new PostBox<SmartPointer<OysterByte>>();
|
||||
|
||||
|
@ -39,11 +46,18 @@ int main()
|
|||
{
|
||||
cout << "errorMessage: unable to start winsock" << endl;
|
||||
}
|
||||
|
||||
/*
|
||||
IServer server;
|
||||
IServer::INIT_DESC initDesc;
|
||||
initDesc.port = 9876;
|
||||
initDesc.proc = clientProc;
|
||||
server.Init(initDesc);
|
||||
*/
|
||||
//Create socket
|
||||
Listener listener;
|
||||
listener.Init(9876);
|
||||
listener.SetPostBox(postBox);
|
||||
listener.Start();
|
||||
Sleep(1000);
|
||||
|
||||
//Start listening
|
||||
|
@ -62,9 +76,14 @@ int main()
|
|||
t.Pack(test, sendBuffer);
|
||||
|
||||
WinTimer timer;
|
||||
|
||||
/* DEBUGGING: Connect 25 clients
|
||||
for(int i = 0; i < 25; i++)
|
||||
{
|
||||
clients.push_back(new ThreadedClient(recvPostBox, 1));
|
||||
}*/
|
||||
|
||||
vector<ThreadedClient*> clients;
|
||||
SmartPointer<int> client = SmartPointer<int>();
|
||||
SmartPointer<int> client = int();
|
||||
while(1)
|
||||
{
|
||||
//Fetch new clients from the postbox
|
||||
|
@ -92,21 +111,22 @@ int main()
|
|||
{
|
||||
t.Unpack(set, recvBuffer);
|
||||
|
||||
PrintOutMessage(set);
|
||||
//PrintOutMessage(set);
|
||||
set->Release();
|
||||
}
|
||||
|
||||
Sleep(1);
|
||||
}
|
||||
//server.Stop();
|
||||
//server.Shutdown();
|
||||
listener.Shutdown();
|
||||
Sleep(1000);
|
||||
|
||||
system("pause");
|
||||
|
||||
for(int i = 0; i < clients.size(); i++)
|
||||
for(int i = 0; i < (int)clients.size(); i++)
|
||||
delete clients.at(i);
|
||||
|
||||
delete postBox;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -134,4 +154,10 @@ void PrintOutMessage(ProtocolSet* set)
|
|||
cout << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void clientProc(IClient* client)
|
||||
{
|
||||
cout << "Proc" << endl;
|
||||
//clients.push_back(client);
|
||||
}
|
Loading…
Reference in New Issue