ObjectDeliverer CSharp
v0.1.0
https://www.nuget.org/packages/ObjectDeliverer/
要使用 NuGet 安装,只需安装 ObjectDeliverer 包:
Install-Package ObjectDeliverer
ObjectDeliverer 是 C# 的数据传输/接收库。
它是 UE4 的同名姐妹库。
https://github.com/ayumax/ObjectDeliverer
它具有以下特点。
以下协议可与内置协议一起使用。您还可以添加自己的协议。
以下规则可用于内置的发送和接收数据的分割规则。
固定尺寸
例)固定1024字节的情况
标头(正文尺寸)+正文
示例)当大小区域为 4 字节时
按端子符号分割
示例)当 0x00 为结束时
创建ObjectDelivererManager,并通过将“通信协议”、“数据包分割规则”和“序列化方法”传递给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 ( ) ) ;
您可以通过更改传递给 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 } ) ;
您可以轻松更改数据包拆分规则。
// 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 ( ) ) ;
使用 DeliveryBox 可以发送和接收非二进制数据(字符串和对象)。
// 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 ) ;
您可以使用自己的序列化方法创建 DeliveryBox。
例如)使用 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 ) ;