|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
5 | 5 | import 'dart:async';
|
6 |
| -import 'dart:io'; |
7 | 6 |
|
8 | 7 | import 'package:dds/src/dap/adapters/dart.dart';
|
9 |
| -import 'package:pedantic/pedantic.dart'; |
10 | 8 |
|
11 | 9 | import 'adapters/dart_cli.dart';
|
12 | 10 | import 'logging.dart';
|
13 | 11 | import 'protocol_stream.dart';
|
14 |
| -import 'protocol_stream_transformers.dart'; |
15 | 12 |
|
16 |
| -/// A DAP server that binds to a port and runs in multi-session mode. |
| 13 | +/// A DAP server that communicates over a [ByteStreamServerChannel], usually |
| 14 | +/// constructed from the processes stdin/stdout streams. |
| 15 | +/// |
| 16 | +/// The server runs in single-user mode and services only a single client. For |
| 17 | +/// multiple debug sessions, there would be multiple servers (and the editor |
| 18 | +/// would have a client for each of them). |
17 | 19 | class DapServer {
|
18 |
| - final ServerSocket _socket; |
| 20 | + final ByteStreamServerChannel channel; |
| 21 | + late final DartDebugAdapter adapter; |
19 | 22 | final bool ipv6;
|
20 | 23 | final bool enableDds;
|
21 | 24 | final bool enableAuthCodes;
|
22 | 25 | final Logger? logger;
|
23 |
| - final _channels = <ByteStreamServerChannel>{}; |
24 |
| - final _adapters = <DartDebugAdapter>{}; |
25 | 26 |
|
26 |
| - DapServer._( |
27 |
| - this._socket, { |
| 27 | + DapServer( |
| 28 | + Stream<List<int>> _input, |
| 29 | + StreamSink<List<int>> _output, { |
28 | 30 | this.ipv6 = false,
|
29 | 31 | this.enableDds = true,
|
30 | 32 | this.enableAuthCodes = true,
|
31 | 33 | this.logger,
|
32 |
| - }) { |
33 |
| - _socket.listen(_acceptConnection); |
34 |
| - } |
35 |
| - |
36 |
| - String get host => _socket.address.host; |
37 |
| - int get port => _socket.port; |
38 |
| - |
39 |
| - Future<void> stop() async { |
40 |
| - _channels.forEach((client) => client.close()); |
41 |
| - await _socket.close(); |
42 |
| - } |
43 |
| - |
44 |
| - void _acceptConnection(Socket client) { |
45 |
| - final address = client.remoteAddress; |
46 |
| - logger?.call('Accepted connection from $address'); |
47 |
| - client.done.then((_) { |
48 |
| - logger?.call('Connection from $address closed'); |
49 |
| - }); |
50 |
| - _createAdapter(client.transform(Uint8ListTransformer()), client); |
51 |
| - } |
52 |
| - |
53 |
| - void _createAdapter(Stream<List<int>> _input, StreamSink<List<int>> _output) { |
54 |
| - // TODO(dantup): This is hard-coded to DartCliDebugAdapter but will |
55 |
| - // ultimately need to support having a factory passed in to support |
56 |
| - // tests and/or being used in flutter_tools. |
57 |
| - final channel = ByteStreamServerChannel(_input, _output, logger); |
58 |
| - final adapter = DartCliDebugAdapter( |
| 34 | + }) : channel = ByteStreamServerChannel(_input, _output, logger) { |
| 35 | + adapter = DartCliDebugAdapter( |
59 | 36 | channel,
|
60 | 37 | ipv6: ipv6,
|
61 | 38 | enableDds: enableDds,
|
62 | 39 | enableAuthCodes: enableAuthCodes,
|
63 | 40 | logger: logger,
|
64 | 41 | );
|
65 |
| - _channels.add(channel); |
66 |
| - _adapters.add(adapter); |
67 |
| - unawaited(channel.closed.then((_) { |
68 |
| - _channels.remove(channel); |
69 |
| - _adapters.remove(adapter); |
70 |
| - adapter.shutdown(); |
71 |
| - })); |
72 | 42 | }
|
73 | 43 |
|
74 |
| - /// Starts a DAP Server listening on [host]:[port]. |
75 |
| - static Future<DapServer> create({ |
76 |
| - String? host, |
77 |
| - int port = 0, |
78 |
| - bool ipv6 = false, |
79 |
| - bool enableDdds = true, |
80 |
| - bool enableAuthCodes = true, |
81 |
| - Logger? logger, |
82 |
| - }) async { |
83 |
| - final hostFallback = |
84 |
| - ipv6 ? InternetAddress.loopbackIPv6 : InternetAddress.loopbackIPv4; |
85 |
| - final _socket = await ServerSocket.bind(host ?? hostFallback, port); |
86 |
| - return DapServer._( |
87 |
| - _socket, |
88 |
| - ipv6: ipv6, |
89 |
| - enableDds: enableDdds, |
90 |
| - enableAuthCodes: enableAuthCodes, |
91 |
| - logger: logger, |
92 |
| - ); |
| 44 | + void stop() { |
| 45 | + channel.close(); |
93 | 46 | }
|
94 | 47 | }
|
0 commit comments