Skip to main content
Version: v3

NetOption

Important Attention Reminder

Coding Style wiki​


NetOption is the abstract base class of NetFrame connection options and defines the auto-reconnection setting shared by all protocols. Three built-in derived classes carry the connection parameters of the three built-in providers; when implementing a custom provider, you can also inherit this class to add your own options.

NamespaceOxGFrame.NetFrame
Typepublic abstract class
SourceNetOption.cs
using OxGFrame.NetFrame;

Declaration

public abstract class NetOption

Derived Class Overview​

ClassProviderDescription
TcpNetOptionTcpNetProviderTCP connection options (host, port, and packet processing settings).
KcpNetOptionKcpNetProviderKCP connection options (host, port, channel, and KCP config).
WebSocketNetOptionWebSocketNetProviderWebSocket connection options (connection URL).

Properties​

PropertyTypeDefaultDescription
autoReconnectCountint-1Number of automatic reconnection attempts. When greater than 0, the node automatically retries after an unintentional disconnection, decrementing the count per attempt; -1 disables auto-reconnection. Use int.MaxValue for practically unlimited retries.

Constructor​

public NetOption(int autoReconnectCount = -1)

Parameters

ParameterTypeDescription
autoReconnectCountintNumber of automatic reconnection attempts.
Default: -1 (auto-reconnection disabled)

Description

Called by derived-class constructors via base(autoReconnectCount) to initialize the reconnection count.

Reminder The reconnection interval and callback can be configured via NetNode.SetReconnectTickerTime (default: 5 seconds) and NetNode.SetReconnectAction.


TcpNetOption​

Connection options for TcpNetProvider (Telepathy).

Typepublic class
SourceTcpNetOption.cs

Declaration

public class TcpNetOption : NetOption

Properties​

PropertyTypeDefaultDescription
hoststring—Server host address.
portint—Server port.
processLimitPerTickint64Maximum number of packets processed per polling tick.
maxBufferSizeint65536Maximum buffer size of a single packet (64KB).

Constructors​

public TcpNetOption(string host, int port, int autoReconnectCount = -1)
public TcpNetOption(string host, int port, int processLimitPerTick, int maxBufferSize = 65536, int autoReconnectCount = -1)

Parameters

ParameterTypeDescription
hoststringServer host address.
portintServer port.
processLimitPerTickintMaximum number of packets processed per polling tick.
maxBufferSizeintMaximum buffer size of a single packet.
Default: 65536
autoReconnectCountintNumber of automatic reconnection attempts.
Default: -1 (auto-reconnection disabled)

Example

// Basic connection (auto-reconnect 3 times after disconnection)
var tcpOption = new TcpNetOption("127.0.0.1", 8888, autoReconnectCount: 3);
NetFrames.Connect(tcpOption);

// Custom per-tick processing limit and buffer size
var tunedOption = new TcpNetOption("127.0.0.1", 8888, processLimitPerTick: 128, maxBufferSize: 131072);

KcpNetOption​

Connection options for KcpNetProvider (kcp2k).

Typepublic class
SourceKcpNetOption.cs

Declaration

public class KcpNetOption : NetOption

Properties​

PropertyTypeDefaultDescription
hoststring—Server host address.
portint—Server port.
kcpChannelKcpChannelKcpChannel.ReliableKCP transmission channel (Reliable / Unreliable).
configKcpConfigBuilt-in default config (see below)kcp2k connection configuration (NoDelay, window sizes, timeout, etc.).

The built-in default value of config:

new KcpConfig
(
NoDelay: true,
DualMode: false,
Interval: 1,
Timeout: 2000,
SendWindowSize: Kcp.WND_SND * 1000,
ReceiveWindowSize: Kcp.WND_RCV * 1000,
CongestionWindow: false,
MaxRetransmits: Kcp.DEADLINK * 2
)

Constructors​

public KcpNetOption(string host, int port, KcpChannel kcpChannel = KcpChannel.Reliable, int autoReconnectCount = -1)
public KcpNetOption(string host, int port, KcpConfig config, KcpChannel kcpChannel = KcpChannel.Reliable, int autoReconnectCount = -1)

Parameters

ParameterTypeDescription
hoststringServer host address.
portintServer port. Cast to ushort (0–65535) when connecting.
configKcpConfigCustom kcp2k connection configuration. If omitted, the built-in default config is used.
kcpChannelKcpChannelKCP transmission channel.
Default: KcpChannel.Reliable
autoReconnectCountintNumber of automatic reconnection attempts.
Default: -1 (auto-reconnection disabled)

Example

using kcp2k;

// Use the built-in default config
var kcpOption = new KcpNetOption("127.0.0.1", 7777);
NetFrames.Connect(kcpOption);

// Custom KcpConfig
var config = new KcpConfig(NoDelay: true, Interval: 10, Timeout: 5000);
var customOption = new KcpNetOption("127.0.0.1", 7777, config, KcpChannel.Reliable, autoReconnectCount: 3);

WebSocketNetOption​

Connection options for WebSocketNetProvider (UnityWebSocket).

Typepublic class
SourceWebSocketNetOption.cs

Declaration

public class WebSocketNetOption : NetOption

Properties​

PropertyTypeDefaultDescription
urlstring—WebSocket connection URL (ws:// or wss://).

Constructor​

public WebSocketNetOption(string url, int autoReconnectCount = -1)

Parameters

ParameterTypeDescription
urlstringWebSocket connection URL (ws:// or wss://).
autoReconnectCountintNumber of automatic reconnection attempts.
Default: -1 (auto-reconnection disabled)

Example

var wsOption = new WebSocketNetOption("ws://127.0.0.1:8080/ws", autoReconnectCount: 3);
NetFrames.Connect(wsOption);

Custom Extension​

When implementing a custom provider, inherit NetOption to add protocol-specific connection parameters:

public class CustomNetOption : NetOption
{
public string host { get; set; }

public int port { get; set; }

public CustomNetOption(string host, int port, int autoReconnectCount = -1) : base(autoReconnectCount)
{
this.host = host;
this.port = port;
}
}

Inside your provider's CreateConnect, cast the NetOption back to CustomNetOption to read the parameters (see the INetProvider implementation example).