NetAio 0.9.17
dotnet add package NetAio --version 0.9.17
NuGet\Install-Package NetAio -Version 0.9.17
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="NetAio" Version="0.9.17" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetAio" Version="0.9.17" />
<PackageReference Include="NetAio" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NetAio --version 0.9.17
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NetAio, 0.9.17"
#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.
#:package NetAio@0.9.17
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NetAio&version=0.9.17
#tool nuget:?package=NetAio&version=0.9.17
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
NetAio
Netaio is a portable high performance sockets library for .Net. The library is based on Windows IO Completion ports.
Using
Connect to a host
// Inherit from INetEventHandle to handle network events
public class Client : INetEventHandle
{
public void OnConnect(INetAio aio)
{
Console.WriteLine($"Connect to server success!");
}
public void OnDisconnect(INetAio aio)
{
Console.WriteLine($"Disconnect from server!");
}
public void OnRead(INetAio aio, byte[] data, int size)
{
Console.WriteLine($"Received {size} bytes data");
}
public void OnError(Error err)
{
Console.WriteLine($"{err}");
}
}
public class Program
{
bool quit = false;
static void Main(string[] args)
{
// Connect to 127.0.0.1:1111
var (c, err) = NetAio.Connect("127.0.0.1:1111", new Client());
if (err != null)
{
return;
}
// main loop
while (!quit)
{
// do something
}
// Close on quit
c.Close();
}
}
Listen an address
// Inherit from INetEventHandle to handle network events, same as Client
public class Server: INetEventHandle
{
public void OnConnect(INetAio aio)
{
Console.WriteLine($"Client {aio.ID} connected");
}
public void OnDisconnect(INetAio aio)
{
Console.WriteLine($"Client {aio.ID} disconnected");
}
public void OnRead(INetAio aio, byte[] data, int size)
{
Console.WriteLine($"Received {size} bytes data from Client {aio.ID} ");
}
public void OnError(Error err)
{
Console.WriteLine($"{err}");
}
}
public class Program
{
bool quit = false;
static void Main(string[] args)
{
// Listen on port 1111
var (l, err) = NetAio.Listen("0.0.0.0:1111", new Server());
if (err != null)
{
return;
}
// main loop
while (!quit)
{
// do something
}
// Close on quit
l.Close();
}
}
Interface
INetAio
public interface INetAio
{
// unique ID
uint ID { get; }
// Hold your user datas
object UserData { get; set; }
// Socket
Socket Socket { get; }
// Local end point
EndPoint LocalEndPoint { get; }
// Remote end point
EndPoint RemoteEndPoint { get; }
// Write all bytes in data array
void Write(byte[] data);
// Write data with offset and count
void Write(byte[] data, int offset, int count);
// Close netaio
void Close();
// Check if closed
bool Closed { get; }
}
INetEventHandler
// Base interface
public interface INetEventHandler
{
// On connect event
void OnConnect(INetAio aio);
// On disconnect event
void OnDisconnect(INetAio aio);
// On read event
void OnRead(INetAio aio, byte[] data, int size);
// On error occurs
void OnError(Error err);
}
// If you don't want extend INetEventHandler, use this instead to register events your own
public class NetEventHandler : INetEventHandler
{
public Action<INetAio> OnConnectEventHandler;
public Action<INetAio> OnDisconnectEventHandler;
public Action<INetAio, byte[], int> OnReadEventHandler;
public Action<Error> OnErrorEventHandler;
public void OnConnect(INetAio aio)
{
OnConnectEventHandler?.Invoke(aio);
}
public void OnDisconnect(INetAio aio)
{
OnDisconnectEventHandler?.Invoke(aio);
}
public void OnRead(INetAio aio, byte[] data, int size)
{
OnReadEventHandler?.Invoke(aio, data, size);
}
public void OnError(Error err)
{
OnErrorEventHandler?.Invoke(err);
}
}
// If you what handle network event in same thread, use NetEventChanel instead
public class NetEventChannel : INetEventHandler
{
// Construct with your handler
public NetEventChannel(INetEventHandler handler);
// Call update in your main thread
public void Update();
}
NetAio
public static class NetAio
{
// Listen address
public static (INetListener, Error) Listen(string address, INetEventHandler handler);
// Listen address with tcp protcol
private static (INetListener, Error) ListenTcp(string address, INetEventHandler handler);
// Connect to an address
public static (INetConnector, Error) Connect(string address, INetEventHandler handler);
// Connect to an address with tcp protcol
private static (INetConnector, Error) ConnectTcp(string address, INetEventHandler handler);
}
PacketQueue
// This class is use to handle 4 bytes size header lead packet
public class PacketQueue
{
// Remain packet count
public int Count;
// Construct with packet max length
public PacketQueue(int packetMaxLength = 65535);
// Dequeue a packet
public byte[] Dequeue();
// Append data
public Error AppendBytes(byte[] data, int size);
}
| Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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.
-
.NETStandard 2.0
- GoErrors (>= 1.0.3)
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 |
|---|---|---|
| 0.9.17 | 262 | 12/26/2023 |