Netcode.IO 规范的纯托管 C# 实现
该项目的目标是提供针对 .NET 3.5 编码的 Netcode.IO 规范的纯托管实现,并使用零本机 DLL 或包装器来实现最大可移植性。该实现没有像原始 C 参考实现那样使用 libsodium,而是使用 Bouncy Castle 加密库的定制版本。您可以在这里找到原始源代码:https://github.com/bcgit/bc-csharp
此外,它是专为在游戏中使用而设计的。为此,它从一开始就被设计为对 GC 分配的影响尽可能小。在大多数情况下,您根本不会看到使用 Netcode.IO.NET 产生的任何 GC 影响。
大部分 API 驻留在命名空间NetcodeIO.NET
中
创建并启动新服务器:
Server server = new Server (
maxClients , // int maximum number of clients which can connect to this server at one time
publicAddress , port , // string public address and int port clients will connect to
protocolID , // ulong protocol ID shared between clients and server
privateKeyBytes // byte[32] private crypto key shared between backend servers
) ;
server . Start ( ) ; // start the server running
监听各种事件:
// Called when a client has connected
server . OnClientConnected += clientConnectedHandler ; // void( RemoteClient client )
// Called when a client disconnects
server . OnClientDisconnected += clientDisconnectedHandler ; // void( RemoteClient client )
// Called when a payload has been received from a client
// Note that you should not keep a reference to the payload, as it will be returned to a pool after this call completes.
server . OnClientMessageRecieved += messageReceivedHandler ; // void( RemoteClient client, byte[] payload, int payloadSize )
// Called when the server logs a message
// If you are not using a custom logger, a handler using Console.Write() is sufficient.
server . OnLogMessage += logMessageHandler ; // void( string message, NetcodeLogLevel logLevel )
要将有效负载发送到连接到服务器的远程客户端:
remoteClient . Send ( byte [ ] payload , int payloadSize ) ;
// or:
server . SendPayload ( RemoteClient client , byte [ ] payload , int payloadSize ) ;
断开客户端连接:
server . Disconnect ( RemoteClient client ) ;
要获取可以通过连接令牌传递的任意 256 字节用户数据:
remoteClient . UserData ; // byte[256]
要停止服务器并断开所有客户端的连接:
server . Stop ( ) ;
创建新客户端:
Client client = new Client ( ) ;
监听各种事件:
// Called when the client's state has changed
// Use this to detect when a client has connected to a server, or has been disconnected from a server, or connection times out, etc.
client . OnStateChanged += clientStateChanged ; // void( ClientState state )
// Called when a payload has been received from the server
// Note that you should not keep a reference to the payload, as it will be returned to a pool after this call completes.
client . OnMessageReceived += messageReceivedHandler ; // void( byte[] payload, int payloadSize )
要使用连接令牌连接到服务器:
client . Connect ( connectToken ) ; // byte[2048] public connect token as returned by a TokenFactory
连接时向服务器发送消息:
client . Send ( byte [ ] payload , int payloadSize ) ;
断开客户端连接:
client . Disconnect ( ) ;
TokenFactory 可用于生成客户端用于连接游戏服务器的公共连接令牌。创建一个新的 TokenFactory:
TokenFactory tokenFactory = new TokenFactory (
protocolID , // must be the same protocol ID as passed to both client and server constructors
privateKey // byte[32], must be the same as the private key passed to the Server constructor
) ;
要生成新的 2048 字节公共连接令牌:
tokenFactory . GenerateConnectToken (
addressList , // IPEndPoint[] list of addresses the client can connect to. Must have at least one and no more than 32.
expirySeconds , // in how many seconds will the token expire
serverTimeout , // how long it takes until a connection attempt times out and the client tries the next server.
sequenceNumber , // ulong token sequence number used to uniquely identify a connect token.
clientID , // ulong ID used to uniquely identify this client
userData // byte[], up to 256 bytes of arbitrary user data (available to the server as RemoteClient.UserData)
) ;
Netcode.IO.NET 是 Netcode.IO 协议的纯端口 - 仅此而已。从本质上讲,Netcode.IO 是基于 UDP 之上的加密和基于连接的抽象。而且,就像 UDP 一样,它的可靠性保证为零。您的消息可能不会成功,也可能不会按顺序发送。这只是互联网上的一个事实。也就是说,任何游戏几乎肯定都需要某种可靠性层。为此,我的 ReliableNetcode.NET 项目提供了一个不可知且易于使用的可靠性层,您可以使用该层将此功能添加到您的游戏中。