Removed Tags file
Updated TODO file TcpClient: Added parameterless contructor, id is now -2 by default which is invalid, Added SendBytes method for C style arrays TcpServer: Added 2 CloseSocket function, one for TcpClient and another one for id, also added GetClientByID client is now declared on the stack in VoidNetClient Made Serializer code smaller, still need to integrate a compressor
This commit is contained in:
6
TODO
6
TODO
@ -8,8 +8,4 @@ new StringConverter class
|
|||||||
|
|
||||||
implement close() in TcpClientWindows
|
implement close() in TcpClientWindows
|
||||||
|
|
||||||
implement ConfigReader methods
|
implement ConfigReader methods
|
||||||
|
|
||||||
NetworkMessage EncodeMessage
|
|
||||||
|
|
||||||
Fix NetworkMessage line 50
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -185,4 +185,10 @@ enum ConnectionCode
|
|||||||
Reject
|
Reject
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum InternalTags
|
||||||
|
{
|
||||||
|
CONNECT = 65534,
|
||||||
|
DISCONNECT = 65535,
|
||||||
|
};
|
||||||
|
|
||||||
#endif // DEFS_HPP
|
#endif // DEFS_HPP
|
||||||
@ -1,14 +0,0 @@
|
|||||||
#ifndef TAGS_HPP
|
|
||||||
#define TAGS_HPP
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#pragma once
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum InternalTags
|
|
||||||
{
|
|
||||||
CONNECT = 65534,
|
|
||||||
DISCONNECT = 65535,
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@ -16,6 +16,7 @@
|
|||||||
class TcpClient
|
class TcpClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
TcpClient();
|
||||||
TcpClient(const SOCKET &socket);
|
TcpClient(const SOCKET &socket);
|
||||||
TcpClient(const std::string &ip);
|
TcpClient(const std::string &ip);
|
||||||
TcpClient(const std::string &ip, uint16 port = default_client_port);
|
TcpClient(const std::string &ip, uint16 port = default_client_port);
|
||||||
@ -43,6 +44,7 @@ public:
|
|||||||
const NetworkMessage &ReceiveMessage();
|
const NetworkMessage &ReceiveMessage();
|
||||||
void SendMessage(const NetworkMessage &message);
|
void SendMessage(const NetworkMessage &message);
|
||||||
void SendBytes(const std::vector<byte> &bytes);
|
void SendBytes(const std::vector<byte> &bytes);
|
||||||
|
void SendBytes(byte *bytes, uint32 lenght);
|
||||||
|
|
||||||
void SetOnDisconnectCallback(void (*func)(uint16));
|
void SetOnDisconnectCallback(void (*func)(uint16));
|
||||||
void SetOnConnectCallback(void (*func)(uint16));
|
void SetOnConnectCallback(void (*func)(uint16));
|
||||||
@ -54,7 +56,7 @@ private:
|
|||||||
static void send_network_message(const NetworkMessage &message, TcpClient *client);
|
static void send_network_message(const NetworkMessage &message, TcpClient *client);
|
||||||
bool initialize(const std::string &ip, uint16 port = default_client_port);
|
bool initialize(const std::string &ip, uint16 port = default_client_port);
|
||||||
|
|
||||||
uint16 id = 0;
|
uint16 id = -2;
|
||||||
std::string ip;
|
std::string ip;
|
||||||
uint16 port = 0;
|
uint16 port = 0;
|
||||||
bool initialized = false;
|
bool initialized = false;
|
||||||
|
|||||||
@ -32,6 +32,11 @@ public:
|
|||||||
void RejectConnection(TcpClient &client);
|
void RejectConnection(TcpClient &client);
|
||||||
void AcceptConnection(TcpClient &client);
|
void AcceptConnection(TcpClient &client);
|
||||||
|
|
||||||
|
void CloseSocket(TcpClient &client);
|
||||||
|
void CloseSocket(uint16 id);
|
||||||
|
|
||||||
|
const TcpClient &GetClientByID(uint16 id);
|
||||||
|
|
||||||
std::function<void(const NetworkMessage &message)> OnMessage;
|
std::function<void(const NetworkMessage &message)> OnMessage;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@ -34,9 +34,8 @@ struct VoidNetClientAPI
|
|||||||
private:
|
private:
|
||||||
static void process_all_data();
|
static void process_all_data();
|
||||||
|
|
||||||
static TcpClient *client;
|
static TcpClient client;
|
||||||
static uint16 id;
|
static uint16 id;
|
||||||
static std::thread receive_thread;
|
|
||||||
static bool receive;
|
static bool receive;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& obje
|
|||||||
|
|
||||||
const byte *begin = reinterpret_cast<const byte*>(std::addressof(object));
|
const byte *begin = reinterpret_cast<const byte*>(std::addressof(object));
|
||||||
const byte *end = begin + sizeof(T);
|
const byte *end = begin + sizeof(T);
|
||||||
std::copy(begin, end, std::begin(bytes));
|
std::copy(begin, end, bytes.begin());
|
||||||
|
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
@ -14,10 +14,7 @@ template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& obje
|
|||||||
template<typename T> const T& Serializer::from_bytes(const std::vector<byte> &bytes, T& object)
|
template<typename T> const T& Serializer::from_bytes(const std::vector<byte> &bytes, T& object)
|
||||||
{
|
{
|
||||||
static_assert(std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type");
|
static_assert(std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type");
|
||||||
|
std::copy(bytes.begin(), bytes.end(), reinterpret_cast<const byte*>(std::addressof(object));
|
||||||
byte *begin_object = reinterpret_cast<byte*>(std::addressof(object));
|
|
||||||
std::vector<byte> bytes_vector(&bytes);
|
|
||||||
std::copy(bytes_vector.begin(), bytes_vector.end(), begin_object);
|
|
||||||
|
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
@ -45,6 +45,10 @@ bool TcpClient::initialize(const std::string &ip, uint16 port)
|
|||||||
return initialized = true;
|
return initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TcpClient::TcpClient()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
TcpClient::TcpClient(const SOCKET & socket)
|
TcpClient::TcpClient(const SOCKET & socket)
|
||||||
{
|
{
|
||||||
tcp_socket = socket;
|
tcp_socket = socket;
|
||||||
@ -70,7 +74,12 @@ TcpClient::~TcpClient()
|
|||||||
|
|
||||||
void TcpClient::Shutdown()
|
void TcpClient::Shutdown()
|
||||||
{
|
{
|
||||||
uint16 code = shutdown(tcp_socket, SD_SEND);
|
NetworkMessage message;
|
||||||
|
message.sender = id;
|
||||||
|
message.distribution_mode = Server;
|
||||||
|
message.tag = DISCONNECT;
|
||||||
|
SendMessage(message);
|
||||||
|
uint16 code = shutdown(tcp_socket, SD_BOTH);
|
||||||
if (code == SOCKET_ERROR)
|
if (code == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
if (Config::GetUsingConsole())
|
if (Config::GetUsingConsole())
|
||||||
@ -148,7 +157,7 @@ const NetworkBuffer &TcpClient::receive_data_array()
|
|||||||
uint16 temp;
|
uint16 temp;
|
||||||
if (DataAvailable(temp) && temp > 0)
|
if (DataAvailable(temp) && temp > 0)
|
||||||
{
|
{
|
||||||
if (!recv(tcp_socket, reinterpret_cast<char*>(&buffer.header[0]), 4, 0))
|
if (!recv(tcp_socket, reinterpret_cast<char*>(buffer.header.data()), 4, 0))
|
||||||
//invalid header
|
//invalid header
|
||||||
return NetworkBuffer();
|
return NetworkBuffer();
|
||||||
}
|
}
|
||||||
@ -156,7 +165,7 @@ const NetworkBuffer &TcpClient::receive_data_array()
|
|||||||
return NetworkBuffer();
|
return NetworkBuffer();
|
||||||
|
|
||||||
uint32 body_size = Utility::BitConverter::ToUint32(buffer.header);
|
uint32 body_size = Utility::BitConverter::ToUint32(buffer.header);
|
||||||
int16 body_received = recv(tcp_socket, reinterpret_cast<char*>(&buffer.body[0]), body_size, 0);
|
int16 body_received = recv(tcp_socket, reinterpret_cast<char*>(buffer.body.data()), body_size, 0);
|
||||||
if (body_received == SOCKET_ERROR || body_received != body_size || WSAGetLastError() != 0)
|
if (body_received == SOCKET_ERROR || body_received != body_size || WSAGetLastError() != 0)
|
||||||
{
|
{
|
||||||
//there was a problem receiving the body of the message or theres no body to receive
|
//there was a problem receiving the body of the message or theres no body to receive
|
||||||
@ -196,8 +205,9 @@ const NetworkMessage & TcpClient::ReceiveMessage()
|
|||||||
void TcpClient::send_network_message(const NetworkMessage &message, TcpClient *client)
|
void TcpClient::send_network_message(const NetworkMessage &message, TcpClient *client)
|
||||||
{
|
{
|
||||||
NetworkBuffer buffer = NetworkMessage::EncodeMessage(message);
|
NetworkBuffer buffer = NetworkMessage::EncodeMessage(message);
|
||||||
int32 bytes_sent = send(client->tcp_socket, reinterpret_cast<char*>(&buffer.body[0]), Utility::BitConverter::ToUint32(buffer.header), 0);
|
int32 lenght = Utility::BitConverter::ToUint32(buffer.header);
|
||||||
if (bytes_sent == SOCKET_ERROR || bytes_sent != Utility::BitConverter::ToInt32(buffer.header) || WSAGetLastError() != 0)
|
int32 bytes_sent = send(client->tcp_socket, reinterpret_cast<char*>(buffer.body.data()), lenght, 0);
|
||||||
|
if (bytes_sent == SOCKET_ERROR || bytes_sent != lenght || WSAGetLastError() != 0)
|
||||||
{
|
{
|
||||||
//something went wrong couldnt send anything/some data
|
//something went wrong couldnt send anything/some data
|
||||||
}
|
}
|
||||||
@ -210,13 +220,22 @@ void TcpClient::SendMessage(const NetworkMessage &message)
|
|||||||
|
|
||||||
void TcpClient::SendBytes(const std::vector<byte>& bytes)
|
void TcpClient::SendBytes(const std::vector<byte>& bytes)
|
||||||
{
|
{
|
||||||
int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(&bytes[0]), bytes.size(), 0);
|
int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(bytes.data()), bytes.size(), 0);
|
||||||
if (bytes_sent == SOCKET_ERROR || bytes_sent != bytes.size() || WSAGetLastError() != 0)
|
if (bytes_sent == SOCKET_ERROR || bytes_sent != bytes.size() || WSAGetLastError() != 0)
|
||||||
{
|
{
|
||||||
//something went wrong couldnt send anything/some data
|
//something went wrong couldnt send anything/some data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TcpClient::SendBytes(byte * bytes, uint32 size)
|
||||||
|
{
|
||||||
|
int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(bytes), size, 0);
|
||||||
|
if (bytes_sent == SOCKET_ERROR || bytes_sent != size || WSAGetLastError() != 0)
|
||||||
|
{
|
||||||
|
//something went wrong couldnt send anything/some data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void TcpClient::SetOnDisconnectCallback(void(*func)(uint16))
|
void TcpClient::SetOnDisconnectCallback(void(*func)(uint16))
|
||||||
{
|
{
|
||||||
OnDisconnect = func;
|
OnDisconnect = func;
|
||||||
|
|||||||
@ -164,6 +164,8 @@ void TcpServer::SendMessage(const NetworkMessage & message)
|
|||||||
}
|
}
|
||||||
case Server: // this will only send the message to the server
|
case Server: // this will only send the message to the server
|
||||||
{
|
{
|
||||||
|
if (message.tag == DISCONNECT)
|
||||||
|
CloseSocket(message.sender);
|
||||||
OnMessage(message);
|
OnMessage(message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -241,3 +243,31 @@ void TcpServer::AcceptConnection(TcpClient & client)
|
|||||||
Handshake handshake(client.GetID(), ConnectionCode::Accept);
|
Handshake handshake(client.GetID(), ConnectionCode::Accept);
|
||||||
client.SendBytes(Handshake::EncodeHandshake(handshake));
|
client.SendBytes(Handshake::EncodeHandshake(handshake));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TcpServer::CloseSocket(TcpClient & client)
|
||||||
|
{
|
||||||
|
NetworkMessage message;
|
||||||
|
message.sender = -1;
|
||||||
|
message.distribution_mode = ID;
|
||||||
|
message.destination_id = client.GetID();
|
||||||
|
message.tag = DISCONNECT;
|
||||||
|
SendMessage(message);
|
||||||
|
clients.erase(std::remove(clients.begin(), clients.end(), client), clients.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
void TcpServer::CloseSocket(uint16 id)
|
||||||
|
{
|
||||||
|
TcpClient client = GetClientByID(id);
|
||||||
|
if (client.GetID() != -2)
|
||||||
|
CloseSocket(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
const TcpClient & TcpServer::GetClientByID(uint16 id)
|
||||||
|
{
|
||||||
|
for (std::vector<TcpClient>::iterator it = clients.begin(); it != clients.end(); ++it)
|
||||||
|
{
|
||||||
|
if ((*it).GetID() == id)
|
||||||
|
return *it;
|
||||||
|
}
|
||||||
|
return TcpClient();
|
||||||
|
}
|
||||||
|
|||||||
@ -8,9 +8,9 @@
|
|||||||
|
|
||||||
bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port)
|
bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port)
|
||||||
{
|
{
|
||||||
client->SetIP(ip);
|
client.SetIP(ip);
|
||||||
client->SetPort(port);
|
client.SetPort(port);
|
||||||
return client->Connect();
|
return client.Connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VoidNetClientAPI::SendMessageToServer(byte tag, byte subject, void *data)
|
void VoidNetClientAPI::SendMessageToServer(byte tag, byte subject, void *data)
|
||||||
@ -47,7 +47,7 @@ void VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id
|
|||||||
message.distribution_mode = distribution_mode;
|
message.distribution_mode = distribution_mode;
|
||||||
message.sender = id;
|
message.sender = id;
|
||||||
message.destination_id = destination_id;
|
message.destination_id = destination_id;
|
||||||
client->SendMessage(message);
|
client.SendMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VoidNetClientAPI::Receive()
|
void VoidNetClientAPI::Receive()
|
||||||
@ -57,12 +57,10 @@ void VoidNetClientAPI::Receive()
|
|||||||
|
|
||||||
void VoidNetClientAPI::process_all_data()
|
void VoidNetClientAPI::process_all_data()
|
||||||
{
|
{
|
||||||
client->ReceiveMessages();
|
client.ReceiveMessages();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VoidNetClientAPI::Disconnect()
|
void VoidNetClientAPI::Disconnect()
|
||||||
{
|
{
|
||||||
Utility::Delete(client);
|
|
||||||
receive = false;
|
receive = false;
|
||||||
receive_thread.join();
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user