/
example.dart
147 lines (103 loc) · 4.01 KB
/
example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import 'dart:typed_data';
import 'package:objectdeliverer_dart/objectdeliverer_dart.dart';
Future quickStart() async {
// 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();
}
Future changeCommunicationProtocol() async {
final deliverer = ObjectDelivererManager<String>();
// TCP/IP Client
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleFixedLength.fromParam(10));
await deliverer.close();
// TCP/IP Server
await deliverer.start(
ProtocolTcpIpServer.fromParam(9013), PacketRuleFixedLength.fromParam(10));
await deliverer.close();
// UDP Sender
await deliverer.start(ProtocolUdpSocketSender.fromParam('127.0.0.1', 9013),
PacketRuleFixedLength.fromParam(10));
await deliverer.close();
// UDP Receiver
await deliverer.start(ProtocolUdpSocketReceiver.fromParam(9013),
PacketRuleFixedLength.fromParam(10));
await deliverer.close();
}
Future changeOfDataDivisionRule() async {
final deliverer = ObjectDelivererManager<String>();
// FixedSize
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleFixedLength.fromParam(10));
await deliverer.close();
// Header(BodySize) + Body
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleSizeBody.fromParam(4, sizeBufferEndian: Endian.big));
await deliverer.close();
// Split by terminal symbol
await deliverer.start(ProtocolTcpIpClient.fromParam('127.0.0.1', 9013),
PacketRuleTerminate.fromParam(Uint8List.fromList([0xFE, 0xFF])));
await deliverer.close();
// Nodivision
await deliverer.start(
ProtocolTcpIpClient.fromParam('127.0.0.1', 9013), PacketRuleNodivision());
await deliverer.close();
}
Future changeOfSerializationMethodString() async {
// 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');
await Future.delayed(const Duration(milliseconds: 100));
await deliverer.close();
}
// Object
class SampleObj extends IJsonSerializable {
int prop;
String stringProp;
String hoge() => '$prop$stringProp';
@override
Map<String, dynamic> toJson() => {'prop': prop, 'stringProp': stringProp};
}
Future changeOfSerializationMethodObject() async {
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);
await Future.delayed(const Duration(milliseconds: 100));
await deliverer.close();
}
Future main() async {
await quickStart();
}