General Communication
ISender, IReceiver, Listener, Channel, and IPacketizer general communication abstractions.
Conventional communication abstraction breaks down "how to send, how to receive, how to monitor, how to close the channel, how to package and unpack" into several groups of small interfaces. The core framework only defines protocol boundaries, and the implementation of specific network connections, message queue connections, event channels, etc. is undertaken by Zongsoft.Net, Zongsoft.Messaging.* or other extension projects.
Type Relationship
ISender
Send bytes of data.
ISender<T>
Send strongly typed packets.
IReceiver
Receive ReadOnlySequence<byte> Source byte sequence.
IListener<T>
Listens for and handles strongly typed communication packets.
ListenerBase<T>
The listener base class implements the process of receiving, unpacking and handing over to the handler.
IChannel
A communication channel that can be closed and released asynchronously.
ChannelBase
The channel base class unifies the closing status, closing events and release process.
IChannelSelector<T>
Select a channel by key.
IPacketizer<TPackage>
Strongly typed Packaging and unpacking interface for communication packets.
IPacketizer<TRequest, TResponse>
Request packaging and response unpacking interface in request/response mode.
Channel Design Ideas
Channel is the lifecycle abstraction of "communication endpoint", not a synonym for a certain protocol. It expresses: whether this endpoint is closed, how to close it, how to notify the outside after closing, and whether it can be held by a selector or manager.
This design allows different communication forms to share the same lifecycle vocabulary:
TCP connection channel:
TcpChannelBase<T>inheritsChannelBaseand implementsISenderandISender<T>at the same time.Event channel:
IEventChannelinheritsIChannelandISender<EventContext>and can send events to the remote end.Message queue channel:
ZeroQueueEventChannelinheritsChannelBase, encodes events and delivers them to the ZeroMQ queue.Message consumer:
MessageConsumerBase<TQueue>inheritsChannelBaseand incorporates the subscription lifecycle into the channel closing model.
Receiving and Unpacking
ListenerBase<T> implements the template process from bytes to strongly typed packets: IPacketizer<T>.Unpack is called after receiving the byte sequence, and handler is called after the unpacking is successful.
Source: framework/Zongsoft.Core/src/Communication/ListenerBase.cs (excerpt; see source for context).
IPacketizer<T> is suitable for handling sticky packets, half packets, length headers, compression, encryption and custom binary protocols. Its Unpack receives ref ReadOnlySequence<byte>, and the implementer can advance the sequence after unpacking a packet, letting the upper layer continue processing subsequent packets in the buffer.
Source: framework/Zongsoft.Core/src/Communication/IPacketizer.cs (excerpt; see source for context).
Implementation in Zongsoft.Net
Zongsoft.Net applies these abstractions to TCP network communication:
TcpServer<T>inheritsListenerBase<T>, holdsIPacketizer<T>, and is responsible for monitoring and creating TCP channels.TcpChannelBase<T>inheritsChannelBaseand is responsible for sending, receiving, packaging, unpacking and closing of a single connection.TcpServerChannelManager<T>maintains the server channel collection and creates channels when the client connects.Packetizerprovides two basic unpacking implementations: headless packet and length header packet.
Source: framework/Zongsoft.Net/samples/server/Program.cs (excerpt; see source for context).
The above TCP server example comes from the framework; Handler is an internal type of the same Program.cs and broadcasts an ACK reply after receiving the text. Discussions does not host TCP channels directly. See Network Communication for the supporting client, startup parameters and shutdown behavior.
Realistic Scene
The channel model is suitable for any scenario that has an open/close lifecycle and can send or receive data. Network connections are just one of them; event buses, message queue subscriptions, device connections, and gateway sessions can also become channels.
Related Resources
Last updated