Network - Fixed copy construct

This commit is contained in:
Pontus Fransson 2013-12-12 12:14:53 +01:00
parent 49cfbbebd5
commit e9b41f8cb8
8 changed files with 51 additions and 10 deletions

View File

@ -26,7 +26,7 @@ namespace Oyster
class NetworkClient; class NetworkClient;
class CustomNetProtocol; class CustomNetProtocol;
typedef void (*ClientConnectCallbackMethod)(NetworkClient&); typedef void (*ClientConnectCallbackMethod)(NetworkClient);
typedef void(*ProtocolRecieverFunction)(CustomNetProtocol& protocol); typedef void(*ProtocolRecieverFunction)(CustomNetProtocol& protocol);
struct ClientConnectedObject struct ClientConnectedObject
{ {

View File

@ -40,11 +40,11 @@ struct NetworkClient::PrivateData : public IThreadObject
bool DoWork(); bool DoWork();
Connection* connection; SmartPointer<Connection> connection;
IPostBox<CustomNetProtocol> *sendPostBox; SmartPointer<IPostBox<CustomNetProtocol>> sendPostBox;
RecieverObject recvObj; SmartPointer<RecieverObject> recvObj;
NetworkProtocolCallbackType callbackType; NetworkProtocolCallbackType callbackType;
Oyster::Thread::OysterThread thread; Oyster::Thread::OysterThread thread;
@ -152,11 +152,11 @@ int NetworkClient::PrivateData::Recv()
recvObjMutex.lock(); recvObjMutex.lock();
if(callbackType == NetworkProtocolCallbackType_Function) if(callbackType == NetworkProtocolCallbackType_Function)
{ {
recvObj.protocolRecieverFnc(protocol); recvObj->protocolRecieverFnc(protocol);
} }
else if(callbackType == NetworkProtocolCallbackType_Object) else if(callbackType == NetworkProtocolCallbackType_Object)
{ {
recvObj.protocolRecievedObject->ProtocolRecievedCallback(protocol); recvObj->protocolRecievedObject->ProtocolRecievedCallback(protocol);
} }
recvObjMutex.unlock(); recvObjMutex.unlock();
} }
@ -187,16 +187,31 @@ NetworkClient::NetworkClient(unsigned int socket)
NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type) NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type)
{ {
privateData = new PrivateData(); privateData = new PrivateData();
this->privateData->recvObj = recvObj; this->privateData->recvObj = SmartPointer<RecieverObject>(&recvObj);;
} }
NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket) NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket)
{ {
privateData = new PrivateData(socket); privateData = new PrivateData(socket);
this->privateData->recvObj = recvObj; this->privateData->recvObj = SmartPointer<RecieverObject>(&recvObj);
this->privateData->callbackType = type; this->privateData->callbackType = type;
} }
NetworkClient::NetworkClient(const NetworkClient& obj)
{
this->privateData = new PrivateData();
this->privateData = obj.privateData;
}
NetworkClient& NetworkClient::operator =(const NetworkClient& obj)
{
delete privateData;
this->privateData = new PrivateData();
this->privateData = obj.privateData;
return *this;
}
NetworkClient::~NetworkClient() NetworkClient::~NetworkClient()
{ {
if(privateData) if(privateData)
@ -241,7 +256,7 @@ void NetworkClient::Send(CustomProtocolObject& protocol)
void NetworkClient::SetRecieverObject(RecieverObject recvObj, NetworkProtocolCallbackType type) void NetworkClient::SetRecieverObject(RecieverObject recvObj, NetworkProtocolCallbackType type)
{ {
privateData->recvObjMutex.lock(); privateData->recvObjMutex.lock();
privateData->recvObj = recvObj; privateData->recvObj = SmartPointer<RecieverObject>(&recvObj);
privateData->callbackType = type; privateData->callbackType = type;
privateData->recvObjMutex.unlock(); privateData->recvObjMutex.unlock();
} }

View File

@ -27,6 +27,9 @@ namespace Oyster
NetworkClient(unsigned int socket); NetworkClient(unsigned int socket);
NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type); NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type);
NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket); NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket);
NetworkClient(const NetworkClient& obj);
NetworkClient& operator =(const NetworkClient& obj);
virtual ~NetworkClient(); virtual ~NetworkClient();
bool Connect(unsigned short port, const char serverIP[]); bool Connect(unsigned short port, const char serverIP[]);

View File

@ -191,7 +191,7 @@ struct Translator::PrivateData
protocol[i].value.netDouble = message.UnpackDouble(*bytes); protocol[i].value.netDouble = message.UnpackDouble(*bytes);
break; break;
case NetAttributeType_CharArray: case NetAttributeType_CharArray:
//protocol[i].value.netCharPtr = message.UnpackStr(*bytes).c_str(); protocol[i].value.netCharPtr = message.UnpackCStr(*bytes);
break; break;
default: default:
numberOfUnknownTypes++; numberOfUnknownTypes++;

View File

@ -240,6 +240,13 @@ double MessageHeader::UnpackDouble(OysterByte& bytes)
return i; return i;
} }
char* MessageHeader::UnpackCStr(OysterByte& bytes)
{
char* str = Packing::UnpackCStr(&bytes.GetByteArray()[size]);
size += 2 + (int)strlen(str);
return str;
}
std::string MessageHeader::UnpackStr(OysterByte& bytes) std::string MessageHeader::UnpackStr(OysterByte& bytes)
{ {
std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]); std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]);

View File

@ -65,6 +65,7 @@ namespace Oyster
float* UnpackFloat(unsigned int& elementCount, OysterByte& bytes); float* UnpackFloat(unsigned int& elementCount, OysterByte& bytes);
double UnpackDouble(OysterByte& bytes); double UnpackDouble(OysterByte& bytes);
char* UnpackCStr(OysterByte& bytes);
std::string UnpackStr(OysterByte& bytes); std::string UnpackStr(OysterByte& bytes);
//Sets the this->size to the first position in msg //Sets the this->size to the first position in msg

View File

@ -279,6 +279,20 @@ namespace Oyster
} }
//string //string
char* UnpackCStr(unsigned char buffer[])
{
short len = UnpackS(buffer);
char* str = new char[len];
buffer += 2;
for(int i = 0; i < len; i++)
{
str[i] = buffer[i];
}
return str;
}
std::string UnpackStr(unsigned char buffer[]) std::string UnpackStr(unsigned char buffer[])
{ {
short len = UnpackS(buffer); short len = UnpackS(buffer);

View File

@ -73,6 +73,7 @@ namespace Oyster
double Unpackd(unsigned char buffer[]); double Unpackd(unsigned char buffer[]);
//string //string
char* UnpackCStr(unsigned char buffer[]);
std::string UnpackStr(unsigned char buffer[]); std::string UnpackStr(unsigned char buffer[]);
long double Unpack754(unsigned __int64 i, unsigned bits, unsigned expbits); long double Unpack754(unsigned __int64 i, unsigned bits, unsigned expbits);