https://www.nuget.org/packages/ObjectDeliverer/
NuGet과 함께 설치하려면 ObjectDeliverer 패키지를 설치하기만 하면 됩니다.
Install-Package ObjectDeliverer
ObjectDeliverer는 C#용 데이터 송수신 라이브러리입니다.
UE4와 같은 이름의 자매 라이브러리입니다.
https://github.com/ayumax/ObjectDeliverer
다음과 같은 특징이 있습니다.
다음 프로토콜을 내장하여 사용할 수 있습니다. 자신만의 프로토콜을 추가할 수도 있습니다.
전송 및 수신된 데이터의 내장 분할 규칙에 다음 규칙을 사용할 수 있습니다.
고정크기
예) 고정 1024바이트의 경우
헤더(BodySize) + 본문
예) 크기 영역이 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 ) ;