Danbias/Code/Network/NetworkAPI/NetworkClient.cpp

411 lines
8.9 KiB
C++
Raw Normal View History

//////////////////////////////////////
// Created by Pontus Fransson 2013 //
// Modified by Dennis Andersen 2014 //
//////////////////////////////////////
#ifndef INCLUDE_WINSOCK_LIB
#define INCLUDE_WINSOCK_LIB
#pragma comment(lib, "ws2_32.lib")
#endif
2013-12-10 08:32:08 +01:00
#include "NetworkClient.h"
#include "Translator.h"
#include "CustomNetProtocol.h"
2014-01-28 09:00:02 +01:00
#include "NetworkSession.h"
#include "../NetworkDependencies/Connection.h"
#include "../NetworkDependencies/PostBox.h"
#include "../NetworkDependencies/WinsockFunctions.h"
2014-02-17 10:38:11 +01:00
#include "Utilities.h"
#include "Thread/IThreadObject.h"
#include "Thread/OysterThread.h"
#include "Packing/Packing.h"
#include <queue>
2014-01-31 22:52:52 +01:00
#include <WinSock2.h>
//For conversion from wstring to string
#include <codecvt>
2013-12-10 08:32:08 +01:00
using namespace Oyster::Network;
using namespace Oyster::Thread;
using namespace Utility::DynamicMemory;
2014-01-28 09:00:02 +01:00
using namespace Utility::Container;
using namespace std;
2013-12-10 08:32:08 +01:00
/*************************************
PrivateData
*************************************/
2014-01-28 09:00:02 +01:00
typedef NetworkClient::ClientEventArgs CEA;
2014-01-29 10:18:01 +01:00
2014-02-12 09:02:44 +01:00
void OnRecieve_Default(NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e) {}
2014-01-29 10:18:01 +01:00
struct NetworkClient::PrivateData : public IThreadObject
{
2014-01-28 09:00:02 +01:00
NetworkSession *owner;
NetworkClient *parent;
2013-12-13 23:47:16 +01:00
Connection connection;
Translator translator;
2014-01-28 09:00:02 +01:00
OysterThread thread;
OysterByte recieveBuffer;
2014-01-28 09:00:02 +01:00
//Message queue for sending and recieving
ThreadSafeQueue<CustomNetProtocol> sendQueue;
ThreadSafeQueue<NetEvent<NetworkClient*, NetworkClient::ClientEventArgs>> recieveQueue;
//ID
static unsigned int currID;
const unsigned int ID;
2013-12-10 08:32:08 +01:00
2014-01-29 10:18:01 +01:00
PrivateData()
2014-01-28 09:00:02 +01:00
: ID(currID++)
, parent(0)
, owner(0)
{
2013-12-13 23:47:16 +01:00
InitWinSock();
2014-01-29 10:18:01 +01:00
this->thread.Create(this, false);
2014-01-28 09:00:02 +01:00
this->thread.SetPriority(Oyster::Thread::OYSTER_THREAD_PRIORITY_1);
2013-12-13 23:47:16 +01:00
}
2014-01-29 10:18:01 +01:00
~PrivateData()
2014-01-28 09:00:02 +01:00
{
2014-02-04 16:27:18 +01:00
this->thread.Terminate();
2013-12-13 23:47:16 +01:00
ShutdownWinSock();
2014-01-28 09:00:02 +01:00
this->connection.Disconnect();
this->owner = 0;
this->parent = 0;
2013-12-13 23:47:16 +01:00
}
2014-01-28 09:00:02 +01:00
bool DoWork() override
{
2014-01-28 09:00:02 +01:00
if(!this->connection.IsConnected()) return false;
2013-12-13 23:47:16 +01:00
Send();
Recv();
2013-12-17 10:25:34 +01:00
2013-12-13 23:47:16 +01:00
return true;
}
int Send()
{
2013-12-13 23:47:16 +01:00
int errorCode = 0;
2014-01-28 09:00:02 +01:00
if(!this->sendQueue.IsEmpty())
2013-12-13 23:47:16 +01:00
{
//printf("\t(%i)\n", this->sendQueue.Size());
2014-01-31 22:52:52 +01:00
OysterByte temp;
2014-01-28 09:00:02 +01:00
CustomNetProtocol p = this->sendQueue.Pop();
if(p[0].value.netShort == 304)
int i = 0;
2014-01-28 09:00:02 +01:00
this->translator.Pack(temp, p);
errorCode = this->connection.Send(temp);
2014-01-31 22:52:52 +01:00
if(errorCode != 0 && errorCode != WSAEWOULDBLOCK)
2014-01-07 10:26:09 +01:00
{
2014-01-28 09:00:02 +01:00
CEA parg;
parg.type = CEA::EventType_ProtocolFailedToSend;
parg.data.protocol = p;
NetEvent<NetworkClient*, CEA> e = { this->parent, parg };
this->recieveQueue.Push(e);
2014-01-07 10:26:09 +01:00
}
2013-12-13 23:47:16 +01:00
}
2013-12-13 23:47:16 +01:00
return errorCode;
}
int Recv()
{
2013-12-13 23:47:16 +01:00
int errorCode = -1;
2013-12-17 10:25:34 +01:00
OysterByte temp = OysterByte();
2014-01-28 09:00:02 +01:00
errorCode = this->connection.Recieve(temp);
2013-12-17 10:25:34 +01:00
if(errorCode == 0 && temp.GetSize())
{
HandleRecievedData(temp);
/* Replaced with EmptyOutbufferedQueue() and HandleRecievedData(OysterByte)
2013-12-17 10:25:34 +01:00
CustomNetProtocol protocol;
2014-01-28 09:00:02 +01:00
bool ok = this->translator.Unpack(protocol, temp);
2013-12-17 10:25:34 +01:00
//Check if the protocol was unpacked correctly
if(ok)
{
2014-01-28 09:00:02 +01:00
CEA parg;
parg.type = CEA::EventType_ProtocolRecieved;
parg.data.protocol = protocol;
NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e = { this->parent, parg };
this->recieveQueue.Push(e);
}*/
}
2014-01-29 10:18:01 +01:00
//else
//{
// CEA parg;
// parg.type = CEA::EventType_ProtocolFailedToRecieve;
// parg.data.nothing = 0;
// NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e = { this->parent, parg };
// this->recieveQueue.Push(e);
//}
2013-12-17 10:25:34 +01:00
2013-12-13 23:47:16 +01:00
return errorCode;
}
void HandleRecievedData(OysterByte& data)
{
//Loop through all packages that was recieved and add them to the queue.
unsigned int size = 0;
Oyster::Network::OysterByte msg;
//If there is part of a message in the buffer.
if(recieveBuffer.GetSize() > 0)
{
//cout << "the buffer size: " << recvBuffer.GetSize() <<endl;
unsigned int temp = recieveBuffer.GetSize();
size = Oyster::Packing::Unpacki(recieveBuffer);
if(temp + data.GetSize() > size)
{
msg = recieveBuffer;
recieveBuffer.Clear();
size -= msg.GetSize();
msg.AppendPartOfArray(data, 0, size);
UnpackMessageAndAddToQueue(msg);
}
else if(temp + data.GetSize() == size)
{
msg = recieveBuffer;
recieveBuffer.Clear();
size -= msg.GetSize();
msg += data;
UnpackMessageAndAddToQueue(msg);
return;
}
else
{
recieveBuffer += data;
size = data.GetSize();
return;
}
}
for(unsigned int i = size; i < data.GetSize(); i += size)
{
size = Oyster::Packing::Unpacki(&data.GetByteArray()[i]);
if(i+size > data.GetSize())
{
//Add it to the recvBuffer instead.
recieveBuffer.AppendPartOfArray(data, i, data.GetSize());
break;
}
msg.Clear();
msg.AppendPartOfArray(data, i, i+size);
UnpackMessageAndAddToQueue(msg);
}
}
void UnpackMessageAndAddToQueue(OysterByte& msg)
{
CustomNetProtocol protocol;
bool ok = this->translator.Unpack(protocol, msg);
//Check if the protocol was unpacked correctly
if(ok)
{
CEA parg;
parg.type = CEA::EventType_ProtocolRecieved;
parg.data.protocol = protocol;
NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e;
e.sender = this->parent;
e.args.data.protocol = parg.data.protocol;
e.args.type = parg.type;
this->recieveQueue.Push(e);
}
}
2013-12-13 23:47:16 +01:00
};
2014-01-29 10:18:01 +01:00
unsigned int NetworkClient::PrivateData::currID = 0;
2014-01-28 09:00:02 +01:00
2013-12-10 08:32:08 +01:00
/*************************************
NetworkClient
*************************************/
NetworkClient::NetworkClient()
2014-02-12 14:33:56 +01:00
: privateData(nullptr),
OnRecieve(OnRecieve_Default)
2014-01-28 09:00:02 +01:00
{ }
2014-01-28 09:00:02 +01:00
NetworkClient::~NetworkClient()
{
2014-01-28 09:00:02 +01:00
if(this->privateData)
{
delete this->privateData;
this->privateData = NULL;
}
2013-12-10 08:32:08 +01:00
}
2014-01-28 09:00:02 +01:00
bool NetworkClient::operator ==(const NetworkClient& obj)
2013-12-12 12:14:53 +01:00
{
2014-01-29 10:18:01 +01:00
return (this->privateData->ID == obj.privateData->ID);
2013-12-12 12:14:53 +01:00
}
2014-01-28 09:00:02 +01:00
bool NetworkClient::operator ==(const int& ID)
2013-12-12 12:14:53 +01:00
{
2014-01-29 10:18:01 +01:00
return this->privateData->ID == ID;
2013-12-12 12:14:53 +01:00
}
2014-01-29 10:18:01 +01:00
void NetworkClient::Update()
2013-12-10 08:32:08 +01:00
{
if(!this->privateData) return;
2014-01-29 10:18:01 +01:00
while (!this->privateData->recieveQueue.IsEmpty())
{
2014-01-29 10:18:01 +01:00
NetEvent<NetworkClient*, ClientEventArgs> temp = this->privateData->recieveQueue.Pop();
this->DataRecieved(temp);
}
}
bool NetworkClient::Connect(ConnectionInfo& socket)
{
2014-01-28 09:00:02 +01:00
if(this->IsConnected()) return true;
if(this->privateData) return false;
if(!this->privateData) this->privateData = new PrivateData();
2014-01-29 10:18:01 +01:00
int result = this->privateData->connection.Connect(socket, false);
//Connect has succeeded
2014-01-29 10:18:01 +01:00
if(result != 0) return false;
this->privateData->parent = this;
this->privateData->thread.Start();
2014-01-28 09:00:02 +01:00
//Connect has failed
2014-01-29 10:18:01 +01:00
return true;
2014-01-28 09:00:02 +01:00
}
2013-12-10 08:32:08 +01:00
2014-01-28 09:00:02 +01:00
bool NetworkClient::Connect(unsigned short port, const char serverIP[])
{
//Return true if you are already connected.
if(this->IsConnected())
return true;
if(!this->privateData)
this->privateData = new PrivateData();
2013-12-17 13:39:10 +01:00
2014-01-29 10:18:01 +01:00
int result = this->privateData->connection.Connect(port, serverIP, false);
2014-01-28 09:00:02 +01:00
//Connect has succeeded
2014-01-29 10:18:01 +01:00
if(result != 0) return false;
this->privateData->owner = 0;
2014-01-29 10:18:01 +01:00
this->privateData->parent = this;
this->privateData->thread.Start();
2013-12-12 09:24:57 +01:00
//Connect has failed
2014-01-29 10:18:01 +01:00
return true;
2013-12-10 08:32:08 +01:00
}
bool NetworkClient::Connect(unsigned short port, std::wstring serverIP)
{
//Convert from wstring to string.
typedef std::codecvt_utf8<wchar_t> convert_typeX;
std::wstring_convert<convert_typeX, wchar_t> converterX;
std::string ip = converterX.to_bytes(serverIP);
return this->Connect(port, ip.c_str());
}
bool NetworkClient::Reconnect()
{
//Return true if you are already connected.
if(this->IsConnected())
return true;
if(!this->privateData) this->privateData = new PrivateData();
int result = this->privateData->connection.Reconnect();
if(result != 0)
{
return false;
}
this->privateData->owner = 0;
this->privateData->parent = this;
this->privateData->thread.Start();
return true;
}
2013-12-10 08:32:08 +01:00
void NetworkClient::Disconnect()
{
if(!privateData) return;
privateData->thread.Stop();
2014-02-05 15:16:31 +01:00
privateData->connection.Disconnect();
this->privateData->sendQueue.Clear();
this->privateData->recieveQueue.Clear();
2013-12-10 08:32:08 +01:00
}
void NetworkClient::Send(CustomProtocolObject& protocol)
2013-12-10 08:32:08 +01:00
{
2014-01-31 22:52:52 +01:00
this->privateData->sendQueue.Push(protocol.GetProtocol());
}
2013-12-10 08:32:08 +01:00
void NetworkClient::Send(CustomNetProtocol& protocol)
2013-12-17 14:15:20 +01:00
{
2014-01-31 22:52:52 +01:00
this->privateData->sendQueue.Push(protocol);
2013-12-17 14:15:20 +01:00
}
2014-01-28 09:00:02 +01:00
void NetworkClient::SetOwner(NetworkSession* owner)
{
2014-01-29 10:18:01 +01:00
this->privateData->owner = owner;
}
2014-02-12 09:02:44 +01:00
void NetworkClient::SetMessagePump( NetworkClient::ClientEventFunction func )
{
if( func )
{
2014-02-12 14:33:56 +01:00
this->OnRecieve = func;
2014-02-12 09:02:44 +01:00
}
else
{
2014-02-12 14:33:56 +01:00
this->OnRecieve = OnRecieve_Default;
2014-02-12 09:02:44 +01:00
}
}
2014-01-28 09:00:02 +01:00
bool NetworkClient::IsConnected()
{
2014-01-28 09:00:02 +01:00
if(!this->privateData) return false;
2014-01-29 10:18:01 +01:00
return privateData->connection.IsConnected();
2013-12-16 09:00:11 +01:00
}
2014-01-28 09:00:02 +01:00
int NetworkClient::GetID() const
2013-12-16 09:00:11 +01:00
{
2014-01-29 10:18:01 +01:00
return this->privateData->ID;
}
void NetworkClient::DataRecieved(NetEvent<NetworkClient*, ClientEventArgs> e)
{
if(this->privateData->owner)
{
this->privateData->owner->ClientEventCallback(e);
}
2014-02-12 09:02:44 +01:00
else
{
2014-02-12 14:33:56 +01:00
this->OnRecieve( e );
2014-02-12 09:02:44 +01:00
}
2013-12-16 09:00:11 +01:00
}
//void NetworkClient::NetworkCallback(Oyster::Network::CustomNetProtocol& p)
//{}
2014-01-28 09:00:02 +01:00
std::string NetworkClient::GetIpAddress()
{
return this->privateData->connection.GetIpAddress();
}