Added Handshake class
Fixed NetworkBuffer include Fixed some data types TcpClient: Added SendBytes, SetOnDisconnectCallback, SetOnConnectCallback, SetOnMessageCallback methods and made callback std::function's private and fixed some method implementations Added AcceptConnection on TcpServer Fixed TcpServer AddToClientsList
This commit is contained in:
40
src/Handshake.cpp
Normal file
40
src/Handshake.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include "Handshake.hpp"
|
||||
#include "Utility.hpp"
|
||||
|
||||
Handshake::Handshake()
|
||||
{
|
||||
id = -2;
|
||||
}
|
||||
|
||||
Handshake::Handshake(uint16 id, byte con_code)
|
||||
{
|
||||
this->id = id;
|
||||
this->con_code = con_code;
|
||||
}
|
||||
|
||||
Handshake::~Handshake()
|
||||
{
|
||||
}
|
||||
|
||||
const std::vector<byte>& Handshake::EncodeHandshake(const Handshake & handshake)
|
||||
{
|
||||
std::vector<byte> handshake_bytes;
|
||||
|
||||
std::vector<byte> id = Utility::BitConverter::FromUint16(handshake.id);
|
||||
std::vector<byte> con_mode = Utility::BitConverter::FromUint8(handshake.con_code);
|
||||
|
||||
handshake_bytes.insert(handshake_bytes.end(), id.begin(), id.end());
|
||||
handshake_bytes.insert(handshake_bytes.end(), con_mode.begin(), con_mode.end());
|
||||
|
||||
return handshake_bytes;
|
||||
}
|
||||
|
||||
Handshake & Handshake::DecodeHandshake(const std::vector<byte>& bytes)
|
||||
{
|
||||
Handshake handshake;
|
||||
|
||||
handshake.id = Utility::BitConverter::ToUint16(bytes);
|
||||
handshake.con_code = Utility::BitConverter::ToUint8(bytes, 2);
|
||||
|
||||
return handshake;
|
||||
}
|
||||
@ -1,5 +1,4 @@
|
||||
#include "NetworkBuffer.hpp"
|
||||
#include "Utility.hpp"
|
||||
|
||||
NetworkBuffer::NetworkBuffer()
|
||||
{
|
||||
|
||||
@ -9,7 +9,7 @@ NetworkMessage::NetworkMessage()
|
||||
{
|
||||
}
|
||||
|
||||
NetworkMessage::NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer) :
|
||||
NetworkMessage::NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, byte subject, NetworkBuffer buffer) :
|
||||
sender(sender), distribution_mode(distribution_mode), destination_id(destination_id), tag(tag), subject(subject), buffer(buffer)
|
||||
{
|
||||
}
|
||||
@ -52,7 +52,7 @@ const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer)
|
||||
message.distribution_mode = buffer.body[3];
|
||||
message.destination_id = Utility::BitConverter::ToUint16(buffer.body, 4);
|
||||
message.tag = buffer.body[6];
|
||||
message.subject = Utility::BitConverter::ToUint16(buffer.body, 7);
|
||||
message.subject = Utility::BitConverter::ToUint8(buffer.body, 7);
|
||||
message.buffer = buffer;
|
||||
message.valid = message.sender != -2 && message.tag != CONNECT && message.tag != DISCONNECT;
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include "Config.hpp"
|
||||
#include "NetworkBuffer.hpp"
|
||||
#include "Tags.hpp"
|
||||
#include "Handshake.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
@ -124,8 +125,15 @@ bool TcpClient::Connect()
|
||||
uint16 connect_code = ::connect(tcp_socket, result->ai_addr, result->ai_addrlen);
|
||||
if (connect_code == SOCKET_ERROR)
|
||||
return false;
|
||||
receive = true;
|
||||
return true;
|
||||
|
||||
NetworkBuffer message(receive_data_array());
|
||||
Handshake handshake = Handshake::DecodeHandshake(message.body);
|
||||
if (handshake.con_code == ConnectionCode::Accept)
|
||||
{
|
||||
receive = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TcpClient::DataAvailable(uint16 &size)
|
||||
@ -137,8 +145,8 @@ const NetworkBuffer &TcpClient::receive_data_array()
|
||||
{
|
||||
NetworkBuffer buffer;
|
||||
|
||||
uint16 body_size;
|
||||
if (DataAvailable(body_size))
|
||||
uint16 temp;
|
||||
if (DataAvailable(temp) && temp > 0)
|
||||
{
|
||||
if (!recv(tcp_socket, reinterpret_cast<char*>(&buffer.header[0]), 4, 0))
|
||||
//invalid header
|
||||
@ -148,10 +156,11 @@ const NetworkBuffer &TcpClient::receive_data_array()
|
||||
return NetworkBuffer();
|
||||
|
||||
uint32 body_size = Utility::BitConverter::ToUint32(buffer.header);
|
||||
int32 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[0]), body_size, 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
|
||||
return NetworkBuffer();
|
||||
}
|
||||
|
||||
return buffer;
|
||||
@ -188,7 +197,7 @@ void TcpClient::send_network_message(const NetworkMessage &message, TcpClient *c
|
||||
{
|
||||
NetworkBuffer buffer = NetworkMessage::EncodeMessage(message);
|
||||
int32 bytes_sent = send(client->tcp_socket, reinterpret_cast<char*>(&buffer.body[0]), Utility::BitConverter::ToUint32(buffer.header), 0);
|
||||
if (bytes_sent == SOCKET_ERROR || bytes_sent != Utility::BitConverter::ToUint32(buffer.header) || WSAGetLastError() != 0)
|
||||
if (bytes_sent == SOCKET_ERROR || bytes_sent != Utility::BitConverter::ToInt32(buffer.header) || WSAGetLastError() != 0)
|
||||
{
|
||||
//something went wrong couldnt send anything/some data
|
||||
}
|
||||
@ -198,3 +207,27 @@ void TcpClient::SendMessage(const NetworkMessage &message)
|
||||
{
|
||||
std::async(std::launch::async, &send_network_message, message, this);
|
||||
}
|
||||
|
||||
void TcpClient::SendBytes(const std::vector<byte>& bytes)
|
||||
{
|
||||
int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(&bytes[0]), bytes.size(), 0);
|
||||
if (bytes_sent == SOCKET_ERROR || bytes_sent != bytes.size() || WSAGetLastError() != 0)
|
||||
{
|
||||
//something went wrong couldnt send anything/some data
|
||||
}
|
||||
}
|
||||
|
||||
void TcpClient::SetOnDisconnectCallback(void(*func)(uint16))
|
||||
{
|
||||
OnDisconnect = func;
|
||||
}
|
||||
|
||||
void TcpClient::SetOnConnectCallback(void(*func)(uint16))
|
||||
{
|
||||
OnConnect = func;
|
||||
}
|
||||
|
||||
void TcpClient::SetOnMessageCallback(void(*func)(uint16, byte, byte, void*))
|
||||
{
|
||||
OnMessage = func;
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include "TcpServer.hpp"
|
||||
#include "Config.hpp"
|
||||
#include "Utility.hpp"
|
||||
#include "Handshake.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
@ -211,27 +212,32 @@ uint16 TcpServer::AllocateID() // this function is only used in the AddToClients
|
||||
return 0;
|
||||
}
|
||||
|
||||
void TcpServer::AddToClientsList(const TcpClient & client_socket)
|
||||
void TcpServer::AddToClientsList(TcpClient & client_socket)
|
||||
{
|
||||
TcpClient client(client_socket);
|
||||
uint16 id = AllocateID();
|
||||
if (id > 0)
|
||||
{
|
||||
client.SetID(id);
|
||||
clients.emplace_back(client);
|
||||
client_socket.SetID(id);
|
||||
clients.emplace_back(client_socket);
|
||||
AcceptConnection(client_socket);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Config::GetUsingConsole())
|
||||
std::cout << "No available ID's" << std::endl;
|
||||
RejectConnection(client);
|
||||
RejectConnection(client_socket);
|
||||
}
|
||||
}
|
||||
|
||||
void TcpServer::RejectConnection(TcpClient &client)
|
||||
{
|
||||
NetworkMessage message;
|
||||
message.sender = -1;
|
||||
message.tag = Reject; // 0 for rejected connection
|
||||
client.SendMessage(message);
|
||||
}
|
||||
Handshake handshake(client.GetID(), ConnectionCode::Reject);
|
||||
client.SendBytes(Handshake::EncodeHandshake(handshake));
|
||||
client.Shutdown();
|
||||
}
|
||||
|
||||
void TcpServer::AcceptConnection(TcpClient & client)
|
||||
{
|
||||
Handshake handshake(client.GetID(), ConnectionCode::Accept);
|
||||
client.SendBytes(Handshake::EncodeHandshake(handshake));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user