Removed Message Queue
Sending and receiving is now in one thread (gotta make them separate)
Created a very crude Plugin System
Renamed AllAndMe to AllAndServer
This commit is contained in:
TheDoctor
2019-10-15 16:27:16 +01:00
parent 32473472e4
commit c50d40341f
48 changed files with 321 additions and 334 deletions

View File

@ -1,6 +1,6 @@
#pragma once
#include "NetworkMessage.hpp"
#include "HLAPI/NetworkMessage.hpp"
namespace std::net
{

View File

@ -1,43 +0,0 @@
#pragma once
#include <queue>
#include <mutex>
#include "NetworkMessage.hpp"
#include "NewConnectionEvent.hpp"
#include "DisconnectedEvent.hpp"
#include "DataReceivedEvent.hpp"
namespace std::net
{
class MessageQueue
{
public:
MessageQueue()
{
}
void EnqueueMessageToSend(const NetworkMessage &msg);
void EnqueueMessageReceived(const NetworkMessage &msg);
void EnqueueDisconnection(const NetworkMessage &msg);
void EnqueueConnection(const NetworkMessage &msg);
NetworkMessage DequeueMessageToSend();
uint32_t SendSize();
private:
std::deque<NetworkMessage> m_messagesToSend;
std::deque<NewConnectionEvent> m_connectionEvents;
std::deque<DisconnectedEvent> m_disconnectedEvents;
std::deque<DataReceivedEvent> m_dataReceivedEvents;
std::mutex m_sendMutex;
std::mutex m_receivedMutex;
std::mutex m_disconnectMutex;
std::mutex m_connectionMutex;
};
}

View File

@ -1,7 +1,7 @@
#pragma once
#include "NetworkHeader.hpp"
#include "ByteConverter.hpp"
#include "HLAPI/NetworkHeader.hpp"
#include "HLAPI/ByteConverter.hpp"
#include <cstdint>
#include <cstring>
@ -14,7 +14,7 @@ namespace std::net
Others,
OthersAndServer,
All,
AllAndMe,
AllAndServer,
Server
};
@ -58,14 +58,14 @@ namespace std::net
}
template<typename T>
uint8_t * SerializeData(uint32_t &size)
uint8_t * SerializeData(uint32_t &size) const
{
int32_t sizeOfNetHeader = sizeof(NetworkHeader);
NetworkHeader header;
header.Size = 13 + sizeOfNetHeader + sizeof(T);
uint8_t *bytes = new uint8_t[header.Size];
uint8_t *bytes = new uint8_t[header.Size]();
memcpy(bytes, &header, sizeOfNetHeader);
uint8_t *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
@ -83,8 +83,9 @@ namespace std::net
return bytes;
}
uint8_t *SerializeData(uint32_t &size);
uint8_t *SerializeData(uint32_t &size) const;
void Deserialize(uint8_t *data, uint32_t size);
void DeserializeWithoutHeader(uint8_t* data, uint32_t size);
template<typename T>
T *GetData() const

View File

