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