Created PostBox and IPostBox.
Listener post new clients to PostBox.
This commit is contained in:
Pontus Fransson 2013-11-28 16:15:28 +01:00
parent 0c40ca1be5
commit 1617606145
13 changed files with 151 additions and 44 deletions

View File

@ -62,7 +62,7 @@ using namespace Utility::DynamicMemory::SmartPointer;
~PrivateData() ~PrivateData()
{ {
//@todo TODO: Make detatch avalible. //@todo TODO: Make detatch avalible.
//this->threadData->workerThread->detach(); this->threadData->workerThread->detach();
this->threadData->owner = 0; this->threadData->owner = 0;

View File

@ -0,0 +1,25 @@
#ifndef NETWORK_DEPENDENCIES_I_POST_BOX_H
#define NETWORK_DEPENDENCIES_I_POST_BOX_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
namespace Oyster
{
namespace Network
{
template <class T>
class IPostBox
{
public:
virtual ~IPostBox() {}
virtual void PostMessage(T& message) = 0;
virtual void FetchMessage(T& message) = 0;
virtual bool IsFull() = 0;
};
}
}
#endif

View File

@ -15,7 +15,7 @@ namespace Oyster
public: public:
virtual void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes) = 0; virtual void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes) = 0;
virtual Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ) = 0; virtual void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ) = 0;
}; };
} }

View File

@ -4,8 +4,6 @@ using namespace Oyster::Network::Server;
Listener::Listener() Listener::Listener()
{ {
newSocket = false;
tempSocket = 0;
} }
Listener::~Listener() Listener::~Listener()
@ -27,19 +25,16 @@ bool Listener::Init(unsigned int port)
return true; return true;
} }
int Listener::GetNewClient() void Listener::Shutdown()
{
thread.Terminate();
}
void Listener::SetPostBox(Oyster::Network::IPostBox<int>* postBox)
{ {
mutex.LockMutex(); mutex.LockMutex();
int temp = -1; this->postBox = postBox;
if(newSocket)
{
temp = tempSocket;
newSocket = false;
}
mutex.UnlockMutex(); mutex.UnlockMutex();
return temp;
} }
int Listener::Accept() int Listener::Accept()
@ -48,8 +43,7 @@ int Listener::Accept()
clientSocket = connection->Listen(); clientSocket = connection->Listen();
mutex.LockMutex(); mutex.LockMutex();
tempSocket = clientSocket; postBox->PostMessage(clientSocket);
newSocket = true;
mutex.UnlockMutex(); mutex.UnlockMutex();
return clientSocket; return clientSocket;

View File

@ -9,7 +9,7 @@
#include "../NetworkDependencies/Connection.h" #include "../NetworkDependencies/Connection.h"
#include "../../Misc/Thread/OysterThread.h" #include "../../Misc/Thread/OysterThread.h"
#include "../../Misc/Thread/OysterMutex.h" #include "../../Misc/Thread/OysterMutex.h"
#include "IPostBox.h"
namespace Oyster namespace Oyster
{ {
@ -24,8 +24,10 @@ namespace Oyster
~Listener(); ~Listener();
bool Init(unsigned int port); bool Init(unsigned int port);
void Shutdown();
int Accept(); int Accept();
int GetNewClient(); int GetNewClient();
void SetPostBox(IPostBox<int>* postBox);
//Thread functions //Thread functions
bool DoWork(); bool DoWork();
@ -37,12 +39,12 @@ namespace Oyster
private: private:
::Oyster::Network::Connection* connection; ::Oyster::Network::Connection* connection;
int tempSocket;
bool newSocket;
::Oyster::Thread::OysterThread thread; ::Oyster::Thread::OysterThread thread;
OysterMutex mutex; OysterMutex mutex;
IPostBox<int>* postBox;
}; };
} }
} }

View File

