-
Notifications
You must be signed in to change notification settings - Fork 35
/
client.dart
226 lines (197 loc) · 7.85 KB
/
client.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:stack_trace/stack_trace.dart';
import 'package:stream_channel/stream_channel.dart';
import 'channel_manager.dart';
import 'exception.dart';
import 'utils.dart';
/// A JSON-RPC 2.0 client.
///
/// A client calls methods on a server and handles the server's responses to
/// those method calls. Methods can be called with [sendRequest], or with
/// [sendNotification] if no response is expected.
class Client {
final ChannelManager _manager;
/// The next request id.
var _id = 0;
/// The current batch of requests to be sent together.
///
/// Each element is a JSON-serializable object.
List _batch;
/// The map of request ids to pending requests.
final _pendingRequests = <int, _Request>{};
/// Returns a [Future] that completes when the underlying connection is
/// closed.
///
/// This is the same future that's returned by [listen] and [close]. It may
/// complete before [close] is called if the remote endpoint closes the
/// connection.
Future get done => _manager.done;
/// Whether the underlying connection is closed.
///
/// Note that this will be `true` before [close] is called if the remote
/// endpoint closes the connection.
bool get isClosed => _manager.isClosed;
/// Creates a [Client] that communicates over [channel].
///
/// Note that the client won't begin listening to [responses] until
/// [Client.listen] is called.
Client(StreamChannel<String> channel)
: this.withoutJson(
jsonDocument.bind(channel).transformStream(ignoreFormatExceptions));
/// Creates a [Client] that communicates using decoded messages over
/// [channel].
///
/// Unlike [new Client], this doesn't read or write JSON strings. Instead, it
/// reads and writes decoded maps or lists.
///
/// Note that the client won't begin listening to [responses] until
/// [Client.listen] is called.
Client.withoutJson(StreamChannel channel)
: _manager = ChannelManager('Client', channel) {
_manager.done.whenComplete(() {
for (var request in _pendingRequests.values) {
request.completer.completeError(
StateError(
'The client closed with pending request "${request.method}".'),
StackTrace.current);
}
_pendingRequests.clear();
}).catchError((_) {
// Avoid an unhandled error.
});
}
/// Starts listening to the underlying stream.
///
/// Returns a [Future] that will complete when the connection is closed or
/// when it has an error. This is the same as [done].
///
/// [listen] may only be called once.
Future listen() => _manager.listen(_handleResponse);
/// Closes the underlying connection.
///
/// Returns a [Future] that completes when all resources have been released.
/// This is the same as [done].
Future close() => _manager.close();
/// Sends a JSON-RPC 2 request to invoke the given [method].
///
/// If passed, [parameters] is the parameters for the method. This must be
/// either an [Iterable] (to pass parameters by position) or a [Map] with
/// [String] keys (to pass parameters by name). Either way, it must be
/// JSON-serializable.
///
/// If the request succeeds, this returns the response result as a decoded
/// JSON-serializable object. If it fails, it throws an [RpcException]
/// describing the failure.
///
/// Throws a [StateError] if the client is closed while the request is in
/// flight, or if the client is closed when this method is called.
Future sendRequest(String method, [parameters]) {
var id = _id++;
_send(method, parameters, id);
var completer = Completer.sync();
_pendingRequests[id] = _Request(method, completer, Chain.current());
return completer.future;
}
/// Sends a JSON-RPC 2 request to invoke the given [method] without expecting
/// a response.
///
/// If passed, [parameters] is the parameters for the method. This must be
/// either an [Iterable] (to pass parameters by position) or a [Map] with
/// [String] keys (to pass parameters by name). Either way, it must be
/// JSON-serializable.
///
/// Since this is just a notification to which the server isn't expected to
/// send a response, it has no return value.
///
/// Throws a [StateError] if the client is closed when this method is called.
void sendNotification(String method, [parameters]) =>
_send(method, parameters);
/// A helper method for [sendRequest] and [sendNotification].
///
/// Sends a request to invoke [method] with [parameters]. If [id] is given,
/// the request uses that id.
void _send(String method, parameters, [int id]) {
if (parameters is Iterable) parameters = parameters.toList();
if (parameters is! Map && parameters is! List && parameters != null) {
throw ArgumentError('Only maps and lists may be used as JSON-RPC '
'parameters, was "$parameters".');
}
if (isClosed) throw StateError('The client is closed.');
var message = <String, dynamic>{'jsonrpc': '2.0', 'method': method};
if (id != null) message['id'] = id;
if (parameters != null) message['params'] = parameters;
if (_batch != null) {
_batch.add(message);
} else {
_manager.add(message);
}
}
/// Runs [callback] and batches any requests sent until it returns.
///
/// A batch of requests is sent in a single message on the underlying stream,
/// and the responses are likewise sent back in a single message.
///
/// [callback] may be synchronous or asynchronous. If it returns a [Future],
/// requests will be batched until that Future returns; otherwise, requests
/// will only be batched while synchronously executing [callback].
///
/// If this is called in the context of another [withBatch] call, it just
/// invokes [callback] without creating another batch. This means that
/// responses are batched until the first batch ends.
void withBatch(Function() callback) {
if (_batch != null) return callback();
_batch = [];
return tryFinally(callback, () {
_manager.add(_batch);
_batch = null;
});
}
/// Handles a decoded response from the server.
void _handleResponse(response) {
if (response is List) {
response.forEach(_handleSingleResponse);
} else {
_handleSingleResponse(response);
}
}
/// Handles a decoded response from the server after batches have been
/// resolved.
void _handleSingleResponse(response) {
if (!_isResponseValid(response)) return;
var request = _pendingRequests.remove(response['id']);
if (response.containsKey('result')) {
request.completer.complete(response['result']);
} else {
request.completer.completeError(
RpcException(response['error']['code'], response['error']['message'],
data: response['error']['data']),
request.chain);
}
}
/// Determines whether the server's response is valid per the spec.
bool _isResponseValid(response) {
if (response is! Map) return false;
if (response['jsonrpc'] != '2.0') return false;
if (!_pendingRequests.containsKey(response['id'])) return false;
if (response.containsKey('result')) return true;
if (!response.containsKey('error')) return false;
var error = response['error'];
if (error is! Map) return false;
if (error['code'] is! int) return false;
if (error['message'] is! String) return false;
return true;
}
}
/// A pending request to the server.
class _Request {
/// THe method that was sent.
final String method;
/// The completer to use to complete the response future.
final Completer completer;
/// The stack chain from where the request was made.
final Chain chain;
_Request(this.method, this.completer, this.chain);
}