https://www.nuget.org/packages/ObjectDeliverer/
Untuk menginstal dengan NuGet, cukup instal paket ObjectDeliverer:
Install-Package ObjectDeliverer
ObjectDeliverer adalah perpustakaan transmisi/penerimaan data untuk C#.
Ini adalah perpustakaan saudara dengan nama yang sama untuk UE4.
https://github.com/ayumax/ObjectDeliverer
Ini memiliki beberapa fitur berikut.
Protokol berikut dapat digunakan dengan built-in. Anda juga dapat menambahkan protokol Anda sendiri.
Aturan berikut tersedia untuk aturan terpisah bawaan dari data yang dikirim dan diterima.
Ukuran Tetap
Contoh) Dalam kasus tetap 1024 byte
Header (Ukuran Tubuh) + Badan
Contoh) Bila ukuran areanya 4 byte
Pisahkan berdasarkan simbol terminal
Contoh) Ketika 0x00 adalah akhir
Buat ObjectDelivererManager dan buat berbagai jalur komunikasi dengan meneruskan "Protokol Komunikasi", "Aturan Pemisahan Paket", dan "Metode Serialisasi" ke argumen metode StartAsync.
// Create an ObjectDelivererManager
var deliverer = new ObjectDelivererManager < string > ( ) ;
// Watching for connection events
deliverer . Connected . Subscribe ( async x =>
{
Console . WriteLine ( "connected" ) ;
// Sending data to a connected party
await deliverer . SendAsync ( new byte [ ] { 0x00 , 0x12 } ) ;
await deliverer . SendAsync ( new byte [ ] { 0x00 , 0x12 , 0x23 } ) ;
} ) ;
// Watching for disconnection events
deliverer . Disconnected . Subscribe ( x => Console . WriteLine ( "disconnected" ) ) ;
// Watching for incoming events
deliverer . ReceiveData . Subscribe ( x =>
{
Console . WriteLine ( $ "received buffer length = { x . Buffer . Length } " ) ;
Console . WriteLine ( $ "received message = { x . Message } " ) ;
} ) ;
// Start the ObjectDelivererManager
await deliverer . StartAsync (
new ProtocolTcpIpClient ( ) { IpAddress = "127.0.0.1" , Port = 9013 } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ,
new DeliveryBoxString ( ) ) ;
Anda dapat beralih ke berbagai protokol komunikasi dengan mengubah Protokol yang diteruskan ke metode StartAsync.
// TCP/IP Client
await deliverer . StartAsync (
new ProtocolTcpIpClient ( ) { IpAddress = "127.0.0.1" , Port = 9013 } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ) ;
// TCP/IP Server
await deliverer . StartAsync (
new ProtocolTcpIpServer ( ) { ListenPort = 9013 } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ) ;
// UDP Sender
await deliverer . StartAsync (
new ProtocolUdpSocketSender ( ) { DestinationIpAddress = "127.0.0.1" , DestinationPort = 9013 } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ) ;
// UDP Receiver
await deliverer . StartAsync (
new ProtocolUdpSocketReceiver ( ) { BoundPort = 9013 } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ) ;
// SharedMemory
await deliverer . StartAsync (
new ProtocolSharedMemory ( ) { SharedMemoryName = "SharedMemory" , SharedMemorySize = 1024 } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ) ;
// Log File Writer
await deliverer . StartAsync (
new ProtocolLogWriter ( ) { FilePath = @"C:logcomlog.txt" } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ) ;
// Log File Reader
await deliverer . StartAsync (
new ProtocolLogReader ( ) { FilePath = @"C:logcomlog.txt" } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ) ;
Anda dapat dengan mudah mengubah aturan pemisahan paket.
// FixedSize
await deliverer . StartAsync (
new ProtocolTcpIpClient ( ) { IpAddress = "127.0.0.1" , Port = 9013 } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ) ;
// Header(BodySize) + Body
await deliverer . StartAsync (
new ProtocolTcpIpClient ( ) { IpAddress = "127.0.0.1" , Port = 9013 } ,
new PacketRuleSizeBody ( ) { SizeLength = 4 , SizeBufferEndian = ECNBufferEndian . Big } ) ;
// Split by terminal symbol
await deliverer . StartAsync (
new ProtocolTcpIpClient ( ) { IpAddress = "127.0.0.1" , Port = 9013 } ,
new PacketRuleTerminate ( ) { Terminate = new byte [ ] { 0xFE , 0xFF } } ) ;
// Nodivision
await deliverer . StartAsync (
new ProtocolTcpIpClient ( ) { IpAddress = "127.0.0.1" , Port = 9013 } ,
new PacketRuleNodivision ( ) ) ;
Menggunakan DeliveryBox memungkinkan pengiriman dan penerimaan data non-biner (string karakter dan objek).
// UTF-8 string
var deliverer = new ObjectDelivererManager < string > ( ) ;
await deliverer . StartAsync (
new ProtocolTcpIpClient ( ) { IpAddress = "127.0.0.1" , Port = 9013 } ,
new PacketRuleFixedLength ( ) { FixedSize = 10 } ,
new DeliveryBoxString ( ) ) ;
deliverer . ReceiveData . Subscribe ( x => Console . WriteLine ( x . Message ) ) ;
await deliverer . SendMessageAsync ( "ABCDEFG" ) ;
// Object
public class SampleObj
{
public int Prop { get ; set ; }
public string StringProp { get ; set ; }
public string Hoge ( ) => $ " { Prop } _ { StringProp } " ;
}
var deliverer = new ObjectDelivererManager < SampleObj > ( ) ;
await deliverer . StartAsync (
new ProtocolTcpIpClient ( ) { IpAddress = "127.0.0.1" , Port = 9013 } ,
new PacketRuleSizeBody ( ) { SizeLength = 4 , SizeBufferEndian = ECNBufferEndian . Big } ,
new DeliveryBoxObjectJson < SampleObj > ( ) ) ;
deliverer . ReceiveData . Subscribe ( x => Console . WriteLine ( x . Message . Hoge ( ) ) ) ;
var sampleObj = new SampleObj ( ) { Prop = 1 , StringProp = "abc" } ;
await deliverer . SendMessageAsync ( sampleObj ) ;
Anda dapat membuat DeliveryBox dengan metode serialisasi Anda sendiri.
Contoh) Saat menerapkan serializer menggunakan MessagePack(https://github.com/neuecc/MessagePack-CSharp)
[ MessagePackObject ]
public class SampleObj2
{
[ Key ( 0 ) ]
public int Prop { get ; set ; }
[ Key ( 1 ) ]
public string StringProp { get ; set ; }
public string Hoge ( ) => $ " { Prop } _ { StringProp } " ;
}
// Definition of DeliveryBox using MessagePack
public class ObjectDeliveryBoxUsingMessagePack < T > : IDeliveryBox < T >
{
public override ReadOnlyMemory < byte > MakeSendBuffer ( T message ) => MessagePackSerializer . Serialize ( message ) ;
public override T BufferToMessage ( ReadOnlyMemory < byte > buffer ) => MessagePackSerializer . Deserialize < T > ( buffer ) ;
}
var deliverer = new ObjectDelivererManager < SampleObj2 > ( ) ;
await deliverer . StartAsync (
new ProtocolTcpIpClient ( ) { IpAddress = "127.0.0.1" , Port = 9013 } ,
new PacketRuleSizeBody ( ) { SizeLength = 4 , SizeBufferEndian = ECNBufferEndian . Big } ,
new ObjectDeliveryBoxUsingMessagePack < SampleObj2 > ( ) ) ;
deliverer . ReceiveData . Subscribe ( x => Console . WriteLine ( x . Message . Hoge ( ) ) ) ;
var sampleObj = new SampleObj2 ( ) { Prop = 1 , StringProp = "abc" } ;
await deliverer . SendMessageAsync ( sampleObj ) ;