@ -149,14 +149,6 @@
<ProjectReference Include="..\..\Misc\Misc.vcxproj"> <ProjectReference Include="..\..\Misc\Misc.vcxproj">
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project> <Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\..\OysterMath\OysterMath.vcxproj">
<Project>{f10cbc03-9809-4cba-95d8-327c287b18ee}</Project>
<Private>true</Private>
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
<CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies>
<LinkLibraryDependencies>false</LinkLibraryDependencies>
<UseLibraryDependencyInputs>false</UseLibraryDependencyInputs>
</ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Connection.cpp" /> <ClCompile Include="Connection.cpp" />
@ -173,6 +165,7 @@
<ClInclude Include="Connection.h" /> <ClInclude Include="Connection.h" />
<ClInclude Include="IConnection.h" /> <ClInclude Include="IConnection.h" />
<ClInclude Include="IListener.h" /> <ClInclude Include="IListener.h" />
<ClInclude Include="IPostBox.h" />
<ClInclude Include="Listener.h" /> <ClInclude Include="Listener.h" />
<ClInclude Include="Messages\MessageHeader.h" /> <ClInclude Include="Messages\MessageHeader.h" />
<ClInclude Include="Messages\MessagesInclude.h" /> <ClInclude Include="Messages\MessagesInclude.h" />
@ -180,6 +173,7 @@
<ClInclude Include="OysterByte.h" /> <ClInclude Include="OysterByte.h" />
<ClInclude Include="Packing.h" /> <ClInclude Include="Packing.h" />
<ClInclude Include="ITranslate.h" /> <ClInclude Include="ITranslate.h" />
<ClInclude Include="PostBox.h" />
<ClInclude Include="Protocols.h" /> <ClInclude Include="Protocols.h" />
<ClInclude Include="Translator.h" /> <ClInclude Include="Translator.h" />
<ClInclude Include="WinsockFunctions.h" /> <ClInclude Include="WinsockFunctions.h" />

View File

@ -25,5 +25,7 @@
<ClInclude Include="IListener.h" /> <ClInclude Include="IListener.h" />
<ClInclude Include="WinsockFunctions.h" /> <ClInclude Include="WinsockFunctions.h" />
<ClInclude Include="OysterByte.h" /> <ClInclude Include="OysterByte.h" />
<ClInclude Include="IPostBox.h" />
<ClInclude Include="PostBox.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,72 @@
#ifndef NETWORK_DEPENDENCIES_POST_BOX_H
#define NETWORK_DEPENDENCIES_POST_BOX_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "IPostBox.h"
#include <queue>
#include "../../Misc/Thread/OysterMutex.h"
namespace Oyster
{
namespace Network
{
template <class T>
class PostBox : public IPostBox<T>
{
public:
PostBox();
virtual ~PostBox();
virtual void PostMessage(T& message);
virtual void FetchMessage(T& message);
virtual bool IsFull();
private:
std::queue<T> messages;
OysterMutex mutex;
};
//Implementation of PostBox
template <class T>
PostBox<T>::PostBox()
{
}
template <class T>
PostBox<T>::~PostBox()
{
}
template <class T>
void PostBox<T>::PostMessage(T& message)
{
mutex.LockMutex();
messages.push(message);
mutex.UnlockMutex();
}
template <class T>
void PostBox<T>::FetchMessage(T& message)
{
mutex.LockMutex();
if(IsFull())
{
message = messages.front();
messages.pop();
}
mutex.UnlockMutex();
}
template <class T>
bool PostBox<T>::IsFull()
{
return !messages.empty();
}
}
}
#endif

View File

@ -28,7 +28,7 @@ void Translator::Pack( ProtocolHeader &header, OysterByte& bytes )
} }
} }
ProtocolSet* Translator::Unpack(ProtocolSet* set, OysterByte& bytes ) void Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
{ {
ProtocolHeader *header = new ProtocolHeader(); ProtocolHeader *header = new ProtocolHeader();
MessageHeader *message = new MessageHeader(); MessageHeader *message = new MessageHeader();
@ -60,5 +60,5 @@ ProtocolSet* Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
} }
delete header; delete header;
return set; //return set;
} }

View File

