Communication Layer - liuli-neko/NekoProtoTools GitHub Wiki
This page provides a detailed reference for the core components within the NekoProtoTools Communication module.
Tutorial: Implementing Communication
The Communication module provides the necessary tools for sending/receiving protocols over networks.
To use the features of this module, include the following primary header(s):
#include <nekoproto/communication/communication_base.hpp>
// Add other commonly required headers for this module
// include like how to support serializer
#include <nekoproto/proto/json_serializer.hpp>
#include <nekoproto/proto/types/types.hpp> // include all type support
The core components of this module reside primarily in the NekoProto
namespace.
- Purpose: All messages sent over the network are prefixed with a header. This class defines the header structure.
-
Key Members:
-
uint32_t length
: The size of the message body. -
int32_t data
: the proto type of this message in Complete message or the slice index in Slice message -
uint16_t messageType
: The type of the message (e.g., Complete, Slice, ..., defined inMessageType
enum).
-
- Format:
length | data | message type |
---|---|---|
length (4 bytes) | data (4 bytes) | message type (2 bytes) |
The length field is the length of the message body and does not include the length of the message header, and in deferent message type, the field will have different meanings.
- Purpose: Wraps a network stream for protocol exchange, providing methods to send and receive IProto messages.
-
Template Parameters:
-
T
: The underlying stream type, which be compatible with the Ilias library (e.g.,TcpClient
).
-
-
Key Constructors / Creation:
-
ProtoStreamClient(ProtoFactory& factory, T&& streamClient)
: Constructs a ProtoStreamClient with a given factory and stream client. -
ProtoStreamClient(const ProtoStreamClient&) = delete
: Copy constructor is deleted to prevent copying. -
ProtoStreamClient(ProtoStreamClient&& /*othre*/)
: Move constructor. -
~ProtoStreamClient() noexcept
: Destructor to clean up resources.
-
-
Key Public Methods:
-
auto setStreamClient(ClientType&& streamClient, bool reconnect = false) -> void
: Sets the underlying stream client and optionally reconnects. (reconnect
is a flag to clear the old stream data, if set to false). -
auto send(const IProto& message, StreamFlag flag = StreamFlag::None) -> IoTask<void>
: Sends a protocol over the network asynchronously. (flag
is a flag to set the stream type, default isStreamFlag::None
.SerializerInThread
means that the serializer will be executed in a new thread.SliceData
means that the message will be sent in slices.VersionVerification
means send a version verification message before sending the actual message.RecvUnknownTypeData
means that the client will receive the unknown type message asRawDataMessage
from the server.). -
auto recv(StreamFlag flag = StreamFlag::None) -> IoTask<IProto>
: Receives a protocol over the network asynchronously. -
auto close() -> IoTask<void>
: Closes the connection to the server. -
auto cancel() -> void
: Cancels the current operation. -
auto setProtoTable(uint32_t version, const std::map<uint32_t, std::string>& protoTable) -> void
: Sets the protocol table for the client. -
auto getProtoTable() const -> const ProtocolTable&
: Gets the protocol table for the client.
-
template <typename T = ILIAS_NAMESPACE::UdpClient>
class ProtoDatagramClient {
public:
ProtoDatagramClient() = default;
ProtoDatagramClient(ProtoFactory& factory, ClientType&& streamClient);
ProtoDatagramClient(const ProtoDatagramClient&) = delete;
ProtoDatagramClient(ProtoDatagramClient&& /*other*/);
~ProtoDatagramClient() noexcept;
auto setStreamClient(ClientType&& streamClient, bool reset = false) -> void;
auto send(const IProto& message, const IPEndpoint& endpoint, StreamFlag flag = StreamFlag::None) -> IoTask<void>;
auto recv(StreamFlag flag = StreamFlag::None) -> IoTask<std::pair<IProto, IPEndpoint>>;
auto close() -> IoTask<void>;
auto cancel() -> void;
auto setProtoTable(uint32_t version, const std::map<uint32_t, std::string>& protoTable) -> void;
auto getProtoTable() const -> const ProtocolTable&;
}
- Purpose: Wraps a datagram stream for protocol exchange, providing methods to send and receive IProto messages.
-
Template Parameters:
-
T
: The underlying stream type, which be compatible with the Ilias library (e.g.,UdpClient
).
-
-
Key Constructors / Creation:
-
ProtoDatagramClient(ProtoFactory& factory, ClientType&& streamClient)
: Constructs a ProtoDatagramClient with a given factory and stream client. -
ProtoDatagramClient(const ProtoDatagramClient&) = delete
: Copy constructor is deleted to prevent copying. -
ProtoDatagramClient(ProtoDatagramClient&& /*othre*/)
: Move constructor. -
~ProtoDatagramClient() noexcept
: Destructor to clean up resources.
-
-
Key Public Methods:
-
auto setStreamClient(ClientType&& streamClient, bool reset = false) -> void
: Sets the underlying stream client and optionally resets the client. (reset
is a flag to clear the old stream data, if set to false). -
auto send(const IProto& message, const IPEndpoint& endpoint, StreamFlag flag = StreamFlag::None) -> IoTask<void>
: Sends a protocol over the network asynchronously. (flag
is a flag to set the stream type, default isStreamFlag::None
.SerializerInThread
means that the serializer will be executed in a new thread.SliceData
means that the message will be sent in slices.VersionVerification
means send a version verification message before sending the actual message.RecvUnknownTypeData
means that the client will receive the unknown type message asRawDataMessage
from the server.). -
auto recv(StreamFlag flag = StreamFlag::None) -> IoTask<std::pair<IProto, IPEndpoint>>
: Receives a protocol over the network asynchronously. -
auto close() -> IoTask<void>
: Closes the connection to the server. -
auto cancel() -> void
: Cancels the current operation. -
auto setProtoTable(uint32_t version, const std::map<uint32_t, std::string>& protoTable) -> void
: Sets the local protocol table. -
auto getProtoTable() const -> const ProtocolTable&
: Gets the local protocol table.
-