MailboxShell 1.1.0-beta

This is a prerelease version of MailboxShell.
dotnet add package MailboxShell --version 1.1.0-beta
NuGet\Install-Package MailboxShell -Version 1.1.0-beta
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MailboxShell" Version="1.1.0-beta" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MailboxShell --version 1.1.0-beta
#r "nuget: MailboxShell, 1.1.0-beta"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MailboxShell as a Cake Addin
#addin nuget:?package=MailboxShell&version=1.1.0-beta&prerelease

// Install MailboxShell as a Cake Tool
#tool nuget:?package=MailboxShell&version=1.1.0-beta&prerelease


Library, that provides Mailbox class. The Mailbox class is a shell for a Socket object for a more simple network packets sending and receiving. So it segments TCP traffic for packets and returns data in this view.

How to use

  1. Get the Socket object
  2. Create Mailbox by Mailbox mailbox = new Mailbox(socket)

Network packets sending and receiving completes by mailbox.tick(); function in the NonBlocking mode. I.E. the best solution is to call this method with some interval (you can create a special thread to call this). mailbox.tick(); returns false when an error occurs or remote host is diconnected

Send a packet

  1. Create Packet object by Packet packet = new Packet([yourdata : byte[]]); (for example Packet packet = new Packet(Encoding.UTF8.GetBytes("Hello!")))
  2. To send packet call mailbox.Send(packet)
  3. Call mailbox.Tick()

Receive a packet

  1. Call mailbox.Tick() in main or other thread with some time intervals
  2. To receive packet on connected host use mailbox.Next() to get a packet if any packet is received (null else) or mailbox.GetAllReceived() to get all received packets at the call time
    !!! Don't forget to call mailbox.Tick() !!!
  3. Received data bytes array is in field

Constructor args

Mailbox class has constructor: public Mailbox(Socket socket, int maxReceiveFragmentsPerTick = 64, int sendPacketsPerTick = 0, int maxPacketSize = 0)

  • Socket socket - socket that this object will be the facade of
  • int maxReceiveFragmentsPerTick - limit of received fragments per tick
  • int sendPacketsPerTick - limit of packets, that can be sended per tick
  • int maxPacketSize - limit of received packet size

Async API

!!! Do not use asynchronous packets listening with ticks !!!

  • Task StartListenAsync(int receivePacketsPerSecond = 0, CancellationToken cancellationToken = default) - start to receive and send packets asynchronously
  • void StopListen(bool interruptSendingWhenAll = false) - stop receive and send packets asynchronously. If interruptSendingWhenAll is True, the packets sender will send all the packets in the send queue before stopping
  • Task StopListenAsync(bool interruptSendingWhenAll = false) - the same as the StopListen method, but returns a Task, that will be finished when the send and the receive tasks will be finished
  • void StopListenWait(bool interruptSendingWhenAll = false) - the same as the StopListen method, but blocks the call thread until the send and the receive tasks will be finished
  • Task<Packet> GetNextAsync(CancellationToken cancellationToken = default) - get the next packet or wait
  • Task<IEnumerable<Packet>> GetAllReceivedAsync(CancellationToken cancellationToken = default) get all received packets if exist or waits for new packet otherwice

Other properties and methods

  • bool IsConnected - checks if socket is connected (Connected socket property)
  • int SendQueueSize - get the number of currently processed packets
  • int ReceivedCount - get the number of received packets in the queue
  • int SendQueueSize - get send queue size
  • void Close() - close socket
  • bool IsSendQueueEmpty - checks if send queue is empty
  • void ClearReceived() - clears received packet queue
  • void ClearReceivedQueue() - clears send packet queue
  • void ClearAll() - clears both packet queues
  • Packet Packet.CloneDataRef() - returns new ready for sending Packet with some data ref

Mailbox's owner

  • You can set owner object of mailbox by void SetOwner(IMailboxOwner) method and get it by TYPE GetOwner<TYPE>() method.
  • Owner object must implement IMailboxOwner interface and have MailboxSafe object

For example:

public class ClientInfo : IMailboxOwner { 

	public MailboxSafe MailboxSafe { get; } = new MailboxSafe();
  • Also call mailbox.RemoveOwner() to remove owner object link.
  • Owner link methods are thread-safe, but don't use it too often. The purpose of these methods is to bind mailbox owner object in the start of handling
  • Also you can use GetMailbox, SetMailbox, RemoveMailbox IMailboxOwner's extension methods. They call similar methods in owned mailbox


(You can find the Test project in this repository MailboxShell solution) Lightweight example:


Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(IPAddress.Parse(SERVER_IP), SERVER_PORT);
Mailbox mailbox = new Mailbox(socket);

while(true) {
	string message = Console.ReadLine();
	mailbox.Send(new Packet(Encoding.UTF8.GetBytes(message)));
	while(true) {
		Packet receivedPacket = mailbox.Next(); //Try to get the next packet
		if(receivedPacket != null) {
			Console.WriteLine($"Server response: \"{Encoding.UTF8.GetString(}\"");


Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
Mailbox mailbox = new Mailbox(serverSocket.Accept());
while(true) {
	mailbox.Tick(); //Tick to receive
	foreach(Packet packet in mailbox.GetAllReceived()) {
		string message = Encoding.UTF8.GetString(; //Get message from
		mailbox.Send(new Packet(Encoding.UTF8.GetBytes("Echo: " + message))); //Send response
	mailbox.Tick(); //Tick to send (but both ticks can be in one in this case)

Asynchronous client

private async Task handlePackets(Mailbox mailbox, CancellationToken cancellationToken) { 
    while(!cancellationToken.IsCancellationRequested) {
        Packet packet = await mailbox.GetNextAsync(cancellationToken);
        string responseString = Encoding.UTF8.GetString(;
        Console.WriteLine($"Server response: \"{responseString}\"");

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(IPAddress.Parse(SERVER_IP), SERVER_PORT);
Mailbox mailbox = new Mailbox(socket);

CancellationTokenSource cts = new CancellationTokenSource();
Task listenTask = mailbox.StartListenAsync(cts.Token);
Task handlePacketsTask = mailbox.handlePackets(cts.Token);


Task.WaitAll(listenTask, handlePacketsTask);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0-beta 228 8/28/2021
1.0.0 340 5/16/2021