Got async methods working for send in TcpClientWindows

All SendMessage functions are now void
This commit is contained in:
xX-TheDoctor-Xx
2016-07-18 20:05:22 +01:00
parent f3d090e08c
commit a1249ab5e9
11 changed files with 29 additions and 46 deletions

2
TODO
View File

@ -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
handle SendNetworkMessage errors for windows

View File

@ -39,7 +39,6 @@
<ClCompile Include="..\..\src\NetworkBuffer.cpp" />
<ClCompile Include="..\..\src\NetworkMessage.cpp" />
<ClCompile Include="..\..\src\Serializer.cpp" />
<ClCompile Include="..\..\src\TcpClient.cpp" />
<ClCompile Include="..\..\src\TcpClientWindows.cpp" />
<ClCompile Include="..\..\src\Utility.cpp" />
<ClCompile Include="..\..\src\VoidNetClient.cpp" />

View File

@ -53,9 +53,6 @@
<ClCompile Include="..\..\src\InitWindows.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\TcpClient.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\TcpClientWindows.cpp">
<Filter>src</Filter>
</ClCompile>

View File

@ -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<NetworkMessage> network_message_queue;
#ifdef _MSC_VER
SOCKET tcp_socket = INVALID_SOCKET;

View File

@ -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();

View File

@ -7,6 +7,7 @@
#include <iostream>
#include <thread>
#include <future>
#undef SendMessage
@ -114,7 +115,7 @@ NetworkBuffer TcpClient::ReceiveDataArray()
buffer.body = new byte[buffer.body_size]();
int32 body_received = recv(tcp_socket, reinterpret_cast<char*>(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<char*>(buffer.body), buffer.body_size, 0);
if (sent_bytes != buffer.body_size)
int32 bytes_sent = send(client->tcp_socket, reinterpret_cast<char*>(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);
}

View File

@ -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()