KingNetwork
v1.0.3 - Pool implementation and simple code refactor.
キューブ Unity の例
キングサーバー
https://www.nuget.org/packages/KingNetwork.Server/
キングクライアント
https://www.nuget.org/packages/KingNetwork.Client/
キングシェアード
https://www.nuget.org/packages/KingNetwork.Shared/
// create and start the async server
var server = new KingServer ( port : 7171 ) ;
server . MessageReceivedHandler = OnMessageReceived ;
//ASync execution
await server . StartAsync ( ) ; //You can pass a out var cancellationToken in parameter
//Sync execution
server . Start ( ) ;
// implements the callback for MessageReceivedHandler
private void OnMessageReceived ( IClient client , IKingBufferReader reader )
{
Console . WriteLine ( $ "Received data from client { client . Id } , data length { reader . Length ( ) } " ) ;
}
// send a message to all clients
using ( var writer = KingBufferWriter . Create ( ) )
{
writer . Write ( "Test message!" ) ;
server . SendMessageToAll ( writer ) ;
}
// stop the server when you don't need it anymore
server . Stop ( ) ;
// create and connect the client
var client = new KingClient ( ) ;
client . MessageReceivedHandler = OnMessageReceived ;
client . Connect ( "127.0.0.1" , 7171 ) ;
// implements the callback for MessageReceivedHandler
private void OnMessageReceived ( IKingBufferReader reader )
{
Console . WriteLine ( $ "Received data from server, data length { reader . Length ( ) } " ) ;
}
/// send a message to server
using ( var writer = KingBufferWriter . Create ( )
{
writer . Write ( "Test message!" ) ;
client . SendMessage ( writer ) ;
}
// disconnect from the server when we are done
client . Disconnect ( ) ;
// create and start the server
var server = new KingServer ( listenerType : NetworkListenerType . UDP , port : 7171 ) ;
server . MessageReceivedHandler = OnMessageReceived ;
//ASync execution
await server . StartAsync ( ) ; //You can pass a out var cancellationToken in parameter
//Sync execution
server . Start ( ) ;
// implements the callback for MessageReceivedHandler
private void OnMessageReceived ( IClient client , IKingBufferReader reader )
{
Console . WriteLine ( $ "Received data from client { client . Id } , data length { reader . Length ( ) } " ) ;
}
// send a message to all clients
using ( var writer = KingBufferWriter . Create ( ) )
{
writer . Write ( "Test message!" ) ;
server . SendMessageToAll ( writer ) ;
}
// stop the server when you don't need it anymore
server . Stop ( ) ;
// create and connect the client
var client = new KingClient ( listenerType : NetworkListenerType . UDP ) ;
client . MessageReceivedHandler = OnMessageReceived ;
client . Connect ( "127.0.0.1" , 7171 ) ;
// implements the callback for MessageReceivedHandler
private void OnMessageReceived ( IKingBufferReader reader )
{
Console . WriteLine ( $ "Received data from server, data length { reader . Length ( ) } " ) ;
}
/// send a message to server
using ( var writer = KingBufferWriter . Create ( )
{
writer . Write ( "Test message!" ) ;
client . SendMessage ( writer ) ;
}
// disconnect from the server when we are done
client . Disconnect ( ) ;
// create and start the server
var server = new KingServer ( listenerType : NetworkListenerType . RUDP , port : 7171 ) ;
server . MessageReceivedHandler = OnMessageReceived ;
//ASync execution
await server . StartAsync ( ) ; //You can pass a out var cancellationToken in parameter
//Sync execution
server . Start ( ) ;
// implements the callback for MessageReceivedHandler
private void OnMessageReceived ( IClient client , IKingBufferReader reader )
{
Console . WriteLine ( $ "Received data from client { client . Id } , data length { reader . Length ( ) } " ) ;
}
// send a message to all clients
using ( var writer = KingBufferWriter . Create ( ) )
{
writer . Write ( "Test message!" ) ;
//You can use RudpMessageType.Reliable to send Reliable messages and RudpMessageType.Unreliable to send Unreliable messages
server . SendMessageToAll ( writer , RudpMessageType . Reliable ) ;
}
// stop the server when you don't need it anymore
server . Stop ( ) ;
// create and connect the client
var client = new KingClient ( listenerType : NetworkListenerType . RUDP ) ;
client . MessageReceivedHandler = OnMessageReceived ;
client . Connect ( "127.0.0.1" , 7171 ) ;
// implements the callback for MessageReceivedHandler
private void OnMessageReceived ( IKingBufferReader reader )
{
Console . WriteLine ( $ "Received data from server, data length { reader . Length ( ) } " ) ;
}
/// send a message to server
using ( var writer = KingBufferWriter . Create ( )
{
writer . Write ( "Test message!" ) ;
//You can use RudpMessageType.Reliable to send Reliable messages and RudpMessageType.Unreliable to send Unreliable messages
client . SendMessage ( writer , RudpMessageType . Reliable ) ;
}
// disconnect from the server when we are done
client . Disconnect ( ) ;
// create and start the server
var server = new KingServer ( listenerType : NetworkListenerType . WSText , port : 7171 ) ; // Or NetworkListenerType.WSBinary
server . MessageReceivedHandler = OnMessageReceived ;
//ASync execution
await server . StartAsync ( ) ; //You can pass a out var cancellationToken in parameter
//Sync execution
server . Start ( ) ;
// implements the callback for MessageReceivedHandler
private void OnMessageReceived ( IClient client , IKingBufferReader reader )
{
Console . WriteLine ( $ "Received data from client { client . Id } , data length { reader . Length ( ) } " ) ;
}
// send a message to all clients
using ( var writer = KingBufferWriter . Create ( ) )
{
writer . Write ( "Test message!" ) ;
server . SendMessageToAll ( writer ) ;
}
// stop the server when you don't need it anymore
server . Stop ( ) ;
// create and connect the client
var client = new KingClient ( listenerType : NetworkListenerType . WSText ) ; // Or NetworkListenerType.WSBinary
client . MessageReceivedHandler = OnMessageReceived ;
client . Connect ( "127.0.0.1" , 7171 ) ;
// implements the callback for MessageReceivedHandler
private void OnMessageReceived ( IKingBufferReader reader )
{
Console . WriteLine ( $ "Received data from server, data length { reader . Length ( ) } " ) ;
}
/// send a message to server
using ( var writer = KingBufferWriter . Create ( )
{
writer . Write ( "Test message!" ) ;
client . SendMessage ( writer ) ;
}
// disconnect from the server when we are done
client . Disconnect ( ) ;
接続テスト
また、1 台のサーバーと 1000 台のクライアントを生成し、各クライアントが 100 バイトを 1 秒あたり 14 回送信し、サーバーが同じメッセージを各クライアントにエコーバックすることで、生の KingNetwork ライブラリのみをテストします。テストコンピューター: 2,9 GHz Intel Core i7 7gen プロセッサーと 32 GB RAM DDR4 を搭載した Acer F 15。
テスト結果:
クライアント | CPU使用率 | ラムの使用量 | 帯域幅クライアント+サーバー | 結果 |
---|---|---|---|---|
64 | 0.5% | 9MB | 0.3MB/秒 | 合格した |
128 | 1% | 10MB | 0.7MB/秒 | 合格した |
500 | 18% | 18MB | 2~3MB/秒 | 合格した |
1000 | 32% | 26MB | 5~6MB/秒 | 合格した |