PostBox
Created PostBox and IPostBox. Listener post new clients to PostBox.
This commit is contained in:
parent
0c40ca1be5
commit
1617606145
|
@ -62,7 +62,7 @@ using namespace Utility::DynamicMemory::SmartPointer;
|
|||
~PrivateData()
|
||||
{
|
||||
//@todo TODO: Make detatch avalible.
|
||||
//this->threadData->workerThread->detach();
|
||||
this->threadData->workerThread->detach();
|
||||
|
||||
this->threadData->owner = 0;
|
||||
|
||||
|
|
|
@ -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
|
|
@ -15,7 +15,7 @@ namespace Oyster
|
|||
|
||||
public:
|
||||
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;
|
||||
|
||||
};
|
||||
}
|
||||
|
|
|
@ -4,8 +4,6 @@ using namespace Oyster::Network::Server;
|
|||
|
||||
Listener::Listener()
|
||||
{
|
||||
newSocket = false;
|
||||
tempSocket = 0;
|
||||
}
|
||||
|
||||
Listener::~Listener()
|
||||
|
@ -27,19 +25,16 @@ bool Listener::Init(unsigned int port)
|
|||
return true;
|
||||
}
|
||||
|
||||
int Listener::GetNewClient()
|
||||
void Listener::Shutdown()
|
||||
{
|
||||
thread.Terminate();
|
||||
}
|
||||
|
||||
void Listener::SetPostBox(Oyster::Network::IPostBox<int>* postBox)
|
||||
{
|
||||
mutex.LockMutex();
|
||||
int temp = -1;
|
||||
|
||||
if(newSocket)
|
||||
{
|
||||
temp = tempSocket;
|
||||
newSocket = false;
|
||||
}
|
||||
this->postBox = postBox;
|
||||
mutex.UnlockMutex();
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
int Listener::Accept()
|
||||
|
@ -48,8 +43,7 @@ int Listener::Accept()
|
|||
clientSocket = connection->Listen();
|
||||
|
||||
mutex.LockMutex();
|
||||
tempSocket = clientSocket;
|
||||
newSocket = true;
|
||||
postBox->PostMessage(clientSocket);
|
||||
mutex.UnlockMutex();
|
||||
|
||||
return clientSocket;
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include "../NetworkDependencies/Connection.h"
|
||||
#include "../../Misc/Thread/OysterThread.h"
|
||||
#include "../../Misc/Thread/OysterMutex.h"
|
||||
|
||||
#include "IPostBox.h"
|
||||
|
||||
namespace Oyster
|
||||
{
|
||||
|
@ -24,8 +24,10 @@ namespace Oyster
|
|||
~Listener();
|
||||
|
||||
bool Init(unsigned int port);
|
||||
void Shutdown();
|
||||
int Accept();
|
||||
int GetNewClient();
|
||||
void SetPostBox(IPostBox<int>* postBox);
|
||||
|
||||
//Thread functions
|
||||
bool DoWork();
|
||||
|
@ -37,12 +39,12 @@ namespace Oyster
|
|||
|
||||
private:
|
||||
::Oyster::Network::Connection* connection;
|
||||
int tempSocket;
|
||||
bool newSocket;
|
||||
|
||||
::Oyster::Thread::OysterThread thread;
|
||||
OysterMutex mutex;
|
||||
|
||||
IPostBox<int>* postBox;
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,14 +149,6 @@
|
|||
<ProjectReference Include="..\..\Misc\Misc.vcxproj">
|
||||
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
|
||||
</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>
|
||||
<ClCompile Include="Connection.cpp" />
|
||||
|
@ -173,6 +165,7 @@
|
|||
<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\MessagesInclude.h" />
|
||||
|
@ -180,6 +173,7 @@
|
|||
<ClInclude Include="OysterByte.h" />
|
||||
<ClInclude Include="Packing.h" />
|
||||
<ClInclude Include="ITranslate.h" />
|
||||
<ClInclude Include="PostBox.h" />
|
||||
<ClInclude Include="Protocols.h" />
|
||||
<ClInclude Include="Translator.h" />
|
||||
<ClInclude Include="WinsockFunctions.h" />
|
||||
|
|
|
@ -25,5 +25,7 @@
|
|||
<ClInclude Include="IListener.h" />
|
||||
<ClInclude Include="WinsockFunctions.h" />
|
||||
<ClInclude Include="OysterByte.h" />
|
||||
<ClInclude Include="IPostBox.h" />
|
||||
<ClInclude Include="PostBox.h" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -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
|
|
@ -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();
|
||||
MessageHeader *message = new MessageHeader();
|
||||
|
@ -60,5 +60,5 @@ ProtocolSet* Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
|
|||
}
|
||||
delete header;
|
||||
|
||||
return set;
|
||||
//return set;
|
||||
}
|
|
@ -17,15 +17,13 @@ namespace Oyster
|
|||
class Translator : public ITranslate
|
||||
{
|
||||
public:
|
||||
Translator () { /*msg = new unsigned char[1601];*/ };
|
||||
~Translator() { /*if(msg != NULL) { delete [] this->msg; }*/ };
|
||||
Translator () { };
|
||||
~Translator() { };
|
||||
|
||||
void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes );
|
||||
Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, OysterByte& bytes );
|
||||
void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes );
|
||||
|
||||
private:
|
||||
//unsigned char* msg;
|
||||
//OysterByte bytes;
|
||||
|
||||
};
|
||||
}
|
||||
|
|
|
@ -88,7 +88,7 @@ void chat(Client &client)
|
|||
set->Release();
|
||||
msgRecv.Clear(1000);
|
||||
|
||||
std::getline(std::cin, msgSend);
|
||||
/*std::getline(std::cin, msgSend);
|
||||
|
||||
|
||||
|
||||
|
@ -111,7 +111,7 @@ void chat(Client &client)
|
|||
chatDone = true;
|
||||
}
|
||||
|
||||
cin.clear();
|
||||
cin.clear();*/
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -149,9 +149,6 @@
|
|||
<ProjectReference Include="..\..\Misc\Misc.vcxproj">
|
||||
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\OysterMath\OysterMath.vcxproj">
|
||||
<Project>{f10cbc03-9809-4cba-95d8-327c287b18ee}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\NetworkDependencies\NetworkDependencies.vcxproj">
|
||||
<Project>{c5aa09d0-6594-4cd3-bd92-1d380c7b3b50}</Project>
|
||||
</ProjectReference>
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
#include <iostream>
|
||||
#include <WinSock2.h>
|
||||
#include <vector>
|
||||
#include <vld.h>
|
||||
#include "../NetworkDependencies/WinsockFunctions.h"
|
||||
#include "../NetworkDependencies/Listener.h"
|
||||
#include "../NetworkDependencies/Translator.h"
|
||||
#include "Client.h"
|
||||
#include "../NetworkDependencies/OysterByte.h"
|
||||
#include "../NetworkDependencies/PostBox.h"
|
||||
#include "../../Misc/WinTimer.h"
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
|
@ -13,10 +16,12 @@ using namespace std;
|
|||
using namespace Oyster::Network::Server;
|
||||
using namespace Oyster::Network;
|
||||
using namespace ::Protocols;
|
||||
using namespace Utility;
|
||||
|
||||
int main()
|
||||
{
|
||||
OysterByte recvBuffer;
|
||||
IPostBox<int>* postBox = new PostBox<int>();
|
||||
|
||||
cout << "Server" << endl;
|
||||
Translator t;
|
||||
|
@ -30,6 +35,7 @@ int main()
|
|||
//Create socket
|
||||
Listener listener;
|
||||
listener.Init(9876);
|
||||
listener.SetPostBox(postBox);
|
||||
Sleep(1000);
|
||||
//Start listening
|
||||
//Accept a client
|
||||
|
@ -37,7 +43,7 @@ int main()
|
|||
test.clientID = 0;
|
||||
test.size = 2;
|
||||
test.textMessage = "hej";
|
||||
test.numOfFloats = 35;
|
||||
test.numOfFloats = 0;
|
||||
test.f = new float[test.numOfFloats];
|
||||
float temp = 395.456f;
|
||||
for(int i = 0; i < (int)test.numOfFloats; i++)
|
||||
|
@ -48,18 +54,35 @@ int main()
|
|||
|
||||
t.Pack(test, recvBuffer);
|
||||
|
||||
WinTimer timer;
|
||||
|
||||
vector<Client*> clients;
|
||||
int client = -1;
|
||||
while(1)
|
||||
{
|
||||
int client = listener.GetNewClient();
|
||||
client = -1;
|
||||
postBox->FetchMessage(client);
|
||||
if(client != -1)
|
||||
{
|
||||
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();
|
||||
|
|
Loading…
Reference in New Issue