Network - Stuff
This commit is contained in:
parent
bbb79a4ba9
commit
a79dc61159
|
@ -94,9 +94,9 @@ namespace Oyster
|
|||
template < typename Type >
|
||||
void ThreadSafeQueue<Type>::Push(Type item)
|
||||
{
|
||||
mutex.LockMutex();
|
||||
Node *e = new Node(item);
|
||||
|
||||
mutex.LockMutex();
|
||||
if(this->front != NULL)
|
||||
{
|
||||
this->back->next = e;
|
||||
|
@ -140,32 +140,33 @@ namespace Oyster
|
|||
Type ThreadSafeQueue<Type>::Front()
|
||||
{
|
||||
mutex.LockMutex();
|
||||
|
||||
return this->front->item;
|
||||
|
||||
Type temp = this->front->item;
|
||||
mutex.UnlockMutex();
|
||||
|
||||
return temp;
|
||||
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
Type ThreadSafeQueue<Type>::Back()
|
||||
{
|
||||
mutex.LockMutex();
|
||||
|
||||
return this->back->item;
|
||||
|
||||
Type temp = this->back->item;
|
||||
mutex.UnlockMutex();
|
||||
|
||||
return temp;
|
||||
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
int ThreadSafeQueue<Type>::Size()
|
||||
{
|
||||
mutex.LockMutex();
|
||||
|
||||
return this->nrOfNodes;
|
||||
|
||||
int size = this->nrOfNodes;
|
||||
mutex.UnlockMutex();
|
||||
|
||||
return size;
|
||||
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
|
|
|
@ -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();
|
||||
|
@ -61,13 +87,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);
|
||||
|
|
|
@ -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>
|
|
@ -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>>;
|
||||
|
@ -57,21 +58,24 @@ int ThreadedClient::Send(SmartPointer<OysterByte>& byte)
|
|||
mutex.LockMutex();
|
||||
this->sendPostBox->PostMessage(temp);
|
||||
mutex.UnlockMutex();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ThreadedClient::Send()
|
||||
{
|
||||
int errorCode = 0;
|
||||
|
||||
mutex.LockMutex();
|
||||
if(sendPostBox->IsFull())
|
||||
{
|
||||
//SmartPointer<OysterByte> temp = NULL;
|
||||
//sendPostBox->FetchMessage(temp);
|
||||
//errorCode = this->connection->Send(temp);
|
||||
SmartPointer<OysterByte> temp = new OysterByte;
|
||||
sendPostBox->FetchMessage(temp);
|
||||
errorCode = this->connection->Send(temp);
|
||||
}
|
||||
mutex.UnlockMutex();
|
||||
|
||||
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
|
@ -79,7 +83,7 @@ int ThreadedClient::Recv()
|
|||
{
|
||||
int errorCode = 0;
|
||||
|
||||
/*SmartPointer<OysterByte> temp = new OysterByte();
|
||||
SmartPointer<OysterByte> temp = new OysterByte;
|
||||
errorCode = this->connection->Recieve(temp);
|
||||
|
||||
if(errorCode == 0)
|
||||
|
@ -88,7 +92,7 @@ int ThreadedClient::Recv()
|
|||
recvPostBox->PostMessage(temp);
|
||||
mutex.UnlockMutex();
|
||||
}
|
||||
*/
|
||||
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
// 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"
|
||||
|
@ -16,19 +16,20 @@ 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);
|
||||
int 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();
|
||||
|
|
|
@ -84,7 +84,7 @@ void chat(ThreadedClient &client)
|
|||
{
|
||||
t->Unpack(set, msgRecv);
|
||||
|
||||
PrintOutMessage(set);
|
||||
//PrintOutMessage(set);
|
||||
set->Release();
|
||||
}
|
||||
|
||||
|
|
|
@ -8,12 +8,13 @@
|
|||
class IClient
|
||||
{
|
||||
public:
|
||||
IClient() {}
|
||||
virtual ~IClient() {}
|
||||
|
||||
virtual void Disconnect() {};
|
||||
virtual bool IsConnected() {};
|
||||
virtual void Disconnect() {}
|
||||
virtual bool IsConnected() {return true;}
|
||||
|
||||
virtual void Send() {};
|
||||
virtual void Send() {}
|
||||
|
||||
private:
|
||||
|
||||
|
|
|
@ -1,35 +1,139 @@
|
|||
#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;
|
||||
}
|
||||
|
@ -43,3 +147,8 @@ void IServer::RemoveSession(ISession* session)
|
|||
{
|
||||
|
||||
}
|
||||
|
||||
bool IServer::IsStarted() const
|
||||
{
|
||||
return privateData->started;
|
||||
}
|
|
@ -5,14 +5,22 @@
|
|||
// Created by Pontus Fransson 2013 //
|
||||
/////////////////////////////////////
|
||||
|
||||
class IServer
|
||||
{
|
||||
class ISession;
|
||||
#include "IClient.h"
|
||||
|
||||
public:
|
||||
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();
|
||||
|
@ -26,9 +34,15 @@ public:
|
|||
virtual void AddSession(ISession* session);
|
||||
virtual void RemoveSession(ISession* session);
|
||||
|
||||
private:
|
||||
virtual bool IsStarted() const;
|
||||
|
||||
private:
|
||||
struct PrivateData;
|
||||
PrivateData* privateData;
|
||||
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
|
@ -13,6 +13,7 @@
|
|||
#include "../../Misc/Utilities-Impl.h"
|
||||
|
||||
#include "IServer.h"
|
||||
#include "IClient.h"
|
||||
#include "ISession.h"
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
@ -25,18 +26,15 @@ using namespace Utility;
|
|||
using namespace ::Utility::DynamicMemory;
|
||||
|
||||
void PrintOutMessage(ProtocolSet* set);
|
||||
void clientProc(IClient* client);
|
||||
vector<ThreadedClient*> clients;
|
||||
|
||||
int main()
|
||||
{
|
||||
IServer server;
|
||||
IServer::INIT_DESC initDesc;
|
||||
server.Init(initDesc);
|
||||
|
||||
|
||||
//Old program
|
||||
SmartPointer<OysterByte> sendBuffer = new OysterByte;
|
||||
SmartPointer<OysterByte> recvBuffer = 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>>();
|
||||
|
||||
|
@ -48,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
|
||||
|
@ -72,7 +77,6 @@ int main()
|
|||
|
||||
WinTimer timer;
|
||||
|
||||
vector<ThreadedClient*> clients;
|
||||
SmartPointer<int> client = int();
|
||||
while(1)
|
||||
{
|
||||
|
@ -101,21 +105,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;
|
||||
}
|
||||
|
||||
|
@ -144,3 +149,9 @@ void PrintOutMessage(ProtocolSet* set)
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void clientProc(IClient* client)
|
||||
{
|
||||
cout << "Proc" << endl;
|
||||
//clients.push_back(client);
|
||||
}
|
Loading…
Reference in New Issue