Added linux includes for networking

TcpClient: added close_connection to shutdown the connection on both send and receive
TcpServer: changed clients list initialization from constructors to using the reserve function
This commit is contained in:
xX-TheDoctor-Xx
2016-09-16 20:38:30 +01:00
parent 3e97c0da6d
commit a9549c33e3
11 changed files with 23 additions and 3 deletions

View File

@ -60,8 +60,13 @@ void TcpClient::receive_data(TcpClient *client)
{
if (message.tag == ConnectTag) // some user has connected - not us, never
std::async(std::launch::async, client->OnConnect, message.sender);
else if (message.tag == DisconnectTag || message.tag == Close) // some user has disconnected, it can be us
else if (message.tag == DisconnectTag) // some user has disconnected, it can be us
std::async(std::launch::async, client->OnDisconnect, message.sender);
else if (message.tag == Close)
{
std::async(std::launch::async, client->OnDisconnect, message.sender);
close_connection(client);
}
}
else
std::async(std::launch::async, client->OnMessage, message.sender, message.tag, message.subject, message.data); // we received data
@ -228,6 +233,12 @@ bool TcpClient::send_network_message(const NetworkMessage &message, TcpClient *c
return bytes_sent == SOCKET_ERROR || bytes_sent != lenght || WSAGetLastError() != 0;
}
void TcpClient::close_connection(TcpClient * client)
{
shutdown(client->tcp_socket, SD_BOTH);
closesocket(client->tcp_socket);
}
bool TcpClient::SendBytes(const std::vector<byte>& bytes)
{
int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(bytes.data()), bytes.size(), 0);

View File

@ -10,13 +10,13 @@
TcpServer::TcpServer()
{
initialize(); // initialize with the default port
clients = std::vector<TcpClient>(max_connections);
clients.reserve(max_connections);
}
TcpServer::TcpServer(uint16 port)
{
initialize(port);
clients = std::vector<TcpClient>(max_connections);
clients.reserve(max_connections);
}
TcpServer::~TcpServer()