TcpServer's OnMessage is now an vector called OnMessageFunctions, each plugin will occupy an index in this vector

Readded receive_data method to VoidNetClient.hpp
Minor fixes
This commit is contained in:
xX-TheDoctor-Xx
2016-08-15 17:46:29 +01:00
parent 8ceefbd9fe
commit e5ca02be0d
9 changed files with 20 additions and 11 deletions

View File

@ -7,6 +7,7 @@
#include "Defs.hpp" #include "Defs.hpp"
#include "TcpClient.hpp" #include "TcpClient.hpp"
#include "Handshake.hpp"
#include <functional> #include <functional>
@ -38,7 +39,7 @@ public:
const TcpClient &GetClientByID(uint16 id); const TcpClient &GetClientByID(uint16 id);
std::function<void(const NetworkMessage &message)> OnMessage; // this is going to be used for plugins std::vector<std::function<void(const NetworkMessage &message)>> OnMessageFunctions; // this is going to be used for plugins
private: private:
static void process_client_messages(TcpServer *server, TcpClient &client); static void process_client_messages(TcpServer *server, TcpClient &client);

View File

@ -30,9 +30,10 @@ struct VoidNetClientAPI
static void Receive(); static void Receive();
private: private:
static void receive_data();
static TcpClient client; static TcpClient client;
static uint16 id; static uint16 id;
static bool receive;
}; };
#endif #endif

View File

@ -55,8 +55,8 @@ void TcpClient::Shutdown()
{ {
NetworkMessage message; NetworkMessage message;
message.sender = id; message.sender = id;
message.distribution_mode = static_cast<byte>(Server); message.distribution_mode = Server;
message.tag = static_cast<byte>(DISCONNECT); message.tag = DISCONNECT;
SendMessage(message); SendMessage(message);
uint16 code = closesocket(tcp_socket); uint16 code = closesocket(tcp_socket);
if (code == SOCKET_ERROR) if (code == SOCKET_ERROR)

View File

@ -60,7 +60,8 @@ void TcpServer::SendMessage(const NetworkMessage & message)
if (message.sender != client.GetID()) if (message.sender != client.GetID())
client.SendMessage(message); client.SendMessage(message);
} }
OnMessage(message); for (uint16 i = 0; i < OnMessageFunctions.size(); i++)
OnMessageFunctions[i](message);
break; break;
} }
case AllAndMe: // this will send the message to EVERYONE including the user that sent it case AllAndMe: // this will send the message to EVERYONE including the user that sent it
@ -70,14 +71,16 @@ void TcpServer::SendMessage(const NetworkMessage & message)
TcpClient client = *it; TcpClient client = *it;
client.SendMessage(message); client.SendMessage(message);
} }
OnMessage(message); for (uint16 i = 0; i < OnMessageFunctions.size(); i++)
OnMessageFunctions[i](message);
break; break;
} }
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) if (message.tag == DISCONNECT)
CloseSocket(message.sender); CloseSocket(message.sender);
OnMessage(message); for (uint16 i = 0; i < OnMessageFunctions.size(); i++)
OnMessageFunctions[i](message);
break; break;
} }
case Others: // this will send the message to others, excluding server and the user that sent it case Others: // this will send the message to others, excluding server and the user that sent it
@ -106,7 +109,7 @@ void TcpServer::SendMessage(const NetworkMessage & message)
} }
} }
void TcpServer::SendHandshake(const Handshake &handshake) void TcpServer::SendHandshake(const Handshake & handshake)
{ {
switch (handshake.distribution_mode) switch (handshake.distribution_mode)
{ {

View File

@ -52,11 +52,15 @@ void VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id
void VoidNetClientAPI::Receive() void VoidNetClientAPI::Receive()
{ {
std::async(std::launch::async, &client.ReceiveMessages); std::async(std::launch::async, &receive_data);
}
void VoidNetClientAPI::receive_data()
{
client.ReceiveMessages();
} }
void VoidNetClientAPI::Disconnect() void VoidNetClientAPI::Disconnect()
{ {
receive = false; client.Shutdown();
SendMessageToServer
} }