diff --git a/Code/Network/NetworkAPI/NetworkCallbackHelper.h b/Code/Network/NetworkAPI/NetworkCallbackHelper.h index a458760e..e4f6bc9f 100644 --- a/Code/Network/NetworkAPI/NetworkCallbackHelper.h +++ b/Code/Network/NetworkAPI/NetworkCallbackHelper.h @@ -26,7 +26,7 @@ namespace Oyster class NetworkClient; class CustomNetProtocol; - typedef void (*ClientConnectCallbackMethod)(NetworkClient&); + typedef void (*ClientConnectCallbackMethod)(NetworkClient); typedef void(*ProtocolRecieverFunction)(CustomNetProtocol& protocol); struct ClientConnectedObject { diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 697cb897..017b0eb4 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -40,11 +40,11 @@ struct NetworkClient::PrivateData : public IThreadObject bool DoWork(); - Connection* connection; + SmartPointer connection; - IPostBox *sendPostBox; + SmartPointer> sendPostBox; - RecieverObject recvObj; + SmartPointer recvObj; NetworkProtocolCallbackType callbackType; Oyster::Thread::OysterThread thread; @@ -152,11 +152,11 @@ int NetworkClient::PrivateData::Recv() recvObjMutex.lock(); if(callbackType == NetworkProtocolCallbackType_Function) { - recvObj.protocolRecieverFnc(protocol); + recvObj->protocolRecieverFnc(protocol); } else if(callbackType == NetworkProtocolCallbackType_Object) { - recvObj.protocolRecievedObject->ProtocolRecievedCallback(protocol); + recvObj->protocolRecievedObject->ProtocolRecievedCallback(protocol); } recvObjMutex.unlock(); } @@ -187,16 +187,31 @@ NetworkClient::NetworkClient(unsigned int socket) NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type) { privateData = new PrivateData(); - this->privateData->recvObj = recvObj; + this->privateData->recvObj = SmartPointer(&recvObj);; } NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket) { privateData = new PrivateData(socket); - this->privateData->recvObj = recvObj; + this->privateData->recvObj = SmartPointer(&recvObj); 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() { if(privateData) @@ -241,7 +256,7 @@ void NetworkClient::Send(CustomProtocolObject& protocol) void NetworkClient::SetRecieverObject(RecieverObject recvObj, NetworkProtocolCallbackType type) { privateData->recvObjMutex.lock(); - privateData->recvObj = recvObj; + privateData->recvObj = SmartPointer(&recvObj); privateData->callbackType = type; privateData->recvObjMutex.unlock(); } \ No newline at end of file diff --git a/Code/Network/NetworkAPI/NetworkClient.h b/Code/Network/NetworkAPI/NetworkClient.h index 353b51b6..b3c30419 100644 --- a/Code/Network/NetworkAPI/NetworkClient.h +++ b/Code/Network/NetworkAPI/NetworkClient.h @@ -27,6 +27,9 @@ namespace Oyster NetworkClient(unsigned int socket); NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type); NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket); + NetworkClient(const NetworkClient& obj); + + NetworkClient& operator =(const NetworkClient& obj); virtual ~NetworkClient(); bool Connect(unsigned short port, const char serverIP[]); diff --git a/Code/Network/NetworkAPI/Translator.cpp b/Code/Network/NetworkAPI/Translator.cpp index eb2a3d17..52b61694 100644 --- a/Code/Network/NetworkAPI/Translator.cpp +++ b/Code/Network/NetworkAPI/Translator.cpp @@ -191,7 +191,7 @@ struct Translator::PrivateData protocol[i].value.netDouble = message.UnpackDouble(*bytes); break; case NetAttributeType_CharArray: - //protocol[i].value.netCharPtr = message.UnpackStr(*bytes).c_str(); + protocol[i].value.netCharPtr = message.UnpackCStr(*bytes); break; default: numberOfUnknownTypes++; diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp index acd45c12..7f7f1771 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp @@ -240,6 +240,13 @@ double MessageHeader::UnpackDouble(OysterByte& bytes) 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 str = Packing::UnpackStr(&bytes.GetByteArray()[size]); diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.h b/Code/Network/NetworkDependencies/Messages/MessageHeader.h index 995d0d8c..b7752401 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.h +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.h @@ -65,6 +65,7 @@ namespace Oyster float* UnpackFloat(unsigned int& elementCount, OysterByte& bytes); double UnpackDouble(OysterByte& bytes); + char* UnpackCStr(OysterByte& bytes); std::string UnpackStr(OysterByte& bytes); //Sets the this->size to the first position in msg diff --git a/Code/Network/NetworkDependencies/Packing.cpp b/Code/Network/NetworkDependencies/Packing.cpp index 405cd5e6..b6903a0a 100644 --- a/Code/Network/NetworkDependencies/Packing.cpp +++ b/Code/Network/NetworkDependencies/Packing.cpp @@ -279,6 +279,20 @@ namespace Oyster } //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[]) { short len = UnpackS(buffer); diff --git a/Code/Network/NetworkDependencies/Packing.h b/Code/Network/NetworkDependencies/Packing.h index 7a52c644..aaf3a1b8 100644 --- a/Code/Network/NetworkDependencies/Packing.h +++ b/Code/Network/NetworkDependencies/Packing.h @@ -73,6 +73,7 @@ namespace Oyster double Unpackd(unsigned char buffer[]); //string + char* UnpackCStr(unsigned char buffer[]); std::string UnpackStr(unsigned char buffer[]); long double Unpack754(unsigned __int64 i, unsigned bits, unsigned expbits);