KingNetwork
v1.0.3 - Pool implementation and simple code refactor.
مثال على وحدة المكعبات
خادم الملك
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 ( ) ;
اختبار الاتصالات
نقوم أيضًا باختبار مكتبة KingNetwork الأولية فقط عن طريق إنشاء خادم واحد و1000 عميل، حيث يرسل كل عميل 100 بايت 14 مرة في الثانية ويعيد الخادم نفس الرسالة مرة أخرى إلى كل عميل. كمبيوتر الاختبار: Acer F 15 مع معالج Intel Core i7 7gen بسرعة 2,9 جيجا هرتز وذاكرة الوصول العشوائي DDR4 بسعة 32 جيجا بايت.
نتائج الاختبار:
العملاء | استخدام وحدة المعالجة المركزية | استخدام الرام | عرض النطاق الترددي العميل + الخادم | نتيجة |
---|---|---|---|---|
64 | 0.5% | 9 ميجا بايت | 0.3 ميجابايت/ثانية | اجتاز |
128 | 1% | 10 ميجابايت | 0.7 ميجابايت/ثانية | اجتاز |
500 | 18% | 18 ميجا بايت | 2 ~ 3 ميجابايت/ثانية | اجتاز |
1000 | 32% | 26 ميجا بايت | 5 ~ 6 ميجابايت/ثانية | اجتاز |