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 ) ;