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 專案提供了一個不可知且易於使用的可靠性層,您可以使用該層將此功能添加到您的遊戲中。