Add TCP support (#12)
All checks were successful
Linux arm64 / Build (push) Successful in 15s
All checks were successful
Linux arm64 / Build (push) Successful in 15s
Reviewed-on: #12 Co-authored-by: Persson-dev <sim16.prib@gmail.com> Co-committed-by: Persson-dev <sim16.prib@gmail.com>
This commit was merged in pull request #12.
This commit is contained in:
@@ -35,6 +35,7 @@ class DataBuffer {
|
||||
typedef Data::difference_type difference_type;
|
||||
|
||||
DataBuffer();
|
||||
DataBuffer(std::size_t a_InitialSize);
|
||||
DataBuffer(const DataBuffer& other);
|
||||
DataBuffer(const DataBuffer& other, difference_type offset);
|
||||
DataBuffer(DataBuffer&& other);
|
||||
|
||||
25
include/sp/common/NonCopyable.h
Normal file
25
include/sp/common/NonCopyable.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file NonCopyable.h
|
||||
* \brief File containing the sp::NonCopyable class
|
||||
*/
|
||||
|
||||
namespace sp {
|
||||
|
||||
/**
|
||||
* \class NonCopyable
|
||||
* \brief Class used to make a class non copyable
|
||||
* \note Inherit from this class privately to make a class non copyable
|
||||
*/
|
||||
class NonCopyable {
|
||||
public:
|
||||
NonCopyable(const NonCopyable&) = delete;
|
||||
NonCopyable& operator=(const NonCopyable&) = delete;
|
||||
|
||||
protected:
|
||||
NonCopyable() {}
|
||||
~NonCopyable() {}
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
@@ -2,4 +2,8 @@
|
||||
|
||||
#if __has_include(<sp/extensions/Compress.h>)
|
||||
#include <sp/extensions/Compress.h>
|
||||
#endif
|
||||
|
||||
#if __has_include(<sp/extensions/Tcp.h>)
|
||||
#include <sp/extensions/Tcp.h>
|
||||
#endif
|
||||
4
include/sp/extensions/Tcp.h
Normal file
4
include/sp/extensions/Tcp.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/extensions/tcp/TcpListener.h>
|
||||
#include <sp/extensions/tcp/TcpSocket.h>
|
||||
65
include/sp/extensions/tcp/TcpListener.h
Normal file
65
include/sp/extensions/tcp/TcpListener.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/extensions/tcp/TcpSocket.h>
|
||||
|
||||
namespace sp {
|
||||
namespace io {
|
||||
|
||||
/**
|
||||
* \class TcpListener
|
||||
*/
|
||||
class TcpListener : private NonCopyable {
|
||||
public:
|
||||
/**
|
||||
* \brief Starts listening for guests to connect
|
||||
* \param port The port to listen to
|
||||
* \param maxConnexions The maximum amount of connexion that can happen at the same time. \n
|
||||
* Every other guests will be kicked if this amount is reached.
|
||||
* \return Whether this action was succesfull
|
||||
*/
|
||||
TcpListener(std::uint16_t a_Port, int a_MaxConnexions);
|
||||
|
||||
/**
|
||||
* \brief Default destructor
|
||||
*/
|
||||
~TcpListener();
|
||||
|
||||
/**
|
||||
* \brief Tries to accept an incoming request to connect
|
||||
* \return the new socket if a new connexion was accepted or nullptr
|
||||
*/
|
||||
std::unique_ptr<TcpSocket> Accept();
|
||||
|
||||
/**
|
||||
* \brief Closes the socket
|
||||
*/
|
||||
void Close();
|
||||
|
||||
/**
|
||||
* \brief Allows to set the socket in non blocking/blocking mode
|
||||
* \param a_Blocking If set to true, every call to Read will wait until the socket receives something
|
||||
* \return true if the operation was successful
|
||||
*/
|
||||
bool SetBlocking(bool a_Blocking);
|
||||
|
||||
/**
|
||||
* \brief Getter of the m_Port member
|
||||
* \return The port which the socket listen to
|
||||
*/
|
||||
std::uint16_t GetListeningPort() const;
|
||||
|
||||
/**
|
||||
* \brief Getter of the m_MaxConnections member
|
||||
* \return The maximum amount of connexions that can happen at the same time.
|
||||
*/
|
||||
int GetMaximumConnections() const;
|
||||
|
||||
private:
|
||||
SocketHandle m_Handle;
|
||||
std::uint16_t m_Port;
|
||||
int m_MaxConnections;
|
||||
};
|
||||
|
||||
|
||||
} // namespace io
|
||||
} // namespace sp
|
||||
86
include/sp/extensions/tcp/TcpSocket.h
Normal file
86
include/sp/extensions/tcp/TcpSocket.h
Normal file
@@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/NonCopyable.h>
|
||||
#include <sp/io/IOInterface.h>
|
||||
|
||||
namespace sp {
|
||||
namespace io {
|
||||
|
||||
using SocketHandle = int;
|
||||
|
||||
struct TcpTag {};
|
||||
|
||||
class SocketError : public std::exception {
|
||||
private:
|
||||
std::string m_Error;
|
||||
|
||||
public:
|
||||
SocketError(std::string&& a_Msg) : m_Error(std::move(a_Msg)) {}
|
||||
|
||||
virtual const char* what() const noexcept override {
|
||||
return m_Error.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
class IOInterface<TcpTag> : private NonCopyable {
|
||||
public:
|
||||
/**
|
||||
* \enum Status
|
||||
* \brief Describes the state of a socket
|
||||
*/
|
||||
enum class Status {
|
||||
/** The socket is connected */
|
||||
Connected,
|
||||
/** The socket is not connected */
|
||||
Disconnected,
|
||||
/** Something bad happened */
|
||||
Error,
|
||||
};
|
||||
|
||||
IOInterface();
|
||||
IOInterface(const std::string& a_Host, std::uint16_t a_Port);
|
||||
IOInterface(IOInterface&& a_Other);
|
||||
IOInterface& operator=(IOInterface&& a_Other);
|
||||
virtual ~IOInterface();
|
||||
|
||||
DataBuffer Read(std::size_t a_Amount);
|
||||
void Write(const sp::DataBuffer& a_Data);
|
||||
|
||||
/**
|
||||
* \brief Allows to set the socket in non blocking/blocking mode
|
||||
* \param a_Block If set to true, every call to Read will wait until the socket receives something
|
||||
* \return true if the operation was successful
|
||||
*/
|
||||
bool SetBlocking(bool a_Block);
|
||||
|
||||
/**
|
||||
* \brief Getter of the m_Status member
|
||||
* \return The TcpSocket::Status of this socket
|
||||
*/
|
||||
Status GetStatus() const;
|
||||
|
||||
/**
|
||||
* \brief Disconnects the socket from the remote
|
||||
* \note Does nothing if the socket is not connected. \n
|
||||
* This function is also called by the destructor.
|
||||
*/
|
||||
void Disconnect();
|
||||
|
||||
|
||||
private:
|
||||
SocketHandle m_Handle;
|
||||
Status m_Status;
|
||||
|
||||
void Connect(const std::string& a_Host, std::uint16_t a_Port);
|
||||
|
||||
friend class TcpListener;
|
||||
};
|
||||
|
||||
/**
|
||||
* \typedef TcpSocket
|
||||
*/
|
||||
using TcpSocket = IOInterface<TcpTag>;
|
||||
|
||||
} // namespace io
|
||||
} // namespace sp
|
||||
@@ -17,7 +17,7 @@ namespace sp {
|
||||
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
|
||||
class MessageDispatcher {
|
||||
private:
|
||||
std::map<MessageIdType, std::vector<std::shared_ptr<MessageHandler>>> m_Handlers;
|
||||
std::map<MessageIdType, std::vector<MessageHandler*>> m_Handlers;
|
||||
|
||||
public:
|
||||
using MessageBaseType = MessageBase;
|
||||
@@ -38,18 +38,20 @@ class MessageDispatcher {
|
||||
* \param type The packet type
|
||||
* \param handler The packet handler
|
||||
*/
|
||||
void RegisterHandler(MessageIdType a_MessageType, const std::shared_ptr<MessageHandler>& a_Handler);
|
||||
void RegisterHandler(MessageIdType a_MessageType, MessageHandler* a_Handler);
|
||||
|
||||
/**
|
||||
* \brief Unregister a packet handler
|
||||
* \param type The packet type
|
||||
* \param handler The packet handler
|
||||
*/
|
||||
void UnregisterHandler(MessageIdType a_MessageType, const std::shared_ptr<MessageHandler>& a_Handler);
|
||||
void UnregisterHandler(MessageIdType a_MessageType, MessageHandler* a_Handler);
|
||||
|
||||
/**
|
||||
* \brief Unregister a packet handler
|
||||
* \param handler The packet handler
|
||||
*/
|
||||
void UnregisterHandler(const std::shared_ptr<MessageHandler>& a_Handler);
|
||||
void UnregisterHandler(MessageHandler* a_Handler);
|
||||
};
|
||||
|
||||
#include <sp/protocol/message/MessageDispatcherImpl.inl>
|
||||
|
||||
@@ -1,34 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
|
||||
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::RegisterHandler(MessageIdType a_MessageType, const std::shared_ptr<MessageHandler>& a_Handler) {
|
||||
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::RegisterHandler(MessageIdType a_MessageType, MessageHandler* a_Handler) {
|
||||
assert(a_Handler);
|
||||
auto found = std::find(m_Handlers[a_MessageType].begin(), m_Handlers[a_MessageType].end(), a_Handler);
|
||||
if (found == m_Handlers[a_MessageType].end())
|
||||
m_Handlers[a_MessageType].push_back(a_Handler);
|
||||
}
|
||||
|
||||
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
|
||||
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::UnregisterHandler(MessageIdType a_MessageType, const std::shared_ptr<MessageHandler>& a_Handler) {
|
||||
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::UnregisterHandler(MessageIdType a_MessageType, MessageHandler* a_Handler) {
|
||||
auto found = std::find(m_Handlers[a_MessageType].begin(), m_Handlers[a_MessageType].end(), a_Handler);
|
||||
if (found != m_Handlers[a_MessageType].end())
|
||||
m_Handlers[a_MessageType].erase(found);
|
||||
}
|
||||
|
||||
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
|
||||
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::UnregisterHandler(const std::shared_ptr<MessageHandler>& a_Handler) {
|
||||
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::UnregisterHandler(MessageHandler* a_Handler) {
|
||||
for (auto& pair : m_Handlers) {
|
||||
if (pair.second.empty())
|
||||
continue;
|
||||
|
||||
MessageIdType type = pair.first;
|
||||
|
||||
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), a_Handler), m_Handlers[type].end());
|
||||
pair.second.erase(std::remove(pair.second.begin(), pair.second.end(), a_Handler), pair.second.end());
|
||||
}
|
||||
}
|
||||
|
||||
template <typename MessageIdType, typename MessageBase, typename MessageHandler>
|
||||
void MessageDispatcher<MessageIdType, MessageBase, MessageHandler>::Dispatch(const MessageBase& a_Message) {
|
||||
MessageIdType type = a_Message.GetId();
|
||||
for (auto& handler : m_Handlers[type])
|
||||
for (auto& handler : m_Handlers[type]) {
|
||||
a_Message.Dispatch(*handler);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user