update socket codes
This commit is contained in:
parent
0bf1fecd73
commit
04289debfd
3 changed files with 12 additions and 6 deletions
|
|
@ -19,7 +19,7 @@ abstract class Communicator
|
|||
{
|
||||
if(!_instanceGenerator.containsKey(type))
|
||||
{
|
||||
throw Exception('Must set protobuf packet creator before use it.');
|
||||
throw Exception('Must set protobuf packet creator before use it. Type: ${(T).toString()}');
|
||||
}
|
||||
return _instanceGenerator[type] as T Function(List<int>);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,16 +14,20 @@ class ProtobufClient extends Communicator
|
|||
final int _headerSize = 4;
|
||||
final Socket _socket;
|
||||
int? _length = null;
|
||||
bool _isAlive = false;
|
||||
late List<int> _arrivedData = [];
|
||||
List<void Function(ProtobufClient)> _onDisconnectListenerList = [];
|
||||
|
||||
|
||||
ProtobufClient(this._socket)
|
||||
ProtobufClient(this._socket, Map<String, GeneratedMessage Function(List<int>)> parserMap)
|
||||
{
|
||||
print('Connected New Client');
|
||||
super.initialize({
|
||||
(TemplateData).toString() : TemplateData.fromBuffer
|
||||
_isAlive = true;
|
||||
parserMap.addAll({
|
||||
(TemplateData).toString() : TemplateData.fromBuffer,
|
||||
(HeartBeat).toString() : HeartBeat.fromBuffer
|
||||
});
|
||||
super.initialize(parserMap);
|
||||
addListener(onHeartBeat);
|
||||
_socket.listen(onData, onError: onError)
|
||||
.asFuture().then(onDisconnected);
|
||||
|
|
|
|||
|
|
@ -3,15 +3,16 @@
|
|||
import 'dart:io';
|
||||
import 'package:dart_framework/socket/packets/message_common.pb.dart';
|
||||
import 'package:dart_framework/socket/protobuf_client.dart';
|
||||
import 'package:dart_framework/utils/system_util.dart';
|
||||
|
||||
abstract class ProtobufServer
|
||||
{
|
||||
late ServerSocket _server;
|
||||
List<ProtobufClient> _clientList = [];
|
||||
late ServerSocket _server;
|
||||
final String _host;
|
||||
final int _port;
|
||||
final ProtobufClient Function(Socket) _createNewClient;
|
||||
|
||||
|
||||
ProtobufServer(this._host, this._port, this._createNewClient);
|
||||
|
||||
Future start() async
|
||||
|
|
@ -24,6 +25,7 @@ abstract class ProtobufServer
|
|||
_clientList.add(client);
|
||||
onClientConnected(client);
|
||||
});
|
||||
return simpleFuture;
|
||||
}
|
||||
|
||||
//implement in child
|
||||
|
|
|
|||
Loading…
Reference in a new issue