Update
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:
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "NetworkMessage.hpp"
|
||||
#include "HLAPI/NetworkMessage.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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
|
||||
|
||||
16
include/HLAPI/Plugin/Plugin.hpp
Normal file
16
include/HLAPI/Plugin/Plugin.hpp
Normal 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;
|
||||
};
|
||||
}
|
||||
29
include/HLAPI/Plugin/PluginManager.hpp
Normal file
29
include/HLAPI/Plugin/PluginManager.hpp
Normal 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*>();
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "TcpListener.hpp"
|
||||
#include "VoidNet/TcpListener.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Response.hpp"
|
||||
#include "Request.hpp"
|
||||
#include "VoidNet/Response.hpp"
|
||||
#include "VoidNet/Request.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#include "Net.hpp"
|
||||
#include "VoidNet/Net.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
#include <ws2tcpip.h>
|
||||
#undef SendMessage
|
||||
#undef SetPort
|
||||
#undef max
|
||||
#undef min
|
||||
|
||||
#define poll WSAPoll
|
||||
#define ioctl ioctlsocket
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -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>
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
|
||||
//#include <openssl/rand.h>
|
||||
//#include <openssl/ssl.h>
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <thread>
|
||||
#include <queue>
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Enums.hpp"
|
||||
#include "IPAddress.hpp"
|
||||
#include "VoidNet/Enums.hpp"
|
||||
#include "VoidNet/IPAddress.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "UdpSocket.hpp"
|
||||
#include "IPAddress.hpp"
|
||||
#include "VoidNet/UdpSocket.hpp"
|
||||
#include "VoidNet/IPAddress.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
#include "Net.hpp"
|
||||
#include "VoidNet/Net.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user