Danbias/Code/Network/OysterNetworkServer/ServerMain.cpp

134 lines
3.0 KiB
C++
Raw Normal View History

2013-11-18 16:47:57 +01:00
#include <iostream>
#include <WinSock2.h>
#include <vector>
2013-11-22 14:23:08 +01:00
#include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/Translator.h"
2013-12-03 13:08:04 +01:00
#include "../NetworkDependencies/ThreadedClient.h"
#include "../NetworkDependencies/OysterByte.h"
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/WinTimer.h"
#include "../../Misc/Utilities.h"
#include "../../Misc/Utilities-Impl.h"
2013-11-18 16:47:57 +01:00
#pragma comment(lib, "ws2_32.lib")
using namespace std;
using namespace Oyster::Network::Server;
2013-11-21 14:49:30 +01:00
using namespace Oyster::Network;
using namespace ::Protocols;
using namespace Utility;
using namespace ::Utility::DynamicMemory;
void PrintOutMessage(ProtocolSet* set);
int main()
{
SmartPointer<OysterByte> sendBuffer = SmartPointer<OysterByte>(new OysterByte);
SmartPointer<OysterByte> recvBuffer = NULL;
ProtocolSet* set = new ProtocolSet;
IPostBox<SmartPointer<int>> *postBox = new PostBox<SmartPointer<int>>();
IPostBox<SmartPointer<OysterByte>> *recvPostBox = new PostBox<SmartPointer<OysterByte>>();
2013-11-21 14:49:30 +01:00
cout << "Server" << endl;
2013-11-22 08:56:00 +01:00
Translator t;
int errorCode = 0;
2013-11-19 14:21:25 +01:00
2013-11-26 13:45:03 +01:00
if(!InitWinSock())
{
2013-11-26 13:45:03 +01:00
cout << "errorMessage: unable to start winsock" << endl;
}
//Create socket
Listener listener;
listener.Init(9876);
listener.SetPostBox(postBox);
Sleep(1000);
//Start listening
2013-11-19 14:21:25 +01:00
//Accept a client
ProtocolPlayerPos test;
test.clientID = 0;
test.ID = 5;
test.nrOfFloats = 10;
test.matrix = new float[test.nrOfFloats];
for(int i = 0; i < (int)test.nrOfFloats; i++)
{
test.matrix[i] = (float)i;
}
t.Pack(test, sendBuffer);
WinTimer timer;
2013-12-03 13:08:04 +01:00
vector<ThreadedClient*> clients;
SmartPointer<int> client = SmartPointer<int>();
while(1)
{
//Fetch new clients from the postbox
if(postBox->FetchMessage(client))
{
cout << "Client connected: " << *client << endl;
clients.push_back(new ThreadedClient(recvPostBox, *client));
clients.at(clients.size()-1)->Send(sendBuffer);
}
//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(sendBuffer);
}
}
//Fetch messages
if(recvPostBox->FetchMessage(recvBuffer))
{
t.Unpack(set, recvBuffer);
delete recvBuffer;
PrintOutMessage(set);
set->Release();
}
Sleep(1);
}
listener.Shutdown();
system("pause");
2013-11-22 15:48:49 +01:00
delete postBox;
return 0;
}
void PrintOutMessage(ProtocolSet* set)
{
switch(set->type)
2013-11-19 14:21:25 +01:00
{
case PackageType_header:
break;
case PackageType_test:
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
2013-11-27 11:28:11 +01:00
for(int i = 0; i < (int)set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ' ;
}
cout << endl;
break;
2013-11-19 14:21:25 +01:00
case PackageType_player_pos:
//cout << "ID " << set->Protocol.pPlayerPos->ID << endl;
for(int i = 0; i < (int)set->Protocol.pPlayerPos->nrOfFloats; i++)
{
cout << set->Protocol.pPlayerPos->matrix[i] << ' ';
}
cout << endl;
break;
2013-11-19 14:21:25 +01:00
}
}