@ -17,15 +17,13 @@ namespace Oyster
class Translator : public ITranslate class Translator : public ITranslate
{ {
public: public:
Translator () { /*msg = new unsigned char[1601];*/ }; Translator () { };
~Translator() { /*if(msg != NULL) { delete [] this->msg; }*/ }; ~Translator() { };
void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes ); void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes );
Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ); void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes );
private: private:
//unsigned char* msg;
//OysterByte bytes;
}; };
} }

View File

@ -88,7 +88,7 @@ void chat(Client &client)
set->Release(); set->Release();
msgRecv.Clear(1000); msgRecv.Clear(1000);
std::getline(std::cin, msgSend); /*std::getline(std::cin, msgSend);
@ -111,7 +111,7 @@ void chat(Client &client)
chatDone = true; chatDone = true;
} }
cin.clear(); cin.clear();*/
} }

View File

@ -149,9 +149,6 @@
<ProjectReference Include="..\..\Misc\Misc.vcxproj"> <ProjectReference Include="..\..\Misc\Misc.vcxproj">
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project> <Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\..\OysterMath\OysterMath.vcxproj">
<Project>{f10cbc03-9809-4cba-95d8-327c287b18ee}</Project>
</ProjectReference>
<ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj"> <ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj">
<Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project> <Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project>
</ProjectReference> </ProjectReference>

View File

@ -1,11 +1,14 @@
#include <iostream> #include <iostream>
#include <WinSock2.h> #include <WinSock2.h>
#include <vector>
#include <vld.h> #include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h" #include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkDependencies/Listener.h" #include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/Translator.h" #include "../NetworkDependencies/Translator.h"
#include "Client.h" #include "Client.h"
#include "../NetworkDependencies/OysterByte.h" #include "../NetworkDependencies/OysterByte.h"
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/WinTimer.h"
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
@ -13,10 +16,12 @@ using namespace std;
using namespace Oyster::Network::Server; using namespace Oyster::Network::Server;
using namespace Oyster::Network; using namespace Oyster::Network;
using namespace ::Protocols; using namespace ::Protocols;
using namespace Utility;
int main() int main()
{ {
OysterByte recvBuffer; OysterByte recvBuffer;
IPostBox<int>* postBox = new PostBox<int>();
cout << "Server" << endl; cout << "Server" << endl;
Translator t; Translator t;
@ -30,6 +35,7 @@ int main()
//Create socket //Create socket
Listener listener; Listener listener;
listener.Init(9876); listener.Init(9876);
listener.SetPostBox(postBox);
Sleep(1000); Sleep(1000);
//Start listening //Start listening
//Accept a client //Accept a client
@ -37,7 +43,7 @@ int main()
test.clientID = 0; test.clientID = 0;
test.size = 2; test.size = 2;
test.textMessage = "hej"; test.textMessage = "hej";
test.numOfFloats = 35; test.numOfFloats = 0;
test.f = new float[test.numOfFloats]; test.f = new float[test.numOfFloats];
float temp = 395.456f; float temp = 395.456f;
for(int i = 0; i < (int)test.numOfFloats; i++) for(int i = 0; i < (int)test.numOfFloats; i++)
@ -48,18 +54,35 @@ int main()
t.Pack(test, recvBuffer); t.Pack(test, recvBuffer);
WinTimer timer;
vector<Client*> clients;
int client = -1;
while(1) while(1)
{ {
int client = listener.GetNewClient(); client = -1;
postBox->FetchMessage(client);
if(client != -1) if(client != -1)
{ {
cout << "Client connected: " << client << endl; cout << "Client connected: " << client << endl;
Client client1(client); clients.push_back(new Client(client));
client1.Send(recvBuffer); clients.at(clients.size()-1)->Send(recvBuffer);
} }
//Sleep(100);
//Send a message every 1 secounds to all clients.
if(timer.getElapsedSeconds() > 1)
{
cout << "Sending to " << clients.size() << " clients." << endl;
timer.reset();
for(int i = 0; i < (int)clients.size(); i++)
{
clients.at(i)->Send(recvBuffer);
} }
}
Sleep(100);
}
listener.Shutdown();
/* int clientSocket = listener.Accept(); /* int clientSocket = listener.Accept();