135
135
"""Send a smart server response down the output stream."""
136
136
assert not self._finished, 'response already sent'
137
137
self._finished = True
138
self._write_protocol_version()
138
139
self._write_func(_encode_tuple(args))
139
140
if body is not None:
140
141
assert isinstance(body, str), 'body must be a str'
141
142
bytes = self._encode_bulk_data(body)
142
143
self._write_func(bytes)
145
def _write_protocol_version(self):
146
"""Write any prefixes this protocol requires.
148
Version one doesn't send protocol versions.
144
151
def next_read_size(self):
145
152
if self._finished:
150
157
return self._body_decoder.next_read_size()
160
class SmartServerRequestProtocolTwo(SmartServerRequestProtocolOne):
161
r"""Version two of the server side of the smart protocol.
163
This prefixes responses with the protocol version: "2\x01".
166
def _write_protocol_version(self):
167
r"""Write any prefixes this protocol requires.
169
Version two sends "2\x01".
171
self._write_func('2\x01')
153
174
class LengthPrefixedBodyDecoder(object):
154
175
"""Decodes the length-prefixed bulk data."""
254
275
self._body_buffer = None
256
277
def call(self, *args):
257
bytes = _encode_tuple(args)
258
self._request.accept_bytes(bytes)
278
self._write_args(args)
259
279
self._request.finished_writing()
261
281
def call_with_body_bytes(self, args, body):
264
284
After calling this, call read_response_tuple to find the result out.
266
bytes = _encode_tuple(args)
267
self._request.accept_bytes(bytes)
286
self._write_args(args)
268
287
bytes = self._encode_bulk_data(body)
269
288
self._request.accept_bytes(bytes)
270
289
self._request.finished_writing()
275
294
The body is encoded with one line per readv offset pair. The numbers in
276
295
each pair are separated by a comma, and no trailing \n is emitted.
278
bytes = _encode_tuple(args)
279
self._request.accept_bytes(bytes)
297
self._write_args(args)
280
298
readv_bytes = self._serialise_offsets(body)
281
299
bytes = self._encode_bulk_data(readv_bytes)
282
300
self._request.accept_bytes(bytes)
336
354
resp = self.read_response_tuple()
337
355
if resp == ('ok', '1'):
357
elif resp == ('ok', '2'):
340
360
raise errors.SmartProtocolError("bad response %r" % (resp,))
362
def _write_args(self, args):
363
self._write_protocol_version()
364
bytes = _encode_tuple(args)
365
self._request.accept_bytes(bytes)
367
def _write_protocol_version(self):
368
"""Write any prefixes this protocol requires.
370
Version one doesn't send protocol versions.
374
class SmartClientRequestProtocolTwo(SmartClientRequestProtocolOne):
375
r"""Version two of the client side of the smart protocol.
377
This prefixes the request with the protocol version: "2\x01".
380
_version_string = '2\x01'
382
def read_response_tuple(self, expect_body=False):
383
"""Read a response tuple from the wire.
385
This should only be called once.
387
version = self._request.read_bytes(2)
388
if version != SmartClientRequestProtocolTwo._version_string:
389
raise errors.SmartProtocolError('bad protocol marker %r' % version)
390
return SmartClientRequestProtocolOne.read_response_tuple(self, expect_body)
392
def _write_protocol_version(self):
393
r"""Write any prefixes this protocol requires.
395
Version two sends "2\x01".
397
self._request.accept_bytes(SmartClientRequestProtocolTwo._version_string)