Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 102 additions & 13 deletions packages/tizen_rpc_port/example/client/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
final Message _client = Message('org.tizen.tizen_rpc_port_server_example');
String _message = 'Not connected';
String _message2 = 'Not connected2';
bool _isConnected = false;
bool _isConnected2 = false;
String _input = '';
Message? _client;
Message? _client2;

Future<void> _connect() async {
setState(() {
_message = 'Connecting...';
});

try {
await _client.connect(
_client ??= Message('org.tizen.tizen_rpc_port_server_example');
await _client!.connect(
onError: (Object error) {
setState(() {
_message = 'Error: $error';
Expand All @@ -46,7 +50,7 @@ class _MyAppState extends State<MyApp> {
});
},
);
_client.register('ClientApp', onMessage);
_client!.register('ClientApp', onMessage);

setState(() {
_isConnected = true;
Expand All @@ -59,23 +63,76 @@ class _MyAppState extends State<MyApp> {
}
}

Future<void> _connect2() async {
setState(() {
_message2 = 'Connecting...';
});

try {
_client2 ??= Message('org.tizen.tizen_rpc_port_server_example');
await _client2!.connect(
onError: (Object error) {
setState(() {
_message2 = 'Error: $error';
});
},
onDisconnected: () async {
setState(() {
_isConnected2 = false;
_message2 = 'Disconnected';
});
},
);
_client2!.register('ClientApp2', onMessage2);

setState(() {
_isConnected2 = true;
_message2 = 'Connected';
});
} catch (error) {
setState(() {
_message2 = 'Error: $error';
});
}
}

void onMessage(String sender, String message) {
setState(() {
_message = 'Received: $message';
});
}

void onMessage2(String sender, String message) {
setState(() {
_message2 = 'Received: $message';
});
}

Future<void> _send() async {
if (_isConnected) {
_client.send(_input);
_client!.send(_input);
}
}

Future<void> _send2() async {
if (_isConnected2) {
_client2!.send(_input);
}
}

@override
void dispose() {
if (_isConnected) {
_client.unregister();
_client.disconnect();
if (_client != null) {
_client!.unregister();
_client!.disconnect();
}
}
if (_isConnected2) {
if (_client2 != null) {
_client2!.unregister();
_client2!.disconnect();
}
}
super.dispose();
}
Expand All @@ -93,6 +150,10 @@ class _MyAppState extends State<MyApp> {
padding: const EdgeInsets.symmetric(vertical: 20),
child: Text(_message),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 20),
child: Text(_message2),
),
TextField(
onChanged: (String text) {
setState(() => _input = text);
Expand All @@ -106,13 +167,41 @@ class _MyAppState extends State<MyApp> {
),
),
persistentFooterButtons: <Widget>[
TextButton(
onPressed: _isConnected ? null : _connect,
child: const Text('Connect'),
),
TextButton(
onPressed: _isConnected ? _send : null,
child: const Text('Send'),
Column(
children: <Widget>[
TextButton(
style: ButtonStyle(
minimumSize:
MaterialStateProperty.all(const Size.fromHeight(100)),
),
onPressed: _connect,
child: const Text('Connect'),
),
TextButton(
style: ButtonStyle(
minimumSize:
MaterialStateProperty.all(const Size.fromHeight(100)),
),
onPressed: _connect2,
child: const Text('Connect2'),
),
TextButton(
style: ButtonStyle(
minimumSize:
MaterialStateProperty.all(const Size.fromHeight(100)),
),
onPressed: _isConnected ? _send : null,
child: const Text('Send'),
),
TextButton(
style: ButtonStyle(
minimumSize:
MaterialStateProperty.all(const Size.fromHeight(100)),
),
onPressed: _isConnected2 ? _send2 : null,
child: const Text('Send2'),
),
],
),
],
),
Expand Down
33 changes: 27 additions & 6 deletions packages/tizen_rpc_port/lib/src/proxy_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ abstract class ProxyBase {
Completer<void> _connectCompleter = Completer<void>();
Completer<void> _disconnectCompleter = Completer<void>();

static const EventChannel _eventChannel = EventChannel(
static const MethodChannel _channel = MethodChannel(
'tizen/rpc_port_proxy',
);

Stream<dynamic>? _stream;
static final Set<int> _activeConnections = <int>{};
StreamSubscription<dynamic>? _streamSubscription;
OnDisconnected? _onDisconnected;

Expand All @@ -90,14 +92,28 @@ abstract class ProxyBase {
throw StateError('Proxy $appid/$portName already connected');
}

final Stream<dynamic> stream = _eventChannel.receiveBroadcastStream(
await _channel.invokeMethod(
'init',
<String, Object>{
'handle': _handle.address,
'portName': portName,
},
);

final EventChannel eventChannel = EventChannel(
'tizen/rpc_port_proxy/$portName/${_handle.address}',
);

_stream = eventChannel.receiveBroadcastStream(
<String, Object>{
'handle': _handle.address,
'appid': appid,
'portName': portName,
},
);
_streamSubscription = stream.listen((dynamic data) async {

_activeConnections.add(_handle.address);
_streamSubscription = _stream!.listen((dynamic data) async {
final Map<String, dynamic> map =
(data as Map<dynamic, dynamic>).cast<String, dynamic>();
final int handle = map['handle'] as int;
Expand All @@ -114,12 +130,20 @@ abstract class ProxyBase {
await _onDisconnectedEvent();
await _streamSubscription?.cancel();
_streamSubscription = null;
_activeConnections.remove(_handle.address);
if (_activeConnections.isEmpty) {
_stream = null;
}
} else if (event == 'rejected') {
_isConnected = false;
final String error = map['error'] as String;
await _onRejectedEvent(error);
await _streamSubscription?.cancel();
_streamSubscription = null;
_activeConnections.remove(_handle.address);
if (_activeConnections.isEmpty) {
_stream = null;
}
} else if (event == 'received') {
final Uint8List rawData = map['rawData'] as Uint8List;
final Parcel parcel = Parcel.fromRaw(rawData);
Expand Down Expand Up @@ -182,9 +206,6 @@ abstract class ProxyBase {
await _onDisconnected?.call();
_onDisconnected = null;

getPort(PortType.main).disconnect();
getPort(PortType.callback).disconnect();

if (!_disconnectCompleter.isCompleted) {
_disconnectCompleter.complete();
_disconnectCompleter = Completer<void>();
Expand Down
Loading