2019-09-18 11:48:44 -07:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
2019-09-07 06:19:40 -07:00
|
|
|
import 'dart:isolate';
|
|
|
|
|
|
|
|
import 'package:analyzer/file_system/physical_file_system.dart';
|
2019-09-18 11:48:44 -07:00
|
|
|
import 'package:analyzer_plugin/channel/channel.dart';
|
|
|
|
import 'package:analyzer_plugin/protocol/protocol.dart';
|
2019-09-07 06:19:40 -07:00
|
|
|
import 'package:analyzer_plugin/starter.dart';
|
|
|
|
import 'package:moor_generator/src/backends/plugin/plugin.dart';
|
|
|
|
|
|
|
|
void start(List<String> args, SendPort sendPort) {
|
|
|
|
ServerPluginStarter(MoorPlugin(PhysicalResourceProvider.INSTANCE))
|
|
|
|
.start(sendPort);
|
|
|
|
}
|
2019-09-18 11:48:44 -07:00
|
|
|
|
|
|
|
class WebSocketPluginServer implements PluginCommunicationChannel {
|
|
|
|
final dynamic address;
|
|
|
|
final int port;
|
|
|
|
|
|
|
|
HttpServer server;
|
|
|
|
|
|
|
|
WebSocket _currentClient;
|
|
|
|
final StreamController<WebSocket> _clientStream =
|
|
|
|
StreamController.broadcast();
|
|
|
|
|
|
|
|
WebSocketPluginServer({dynamic address, this.port = 9999})
|
|
|
|
: address = address ?? InternetAddress.loopbackIPv4 {
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:45:09 -08:00
|
|
|
Future<void> _init() async {
|
2019-09-18 11:48:44 -07:00
|
|
|
server = await HttpServer.bind(address, port);
|
|
|
|
print('listening on $address at port $port');
|
|
|
|
server.transform(WebSocketTransformer()).listen(_handleClientAdded);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleClientAdded(WebSocket socket) {
|
|
|
|
if (_currentClient != null) {
|
2019-12-04 12:45:09 -08:00
|
|
|
print('ignoring connection attempt because an active client already '
|
|
|
|
'exists');
|
2019-09-18 11:48:44 -07:00
|
|
|
socket.close();
|
|
|
|
} else {
|
|
|
|
print('client connected');
|
|
|
|
_currentClient = socket;
|
|
|
|
_clientStream.add(_currentClient);
|
|
|
|
_currentClient.done.then((_) {
|
|
|
|
print('client disconnected');
|
|
|
|
_currentClient = null;
|
|
|
|
_clientStream.add(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void close() {
|
|
|
|
server?.close(force: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void listen(void Function(Request request) onRequest,
|
|
|
|
{Function onError, void Function() onDone}) {
|
|
|
|
final stream = _clientStream.stream;
|
|
|
|
|
|
|
|
// wait until we're connected
|
|
|
|
stream.firstWhere((socket) => socket != null).then((_) {
|
|
|
|
_currentClient.listen((data) {
|
|
|
|
print('I: $data');
|
|
|
|
onRequest(Request.fromJson(
|
|
|
|
json.decode(data as String) as Map<String, dynamic>));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
stream.firstWhere((socket) => socket == null).then((_) => onDone());
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void sendNotification(Notification notification) {
|
|
|
|
print('N: ${notification.toJson()}');
|
|
|
|
_currentClient?.add(json.encode(notification.toJson()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void sendResponse(Response response) {
|
|
|
|
print('O: ${response.toJson()}');
|
|
|
|
_currentClient?.add(json.encode(response.toJson()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts the plugin over a websocket service.
|
|
|
|
void main() {
|
|
|
|
MoorPlugin(PhysicalResourceProvider.INSTANCE).start(WebSocketPluginServer());
|
|
|
|
}
|