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-12-04 12:40:49 +01:00
|
|
|
#include "../../Misc/Utilities.h"
|
|
|
|
#include "../../Misc/Utilities-Impl.h"
|
2013-11-18 16:47:57 +01:00
|
|
|
|
2013-12-06 10:45:53 +01:00
|
|
|
#include "IServer.h"
|
2013-12-08 23:56:17 +01:00
|
|
|
#include "IClient.h"
|
2013-12-06 10:45:53 +01:00
|
|
|
#include "ISession.h"
|
|
|
|
|
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-12-04 12:40:49 +01:00
|
|
|
using namespace ::Utility::DynamicMemory;
|
2013-11-21 13:42:38 +01:00
|
|
|
|
2013-12-03 23:12:48 +01:00
|
|
|
void PrintOutMessage(ProtocolSet* set);
|
2013-12-08 23:56:17 +01:00
|
|
|
void clientProc(IClient* client);
|
|
|
|
vector<ThreadedClient*> clients;
|
2013-12-03 23:12:48 +01:00
|
|
|
|
2013-11-18 16:34:50 +01:00
|
|
|
int main()
|
2013-12-06 10:45:53 +01:00
|
|
|
{
|
|
|
|
SmartPointer<OysterByte> sendBuffer = new OysterByte;
|
2013-12-08 23:56:17 +01:00
|
|
|
SmartPointer<OysterByte> recvBuffer = new OysterByte;
|
2013-12-03 23:12:48 +01:00
|
|
|
ProtocolSet* set = new ProtocolSet;
|
2013-12-08 23:56:17 +01:00
|
|
|
|
2013-12-04 12:40:49 +01:00
|
|
|
IPostBox<SmartPointer<int>> *postBox = new PostBox<SmartPointer<int>>();
|
|
|
|
IPostBox<SmartPointer<OysterByte>> *recvPostBox = new PostBox<SmartPointer<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-12-04 12:40:49 +01:00
|
|
|
int errorCode = 0;
|
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
|
|
|
}
|
2013-12-08 23:56:17 +01:00
|
|
|
/*
|
|
|
|
IServer server;
|
|
|
|
IServer::INIT_DESC initDesc;
|
|
|
|
initDesc.port = 9876;
|
|
|
|
initDesc.proc = clientProc;
|
|
|
|
server.Init(initDesc);
|
|
|
|
*/
|
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-12-08 23:56:17 +01:00
|
|
|
listener.Start();
|
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-09 10:48:43 +01:00
|
|
|
|
|
|
|
/* DEBUGGING: Connect 25 clients
|
|
|
|
for(int i = 0; i < 25; i++)
|
|
|
|
{
|
|
|
|
clients.push_back(new ThreadedClient(recvPostBox, 1));
|
|
|
|
}*/
|
2013-11-28 16:15:28 +01:00
|
|
|
|
2013-12-06 10:45:53 +01:00
|
|
|
SmartPointer<int> client = int();
|
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
|
|
|
{
|
2013-12-04 12:40:49 +01:00
|
|
|
cout << "Client connected: " << *client << endl;
|
|
|
|
clients.push_back(new ThreadedClient(recvPostBox, *client));
|
2013-11-27 14:33:08 +01:00
|
|
|
|
2013-12-04 12:40:49 +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-04 12:40:49 +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))
|
|
|
|
{
|
2013-12-04 12:40:49 +01:00
|
|
|
t.Unpack(set, recvBuffer);
|
2013-12-03 23:12:48 +01:00
|
|
|
|
2013-12-08 23:56:17 +01:00
|
|
|
//PrintOutMessage(set);
|
2013-12-03 23:12:48 +01:00
|
|
|
set->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
Sleep(1);
|
2013-11-27 14:33:08 +01:00
|
|
|
}
|
2013-12-08 23:56:17 +01:00
|
|
|
//server.Stop();
|
|
|
|
//server.Shutdown();
|
2013-11-28 16:15:28 +01:00
|
|
|
listener.Shutdown();
|
2013-12-04 14:58:15 +01:00
|
|
|
Sleep(1000);
|
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-08 23:56:17 +01:00
|
|
|
for(int i = 0; i < (int)clients.size(); i++)
|
2013-12-04 14:58:15 +01:00
|
|
|
delete clients.at(i);
|
|
|
|
|
2013-12-03 23:12:48 +01:00
|
|
|
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-08 23:56:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void clientProc(IClient* client)
|
|
|
|
{
|
|
|
|
cout << "Proc" << endl;
|
|
|
|
//clients.push_back(client);
|
2013-12-03 23:12:48 +01:00
|
|
|
}
|