@ -0,0 +1,16 @@
#pragma once
#include <HLAPI/NetworkMessage.hpp>
namespace std::net
{
class Plugin
{
public:
Plugin()
{
}
virtual void HandleMessage(const NetworkMessage& msg) = 0;
};
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <vector>
#include "HLAPI/Plugin/Plugin.hpp"
namespace std::net
{
class PluginManager
{
public:
PluginManager();
~PluginManager();
void HandleMessage(const NetworkMessage& msg)
{
for (size_t i = 0; i < m_plugins.size(); i++)
m_plugins.at(i)->HandleMessage(msg);
}
void AddPlugin(Plugin* p)
{
m_plugins.emplace_back(p);
}
private:
std::vector<Plugin*> m_plugins = std::vector<Plugin*>();
};
}

View File

@ -1,14 +1,13 @@
#pragma once
#include <Net.hpp>
#include <VoidNet/Net.hpp>
#include <memory>
namespace std::net
{
class TcpServer;
class MessageQueue;
class Plugin;
class Server
{
@ -18,6 +17,8 @@ namespace std::net
void Start();
void Stop();
void AddPlugin(Plugin* plugin);
private:
std::shared_ptr<std::net::TcpServer> m_tcpServer;
};

View File

@ -2,9 +2,9 @@
#include <functional>
#include "TcpClient.hpp"
#include "VoidNet/TcpClient.hpp"
#include "NetworkMessage.hpp"
#include "HLAPI/NetworkMessage.hpp"
namespace std::net
{
@ -32,7 +32,7 @@ namespace std::net
std::function<void(uint32_t, void*)> NewConnectionEvent;
private:
bool sendMessage(NetworkMessage &msg);
bool sendMessage(const NetworkMessage &msg);
std::shared_ptr<TcpClient> m_client;
uint32_t m_id;

View File

@ -4,8 +4,9 @@
#include <atomic>
#include <queue>
#include <TcpListener.hpp>
#include <TcpConnection.hpp>
#include <VoidNet/TcpListener.hpp>
#include <HLAPI/TcpConnection.hpp>
#include <HLAPI/Plugin/PluginManager.hpp>
namespace std::net
{
@ -30,14 +31,15 @@ namespace std::net
void AddClient(std::shared_ptr<TcpConnection> &c);
void SetMaxConnections(uint32_t max_connections);
void HandlePluginMessage(const NetworkMessage& message);
uint32_t GetAvailableID();
private:
void HandleReceiveMsgAndConns();
void HandleSend();
void HandleMessage(const NetworkMessage &msg);
void HandleReceiveMsgAndConnsThreaded();
void HandleSendThreaded();
private:
std::vector<std::shared_ptr<TcpConnection>> m_list;
@ -54,6 +56,8 @@ namespace std::net
std::shared_ptr<TcpListener> m_listenerPtr;
std::shared_ptr<PluginManager> m_pluginManager;
std::vector<pollfd> poll_fds;
};
}

View File

@ -2,7 +2,7 @@
#include <atomic>
#include "TcpListener.hpp"
#include "VoidNet/TcpListener.hpp"
namespace std::net
{

View File

@ -2,8 +2,8 @@
#pragma once
#include "Response.hpp"
#include "Request.hpp"
#include "VoidNet/Response.hpp"
#include "VoidNet/Request.hpp"
namespace std::net
{

View File

@ -5,7 +5,7 @@
#include <ostream>
#include <string>
#include "Net.hpp"
#include "VoidNet/Net.hpp"
namespace std::net
{

View File

@ -4,9 +4,9 @@
#include <chrono>
#include <memory>
#include "Net.hpp"
#include "IPAddress.hpp"
#include "Enums.hpp"
#include "VoidNet/Net.hpp"
#include "VoidNet/IPAddress.hpp"
#include "VoidNet/Enums.hpp"
namespace std::net
{

View File

@ -7,6 +7,8 @@
#include <ws2tcpip.h>
#undef SendMessage
#undef SetPort
#undef max
#undef min
#define poll WSAPoll
#define ioctl ioctlsocket

View File

@ -2,9 +2,9 @@
#pragma once
#include "Uri.hpp"
#include "Headers.hpp"
#include "Enums.hpp"
#include "VoidNet/Uri.hpp"
#include "VoidNet/Headers.hpp"
#include "VoidNet/Enums.hpp"
namespace std::net
{

View File

@ -2,9 +2,9 @@
#pragma once
#include "Headers.hpp"
#include "Cookies.hpp"
#include "Enums.hpp"
#include "VoidNet/Headers.hpp"
#include "VoidNet/Cookies.hpp"
#include "VoidNet/Enums.hpp"
#include <string>

View File

@ -1,6 +1,6 @@
#pragma once
#include "Socket.hpp"
#include "VoidNet/Socket.hpp"
//#include <openssl/rand.h>
//#include <openssl/ssl.h>

View File

@ -1,7 +1,7 @@
#pragma once
#include "ISocket.hpp"
#include "Enums.hpp"
#include "VoidNet/ISocket.hpp"
#include "VoidNet/Enums.hpp"
namespace std::net
{
@ -14,7 +14,7 @@ namespace std::net
init();
}
inline Socket(SOCKET newSocket, SocketType socketType, SocketProtocol protocol = SocketProtocol::IPv4)
inline Socket(SOCKET newSocket, SocketType socketType = SocketType::Streaming, SocketProtocol protocol = SocketProtocol::IPv4)
: ISocket(socketType, protocol)
, m_socket(newSocket)
{

View File

@ -1,6 +1,6 @@
#pragma once
#include "Socket.hpp"
#include "VoidNet/Socket.hpp"
namespace std::net
{

View File

@ -3,7 +3,7 @@
#include <thread>
#include <queue>
#include "Socket.hpp"
#include "VoidNet/Socket.hpp"
namespace std::net
{

View File

@ -1,7 +1,7 @@
#pragma once
#include "Enums.hpp"
#include "IPAddress.hpp"
#include "VoidNet/Enums.hpp"
#include "VoidNet/IPAddress.hpp"
namespace std::net
{

View File

@ -1,6 +1,6 @@
#pragma once
#include "Socket.hpp"
#include "VoidNet/Socket.hpp"
#include <memory>

View File

@ -1,7 +1,7 @@
#pragma once
#include "UdpSocket.hpp"
#include "IPAddress.hpp"
#include "VoidNet/UdpSocket.hpp"
#include "VoidNet/IPAddress.hpp"
#include <vector>

View File

@ -3,7 +3,7 @@
#include <vector>
#include <sstream>
#include "Net.hpp"
#include "VoidNet/Net.hpp"
namespace std::net
{