Danbias/Code/Network/OysterNetworkClient/ClientMain.cpp

130 lines
2.7 KiB
C++
Raw Normal View History

2013-11-18 16:47:57 +01:00
#include <iostream>
#include <WinSock2.h>
2013-11-22 14:23:08 +01:00
#include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
2013-11-22 09:17:07 +01:00
#include "..\NetworkDependencies\Translator.h"
2013-11-22 14:23:08 +01:00
#include "..\NetworkDependencies\Protocols.h"
#include "../NetworkDependencies/OysterByte.h"
#include "../../Misc/ThreadSafeQueue.h"
2013-12-03 13:08:04 +01:00
#include "../NetworkDependencies/ThreadedClient.h"
#include "../../Misc/WinTimer.h"
#include "../../Misc/Utilities.h"
#pragma comment(lib, "ws2_32.lib")
2013-11-22 09:17:07 +01:00
2013-11-18 16:47:57 +01:00
using namespace std;
using namespace Oyster::Network::Protocols;
2013-12-03 13:08:04 +01:00
using namespace Oyster::Network;
using namespace Utility;
using namespace Utility::DynamicMemory;
2013-12-03 13:08:04 +01:00
void chat(ThreadedClient &client);
void PrintOutMessage(ProtocolSet* set);
2013-11-22 08:56:00 +01:00
int main()
{
2013-11-26 13:45:03 +01:00
int errorCode;
char msgRecv[255] = "\0";
2013-11-26 13:45:03 +01:00
InitWinSock();
cout << "Client" << endl;
2013-11-18 16:47:57 +01:00
//Create Client
ThreadedClient* client = new ThreadedClient;
//Connect to server
errorCode = client->Connect(9876, "localhost");
2013-11-26 13:45:03 +01:00
if(errorCode != 0)
{
wstring errorTest = GetErrorMessage(errorCode);
wcout << "errorMessage: " << errorTest << endl;
}
2013-11-22 08:56:00 +01:00
chat(*client);
delete client;
2013-11-26 13:45:03 +01:00
ShutdownWinSock();
system("pause");
return 0;
}
2013-12-03 13:08:04 +01:00
void chat(ThreadedClient &client)
{
2013-11-22 09:17:07 +01:00
Oyster::Network::Translator *t = new Oyster::Network::Translator();
IPostBox< SmartPointer<OysterByte >> *postBox = new PostBox< SmartPointer<OysterByte >>;
2013-11-22 09:17:07 +01:00
client.setRecvPostBox(postBox);
SmartPointer<OysterByte> msgRecv = new OysterByte();
SmartPointer<OysterByte> msgSend = new OysterByte();
2013-11-22 09:17:07 +01:00
ProtocolSet* set = new ProtocolSet;
ProtocolPlayerPos test;
test.ID = 5;
test.nrOfFloats = 5;
test.matrix = new float[test.nrOfFloats];
float temp = 10;
for(int i = 0; i < (int)test.nrOfFloats; i++)
{
test.matrix[i] = temp;
temp++;
}
t->Pack(test, msgSend);
WinTimer timer;
while(1)
{
//Fetch new messages from the postbox
if(postBox->FetchMessage(msgRecv))
2013-11-22 09:17:07 +01:00
{
t->Unpack(set, msgRecv);
2013-12-08 23:56:17 +01:00
//PrintOutMessage(set);
set->Release();
}
//Send message to server each second
if(timer.getElapsedSeconds() > 1)
2013-11-22 09:17:07 +01:00
{
cout << "Sending to server." << endl;
timer.reset();
client.Send(msgSend);
}
Sleep(1);
}
2013-11-22 09:17:07 +01:00
delete postBox;
delete t;
delete set;
}
2013-11-22 09:17:07 +01:00
void PrintOutMessage(ProtocolSet* set)
{
switch(set->type)
{
case PackageType_header:
break;
case PackageType_test:
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
for(int i = 0; i < (int)set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ' ;
}
cout << endl;
break;
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;
}
}