From a1249ab5e949667f87724f52c11adaaab903426b Mon Sep 17 00:00:00 2001 From: xX-TheDoctor-Xx Date: Mon, 18 Jul 2016 20:05:22 +0100 Subject: [PATCH] Got async methods working for send in TcpClientWindows All SendMessage functions are now void --- TODO | 2 +- .../Release/VoidNetVS.tlog/CL.read.1.tlog | Bin 204740 -> 188988 bytes .../VoidNetVS.tlog/Lib-link.write.1.tlog | Bin 1180 -> 1074 bytes .../Release/VoidNetVS.tlog/Lib.read.1.tlog | Bin 2836 -> 2622 bytes .../Release/VoidNetVS.tlog/lib.command.1.tlog | Bin 1670 -> 1522 bytes VoidNetVS/VoidNetVS/VoidNetVS.vcxproj | 1 - VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters | 3 --- include/TcpClient.hpp | 9 ++----- include/VoidNetClient.hpp | 12 ++++----- src/TcpClientWindows.cpp | 24 ++++++------------ src/VoidNetClient.cpp | 24 +++++++++--------- 11 files changed, 29 insertions(+), 46 deletions(-) diff --git a/TODO b/TODO index d22f382..e1d0eae 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,3 @@ initialization code for other operating systems - currently VoidNet only supports windows initialization code for tcp client for other operating systems - " -implement thread pool - god dammit so many tries and it doesnt work \ No newline at end of file +handle SendNetworkMessage errors for windows \ No newline at end of file diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog index 02d72cf1ae97a4ab229b214144e3088927dd9972..4721096624434d757fbf72b5ba336c254342f414 100644 GIT binary patch delta 68 zcmV-K0K5OhzYM&@3xKo%#`uxW5VwLo0x)rtoH%}$a5w^vmliexjFDglm(Vu?ptt@w a0#XE*tR(?{moh8@7L#brAh*)40`hPGDIBf< delta 323 zcmXAkKS%;$7{>2?Jx{9}4vC~CoTEWP2qGd58fplGjD`kluuFr^YKRbtsE7zT`HHvE z!9j>hI_X=)jyRec6hfqyTAYts3>rFXd3gTt48N+~XFtgM6?p1UGl&5ef>Cl3C{gXz zmG>a}Y4sb%$!eiP6B=fjVvDAAH3YmI#wwUoq90j&>yp=#5b;vf2LFWvSyR7Ok^PM2 z%<}CWP$+^TSq)IRwFAfQOKAh?GgxG0b&ixr)L49biAM+F#6y__T;|3eh_b1_O#(OC zu-ik{J)Tn`0}|C=;M6xS#Dr-fMl0)hMEz+Tpi&95q~}5OxZ8g}qM<>S4_9l( K4SY6@pZYK9@PLE> diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog index a56c9d633a99be2d5d60bbb187809ad821b3e310..47d98bc991256338087c75d9f60522e2519d4871 100644 GIT binary patch delta 12 TcmbQkxrt-L3#QFM%wCKDAVdUB delta 20 ccmdnQF^6-*3#Q5Em^mg(F$HYC#uUQ{08{7(O#lD@ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog index 713dfb912c21fbab4d8a76c8a10dfb21f00ef422..abc774e0b517219e4fe70b9467168bf1da5c1159 100644 GIT binary patch delta 29 lcmbOtwohck3#Q3!oMMxum;xrh;}qL`j@gHGa~o$LBLJu(3X}i< delta 37 tcmdldGDU2|3#Q5Em^mg(F$GMX$Dy(LAJZmQpa9S09Cp9W^EehT0ss{>4hsMP diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog index 46e8865a242acaabd47da795579ba63c66d29f6a..0e35fa9a439736af5e20155ce4d7ecaa731081d7 100644 GIT binary patch delta 28 icmZqU{lvZD1=C~^HnGW4OaYUv*u*xUW0nC46afH=p$L}% delta 36 ncmeyw-Nw7&1=Hkn%p8-Ym;xr}v1)Ao$J7K7n8qpq6lehe0tXE* diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj index 4fd9b6b..24cf878 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj @@ -39,7 +39,6 @@ - diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters index 06e1184..33f075c 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters @@ -53,9 +53,6 @@ src - - src - src diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index 2c17a3f..3cc68ab 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -30,20 +30,15 @@ public: NetworkBuffer ReceiveDataArray(); const NetworkMessage &ReceiveData(); - void StartSender(); - VoidCode SendMessage(const NetworkMessage &message); + void SendMessage(const NetworkMessage &message); private: - void SendNetworkMessage(const NetworkMessage &message); - void SendNetworkMessageNow(const NetworkMessage &message); + static void SendNetworkMessage(const NetworkMessage &message, TcpClient *client); VoidCode Initialize(const std::string &ip, uint16 port = default_port); std::string ip; uint16 port = 0; bool initialized = false; - bool run_sender = false; - - std::vector network_message_queue; #ifdef _MSC_VER SOCKET tcp_socket = INVALID_SOCKET; diff --git a/include/VoidNetClient.hpp b/include/VoidNetClient.hpp index ae27026..44ce6b4 100644 --- a/include/VoidNetClient.hpp +++ b/include/VoidNetClient.hpp @@ -22,13 +22,13 @@ struct VoidNetClientAPI static bool Connect(const std::string &ip, uint16 port = default_port); static void Disconnect(); - static bool SendMessageToServer(byte tag, byte subject, void *data); - static bool SendMessageToID(uint16 id, byte tag, byte subject, void *data); - static bool SendMessageToOthers(byte tag, byte subject, void *data); - static bool SendMessageToAll(byte tag, byte subject, void *data); - static bool SendMessageToAllAndMe(byte tag, byte subject, void *data); + static void SendMessageToServer(byte tag, byte subject, void *data); + static void SendMessageToID(uint16 id, byte tag, byte subject, void *data); + static void SendMessageToOthers(byte tag, byte subject, void *data); + static void SendMessageToAll(byte tag, byte subject, void *data); + static void SendMessageToAllAndMe(byte tag, byte subject, void *data); - static bool SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data); + static void SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data); static void Receive(); diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp index d003232..5b8712b 100644 --- a/src/TcpClientWindows.cpp +++ b/src/TcpClientWindows.cpp @@ -7,6 +7,7 @@ #include #include +#include #undef SendMessage @@ -114,7 +115,7 @@ NetworkBuffer TcpClient::ReceiveDataArray() buffer.body = new byte[buffer.body_size](); int32 body_received = recv(tcp_socket, reinterpret_cast(buffer.body), buffer.body_size, 0); - if (body_received != buffer.body_size || WSAGetLastError() != 0) + if (body_received == SOCKET_ERROR || body_received != buffer.body_size || WSAGetLastError() != 0) { //there was a problem receiving the body of the message or theres no body to receive return NetworkBuffer(); @@ -135,26 +136,17 @@ const NetworkMessage &TcpClient::ReceiveData() return message; } -void TcpClient::SendNetworkMessage(const NetworkMessage &message) -{ - network_message_queue.emplace_back(message); -} - -void TcpClient::SendNetworkMessageNow(const NetworkMessage &message) +void TcpClient::SendNetworkMessage(const NetworkMessage &message, TcpClient *client) { NetworkBuffer buffer = message.EncodeMessage(message); - int32 sent_bytes = send(tcp_socket, reinterpret_cast(buffer.body), buffer.body_size, 0); - if (sent_bytes != buffer.body_size) + int32 bytes_sent = send(client->tcp_socket, reinterpret_cast(buffer.body), buffer.body_size, 0); + if (bytes_sent == SOCKET_ERROR || bytes_sent != buffer.body_size || WSAGetLastError() != 0) { - + //something went wrong couldnt send anything/some data } } -VoidCode TcpClient::SendMessage(const NetworkMessage &message) +void TcpClient::SendMessage(const NetworkMessage &message) { -} - -void TcpClient::StartSender() -{ - //std::thread thread = std::thread(&SendNetworkMessageNow, message); + std::async(std::launch::async, &SendNetworkMessage, message, this); } \ No newline at end of file diff --git a/src/VoidNetClient.cpp b/src/VoidNetClient.cpp index 9ddc1f0..56a4549 100644 --- a/src/VoidNetClient.cpp +++ b/src/VoidNetClient.cpp @@ -11,32 +11,32 @@ bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port) return client->Connect() == VOID_SUCCESS; } -bool VoidNetClientAPI::SendMessageToServer(byte tag, byte subject, void *data) +void VoidNetClientAPI::SendMessageToServer(byte tag, byte subject, void *data) { - return SendMessage(Server, 0, tag, subject, data); + SendMessage(Server, 0, tag, subject, data); } -bool VoidNetClientAPI::SendMessageToID(uint16 destination_id, byte tag, byte subject, void *data) +void VoidNetClientAPI::SendMessageToID(uint16 destination_id, byte tag, byte subject, void *data) { - return SendMessage(ID, destination_id, tag, subject, data); + SendMessage(ID, destination_id, tag, subject, data); } -bool VoidNetClientAPI::SendMessageToOthers(byte tag, byte subject, void *data) +void VoidNetClientAPI::SendMessageToOthers(byte tag, byte subject, void *data) { - return SendMessage(Others, 0, tag, subject, data); + SendMessage(Others, 0, tag, subject, data); } -bool VoidNetClientAPI::SendMessageToAll(byte tag, byte subject, void *data) +void VoidNetClientAPI::SendMessageToAll(byte tag, byte subject, void *data) { - return SendMessage(All, 0, tag, subject, data); + SendMessage(All, 0, tag, subject, data); } -bool VoidNetClientAPI::SendMessageToAllAndMe(byte tag, byte subject, void *data) +void VoidNetClientAPI::SendMessageToAllAndMe(byte tag, byte subject, void *data) { - return SendMessage(AllAndMe, 0, tag, subject, data); + SendMessage(AllAndMe, 0, tag, subject, data); } -bool VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data) +void VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data) { NetworkMessage message; message.tag = tag; @@ -45,7 +45,7 @@ bool VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id message.distribution_mode = distribution_mode; message.sender = id; message.destination_id = destination_id; - return client->SendMessage(message) == VOID_SUCCESS; + client->SendMessage(message); } void VoidNetClientAPI::Receive()