Network - Stuff

This commit is contained in:
Pontus Fransson 2013-12-08 23:56:17 +01:00
parent bbb79a4ba9
commit a79dc61159
14 changed files with 251 additions and 114 deletions

View File

@ -94,9 +94,9 @@ namespace Oyster
template < typename Type > template < typename Type >
void ThreadSafeQueue<Type>::Push(Type item) void ThreadSafeQueue<Type>::Push(Type item)
{ {
mutex.LockMutex();
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;
@ -140,32 +140,33 @@ namespace Oyster
Type ThreadSafeQueue<Type>::Front() Type ThreadSafeQueue<Type>::Front()
{ {
mutex.LockMutex(); mutex.LockMutex();
Type temp = this->front->item;
return this->front->item;
mutex.UnlockMutex(); mutex.UnlockMutex();
return temp;
} }
template < typename Type > template < typename Type >
Type ThreadSafeQueue<Type>::Back() Type ThreadSafeQueue<Type>::Back()
{ {
mutex.LockMutex(); mutex.LockMutex();
Type temp = this->back->item;
return this->back->item;
mutex.UnlockMutex(); mutex.UnlockMutex();
return temp;
} }
template < typename Type > template < typename Type >
int ThreadSafeQueue<Type>::Size() int ThreadSafeQueue<Type>::Size()
{ {
mutex.LockMutex(); mutex.LockMutex();
int size = this->nrOfNodes;
return this->nrOfNodes;
mutex.UnlockMutex(); mutex.UnlockMutex();
return size;
} }
template < typename Type > template < typename Type >

View File

@ -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

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

@ -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();
@ -61,13 +87,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,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);

View File

@ -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" />

View File

@ -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>

View File

@ -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>>;
@ -57,29 +58,32 @@ int ThreadedClient::Send(SmartPointer<OysterByte>& byte)
mutex.LockMutex(); mutex.LockMutex();
this->sendPostBox->PostMessage(temp); this->sendPostBox->PostMessage(temp);
mutex.UnlockMutex(); mutex.UnlockMutex();
return 0; return 0;
} }
int ThreadedClient::Send() int ThreadedClient::Send()
{ {
int errorCode = 0; int errorCode = 0;
mutex.LockMutex(); 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(); mutex.UnlockMutex();
return errorCode; return errorCode;
} }
int ThreadedClient::Recv() int ThreadedClient::Recv()
{ {
int errorCode = 0; int errorCode = 0;
/*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)
@ -88,7 +92,7 @@ int ThreadedClient::Recv()
recvPostBox->PostMessage(temp); recvPostBox->PostMessage(temp);
mutex.UnlockMutex(); mutex.UnlockMutex();
} }
*/
return errorCode; return errorCode;
} }

View File

@ -5,9 +5,9 @@
// 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"
@ -16,19 +16,20 @@ namespace Oyster
{ {
namespace Network namespace Network
{ {
class ThreadedClient : public IClient, public Thread::IThreadObject class OysterByte;
class ThreadedClient : public Thread::IThreadObject
{ {
public: public:
ThreadedClient(); ThreadedClient();
ThreadedClient(unsigned int socket); 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(); virtual ~ThreadedClient();
int Send(Utility::DynamicMemory::SmartPointer< OysterByte >& byte); int Send(Utility::DynamicMemory::SmartPointer<OysterByte>& byte);
int Connect(unsigned short port, const char serverName[]); 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: private:
virtual int Send(); virtual int Send();

View File

@ -84,7 +84,7 @@ void chat(ThreadedClient &client)
{ {
t->Unpack(set, msgRecv); t->Unpack(set, msgRecv);
PrintOutMessage(set); //PrintOutMessage(set);
set->Release(); set->Release();
} }

View File

@ -8,12 +8,13 @@
class IClient class IClient
{ {
public: public:
IClient() {}
virtual ~IClient() {} virtual ~IClient() {}
virtual void Disconnect() {}; virtual void Disconnect() {}
virtual bool IsConnected() {}; virtual bool IsConnected() {return true;}
virtual void Send() {}; virtual void Send() {}
private: private:

View File

@ -1,35 +1,139 @@
#include "IServer.h" #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() IServer::IServer()
{ {
privateData = new PrivateData();
} }
IServer::~IServer() IServer::~IServer()
{ {
if(privateData)
{
delete privateData;
}
} }
bool IServer::Init(INIT_DESC& initDesc) bool IServer::Init(INIT_DESC& initDesc)
{ {
privateData->Init(initDesc);
return true; return true;
} }
bool IServer::Start() bool IServer::Start()
{ {
privateData->Start();
return true; return true;
} }
bool IServer::Stop() bool IServer::Stop()
{ {
privateData->Stop();
return true; return true;
} }
bool IServer::Shutdown() bool IServer::Shutdown()
{ {
privateData->Shutdown();
return true; return true;
} }
@ -42,4 +146,9 @@ void IServer::AddSession(ISession* session)
void IServer::RemoveSession(ISession* session) void IServer::RemoveSession(ISession* session)
{ {
}
bool IServer::IsStarted() const
{
return privateData->started;
} }

View File

@ -5,30 +5,44 @@
// Created by Pontus Fransson 2013 // // Created by Pontus Fransson 2013 //
///////////////////////////////////// /////////////////////////////////////
class IServer #include "IClient.h"
namespace Oyster
{ {
class ISession; namespace Network
public:
struct INIT_DESC
{ {
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();
IServer(); virtual bool Init(INIT_DESC& initDesc);
virtual ~IServer(); virtual bool Start();
virtual bool Stop();
virtual bool Shutdown();
virtual bool Init(INIT_DESC& initDesc); virtual void AddSession(ISession* session);
virtual bool Start(); virtual void RemoveSession(ISession* session);
virtual bool Stop();
virtual bool Shutdown();
virtual void AddSession(ISession* session); virtual bool IsStarted() const;
virtual void RemoveSession(ISession* session);
private: private:
struct PrivateData;
PrivateData* privateData;
};
}; }
}
}
#endif #endif

View File

@ -13,6 +13,7 @@
#include "../../Misc/Utilities-Impl.h" #include "../../Misc/Utilities-Impl.h"
#include "IServer.h" #include "IServer.h"
#include "IClient.h"
#include "ISession.h" #include "ISession.h"
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
@ -25,18 +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()
{ {
IServer server;
IServer::INIT_DESC initDesc;
server.Init(initDesc);
//Old program
SmartPointer<OysterByte> sendBuffer = new OysterByte; SmartPointer<OysterByte> sendBuffer = new OysterByte;
SmartPointer<OysterByte> recvBuffer = 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>>();
@ -48,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
@ -72,7 +77,6 @@ int main()
WinTimer timer; WinTimer timer;
vector<ThreadedClient*> clients;
SmartPointer<int> client = int(); SmartPointer<int> client = int();
while(1) while(1)
{ {
@ -101,21 +105,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;
} }
@ -143,4 +148,10 @@ void PrintOutMessage(ProtocolSet* set)
cout << endl; cout << endl;
break; break;
} }
}
void clientProc(IClient* client)
{
cout << "Proc" << endl;
//clients.push_back(client);
} }