objectdeliverer_dart 0.0.5
objectdeliverer_dart: ^0.0.5 copied to clipboard
ObjectDeliverer is a data transmission / reception library for Dart.
objectdeliverer_dart #
pub.dev #
https://pub.dev/packages/objectdeliverer_dart
To install with pub.dev, just install the objectdeliverer_dart package:
https://pub.dev/packages/objectdeliverer_dart/install
Description #
ObjectDeliverer is a data transmission / reception library for dart.
It is a sister library of the same name for UE4.
https://github.com/ayumax/ObjectDeliverer
It has the following features.
- Communication protocol, data division rule, serialization method can be switched by part replacement.
- It is also possible to apply your own object serialization method
Communication protocol #
The following protocols can be used with built-in. You can also add your own protocol.
- TCP/IP Server(Connectable to multiple clients)
- TCP/IP Client
- UDP(Sender)
- UDP(Receiver)
Data division rule #
The following rules are available for built-in split rules of transmitted and received data.
-
FixedSize
Example) In the case of fixed 1024 bytes -
Header(BodySize) + Body
Example) When the size area is 4 bytes
-
Split by terminal symbol
Example) When 0x00 is the end
Serialization method #
- Byte Array
- UTF-8 string
- Object(Json)
Quick Start #
Create ObjectDelivererManager and create various communication paths by passing "Communication Protocol", "Packet Split Rule" and "Serialization Method" to the arguments of Start method.
// Create an ObjectDelivererManager
final deliverer = ObjectDelivererManager<String>();
// Watching for connection events
deliverer.connected.listen((x) async {
print('connected');
// Sending data to a connected party
await deliverer.send(Uint8List.fromList([0x00, 0x12]));
await deliverer.send(Uint8List.fromList([0x00, 0x12, 0x23]));
});
// Watching for disconnection events
deliverer.disconnected.listen((x) => print('disconnected'));
// Watching for incoming events
deliverer.receiveData.listen((x) {
print('received buffer length = ${x.buffer.length}');
print('received message = ${x.message}');
});
// Start the ObjectDelivererManager
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleFixedLength.fromParam(10), Utf8StringDeliveryBox());
await Future.delayed(const Duration(milliseconds: 100));
// Close ObjectDelivererManager
await deliverer.close();
Change communication protocol #
You can switch to various communication protocols by changing the Protocol passed to the start method.
// TCP/IP Client
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleFixedLength.fromParam(10));
// TCP/IP Server
await deliverer.start(
ProtocolTcpIpServer.fromParam(9013), PacketRuleFixedLength.fromParam(10));
// UDP Sender
await deliverer.start(
ProtocolUdpSocketSender.fromParam('127.0.0.1', 9013),
PacketRuleFixedLength.fromParam(10));
// UDP Receiver
await deliverer.start(ProtocolUdpSocketReceiver.fromParam(9013),
PacketRuleFixedLength.fromParam(10));
Change of data division rule #
You can easily change the packet splitting rule.
// FixedSize
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleFixedLength.fromParam(10));
// Header(BodySize) + Body
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleSizeBody.fromParam(4, sizeBufferEndian: Endian.big));
// Split by terminal symbol
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleTerminate.fromParam(Uint8List.fromList([0xFE, 0xFF])));
// Nodivision
await deliverer.start(
ProtocolTcpIpClient.fromParam('127.0.0.1', 9013), PacketRuleNodivision());
Change of Serialization method #
Using DeliveryBox enables sending and receiving of non-binary data (character strings and objects).
// UTF-8 string
final deliverer = ObjectDelivererManager<String>();
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleFixedLength.fromParam(10), Utf8StringDeliveryBox());
deliverer.receiveData.listen((x) => print(x.message));
await deliverer.sendMessage('ABCDEFG');
// Object
class SampleObj extends IJsonSerializable {
int prop;
String stringProp;
String hoge() => '$prop$stringProp';
@override
Map<String, dynamic> toJson() => {'prop': prop, 'stringProp': stringProp};
}
// Register the deserialization function of object in advance
IJsonSerializable.addMakeInstanceFunction(SampleObj, (json) {
final obj = SampleObj()
..prop = json['prop'] as int
..stringProp = json['stringProp'] as String;
return obj;
});
final deliverer = ObjectDelivererManager<SampleObj>();
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleFixedLength.fromParam(10), ObjectJsonDeliveryBox<SampleObj>());
deliverer.receiveData.listen((x) => print(x.message.hoge()));
final sampleObj = SampleObj()
..prop = 1
..stringProp = 'abc';
await deliverer.sendMessage(sampleObj);