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

@ -245,4 +245,12 @@ enum InternalTags
#define IS_HANDSHAKE(name) name.subject == 1 || (name.tag == DisconnectTag || name.tag == ConnectTag || name.tag == Accept || name.tag == Close || name.tag == Reject) #define IS_HANDSHAKE(name) name.subject == 1 || (name.tag == DisconnectTag || name.tag == ConnectTag || name.tag == Accept || name.tag == Close || name.tag == Reject)
#ifdef __linux__
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void closesocket(int socket) { close(socket); }
#endif
#endif // DEFS_HPP #endif // DEFS_HPP

View File

@ -57,6 +57,7 @@ private:
const NetworkBuffer &receive_data_array(); const NetworkBuffer &receive_data_array();
static void receive_data(TcpClient *client); static void receive_data(TcpClient *client);
static bool send_network_message(const NetworkMessage &message, TcpClient *client); static bool send_network_message(const NetworkMessage &message, TcpClient *client);
static void close_connection(TcpClient *client);
bool initialize(const std::string &ip, uint16 port = default_client_port); bool initialize(const std::string &ip, uint16 port = default_client_port);

View File

@ -60,8 +60,13 @@ void TcpClient::receive_data(TcpClient *client)
{ {
if (message.tag == ConnectTag) // some user has connected - not us, never if (message.tag == ConnectTag) // some user has connected - not us, never
std::async(std::launch::async, client->OnConnect, message.sender); 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); 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 else
std::async(std::launch::async, client->OnMessage, message.sender, message.tag, message.subject, message.data); // we received data 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; 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) bool TcpClient::SendBytes(const std::vector<byte>& bytes)
{ {
int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(bytes.data()), bytes.size(), 0); int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(bytes.data()), bytes.size(), 0);

View File

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