Network - Fixed receiving parts of packages.
This commit is contained in:
parent
4167b6a93b
commit
3b8a8e9c09
|
@ -16,11 +16,15 @@
|
||||||
#include "../../Misc/Utilities.h"
|
#include "../../Misc/Utilities.h"
|
||||||
#include "../../Misc/Thread/IThreadObject.h"
|
#include "../../Misc/Thread/IThreadObject.h"
|
||||||
#include "../../Misc/Thread/OysterThread.h"
|
#include "../../Misc/Thread/OysterThread.h"
|
||||||
|
#include "../../Misc/Packing/Packing.h"
|
||||||
|
|
||||||
|
#include <queue>
|
||||||
|
|
||||||
using namespace Oyster::Network;
|
using namespace Oyster::Network;
|
||||||
using namespace Oyster::Thread;
|
using namespace Oyster::Thread;
|
||||||
using namespace Utility::DynamicMemory;
|
using namespace Utility::DynamicMemory;
|
||||||
using namespace Utility::Container;
|
using namespace Utility::Container;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
/*************************************
|
/*************************************
|
||||||
PrivateData
|
PrivateData
|
||||||
|
@ -35,11 +39,12 @@ struct NetworkClient::PrivateData : public IThreadObject
|
||||||
Translator translator;
|
Translator translator;
|
||||||
OysterThread thread;
|
OysterThread thread;
|
||||||
|
|
||||||
|
OysterByte recieveBuffer;
|
||||||
|
|
||||||
//Message queue for sending and recieving
|
//Message queue for sending and recieving
|
||||||
ThreadSafeQueue<CustomNetProtocol> sendQueue;
|
ThreadSafeQueue<CustomNetProtocol> sendQueue;
|
||||||
ThreadSafeQueue<NetEvent<NetworkClient*, NetworkClient::ClientEventArgs>> recieveQueue;
|
ThreadSafeQueue<NetEvent<NetworkClient*, NetworkClient::ClientEventArgs>> recieveQueue;
|
||||||
|
|
||||||
//ID
|
//ID
|
||||||
static unsigned int currID;
|
static unsigned int currID;
|
||||||
const unsigned int ID;
|
const unsigned int ID;
|
||||||
|
@ -66,7 +71,7 @@ struct NetworkClient::PrivateData : public IThreadObject
|
||||||
bool DoWork() override
|
bool DoWork() override
|
||||||
{
|
{
|
||||||
if(!this->connection.IsConnected()) return false;
|
if(!this->connection.IsConnected()) return false;
|
||||||
|
|
||||||
Send();
|
Send();
|
||||||
Recv();
|
Recv();
|
||||||
|
|
||||||
|
@ -82,6 +87,7 @@ struct NetworkClient::PrivateData : public IThreadObject
|
||||||
CustomNetProtocol p = this->sendQueue.Pop();
|
CustomNetProtocol p = this->sendQueue.Pop();
|
||||||
this->translator.Pack(temp, p);
|
this->translator.Pack(temp, p);
|
||||||
errorCode = this->connection.Send(temp);
|
errorCode = this->connection.Send(temp);
|
||||||
|
|
||||||
if(errorCode != 0)
|
if(errorCode != 0)
|
||||||
{
|
{
|
||||||
CEA parg;
|
CEA parg;
|
||||||
|
@ -103,9 +109,13 @@ struct NetworkClient::PrivateData : public IThreadObject
|
||||||
|
|
||||||
if(errorCode == 0 && temp.GetSize())
|
if(errorCode == 0 && temp.GetSize())
|
||||||
{
|
{
|
||||||
|
HandleRecievedData(temp);
|
||||||
|
|
||||||
|
|
||||||
|
/* Replaced with EmptyOutbufferedQueue() and HandleRecievedData(OysterByte)
|
||||||
CustomNetProtocol protocol;
|
CustomNetProtocol protocol;
|
||||||
bool ok = this->translator.Unpack(protocol, temp);
|
bool ok = this->translator.Unpack(protocol, temp);
|
||||||
|
|
||||||
//Check if the protocol was unpacked correctly
|
//Check if the protocol was unpacked correctly
|
||||||
if(ok)
|
if(ok)
|
||||||
{
|
{
|
||||||
|
@ -114,7 +124,8 @@ struct NetworkClient::PrivateData : public IThreadObject
|
||||||
parg.data.protocol = protocol;
|
parg.data.protocol = protocol;
|
||||||
NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e = { this->parent, parg };
|
NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e = { this->parent, parg };
|
||||||
this->recieveQueue.Push(e);
|
this->recieveQueue.Push(e);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
//else
|
//else
|
||||||
//{
|
//{
|
||||||
|
@ -127,6 +138,76 @@ struct NetworkClient::PrivateData : public IThreadObject
|
||||||
|
|
||||||
return errorCode;
|
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 = { this->parent, parg };
|
||||||
|
this->recieveQueue.Push(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
unsigned int NetworkClient::PrivateData::currID = 0;
|
unsigned int NetworkClient::PrivateData::currID = 0;
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ void OysterByte::Resize(unsigned int cap)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int OysterByte::GetSize()
|
unsigned int OysterByte::GetSize()
|
||||||
{
|
{
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ unsigned char* OysterByte::GetByteArray()
|
||||||
|
|
||||||
void OysterByte::AddSize(unsigned int size)
|
void OysterByte::AddSize(unsigned int size)
|
||||||
{
|
{
|
||||||
int newCapacity = this->size + size;
|
unsigned int newCapacity = this->size + size;
|
||||||
|
|
||||||
if(newCapacity >= capacity)
|
if(newCapacity >= capacity)
|
||||||
{
|
{
|
||||||
|
@ -81,6 +81,32 @@ void OysterByte::SetSize(unsigned int size)
|
||||||
this->size = size;
|
this->size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OysterByte::AppendPartOfArray(OysterByte& source, unsigned int startIndex, unsigned int endIndex)
|
||||||
|
{
|
||||||
|
if(startIndex < 0 && startIndex >= endIndex)
|
||||||
|
return;
|
||||||
|
if(endIndex > source.size)
|
||||||
|
return;
|
||||||
|
|
||||||
|
unsigned int totalSize = endIndex - startIndex;
|
||||||
|
totalSize += size;
|
||||||
|
|
||||||
|
//Make sure the new data can fit in the array.
|
||||||
|
if(totalSize > capacity)
|
||||||
|
{
|
||||||
|
IncreaseCapacity(totalSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Copy over new data.
|
||||||
|
for(unsigned int i = size; i < totalSize; i++)
|
||||||
|
{
|
||||||
|
byteArray[i] = source.byteArray[startIndex++];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set the new size
|
||||||
|
size = totalSize;
|
||||||
|
}
|
||||||
|
|
||||||
OysterByte& OysterByte::operator =(const OysterByte& obj)
|
OysterByte& OysterByte::operator =(const OysterByte& obj)
|
||||||
{
|
{
|
||||||
delete[] this->byteArray;
|
delete[] this->byteArray;
|
||||||
|
|
|
@ -25,7 +25,7 @@ namespace Oyster
|
||||||
//Resizes the array with, it does not keep anything in it.
|
//Resizes the array with, it does not keep anything in it.
|
||||||
void Resize(unsigned int cap);
|
void Resize(unsigned int cap);
|
||||||
|
|
||||||
int GetSize();
|
unsigned int GetSize();
|
||||||
unsigned char* GetByteArray();
|
unsigned char* GetByteArray();
|
||||||
|
|
||||||
void AddSize(unsigned int size);
|
void AddSize(unsigned int size);
|
||||||
|
@ -34,6 +34,9 @@ namespace Oyster
|
||||||
//Only sets the private variable 'size'
|
//Only sets the private variable 'size'
|
||||||
void SetSize(unsigned int size);
|
void SetSize(unsigned int size);
|
||||||
|
|
||||||
|
//Copies over a part of the addFrom array and adds it to the end of this array.
|
||||||
|
void AppendPartOfArray(OysterByte& source, unsigned int startIndex, unsigned int endIndex);
|
||||||
|
|
||||||
OysterByte& operator =(const OysterByte& obj);
|
OysterByte& operator =(const OysterByte& obj);
|
||||||
|
|
||||||
operator char*();
|
operator char*();
|
||||||
|
|
Loading…
Reference in New Issue