Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/wireprotoframing.py @ 52716:1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
This was low hanging fruit, and will point out an apparent programming bug.
The arguments are left untyped for now, because the internal compressors and
decompressors generally take a `Buffer` object, and return bytes. I've had
issues around using `typing_extensions.Buffer` with pytype in the past, but also
the identity classes return the argument unmodified, so that will break the type
annotation. We'd probably need an instance check, and a cast if it's not bytes.
Having to roll up all of the encoder and decoder types into a union type is a
bit annoying, but the protocol class can't be used in the map because it then
assumes that type will be instantiated. I also couldn't get it to work with a
`TypeVar` bound to indicate that it's actually a subclass either. Oh well.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Mon, 13 Jan 2025 15:37:08 -0500 |
parents | e627cc25b6f3 |
children | 068398a8c9cb |
rev | line source |
---|---|
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 # wireprotoframing.py - unified framing protocol for wire protocol |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 # |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 # Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com> |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 # |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 # This file contains functionality to support the unified frame-based wire |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 # protocol. For details about the protocol, see |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
10 # `hg help internals.wireprotocol`. |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 |
51901
f4733654f144
typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents:
51788
diff
changeset
|
12 from __future__ import annotations |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
13 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
14 import abc |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
15 import collections |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 import struct |
51788
278af66e6595
typing: induce pytype to use the standard `attr` instead of the vendored copy
Matt Harbison <matt_harbison@yahoo.com>
parents:
51762
diff
changeset
|
17 import typing |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
19 from typing import ( |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
20 Protocol, |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
21 Type, |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
22 ) |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
23 |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
24 from .i18n import _ |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
25 from .thirdparty import attr |
51788
278af66e6595
typing: induce pytype to use the standard `attr` instead of the vendored copy
Matt Harbison <matt_harbison@yahoo.com>
parents:
51762
diff
changeset
|
26 |
278af66e6595
typing: induce pytype to use the standard `attr` instead of the vendored copy
Matt Harbison <matt_harbison@yahoo.com>
parents:
51762
diff
changeset
|
27 # Force pytype to use the non-vendored package |
278af66e6595
typing: induce pytype to use the standard `attr` instead of the vendored copy
Matt Harbison <matt_harbison@yahoo.com>
parents:
51762
diff
changeset
|
28 if typing.TYPE_CHECKING: |
278af66e6595
typing: induce pytype to use the standard `attr` instead of the vendored copy
Matt Harbison <matt_harbison@yahoo.com>
parents:
51762
diff
changeset
|
29 # noinspection PyPackageRequirements |
278af66e6595
typing: induce pytype to use the standard `attr` instead of the vendored copy
Matt Harbison <matt_harbison@yahoo.com>
parents:
51762
diff
changeset
|
30 import attr |
278af66e6595
typing: induce pytype to use the standard `attr` instead of the vendored copy
Matt Harbison <matt_harbison@yahoo.com>
parents:
51762
diff
changeset
|
31 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
32 from . import ( |
37474
d33997123ea5
py3: system-stringify repr(frame)
Yuya Nishihara <yuya@tcha.org>
parents:
37473
diff
changeset
|
33 encoding, |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
34 error, |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
35 pycompat, |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
36 util, |
40021
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
37 wireprototypes, |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
38 ) |
37087
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37066
diff
changeset
|
39 from .utils import ( |
39465
36f487a332ad
wireprotoframing: use our CBOR module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37728
diff
changeset
|
40 cborutil, |
37087
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37066
diff
changeset
|
41 stringutil, |
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37066
diff
changeset
|
42 ) |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
43 |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
44 FRAME_HEADER_SIZE = 8 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
45 DEFAULT_MAX_FRAME_SIZE = 32768 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
46 |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
47 STREAM_FLAG_BEGIN_STREAM = 0x01 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
48 STREAM_FLAG_END_STREAM = 0x02 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
49 STREAM_FLAG_ENCODING_APPLIED = 0x04 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
50 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
51 STREAM_FLAGS = { |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
52 b'stream-begin': STREAM_FLAG_BEGIN_STREAM, |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
53 b'stream-end': STREAM_FLAG_END_STREAM, |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
54 b'encoded': STREAM_FLAG_ENCODING_APPLIED, |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
55 } |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
56 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
57 FRAME_TYPE_COMMAND_REQUEST = 0x01 |
37723
e8fba6d578f0
wireprotov2: change frame type value for command data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37722
diff
changeset
|
58 FRAME_TYPE_COMMAND_DATA = 0x02 |
37724
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
59 FRAME_TYPE_COMMAND_RESPONSE = 0x03 |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
60 FRAME_TYPE_ERROR_RESPONSE = 0x05 |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
61 FRAME_TYPE_TEXT_OUTPUT = 0x06 |
37291
b0041036214e
wireproto: define frame to represent progress updates
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37290
diff
changeset
|
62 FRAME_TYPE_PROGRESS = 0x07 |
40126
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
63 FRAME_TYPE_SENDER_PROTOCOL_SETTINGS = 0x08 |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
64 FRAME_TYPE_STREAM_SETTINGS = 0x09 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
65 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
66 FRAME_TYPES = { |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
67 b'command-request': FRAME_TYPE_COMMAND_REQUEST, |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
68 b'command-data': FRAME_TYPE_COMMAND_DATA, |
37724
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
69 b'command-response': FRAME_TYPE_COMMAND_RESPONSE, |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
70 b'error-response': FRAME_TYPE_ERROR_RESPONSE, |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
71 b'text-output': FRAME_TYPE_TEXT_OUTPUT, |
37291
b0041036214e
wireproto: define frame to represent progress updates
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37290
diff
changeset
|
72 b'progress': FRAME_TYPE_PROGRESS, |
40126
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
73 b'sender-protocol-settings': FRAME_TYPE_SENDER_PROTOCOL_SETTINGS, |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
74 b'stream-settings': FRAME_TYPE_STREAM_SETTINGS, |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
75 } |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
76 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
77 FLAG_COMMAND_REQUEST_NEW = 0x01 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
78 FLAG_COMMAND_REQUEST_CONTINUATION = 0x02 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
79 FLAG_COMMAND_REQUEST_MORE_FRAMES = 0x04 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
80 FLAG_COMMAND_REQUEST_EXPECT_DATA = 0x08 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
81 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
82 FLAGS_COMMAND_REQUEST = { |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
83 b'new': FLAG_COMMAND_REQUEST_NEW, |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
84 b'continuation': FLAG_COMMAND_REQUEST_CONTINUATION, |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
85 b'more': FLAG_COMMAND_REQUEST_MORE_FRAMES, |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
86 b'have-data': FLAG_COMMAND_REQUEST_EXPECT_DATA, |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
87 } |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
88 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
89 FLAG_COMMAND_DATA_CONTINUATION = 0x01 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
90 FLAG_COMMAND_DATA_EOS = 0x02 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
91 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
92 FLAGS_COMMAND_DATA = { |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
93 b'continuation': FLAG_COMMAND_DATA_CONTINUATION, |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
94 b'eos': FLAG_COMMAND_DATA_EOS, |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
95 } |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
96 |
37724
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
97 FLAG_COMMAND_RESPONSE_CONTINUATION = 0x01 |
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
98 FLAG_COMMAND_RESPONSE_EOS = 0x02 |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
99 |
37724
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
100 FLAGS_COMMAND_RESPONSE = { |
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
101 b'continuation': FLAG_COMMAND_RESPONSE_CONTINUATION, |
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
102 b'eos': FLAG_COMMAND_RESPONSE_EOS, |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
103 } |
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
104 |
40126
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
105 FLAG_SENDER_PROTOCOL_SETTINGS_CONTINUATION = 0x01 |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
106 FLAG_SENDER_PROTOCOL_SETTINGS_EOS = 0x02 |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
107 |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
108 FLAGS_SENDER_PROTOCOL_SETTINGS = { |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
109 b'continuation': FLAG_SENDER_PROTOCOL_SETTINGS_CONTINUATION, |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
110 b'eos': FLAG_SENDER_PROTOCOL_SETTINGS_EOS, |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
111 } |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
112 |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
113 FLAG_STREAM_ENCODING_SETTINGS_CONTINUATION = 0x01 |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
114 FLAG_STREAM_ENCODING_SETTINGS_EOS = 0x02 |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
115 |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
116 FLAGS_STREAM_ENCODING_SETTINGS = { |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
117 b'continuation': FLAG_STREAM_ENCODING_SETTINGS_CONTINUATION, |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
118 b'eos': FLAG_STREAM_ENCODING_SETTINGS_EOS, |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
119 } |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
120 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
121 # Maps frame types to their available flags. |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
122 FRAME_TYPE_FLAGS = { |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
123 FRAME_TYPE_COMMAND_REQUEST: FLAGS_COMMAND_REQUEST, |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
124 FRAME_TYPE_COMMAND_DATA: FLAGS_COMMAND_DATA, |
37724
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
125 FRAME_TYPE_COMMAND_RESPONSE: FLAGS_COMMAND_RESPONSE, |
37726
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
126 FRAME_TYPE_ERROR_RESPONSE: {}, |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
127 FRAME_TYPE_TEXT_OUTPUT: {}, |
37291
b0041036214e
wireproto: define frame to represent progress updates
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37290
diff
changeset
|
128 FRAME_TYPE_PROGRESS: {}, |
40126
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
129 FRAME_TYPE_SENDER_PROTOCOL_SETTINGS: FLAGS_SENDER_PROTOCOL_SETTINGS, |
e2fe1074024c
wireprotov2: update stream encoding specification
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40026
diff
changeset
|
130 FRAME_TYPE_STREAM_SETTINGS: FLAGS_STREAM_ENCODING_SETTINGS, |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
131 } |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
132 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
133 ARGUMENT_RECORD_HEADER = struct.Struct('<HH') |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
134 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
135 |
37298
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
136 def humanflags(mapping, value): |
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
137 """Convert a numeric flags value to a human value, using a mapping table.""" |
49004
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48991
diff
changeset
|
138 namemap = {v: k for k, v in mapping.items()} |
37298
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
139 flags = [] |
37473
7c2c7c749411
wireproto: show unknown id and flags in repr(frame)
Yuya Nishihara <yuya@tcha.org>
parents:
37472
diff
changeset
|
140 val = 1 |
7c2c7c749411
wireproto: show unknown id and flags in repr(frame)
Yuya Nishihara <yuya@tcha.org>
parents:
37472
diff
changeset
|
141 while value >= val: |
37298
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
142 if value & val: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
143 flags.append(namemap.get(val, b'<unknown 0x%02x>' % val)) |
37473
7c2c7c749411
wireproto: show unknown id and flags in repr(frame)
Yuya Nishihara <yuya@tcha.org>
parents:
37472
diff
changeset
|
144 val <<= 1 |
37298
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
145 |
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
146 return b'|'.join(flags) |
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
147 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
148 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
149 @attr.s(slots=True) |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
49004
diff
changeset
|
150 class frameheader: |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
151 """Represents the data in a frame header.""" |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
152 |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
153 length = attr.ib() |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
154 requestid = attr.ib() |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
155 streamid = attr.ib() |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
156 streamflags = attr.ib() |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
157 typeid = attr.ib() |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
158 flags = attr.ib() |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
159 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
160 |
37298
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
161 @attr.s(slots=True, repr=False) |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
49004
diff
changeset
|
162 class frame: |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
163 """Represents a parsed frame.""" |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
164 |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
165 requestid = attr.ib() |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
166 streamid = attr.ib() |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
167 streamflags = attr.ib() |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
168 typeid = attr.ib() |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
169 flags = attr.ib() |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
170 payload = attr.ib() |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
171 |
37474
d33997123ea5
py3: system-stringify repr(frame)
Yuya Nishihara <yuya@tcha.org>
parents:
37473
diff
changeset
|
172 @encoding.strmethod |
37298
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
173 def __repr__(self): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
174 typename = b'<unknown 0x%02x>' % self.typeid |
49004
f254fc73d956
global: bulk replace simple pycompat.iteritems(x) with x.items()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48991
diff
changeset
|
175 for name, value in FRAME_TYPES.items(): |
37298
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
176 if value == self.typeid: |
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
177 typename = name |
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
178 break |
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
179 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
180 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
181 b'frame(size=%d; request=%d; stream=%d; streamflags=%s; ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
182 b'type=%s; flags=%s)' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
183 % ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
184 len(self.payload), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
185 self.requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
186 self.streamid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
187 humanflags(STREAM_FLAGS, self.streamflags), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
188 typename, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
189 humanflags(FRAME_TYPE_FLAGS.get(self.typeid, {}), self.flags), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
190 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
191 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
192 |
37298
5ef2da00e935
wireproto: implement custom __repr__ for frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37292
diff
changeset
|
193 |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
194 def makeframe(requestid, streamid, streamflags, typeid, flags, payload): |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
195 """Assemble a frame into a byte array.""" |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
196 # TODO assert size of payload. |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
197 frame = bytearray(FRAME_HEADER_SIZE + len(payload)) |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
198 |
37060
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
199 # 24 bits length |
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
200 # 16 bits request id |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
201 # 8 bits stream id |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
202 # 8 bits stream flags |
37060
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
203 # 4 bits type |
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
204 # 4 bits flags |
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
205 |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
206 l = struct.pack('<I', len(payload)) |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
207 frame[0:3] = l[0:3] |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
208 struct.pack_into('<HBB', frame, 3, requestid, streamid, streamflags) |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
209 frame[7] = (typeid << 4) | flags |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
210 frame[8:] = payload |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
211 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
212 return frame |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
213 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
214 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
215 def makeframefromhumanstring(s): |
37060
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
216 """Create a frame from a human readable string |
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
217 |
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
218 Strings have the form: |
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
219 |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
220 <request-id> <stream-id> <stream-flags> <type> <flags> <payload> |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
221 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
222 This can be used by user-facing applications and tests for creating |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
223 frames easily without having to type out a bunch of constants. |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
224 |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
225 Request ID and stream IDs are integers. |
37060
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
226 |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
227 Stream flags, frame type, and flags can be specified by integer or |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
228 named constant. |
37060
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
229 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
230 Flags can be delimited by `|` to bitwise OR them together. |
37290
cc5a040fe150
wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37289
diff
changeset
|
231 |
cc5a040fe150
wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37289
diff
changeset
|
232 If the payload begins with ``cbor:``, the following string will be |
37476
e9dea82ea1f3
wireproto: convert python literal to object without using unsafe eval()
Yuya Nishihara <yuya@tcha.org>
parents:
37474
diff
changeset
|
233 evaluated as Python literal and the resulting object will be fed into |
37290
cc5a040fe150
wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37289
diff
changeset
|
234 a CBOR encoder. Otherwise, the payload is interpreted as a Python |
cc5a040fe150
wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37289
diff
changeset
|
235 byte string literal. |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
236 """ |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
237 fields = s.split(b' ', 5) |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
238 requestid, streamid, streamflags, frametype, frameflags, payload = fields |
37060
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
239 |
2ec1fb9de638
wireproto: add request IDs to frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37059
diff
changeset
|
240 requestid = int(requestid) |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
241 streamid = int(streamid) |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
242 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
243 finalstreamflags = 0 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
244 for flag in streamflags.split(b'|'): |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
245 if flag in STREAM_FLAGS: |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
246 finalstreamflags |= STREAM_FLAGS[flag] |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
247 else: |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
248 finalstreamflags |= int(flag) |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
249 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
250 if frametype in FRAME_TYPES: |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
251 frametype = FRAME_TYPES[frametype] |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
252 else: |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
253 frametype = int(frametype) |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
254 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
255 finalflags = 0 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
256 validflags = FRAME_TYPE_FLAGS[frametype] |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
257 for flag in frameflags.split(b'|'): |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
258 if flag in validflags: |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
259 finalflags |= validflags[flag] |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
260 else: |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
261 finalflags |= int(flag) |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
262 |
37290
cc5a040fe150
wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37289
diff
changeset
|
263 if payload.startswith(b'cbor:'): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
264 payload = b''.join( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
265 cborutil.streamencode(stringutil.evalpythonliteral(payload[5:])) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
266 ) |
37290
cc5a040fe150
wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37289
diff
changeset
|
267 |
cc5a040fe150
wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37289
diff
changeset
|
268 else: |
cc5a040fe150
wireproto: syntax for encoding CBOR into frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37289
diff
changeset
|
269 payload = stringutil.unescapestr(payload) |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
270 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
271 return makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
272 requestid=requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
273 streamid=streamid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
274 streamflags=finalstreamflags, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
275 typeid=frametype, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
276 flags=finalflags, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
277 payload=payload, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
278 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
279 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
280 |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
281 def parseheader(data): |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
282 """Parse a unified framing protocol frame header from a buffer. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
283 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
284 The header is expected to be in the buffer at offset 0 and the |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
285 buffer is expected to be large enough to hold a full header. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
286 """ |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
287 # 24 bits payload length (little endian) |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
288 # 16 bits request ID |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
289 # 8 bits stream ID |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
290 # 8 bits stream flags |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
291 # 4 bits frame type |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
292 # 4 bits frame flags |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
293 # ... payload |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
294 framelength = data[0] + 256 * data[1] + 16384 * data[2] |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
295 requestid, streamid, streamflags = struct.unpack_from('<HBB', data, 3) |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
296 typeflags = data[7] |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
297 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
298 frametype = (typeflags & 0xF0) >> 4 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
299 frameflags = typeflags & 0x0F |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
300 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
301 return frameheader( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
302 framelength, requestid, streamid, streamflags, frametype, frameflags |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
303 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
304 |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
305 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
306 def readframe(fh): |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
307 """Read a unified framing protocol frame from a file object. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
308 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
309 Returns a 3-tuple of (type, flags, payload) for the decoded frame or |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
310 None if no frame is available. May raise if a malformed frame is |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
311 seen. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
312 """ |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
313 header = bytearray(FRAME_HEADER_SIZE) |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
314 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
315 readcount = fh.readinto(header) |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
316 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
317 if readcount == 0: |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
318 return None |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
319 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
320 if readcount != FRAME_HEADER_SIZE: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
321 raise error.Abort( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
322 _(b'received incomplete frame: got %d bytes: %s') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
323 % (readcount, header) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
324 ) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
325 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
326 h = parseheader(header) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
327 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
328 payload = fh.read(h.length) |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
329 if len(payload) != h.length: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
330 raise error.Abort( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
331 _(b'frame length error: expected %d; got %d') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
332 % (h.length, len(payload)) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
333 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
334 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
335 return frame( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
336 h.requestid, h.streamid, h.streamflags, h.typeid, h.flags, payload |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
337 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
338 |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
339 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
340 def createcommandframes( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
341 stream, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
342 requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
343 cmd, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
344 args, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
345 datafh=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
346 maxframesize=DEFAULT_MAX_FRAME_SIZE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
347 redirect=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
348 ): |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
349 """Create frames necessary to transmit a request to run a command. |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
350 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
351 This is a generator of bytearrays. Each item represents a frame |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
352 ready to be sent over the wire to a peer. |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
353 """ |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
354 data = {b'name': cmd} |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
355 if args: |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
356 data[b'args'] = args |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
357 |
40025
86b22a4cfab1
wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
358 if redirect: |
86b22a4cfab1
wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
359 data[b'redirect'] = redirect |
86b22a4cfab1
wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
360 |
39465
36f487a332ad
wireprotoframing: use our CBOR module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37728
diff
changeset
|
361 data = b''.join(cborutil.streamencode(data)) |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
362 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
363 offset = 0 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
364 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
365 while True: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
366 flags = 0 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
367 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
368 # Must set new or continuation flag. |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
369 if not offset: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
370 flags |= FLAG_COMMAND_REQUEST_NEW |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
371 else: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
372 flags |= FLAG_COMMAND_REQUEST_CONTINUATION |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
373 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
374 # Data frames is set on all frames. |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
375 if datafh: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
376 flags |= FLAG_COMMAND_REQUEST_EXPECT_DATA |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
377 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
378 payload = data[offset : offset + maxframesize] |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
379 offset += len(payload) |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
380 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
381 if len(payload) == maxframesize and offset < len(data): |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
382 flags |= FLAG_COMMAND_REQUEST_MORE_FRAMES |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
383 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
384 yield stream.makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
385 requestid=requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
386 typeid=FRAME_TYPE_COMMAND_REQUEST, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
387 flags=flags, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
388 payload=payload, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
389 ) |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
390 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
391 if not (flags & FLAG_COMMAND_REQUEST_MORE_FRAMES): |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
392 break |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
393 |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
394 if datafh: |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
395 while True: |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
396 data = datafh.read(DEFAULT_MAX_FRAME_SIZE) |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
397 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
398 done = False |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
399 if len(data) == DEFAULT_MAX_FRAME_SIZE: |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
400 flags = FLAG_COMMAND_DATA_CONTINUATION |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
401 else: |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
402 flags = FLAG_COMMAND_DATA_EOS |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
403 assert datafh.read(1) == b'' |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
404 done = True |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
405 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
406 yield stream.makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
407 requestid=requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
408 typeid=FRAME_TYPE_COMMAND_DATA, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
409 flags=flags, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
410 payload=data, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
411 ) |
37054
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
412 |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
413 if done: |
40206e227412
wireproto: define and implement protocol for issuing requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
414 break |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
415 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
416 |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
417 def createcommandresponseokframe(stream, requestid): |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
418 overall = b''.join(cborutil.streamencode({b'status': b'ok'})) |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
419 |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
420 if stream.streamsettingssent: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
421 overall = stream.encode(overall) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
422 encoded = True |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
423 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
424 if not overall: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
425 return None |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
426 else: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
427 encoded = False |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
428 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
429 return stream.makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
430 requestid=requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
431 typeid=FRAME_TYPE_COMMAND_RESPONSE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
432 flags=FLAG_COMMAND_RESPONSE_CONTINUATION, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
433 payload=overall, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
434 encoded=encoded, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
435 ) |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
436 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
437 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
438 def createcommandresponseeosframes( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
439 stream, requestid, maxframesize=DEFAULT_MAX_FRAME_SIZE |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
440 ): |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
441 """Create an empty payload frame representing command end-of-stream.""" |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
442 payload = stream.flush() |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
443 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
444 offset = 0 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
445 while True: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
446 chunk = payload[offset : offset + maxframesize] |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
447 offset += len(chunk) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
448 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
449 done = offset == len(payload) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
450 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
451 if done: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
452 flags = FLAG_COMMAND_RESPONSE_EOS |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
453 else: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
454 flags = FLAG_COMMAND_RESPONSE_CONTINUATION |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
455 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
456 yield stream.makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
457 requestid=requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
458 typeid=FRAME_TYPE_COMMAND_RESPONSE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
459 flags=flags, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
460 payload=chunk, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
461 encoded=payload != b'', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
462 ) |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
463 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
464 if done: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
465 break |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
466 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
467 |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
468 def createalternatelocationresponseframe(stream, requestid, location): |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
469 data = { |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
470 b'status': b'redirect', |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
471 b'location': { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
472 b'url': location.url, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
473 b'mediatype': location.mediatype, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
474 }, |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
475 } |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
476 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
477 for a in ( |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
478 'size', |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
479 'fullhashes', |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
480 'fullhashseed', |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
481 'serverdercerts', |
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
482 'servercadercerts', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
483 ): |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
484 value = getattr(location, a) |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
485 if value is not None: |
52129
0c260e7158e0
typing: suppress bogus pytype errors in `mercurial/wireprotoframing.py`
Matt Harbison <matt_harbison@yahoo.com>
parents:
51901
diff
changeset
|
486 # pytype: disable=unsupported-operands |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
487 data[b'location'][pycompat.bytestr(a)] = value |
52129
0c260e7158e0
typing: suppress bogus pytype errors in `mercurial/wireprotoframing.py`
Matt Harbison <matt_harbison@yahoo.com>
parents:
51901
diff
changeset
|
488 # pytype: enable=unsupported-operands |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
489 |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
490 payload = b''.join(cborutil.streamencode(data)) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
491 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
492 if stream.streamsettingssent: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
493 payload = stream.encode(payload) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
494 encoded = True |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
495 else: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
496 encoded = False |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
497 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
498 return stream.makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
499 requestid=requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
500 typeid=FRAME_TYPE_COMMAND_RESPONSE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
501 flags=FLAG_COMMAND_RESPONSE_CONTINUATION, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
502 payload=payload, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
503 encoded=encoded, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
504 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
505 |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
506 |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
507 def createcommanderrorresponse(stream, requestid, message, args=None): |
39503
43d92d68ac88
wireprotov2peer: properly format errors
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39465
diff
changeset
|
508 # TODO should this be using a list of {'msg': ..., 'args': {}} so atom |
43d92d68ac88
wireprotov2peer: properly format errors
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39465
diff
changeset
|
509 # formatting works consistently? |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
510 m = { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
511 b'status': b'error', |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
512 b'error': { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
513 b'message': message, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
514 }, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
515 } |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
516 |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
517 if args: |
52129
0c260e7158e0
typing: suppress bogus pytype errors in `mercurial/wireprotoframing.py`
Matt Harbison <matt_harbison@yahoo.com>
parents:
51901
diff
changeset
|
518 m[b'error'][b'args'] = args # pytype: disable=unsupported-operands |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
519 |
39465
36f487a332ad
wireprotoframing: use our CBOR module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37728
diff
changeset
|
520 overall = b''.join(cborutil.streamencode(m)) |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
521 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
522 yield stream.makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
523 requestid=requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
524 typeid=FRAME_TYPE_COMMAND_RESPONSE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
525 flags=FLAG_COMMAND_RESPONSE_EOS, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
526 payload=overall, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
527 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
528 |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
529 |
37726
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
530 def createerrorframe(stream, requestid, msg, errtype): |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
531 # TODO properly handle frame size limits. |
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
532 assert len(msg) <= DEFAULT_MAX_FRAME_SIZE |
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
533 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
534 payload = b''.join( |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
535 cborutil.streamencode( |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
536 { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
537 b'type': errtype, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
538 b'message': [{b'msg': msg}], |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
539 } |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
540 ) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
541 ) |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
542 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
543 yield stream.makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
544 requestid=requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
545 typeid=FRAME_TYPE_ERROR_RESPONSE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
546 flags=0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
547 payload=payload, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
548 ) |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
549 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
550 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
551 def createtextoutputframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
552 stream, requestid, atoms, maxframesize=DEFAULT_MAX_FRAME_SIZE |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
553 ): |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
554 """Create a text output frame to render text to people. |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
555 |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
556 ``atoms`` is a 3-tuple of (formatting string, args, labels). |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
557 |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
558 The formatting string contains ``%s`` tokens to be replaced by the |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
559 corresponding indexed entry in ``args``. ``labels`` is an iterable of |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
560 formatters to be applied at rendering time. In terms of the ``ui`` |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
561 class, each atom corresponds to a ``ui.write()``. |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
562 """ |
37319
36d17f37db91
wireproto: convert human output frames to CBOR
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37299
diff
changeset
|
563 atomdicts = [] |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
564 |
51762
ca7bde5dbafb
black: format the codebase with 23.3.0
Rapha?l Gom?s <rgomes@octobus.net>
parents:
51759
diff
changeset
|
565 for formatting, args, labels in atoms: |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
566 # TODO look for localstr, other types here? |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
567 |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
568 if not isinstance(formatting, bytes): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
569 raise ValueError(b'must use bytes formatting strings') |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
570 for arg in args: |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
571 if not isinstance(arg, bytes): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
572 raise ValueError(b'must use bytes for arguments') |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
573 for label in labels: |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
574 if not isinstance(label, bytes): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
575 raise ValueError(b'must use bytes for labels') |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
576 |
37319
36d17f37db91
wireproto: convert human output frames to CBOR
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37299
diff
changeset
|
577 # Formatting string must be ASCII. |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
578 formatting = formatting.decode('ascii', 'replace').encode('ascii') |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
579 |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
580 # Arguments must be UTF-8. |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
581 args = [a.decode('utf-8', 'replace').encode('utf-8') for a in args] |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
582 |
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
583 # Labels must be ASCII. |
43554
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43117
diff
changeset
|
584 labels = [l.decode('ascii', 'strict').encode('ascii') for l in labels] |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
585 |
37319
36d17f37db91
wireproto: convert human output frames to CBOR
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37299
diff
changeset
|
586 atom = {b'msg': formatting} |
36d17f37db91
wireproto: convert human output frames to CBOR
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37299
diff
changeset
|
587 if args: |
36d17f37db91
wireproto: convert human output frames to CBOR
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37299
diff
changeset
|
588 atom[b'args'] = args |
36d17f37db91
wireproto: convert human output frames to CBOR
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37299
diff
changeset
|
589 if labels: |
36d17f37db91
wireproto: convert human output frames to CBOR
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37299
diff
changeset
|
590 atom[b'labels'] = labels |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
591 |
37319
36d17f37db91
wireproto: convert human output frames to CBOR
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37299
diff
changeset
|
592 atomdicts.append(atom) |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
593 |
39465
36f487a332ad
wireprotoframing: use our CBOR module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37728
diff
changeset
|
594 payload = b''.join(cborutil.streamencode(atomdicts)) |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
595 |
37319
36d17f37db91
wireproto: convert human output frames to CBOR
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37299
diff
changeset
|
596 if len(payload) > maxframesize: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
597 raise ValueError(b'cannot encode data in a single frame') |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
598 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
599 yield stream.makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
600 requestid=requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
601 typeid=FRAME_TYPE_TEXT_OUTPUT, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
602 flags=0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
603 payload=payload, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
604 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
605 |
37287
3ed344546d9e
wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37087
diff
changeset
|
606 |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
49004
diff
changeset
|
607 class bufferingcommandresponseemitter: |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
608 """Helper object to emit command response frames intelligently. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
609 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
610 Raw command response data is likely emitted in chunks much smaller |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
611 than what can fit in a single frame. This class exists to buffer |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
612 chunks until enough data is available to fit in a single frame. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
613 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
614 TODO we'll need something like this when compression is supported. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
615 So it might make sense to implement this functionality at the stream |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
616 level. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
617 """ |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
618 |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
619 def __init__(self, stream, requestid, maxframesize=DEFAULT_MAX_FRAME_SIZE): |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
620 self._stream = stream |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
621 self._requestid = requestid |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
622 self._maxsize = maxframesize |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
623 self._chunks = [] |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
624 self._chunkssize = 0 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
625 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
626 def send(self, data): |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
627 """Send new data for emission. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
628 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
629 Is a generator of new frames that were derived from the new input. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
630 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
631 If the special input ``None`` is received, flushes all buffered |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
632 data to frames. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
633 """ |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
634 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
635 if data is None: |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
636 yield from self._flush() |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
637 return |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
638 |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
639 data = self._stream.encode(data) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
640 |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
641 # There is a ton of potential to do more complicated things here. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
642 # Our immediate goal is to coalesce small chunks into big frames, |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
643 # not achieve the fewest number of frames possible. So we go with |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
644 # a simple implementation: |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
645 # |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
646 # * If a chunk is too large for a frame, we flush and emit frames |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
647 # for the new chunk. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
648 # * If a chunk can be buffered without total buffered size limits |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
649 # being exceeded, we do that. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
650 # * If a chunk causes us to go over our buffering limit, we flush |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
651 # and then buffer the new chunk. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
652 |
40136
3a6d6c54bd81
wireprotov2: don't emit empty frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40135
diff
changeset
|
653 if not data: |
3a6d6c54bd81
wireprotov2: don't emit empty frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40135
diff
changeset
|
654 return |
3a6d6c54bd81
wireprotov2: don't emit empty frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40135
diff
changeset
|
655 |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
656 if len(data) > self._maxsize: |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
657 yield from self._flush() |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
658 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
659 # Now emit frames for the big chunk. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
660 offset = 0 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
661 while True: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
662 chunk = data[offset : offset + self._maxsize] |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
663 offset += len(chunk) |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
664 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
665 yield self._stream.makeframe( |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
666 self._requestid, |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
667 typeid=FRAME_TYPE_COMMAND_RESPONSE, |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
668 flags=FLAG_COMMAND_RESPONSE_CONTINUATION, |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
669 payload=chunk, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
670 encoded=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
671 ) |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
672 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
673 if offset == len(data): |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
674 return |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
675 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
676 # If we don't have enough to constitute a full frame, buffer and |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
677 # return. |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
678 if len(data) + self._chunkssize < self._maxsize: |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
679 self._chunks.append(data) |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
680 self._chunkssize += len(data) |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
681 return |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
682 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
683 # Else flush what we have and buffer the new chunk. We could do |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
684 # something more intelligent here, like break the chunk. Let's |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
685 # keep things simple for now. |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
686 yield from self._flush() |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
687 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
688 self._chunks.append(data) |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
689 self._chunkssize = len(data) |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
690 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
691 def _flush(self): |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
692 payload = b''.join(self._chunks) |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
693 assert len(payload) <= self._maxsize |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
694 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
695 self._chunks[:] = [] |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
696 self._chunkssize = 0 |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
697 |
40136
3a6d6c54bd81
wireprotov2: don't emit empty frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40135
diff
changeset
|
698 if not payload: |
3a6d6c54bd81
wireprotov2: don't emit empty frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40135
diff
changeset
|
699 return |
3a6d6c54bd81
wireprotov2: don't emit empty frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40135
diff
changeset
|
700 |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
701 yield self._stream.makeframe( |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
702 self._requestid, |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
703 typeid=FRAME_TYPE_COMMAND_RESPONSE, |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
704 flags=FLAG_COMMAND_RESPONSE_CONTINUATION, |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
705 payload=payload, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
706 encoded=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
707 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
708 |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
709 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
710 # TODO consider defining encoders/decoders using the util.compressionengine |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
711 # mechanism. |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
712 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
713 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
714 class Encoder(Protocol): |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
715 """A protocol class for the various encoder implementations.""" |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
716 |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
717 @abc.abstractmethod |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
718 def encode(self, data) -> bytes: |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
719 raise NotImplementedError |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
720 |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
721 @abc.abstractmethod |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
722 def flush(self) -> bytes: |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
723 raise NotImplementedError |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
724 |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
725 @abc.abstractmethod |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
726 def finish(self) -> bytes: |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
727 raise NotImplementedError |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
728 |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
729 |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
730 class Decoder(Protocol): |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
731 """A protocol class for the various encoder implementations.""" |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
732 |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
733 @abc.abstractmethod |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
734 def decode(self, data) -> bytes: |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
735 raise NotImplementedError |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
736 |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
737 |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
738 class identityencoder(Encoder): |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
739 """Encoder for the "identity" stream encoding profile.""" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
740 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
741 def __init__(self, ui) -> None: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
742 pass |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
743 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
744 def encode(self, data) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
745 return data |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
746 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
747 def flush(self) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
748 return b'' |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
749 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
750 def finish(self) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
751 return b'' |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
752 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
753 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
754 class identitydecoder(Decoder): |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
755 """Decoder for the "identity" stream encoding profile.""" |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
756 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
757 def __init__(self, ui, extraobjs) -> None: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
758 if extraobjs: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
759 raise error.Abort( |
43117
8ff1ecfadcd1
cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
760 _(b'identity decoder received unexpected additional values') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
761 ) |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
762 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
763 def decode(self, data) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
764 return data |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
765 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
766 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
767 class zlibencoder(Encoder): |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
768 def __init__(self, ui) -> None: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
769 import zlib |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
770 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
771 self._zlib = zlib |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
772 self._compressor = zlib.compressobj() |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
773 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
774 def encode(self, data) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
775 return self._compressor.compress(data) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
776 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
777 def flush(self) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
778 # Z_SYNC_FLUSH doesn't reset compression context, which is |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
779 # what we want. |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
780 return self._compressor.flush(self._zlib.Z_SYNC_FLUSH) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
781 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
782 def finish(self) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
783 res = self._compressor.flush(self._zlib.Z_FINISH) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
784 self._compressor = None |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
785 return res |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
786 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
787 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
788 class zlibdecoder(Decoder): |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
789 def __init__(self, ui, extraobjs) -> None: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
790 import zlib |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
791 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
792 if extraobjs: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
793 raise error.Abort( |
43117
8ff1ecfadcd1
cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
794 _(b'zlib decoder received unexpected additional values') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
795 ) |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
796 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
797 self._decompressor = zlib.decompressobj() |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
798 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
799 def decode(self, data) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
800 return self._decompressor.decompress(data) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
801 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
802 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
803 class zstdbaseencoder(Encoder): |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
804 def __init__(self, level: int) -> None: |
52129
0c260e7158e0
typing: suppress bogus pytype errors in `mercurial/wireprotoframing.py`
Matt Harbison <matt_harbison@yahoo.com>
parents:
51901
diff
changeset
|
805 from . import zstd # pytype: disable=import-error |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
806 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
807 self._zstd = zstd |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
808 cctx = zstd.ZstdCompressor(level=level) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
809 self._compressor = cctx.compressobj() |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
810 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
811 def encode(self, data) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
812 return self._compressor.compress(data) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
813 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
814 def flush(self) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
815 # COMPRESSOBJ_FLUSH_BLOCK flushes all data previously fed into the |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
816 # compressor and allows a decompressor to access all encoded data |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
817 # up to this point. |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
818 return self._compressor.flush(self._zstd.COMPRESSOBJ_FLUSH_BLOCK) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
819 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
820 def finish(self) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
821 res = self._compressor.flush(self._zstd.COMPRESSOBJ_FLUSH_FINISH) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
822 self._compressor = None |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
823 return res |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
824 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
825 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
826 class zstd8mbencoder(zstdbaseencoder): |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
827 def __init__(self, ui) -> None: |
52668
5cc8deb96b48
pyupgrade: modernize calls to superclass methods
Matt Harbison <matt_harbison@yahoo.com>
parents:
52129
diff
changeset
|
828 super().__init__(3) |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
829 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
830 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
831 class zstdbasedecoder(Decoder): |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
832 def __init__(self, maxwindowsize: int) -> None: |
52129
0c260e7158e0
typing: suppress bogus pytype errors in `mercurial/wireprotoframing.py`
Matt Harbison <matt_harbison@yahoo.com>
parents:
51901
diff
changeset
|
833 from . import zstd # pytype: disable=import-error |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
834 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
835 dctx = zstd.ZstdDecompressor(max_window_size=maxwindowsize) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
836 self._decompressor = dctx.decompressobj() |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
837 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
838 def decode(self, data) -> bytes: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
839 return self._decompressor.decompress(data) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
840 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
841 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
842 class zstd8mbdecoder(zstdbasedecoder): |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
843 def __init__(self, ui, extraobjs) -> None: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
844 if extraobjs: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
845 raise error.Abort( |
43117
8ff1ecfadcd1
cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
846 _(b'zstd8mb decoder received unexpected additional values') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
847 ) |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
848 |
52668
5cc8deb96b48
pyupgrade: modernize calls to superclass methods
Matt Harbison <matt_harbison@yahoo.com>
parents:
52129
diff
changeset
|
849 super().__init__(maxwindowsize=8 * 1048576) |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
850 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
851 |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
852 # TypeVar('EncoderT', bound=Encoder) was flagged as "not in scope" when used |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
853 # on the STREAM_ENCODERS dict below. |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
854 if typing.TYPE_CHECKING: |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
855 EncoderT = Type[identityencoder | zlibencoder | zstd8mbencoder] |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
856 DecoderT = Type[identitydecoder | zlibdecoder | zstd8mbdecoder] |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
857 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
858 # We lazily populate this to avoid excessive module imports when importing |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
859 # this module. |
52716
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
860 STREAM_ENCODERS: dict[bytes, tuple[EncoderT, DecoderT]] = {} |
1a612f9ec2c4
typing: add type annotations for wireprotoframing encoder/decoder classes
Matt Harbison <matt_harbison@yahoo.com>
parents:
52669
diff
changeset
|
861 STREAM_ENCODERS_ORDER: list[bytes] = [] |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
862 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
863 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
864 def populatestreamencoders(): |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
865 if STREAM_ENCODERS: |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
866 return |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
867 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
868 try: |
52129
0c260e7158e0
typing: suppress bogus pytype errors in `mercurial/wireprotoframing.py`
Matt Harbison <matt_harbison@yahoo.com>
parents:
51901
diff
changeset
|
869 from . import zstd # pytype: disable=import-error |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
870 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
871 zstd.__version__ |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
872 except ImportError: |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
873 zstd = None |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
874 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
875 # zstandard is fastest and is preferred. |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
876 if zstd: |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
877 STREAM_ENCODERS[b'zstd-8mb'] = (zstd8mbencoder, zstd8mbdecoder) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
878 STREAM_ENCODERS_ORDER.append(b'zstd-8mb') |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
879 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
880 STREAM_ENCODERS[b'zlib'] = (zlibencoder, zlibdecoder) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
881 STREAM_ENCODERS_ORDER.append(b'zlib') |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
882 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
883 STREAM_ENCODERS[b'identity'] = (identityencoder, identitydecoder) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
884 STREAM_ENCODERS_ORDER.append(b'identity') |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
885 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
886 |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
49004
diff
changeset
|
887 class stream: |
37287
3ed344546d9e
wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37087
diff
changeset
|
888 """Represents a logical unidirectional series of frames.""" |
3ed344546d9e
wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37087
diff
changeset
|
889 |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
890 def __init__(self, streamid, active=False): |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
891 self.streamid = streamid |
37655
b9502b5f2066
wireprotoframing: use value passed into function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37544
diff
changeset
|
892 self._active = active |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
893 |
37287
3ed344546d9e
wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37087
diff
changeset
|
894 def makeframe(self, requestid, typeid, flags, payload): |
3ed344546d9e
wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37087
diff
changeset
|
895 """Create a frame to be sent out over this stream. |
3ed344546d9e
wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37087
diff
changeset
|
896 |
3ed344546d9e
wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37087
diff
changeset
|
897 Only returns the frame instance. Does not actually send it. |
3ed344546d9e
wireproto: start to associate frame generation with a stream
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37087
diff
changeset
|
898 """ |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
899 streamflags = 0 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
900 if not self._active: |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
901 streamflags |= STREAM_FLAG_BEGIN_STREAM |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
902 self._active = True |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
903 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
904 return makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
905 requestid, self.streamid, streamflags, typeid, flags, payload |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
906 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
907 |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
908 |
40131
5d44c4d1d516
wireprotov2: establish dedicated classes for input and output streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40130
diff
changeset
|
909 class inputstream(stream): |
5d44c4d1d516
wireprotov2: establish dedicated classes for input and output streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40130
diff
changeset
|
910 """Represents a stream used for receiving data.""" |
5d44c4d1d516
wireprotov2: establish dedicated classes for input and output streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40130
diff
changeset
|
911 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
912 def __init__(self, streamid, active=False): |
52668
5cc8deb96b48
pyupgrade: modernize calls to superclass methods
Matt Harbison <matt_harbison@yahoo.com>
parents:
52129
diff
changeset
|
913 super().__init__(streamid, active=active) |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
914 self._decoder = None |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
915 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
916 def setdecoder(self, ui, name, extraobjs): |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
917 """Set the decoder for this stream. |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
918 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
919 Receives the stream profile name and any additional CBOR objects |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
920 decoded from the stream encoding settings frame payloads. |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
921 """ |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
922 if name not in STREAM_ENCODERS: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
923 raise error.Abort(_(b'unknown stream decoder: %s') % name) |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
924 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
925 self._decoder = STREAM_ENCODERS[name][1](ui, extraobjs) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
926 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
927 def decode(self, data): |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
928 # Default is identity decoder. We don't bother instantiating one |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
929 # because it is trivial. |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
930 if not self._decoder: |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
931 return data |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
932 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
933 return self._decoder.decode(data) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
934 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
935 def flush(self): |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
936 if not self._decoder: |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
937 return b'' |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
938 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
939 return self._decoder.flush() |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
940 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
941 |
40131
5d44c4d1d516
wireprotov2: establish dedicated classes for input and output streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40130
diff
changeset
|
942 class outputstream(stream): |
5d44c4d1d516
wireprotov2: establish dedicated classes for input and output streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40130
diff
changeset
|
943 """Represents a stream used for sending data.""" |
5d44c4d1d516
wireprotov2: establish dedicated classes for input and output streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40130
diff
changeset
|
944 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
945 def __init__(self, streamid, active=False): |
52668
5cc8deb96b48
pyupgrade: modernize calls to superclass methods
Matt Harbison <matt_harbison@yahoo.com>
parents:
52129
diff
changeset
|
946 super().__init__(streamid, active=active) |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
947 self.streamsettingssent = False |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
948 self._encoder = None |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
949 self._encodername = None |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
950 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
951 def setencoder(self, ui, name): |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
952 """Set the encoder for this stream. |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
953 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
954 Receives the stream profile name. |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
955 """ |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
956 if name not in STREAM_ENCODERS: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
957 raise error.Abort(_(b'unknown stream encoder: %s') % name) |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
958 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
959 self._encoder = STREAM_ENCODERS[name][0](ui) |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
960 self._encodername = name |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
961 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
962 def encode(self, data): |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
963 if not self._encoder: |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
964 return data |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
965 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
966 return self._encoder.encode(data) |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
967 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
968 def flush(self): |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
969 if not self._encoder: |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
970 return b'' |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
971 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
972 return self._encoder.flush() |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
973 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
974 def finish(self): |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
975 if not self._encoder: |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
976 return b'' |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
977 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
978 self._encoder.finish() |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
979 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
980 def makeframe(self, requestid, typeid, flags, payload, encoded=False): |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
981 """Create a frame to be sent out over this stream. |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
982 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
983 Only returns the frame instance. Does not actually send it. |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
984 """ |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
985 streamflags = 0 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
986 if not self._active: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
987 streamflags |= STREAM_FLAG_BEGIN_STREAM |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
988 self._active = True |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
989 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
990 if encoded: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
991 if not self.streamsettingssent: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
992 raise error.ProgrammingError( |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
993 b'attempting to send encoded frame without sending stream ' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
994 b'settings' |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
995 ) |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
996 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
997 streamflags |= STREAM_FLAG_ENCODING_APPLIED |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
998 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
999 if ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1000 typeid == FRAME_TYPE_STREAM_SETTINGS |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1001 and flags & FLAG_STREAM_ENCODING_SETTINGS_EOS |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1002 ): |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1003 self.streamsettingssent = True |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1004 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1005 return makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1006 requestid, self.streamid, streamflags, typeid, flags, payload |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1007 ) |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1008 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1009 def makestreamsettingsframe(self, requestid): |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1010 """Create a stream settings frame for this stream. |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1011 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1012 Returns frame data or None if no stream settings frame is needed or has |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1013 already been sent. |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1014 """ |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1015 if not self._encoder or self.streamsettingssent: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1016 return None |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1017 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1018 payload = b''.join(cborutil.streamencode(self._encodername)) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1019 return self.makeframe( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1020 requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1021 FRAME_TYPE_STREAM_SETTINGS, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1022 FLAG_STREAM_ENCODING_SETTINGS_EOS, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1023 payload, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1024 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1025 |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1026 |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1027 def ensureserverstream(stream): |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1028 if stream.streamid % 2: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1029 raise error.ProgrammingError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1030 b'server should only write to even ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1031 b'numbered streams; %d is not even' % stream.streamid |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1032 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1033 |
37063
0a6c5cc09a88
wireproto: define human output side channel frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37061
diff
changeset
|
1034 |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1035 DEFAULT_PROTOCOL_SETTINGS = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1036 b'contentencodings': [b'identity'], |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1037 } |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1038 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1039 |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
49004
diff
changeset
|
1040 class serverreactor: |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1041 """Holds state of a server handling frame-based protocol requests. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1042 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1043 This class is the "brain" of the unified frame-based protocol server |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1044 component. While the protocol is stateless from the perspective of |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1045 requests/commands, something needs to track which frames have been |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1046 received, what frames to expect, etc. This class is that thing. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1047 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1048 Instances are modeled as a state machine of sorts. Instances are also |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1049 reactionary to external events. The point of this class is to encapsulate |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1050 the state of the connection and the exchange of frames, not to perform |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1051 work. Instead, callers tell this class when something occurs, like a |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1052 frame arriving. If that activity is worthy of a follow-up action (say |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1053 *run a command*), the return value of that handler will say so. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1054 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1055 I/O and CPU intensive operations are purposefully delegated outside of |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1056 this class. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1057 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1058 Consumers are expected to tell instances when events occur. They do so by |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1059 calling the various ``on*`` methods. These methods return a 2-tuple |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1060 describing any follow-up action(s) to take. The first element is the |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1061 name of an action to perform. The second is a data structure (usually |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1062 a dict) specific to that action that contains more information. e.g. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1063 if the server wants to send frames back to the client, the data structure |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1064 will contain a reference to those frames. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1065 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1066 Valid actions that consumers can be instructed to take are: |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1067 |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
1068 sendframes |
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
1069 Indicates that frames should be sent to the client. The ``framegen`` |
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
1070 key contains a generator of frames that should be sent. The server |
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
1071 assumes that all frames are sent to the client. |
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
1072 |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1073 error |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1074 Indicates that an error occurred. Consumer should probably abort. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1075 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1076 runcommand |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1077 Indicates that the consumer should run a wire protocol command. Details |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1078 of the command to run are given in the data structure. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1079 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1080 wantframe |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1081 Indicates that nothing of interest happened and the server is waiting on |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1082 more frames from the client before anything interesting can be done. |
37059
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1083 |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1084 noop |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1085 Indicates no additional action is required. |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1086 |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1087 Known Issues |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1088 ------------ |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1089 |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1090 There are no limits to the number of partially received commands or their |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1091 size. A malicious client could stream command request data and exhaust the |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1092 server's memory. |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1093 |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1094 Partially received commands are not acted upon when end of input is |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1095 reached. Should the server error if it receives a partial request? |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1096 Should the client send a message to abort a partially transmitted request |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1097 to facilitate graceful shutdown? |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1098 |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1099 Active requests that haven't been responded to aren't tracked. This means |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1100 that if we receive a command and instruct its dispatch, another command |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1101 with its request ID can come in over the wire and there will be a race |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1102 between who responds to what. |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1103 """ |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1104 |
40130
293835e0fff7
wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40129
diff
changeset
|
1105 def __init__(self, ui, deferoutput=False): |
37059
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1106 """Construct a new server reactor. |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1107 |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1108 ``deferoutput`` can be used to indicate that no output frames should be |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1109 instructed to be sent until input has been exhausted. In this mode, |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1110 events that would normally generate output frames (such as a command |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1111 response being ready) will instead defer instructing the consumer to |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1112 send those frames. This is useful for half-duplex transports where the |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1113 sender cannot receive until all data has been transmitted. |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1114 """ |
40130
293835e0fff7
wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40129
diff
changeset
|
1115 self._ui = ui |
37059
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1116 self._deferoutput = deferoutput |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1117 self._state = b'initial' |
37289
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1118 self._nextoutgoingstreamid = 2 |
37059
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1119 self._bufferedframegens = [] |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1120 # stream id -> stream instance for all active streams from the client. |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1121 self._incomingstreams = {} |
37289
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1122 self._outgoingstreams = {} |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1123 # request id -> dict of commands that are actively being received. |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1124 self._receivingcommands = {} |
37066
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1125 # Request IDs that have been received and are actively being processed. |
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1126 # Once all output for a request has been sent, it is removed from this |
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1127 # set. |
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1128 self._activecommands = set() |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1129 |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1130 self._protocolsettingsdecoder = None |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1131 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1132 # Sender protocol settings are optional. Set implied default values. |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1133 self._sendersettings = dict(DEFAULT_PROTOCOL_SETTINGS) |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1134 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1135 populatestreamencoders() |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1136 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1137 def onframerecv(self, frame): |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1138 """Process a frame that has been received off the wire. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1139 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1140 Returns a dict with an ``action`` key that details what action, |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1141 if any, the consumer should take next. |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1142 """ |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1143 if not frame.streamid % 2: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1144 self._state = b'errored' |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1145 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1146 _(b'received frame with even numbered stream ID: %d') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1147 % frame.streamid |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1148 ) |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1149 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1150 if frame.streamid not in self._incomingstreams: |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1151 if not frame.streamflags & STREAM_FLAG_BEGIN_STREAM: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1152 self._state = b'errored' |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1153 return self._makeerrorresult( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1154 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1155 b'received frame on unknown inactive stream without ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1156 b'beginning of stream flag set' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1157 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1158 ) |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1159 |
40131
5d44c4d1d516
wireprotov2: establish dedicated classes for input and output streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40130
diff
changeset
|
1160 self._incomingstreams[frame.streamid] = inputstream(frame.streamid) |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1161 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1162 if frame.streamflags & STREAM_FLAG_ENCODING_APPLIED: |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1163 # TODO handle decoding frames |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1164 self._state = b'errored' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1165 raise error.ProgrammingError( |
43117
8ff1ecfadcd1
cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
1166 b'support for decoding stream payloads not yet implemented' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1167 ) |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1168 |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1169 if frame.streamflags & STREAM_FLAG_END_STREAM: |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1170 del self._incomingstreams[frame.streamid] |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1171 |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1172 handlers = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1173 b'initial': self._onframeinitial, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1174 b'protocol-settings-receiving': self._onframeprotocolsettings, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1175 b'idle': self._onframeidle, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1176 b'command-receiving': self._onframecommandreceiving, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1177 b'errored': self._onframeerrored, |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1178 } |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1179 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1180 meth = handlers.get(self._state) |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1181 if not meth: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1182 raise error.ProgrammingError(b'unhandled state: %s' % self._state) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1183 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1184 return meth(frame) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1185 |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1186 def oncommandresponsereadyobjects(self, stream, requestid, objs): |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1187 """Signal that objects are ready to be sent to the client. |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1188 |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1189 ``objs`` is an iterable of objects (typically a generator) that will |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1190 be encoded via CBOR and added to frames, which will be sent to the |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1191 client. |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1192 """ |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1193 ensureserverstream(stream) |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1194 |
40135
966b5f7fd30b
wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40133
diff
changeset
|
1195 # A more robust solution would be to check for objs.{next,__next__}. |
966b5f7fd30b
wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40133
diff
changeset
|
1196 if isinstance(objs, list): |
966b5f7fd30b
wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40133
diff
changeset
|
1197 objs = iter(objs) |
966b5f7fd30b
wireprotov2: remove functions for creating response frames from bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40133
diff
changeset
|
1198 |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1199 # We need to take care over exception handling. Uncaught exceptions |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1200 # when generating frames could lead to premature end of the frame |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1201 # stream and the possibility of the server or client process getting |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1202 # in a bad state. |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1203 # |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1204 # Keep in mind that if ``objs`` is a generator, advancing it could |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1205 # raise exceptions that originated in e.g. wire protocol command |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1206 # functions. That is why we differentiate between exceptions raised |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1207 # when iterating versus other exceptions that occur. |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1208 # |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1209 # In all cases, when the function finishes, the request is fully |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1210 # handled and no new frames for it should be seen. |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1211 |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1212 def sendframes(): |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1213 emitted = False |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1214 alternatelocationsent = False |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
1215 emitter = bufferingcommandresponseemitter(stream, requestid) |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1216 while True: |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1217 try: |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1218 o = next(objs) |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1219 except StopIteration: |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
1220 for frame in emitter.send(None): |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
1221 yield frame |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
1222 |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1223 if emitted: |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1224 for frame in createcommandresponseeosframes( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1225 stream, requestid |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1226 ): |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1227 yield frame |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1228 break |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1229 |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1230 except error.WireprotoCommandError as e: |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1231 for frame in createcommanderrorresponse( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1232 stream, requestid, e.message, e.messageargs |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1233 ): |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1234 yield frame |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1235 break |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1236 |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1237 except Exception as e: |
39843
bce1c1af7518
py3: cast exception to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39576
diff
changeset
|
1238 for frame in createerrorframe( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1239 stream, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1240 requestid, |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1241 b'%s' % stringutil.forcebytestr(e), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1242 errtype=b'server', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1243 ): |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1244 yield frame |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1245 |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1246 break |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1247 |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1248 try: |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1249 # Alternate location responses can only be the first and |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1250 # only object in the output stream. |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1251 if isinstance(o, wireprototypes.alternatelocationresponse): |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1252 if emitted: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1253 raise error.ProgrammingError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1254 b'alternatelocationresponse seen after initial ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1255 b'output object' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1256 ) |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1257 |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1258 frame = stream.makestreamsettingsframe(requestid) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1259 if frame: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1260 yield frame |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1261 |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1262 yield createalternatelocationresponseframe( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1263 stream, requestid, o |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1264 ) |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1265 |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1266 alternatelocationsent = True |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1267 emitted = True |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1268 continue |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1269 |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1270 if alternatelocationsent: |
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1271 raise error.ProgrammingError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1272 b'object follows alternatelocationresponse' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1273 ) |
40026
b099e6032f38
wireprotov2: server support for sending content redirects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40025
diff
changeset
|
1274 |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1275 if not emitted: |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1276 # Frame is optional. |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1277 frame = stream.makestreamsettingsframe(requestid) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1278 if frame: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1279 yield frame |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1280 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1281 # May be None if empty frame (due to encoding). |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1282 frame = createcommandresponseokframe(stream, requestid) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1283 if frame: |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1284 yield frame |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1285 |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1286 emitted = True |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1287 |
40021
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1288 # Objects emitted by command functions can be serializable |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1289 # data structures or special types. |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1290 # TODO consider extracting the content normalization to a |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1291 # standalone function, as it may be useful for e.g. cachers. |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1292 |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1293 # A pre-encoded object is sent directly to the emitter. |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1294 if isinstance(o, wireprototypes.encodedresponse): |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1295 for frame in emitter.send(o.data): |
39576
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
1296 yield frame |
84bf6ded9317
wireprotoframing: buffer emitted data to reduce frame count
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39575
diff
changeset
|
1297 |
40328
2c55716f8a1c
wireprotov2: add response type that serializes to indefinite length bytestring
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40138
diff
changeset
|
1298 elif isinstance( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1299 o, wireprototypes.indefinitebytestringresponse |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1300 ): |
40328
2c55716f8a1c
wireprotov2: add response type that serializes to indefinite length bytestring
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40138
diff
changeset
|
1301 for chunk in cborutil.streamencodebytestringfromiter( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1302 o.chunks |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1303 ): |
40328
2c55716f8a1c
wireprotov2: add response type that serializes to indefinite length bytestring
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40138
diff
changeset
|
1304 for frame in emitter.send(chunk): |
2c55716f8a1c
wireprotov2: add response type that serializes to indefinite length bytestring
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40138
diff
changeset
|
1305 yield frame |
2c55716f8a1c
wireprotov2: add response type that serializes to indefinite length bytestring
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40138
diff
changeset
|
1306 |
40021
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1307 # A regular object is CBOR encoded. |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1308 else: |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1309 for chunk in cborutil.streamencode(o): |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1310 for frame in emitter.send(chunk): |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1311 yield frame |
ed919b90acda
wireprotov2: define type to represent pre-encoded object
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39843
diff
changeset
|
1312 |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1313 except Exception as e: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1314 for frame in createerrorframe( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1315 stream, requestid, b'%s' % e, errtype=b'server' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1316 ): |
39575
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1317 yield frame |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1318 |
07b58266bce3
wireprotov2: implement commands as a generator of objects
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39503
diff
changeset
|
1319 break |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1320 |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1321 self._activecommands.remove(requestid) |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1322 |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1323 return self._handlesendframes(sendframes()) |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1324 |
37059
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1325 def oninputeof(self): |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1326 """Signals that end of input has been received. |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1327 |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1328 No more frames will be received. All pending activity should be |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1329 completed. |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1330 """ |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1331 # TODO should we do anything about in-flight commands? |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1332 |
37059
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1333 if not self._deferoutput or not self._bufferedframegens: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1334 return b'noop', {} |
37059
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1335 |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1336 # If we buffered all our responses, emit those. |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1337 def makegen(): |
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1338 for gen in self._bufferedframegens: |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
1339 yield from gen |
37059
861e9d37e56e
wireproto: buffer output frames when in half duplex mode
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37058
diff
changeset
|
1340 |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1341 return b'sendframes', { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1342 b'framegen': makegen(), |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1343 } |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
1344 |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1345 def _handlesendframes(self, framegen): |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1346 if self._deferoutput: |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1347 self._bufferedframegens.append(framegen) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1348 return b'noop', {} |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1349 else: |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1350 return b'sendframes', { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1351 b'framegen': framegen, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1352 } |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1353 |
37726
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
1354 def onservererror(self, stream, requestid, msg): |
37288
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1355 ensureserverstream(stream) |
9bfcbe4f4745
wireproto: add streams to frame-based protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37287
diff
changeset
|
1356 |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1357 def sendframes(): |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
1358 yield from createerrorframe( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1359 stream, requestid, msg, errtype=b'server' |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
1360 ) |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1361 |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1362 self._activecommands.remove(requestid) |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1363 |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1364 return self._handlesendframes(sendframes()) |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1365 |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1366 def oncommanderror(self, stream, requestid, message, args=None): |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1367 """Called when a command encountered an error before sending output.""" |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1368 ensureserverstream(stream) |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1369 |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1370 def sendframes(): |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
1371 yield from createcommanderrorresponse( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1372 stream, requestid, message, args |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
1373 ) |
37728
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1374 |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1375 self._activecommands.remove(requestid) |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1376 |
564a3eec6e63
wireprotov2: add support for more response types
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37726
diff
changeset
|
1377 return self._handlesendframes(sendframes()) |
37058
61393f888dfe
wireproto: define and implement responses in framing protocol
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37055
diff
changeset
|
1378 |
37289
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1379 def makeoutputstream(self): |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1380 """Create a stream to be used for sending data to the client. |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1381 |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1382 If this is called before protocol settings frames are received, we |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1383 don't know what stream encodings are supported by the client and |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1384 we will default to identity. |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1385 """ |
37289
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1386 streamid = self._nextoutgoingstreamid |
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1387 self._nextoutgoingstreamid += 2 |
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1388 |
40131
5d44c4d1d516
wireprotov2: establish dedicated classes for input and output streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40130
diff
changeset
|
1389 s = outputstream(streamid) |
37289
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1390 self._outgoingstreams[streamid] = s |
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1391 |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1392 # Always use the *server's* preferred encoder over the client's, |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1393 # as servers have more to lose from sub-optimal encoders being used. |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1394 for name in STREAM_ENCODERS_ORDER: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1395 if name in self._sendersettings[b'contentencodings']: |
40138
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1396 s.setencoder(self._ui, name) |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1397 break |
b5bf3dd6ec5b
wireprotov2: send content encoded frames from server
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40136
diff
changeset
|
1398 |
37289
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1399 return s |
5fadc63ac99f
wireproto: explicit API to create outgoing streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37288
diff
changeset
|
1400 |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1401 def _makeerrorresult(self, msg): |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1402 return b'error', { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1403 b'message': msg, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1404 } |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1405 |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1406 def _makeruncommandresult(self, requestid): |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1407 entry = self._receivingcommands[requestid] |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1408 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1409 if not entry[b'requestdone']: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1410 self._state = b'errored' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1411 raise error.ProgrammingError( |
43117
8ff1ecfadcd1
cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
1412 b'should not be called without requestdone set' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1413 ) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1414 |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1415 del self._receivingcommands[requestid] |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1416 |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1417 if self._receivingcommands: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1418 self._state = b'command-receiving' |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1419 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1420 self._state = b'idle' |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1421 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1422 # Decode the payloads as CBOR. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1423 entry[b'payload'].seek(0) |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1424 request = cborutil.decodeall(entry[b'payload'].getvalue())[0] |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1425 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1426 if b'name' not in request: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1427 self._state = b'errored' |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1428 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1429 _(b'command request missing "name" field') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1430 ) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1431 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1432 if b'args' not in request: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1433 request[b'args'] = {} |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1434 |
37066
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1435 assert requestid not in self._activecommands |
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1436 self._activecommands.add(requestid) |
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1437 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1438 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1439 b'runcommand', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1440 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1441 b'requestid': requestid, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1442 b'command': request[b'name'], |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1443 b'args': request[b'args'], |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1444 b'redirect': request.get(b'redirect'), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1445 b'data': entry[b'data'].getvalue() if entry[b'data'] else None, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1446 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1447 ) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1448 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1449 def _makewantframeresult(self): |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1450 return b'wantframe', { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1451 b'state': self._state, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1452 } |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1453 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1454 def _validatecommandrequestframe(self, frame): |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1455 new = frame.flags & FLAG_COMMAND_REQUEST_NEW |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1456 continuation = frame.flags & FLAG_COMMAND_REQUEST_CONTINUATION |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1457 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1458 if new and continuation: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1459 self._state = b'errored' |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1460 return self._makeerrorresult( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1461 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1462 b'received command request frame with both new and ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1463 b'continuation flags set' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1464 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1465 ) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1466 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1467 if not new and not continuation: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1468 self._state = b'errored' |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1469 return self._makeerrorresult( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1470 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1471 b'received command request frame with neither new nor ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1472 b'continuation flags set' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1473 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1474 ) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1475 |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1476 def _onframeinitial(self, frame): |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1477 # Called when we receive a frame when in the "initial" state. |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1478 if frame.typeid == FRAME_TYPE_SENDER_PROTOCOL_SETTINGS: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1479 self._state = b'protocol-settings-receiving' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1480 self._protocolsettingsdecoder = cborutil.bufferingdecoder() |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1481 return self._onframeprotocolsettings(frame) |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1482 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1483 elif frame.typeid == FRAME_TYPE_COMMAND_REQUEST: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1484 self._state = b'idle' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1485 return self._onframeidle(frame) |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1486 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1487 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1488 self._state = b'errored' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1489 return self._makeerrorresult( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1490 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1491 b'expected sender protocol settings or command request ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1492 b'frame; got %d' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1493 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1494 % frame.typeid |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1495 ) |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1496 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1497 def _onframeprotocolsettings(self, frame): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1498 assert self._state == b'protocol-settings-receiving' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1499 assert self._protocolsettingsdecoder is not None |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1500 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1501 if frame.typeid != FRAME_TYPE_SENDER_PROTOCOL_SETTINGS: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1502 self._state = b'errored' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1503 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1504 _(b'expected sender protocol settings frame; got %d') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1505 % frame.typeid |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1506 ) |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1507 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1508 more = frame.flags & FLAG_SENDER_PROTOCOL_SETTINGS_CONTINUATION |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1509 eos = frame.flags & FLAG_SENDER_PROTOCOL_SETTINGS_EOS |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1510 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1511 if more and eos: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1512 self._state = b'errored' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1513 return self._makeerrorresult( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1514 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1515 b'sender protocol settings frame cannot have both ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1516 b'continuation and end of stream flags set' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1517 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1518 ) |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1519 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1520 if not more and not eos: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1521 self._state = b'errored' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1522 return self._makeerrorresult( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1523 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1524 b'sender protocol settings frame must have continuation or ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1525 b'end of stream flag set' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1526 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1527 ) |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1528 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1529 # TODO establish limits for maximum amount of data that can be |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1530 # buffered. |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1531 try: |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1532 self._protocolsettingsdecoder.decode(frame.payload) |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1533 except Exception as e: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1534 self._state = b'errored' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1535 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1536 _( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1537 b'error decoding CBOR from sender protocol settings frame: %s' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1538 ) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1539 % stringutil.forcebytestr(e) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1540 ) |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1541 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1542 if more: |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1543 return self._makewantframeresult() |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1544 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1545 assert eos |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1546 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1547 decoded = self._protocolsettingsdecoder.getavailable() |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1548 self._protocolsettingsdecoder = None |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1549 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1550 if not decoded: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1551 self._state = b'errored' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1552 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1553 _(b'sender protocol settings frame did not contain CBOR data') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1554 ) |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1555 elif len(decoded) > 1: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1556 self._state = b'errored' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1557 return self._makeerrorresult( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1558 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1559 b'sender protocol settings frame contained multiple CBOR ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1560 b'values' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1561 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1562 ) |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1563 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1564 d = decoded[0] |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1565 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1566 if b'contentencodings' in d: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1567 self._sendersettings[b'contentencodings'] = d[b'contentencodings'] |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1568 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1569 self._state = b'idle' |
40127
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1570 |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1571 return self._makewantframeresult() |
327d40b94bed
wireprotov2: handle sender protocol settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40126
diff
changeset
|
1572 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1573 def _onframeidle(self, frame): |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1574 # The only frame type that should be received in this state is a |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1575 # command request. |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1576 if frame.typeid != FRAME_TYPE_COMMAND_REQUEST: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1577 self._state = b'errored' |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1578 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1579 _(b'expected command request frame; got %d') % frame.typeid |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1580 ) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1581 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1582 res = self._validatecommandrequestframe(frame) |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1583 if res: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1584 return res |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1585 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1586 if frame.requestid in self._receivingcommands: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1587 self._state = b'errored' |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1588 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1589 _(b'request with ID %d already received') % frame.requestid |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1590 ) |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1591 |
37066
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1592 if frame.requestid in self._activecommands: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1593 self._state = b'errored' |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1594 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1595 _(b'request with ID %d is already active') % frame.requestid |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1596 ) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1597 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1598 new = frame.flags & FLAG_COMMAND_REQUEST_NEW |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1599 moreframes = frame.flags & FLAG_COMMAND_REQUEST_MORE_FRAMES |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1600 expectingdata = frame.flags & FLAG_COMMAND_REQUEST_EXPECT_DATA |
37066
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1601 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1602 if not new: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1603 self._state = b'errored' |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1604 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1605 _(b'received command request frame without new flag set') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1606 ) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1607 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1608 payload = util.bytesio() |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1609 payload.write(frame.payload) |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1610 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1611 self._receivingcommands[frame.requestid] = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1612 b'payload': payload, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1613 b'data': None, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1614 b'requestdone': not moreframes, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1615 b'expectingdata': bool(expectingdata), |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1616 } |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1617 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1618 # This is the final frame for this request. Dispatch it. |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1619 if not moreframes and not expectingdata: |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1620 return self._makeruncommandresult(frame.requestid) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1621 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1622 assert moreframes or expectingdata |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1623 self._state = b'command-receiving' |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1624 return self._makewantframeresult() |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1625 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1626 def _onframecommandreceiving(self, frame): |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1627 if frame.typeid == FRAME_TYPE_COMMAND_REQUEST: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1628 # Process new command requests as such. |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1629 if frame.flags & FLAG_COMMAND_REQUEST_NEW: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1630 return self._onframeidle(frame) |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1631 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1632 res = self._validatecommandrequestframe(frame) |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1633 if res: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1634 return res |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1635 |
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1636 # All other frames should be related to a command that is currently |
37066
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1637 # receiving but is not active. |
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1638 if frame.requestid in self._activecommands: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1639 self._state = b'errored' |
37066
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1640 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1641 _(b'received frame for request that is still active: %d') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1642 % frame.requestid |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1643 ) |
37066
39304dd63589
wireproto: explicitly track which requests are active
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37065
diff
changeset
|
1644 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1645 if frame.requestid not in self._receivingcommands: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1646 self._state = b'errored' |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1647 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1648 _(b'received frame for request that is not receiving: %d') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1649 % frame.requestid |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1650 ) |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1651 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1652 entry = self._receivingcommands[frame.requestid] |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1653 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1654 if frame.typeid == FRAME_TYPE_COMMAND_REQUEST: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1655 moreframes = frame.flags & FLAG_COMMAND_REQUEST_MORE_FRAMES |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1656 expectingdata = bool(frame.flags & FLAG_COMMAND_REQUEST_EXPECT_DATA) |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1657 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1658 if entry[b'requestdone']: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1659 self._state = b'errored' |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1660 return self._makeerrorresult( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1661 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1662 b'received command request frame when request frames ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1663 b'were supposedly done' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1664 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1665 ) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1666 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1667 if expectingdata != entry[b'expectingdata']: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1668 self._state = b'errored' |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1669 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1670 _(b'mismatch between expect data flag and previous frame') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1671 ) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1672 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1673 entry[b'payload'].write(frame.payload) |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1674 |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1675 if not moreframes: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1676 entry[b'requestdone'] = True |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1677 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1678 if not moreframes and not expectingdata: |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1679 return self._makeruncommandresult(frame.requestid) |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1680 |
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1681 return self._makewantframeresult() |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1682 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1683 elif frame.typeid == FRAME_TYPE_COMMAND_DATA: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1684 if not entry[b'expectingdata']: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1685 self._state = b'errored' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1686 return self._makeerrorresult( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1687 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1688 b'received command data frame for request that is not ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1689 b'expecting data: %d' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1690 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1691 % frame.requestid |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1692 ) |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1693 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1694 if entry[b'data'] is None: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1695 entry[b'data'] = util.bytesio() |
37061
c5e9c3b47366
wireproto: support for receiving multiple requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37060
diff
changeset
|
1696 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1697 return self._handlecommanddataframe(frame, entry) |
37292
3d0e2cd86e05
wireproto: use CBOR for command requests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37291
diff
changeset
|
1698 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1699 self._state = b'errored' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1700 return self._makeerrorresult( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1701 _(b'received unexpected frame type: %d') % frame.typeid |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1702 ) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1703 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1704 def _handlecommanddataframe(self, frame, entry): |
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1705 assert frame.typeid == FRAME_TYPE_COMMAND_DATA |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1706 |
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1707 # TODO support streaming data instead of buffering it. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1708 entry[b'data'].write(frame.payload) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1709 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1710 if frame.flags & FLAG_COMMAND_DATA_CONTINUATION: |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1711 return self._makewantframeresult() |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1712 elif frame.flags & FLAG_COMMAND_DATA_EOS: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1713 entry[b'data'].seek(0) |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1714 return self._makeruncommandresult(frame.requestid) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1715 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1716 self._state = b'errored' |
43117
8ff1ecfadcd1
cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
1717 return self._makeerrorresult(_(b'command data frame without flags')) |
37055
8c3c47362934
wireproto: implement basic frame reading and processing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37054
diff
changeset
|
1718 |
37064
884a0c1604ad
wireproto: define attr-based classes for representing frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37063
diff
changeset
|
1719 def _onframeerrored(self, frame): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1720 return self._makeerrorresult(_(b'server already errored')) |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1721 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1722 |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
49004
diff
changeset
|
1723 class commandrequest: |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1724 """Represents a request to run a command.""" |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1725 |
40025
86b22a4cfab1
wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
1726 def __init__(self, requestid, name, args, datafh=None, redirect=None): |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1727 self.requestid = requestid |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1728 self.name = name |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1729 self.args = args |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1730 self.datafh = datafh |
40025
86b22a4cfab1
wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
1731 self.redirect = redirect |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1732 self.state = b'pending' |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1733 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1734 |
49037
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
49004
diff
changeset
|
1735 class clientreactor: |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1736 """Holds state of a client issuing frame-based protocol requests. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1737 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1738 This is like ``serverreactor`` but for client-side state. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1739 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1740 Each instance is bound to the lifetime of a connection. For persistent |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1741 connection transports using e.g. TCP sockets and speaking the raw |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1742 framing protocol, there will be a single instance for the lifetime of |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1743 the TCP socket. For transports where there are multiple discrete |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1744 interactions (say tunneled within in HTTP request), there will be a |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1745 separate instance for each distinct interaction. |
40128
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1746 |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1747 Consumers are expected to tell instances when events occur by calling |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1748 various methods. These methods return a 2-tuple describing any follow-up |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1749 action(s) to take. The first element is the name of an action to |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1750 perform. The second is a data structure (usually a dict) specific to |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1751 that action that contains more information. e.g. if the reactor wants |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1752 to send frames to the server, the data structure will contain a reference |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1753 to those frames. |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1754 |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1755 Valid actions that consumers can be instructed to take are: |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1756 |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1757 noop |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1758 Indicates no additional action is required. |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1759 |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1760 sendframes |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1761 Indicates that frames should be sent to the server. The ``framegen`` |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1762 key contains a generator of frames that should be sent. The reactor |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1763 assumes that all frames in this generator are sent to the server. |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1764 |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1765 error |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1766 Indicates that an error occurred. The ``message`` key contains an |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1767 error message describing the failure. |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1768 |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1769 responsedata |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1770 Indicates a response to a previously-issued command was received. |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1771 |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1772 The ``request`` key contains the ``commandrequest`` instance that |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1773 represents the request this data is for. |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1774 |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1775 The ``data`` key contains the decoded data from the server. |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1776 |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1777 ``expectmore`` and ``eos`` evaluate to True when more response data |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1778 is expected to follow or we're at the end of the response stream, |
080419fa4fe4
wireprotov2: document client reactor actions
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40127
diff
changeset
|
1779 respectively. |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1780 """ |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1781 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1782 def __init__( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1783 self, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1784 ui, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1785 hasmultiplesend=False, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1786 buffersends=True, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1787 clientcontentencoders=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1788 ): |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1789 """Create a new instance. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1790 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1791 ``hasmultiplesend`` indicates whether multiple sends are supported |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1792 by the transport. When True, it is possible to send commands immediately |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1793 instead of buffering until the caller signals an intent to finish a |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1794 send operation. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1795 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1796 ``buffercommands`` indicates whether sends should be buffered until the |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1797 last request has been issued. |
40133
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1798 |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1799 ``clientcontentencoders`` is an iterable of content encoders the client |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1800 will advertise to the server and that the server can use for encoding |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1801 data. If not defined, the client will not advertise content encoders |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1802 to the server. |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1803 """ |
40130
293835e0fff7
wireprotov2: pass ui into clientreactor and serverreactor
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40129
diff
changeset
|
1804 self._ui = ui |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1805 self._hasmultiplesend = hasmultiplesend |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1806 self._buffersends = buffersends |
40133
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1807 self._clientcontentencoders = clientcontentencoders |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1808 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1809 self._canissuecommands = True |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1810 self._cansend = True |
40133
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1811 self._protocolsettingssent = False |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1812 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1813 self._nextrequestid = 1 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1814 # We only support a single outgoing stream for now. |
40131
5d44c4d1d516
wireprotov2: establish dedicated classes for input and output streams
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40130
diff
changeset
|
1815 self._outgoingstream = outputstream(1) |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1816 self._pendingrequests = collections.deque() |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1817 self._activerequests = {} |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1818 self._incomingstreams = {} |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
1819 self._streamsettingsdecoders = {} |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1820 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1821 populatestreamencoders() |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1822 |
40025
86b22a4cfab1
wireprotov2: client support for advertising redirect targets
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40021
diff
changeset
|
1823 def callcommand(self, name, args, datafh=None, redirect=None): |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1824 """Request that a command be executed. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1825 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1826 Receives the command name, a dict of arguments to pass to the command, |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1827 and an optional file object containing the raw data for the command. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1828 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1829 Returns a 3-tuple of (request, action, action data). |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1830 """ |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1831 if not self._canissuecommands: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1832 raise error.ProgrammingError(b'cannot issue new commands') |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1833 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1834 requestid = self._nextrequestid |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1835 self._nextrequestid += 2 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1836 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1837 request = commandrequest( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1838 requestid, name, args, datafh=datafh, redirect=redirect |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1839 ) |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1840 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1841 if self._buffersends: |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1842 self._pendingrequests.append(request) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1843 return request, b'noop', {} |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1844 else: |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1845 if not self._cansend: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1846 raise error.ProgrammingError( |
43117
8ff1ecfadcd1
cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
1847 b'sends cannot be performed on this instance' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1848 ) |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1849 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1850 if not self._hasmultiplesend: |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1851 self._cansend = False |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1852 self._canissuecommands = False |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1853 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1854 return ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1855 request, |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1856 b'sendframes', |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1857 { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1858 b'framegen': self._makecommandframes(request), |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1859 }, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1860 ) |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1861 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1862 def flushcommands(self): |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1863 """Request that all queued commands be sent. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1864 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1865 If any commands are buffered, this will instruct the caller to send |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1866 them over the wire. If no commands are buffered it instructs the client |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1867 to no-op. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1868 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1869 If instances aren't configured for multiple sends, no new command |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1870 requests are allowed after this is called. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1871 """ |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1872 if not self._pendingrequests: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1873 return b'noop', {} |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1874 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1875 if not self._cansend: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1876 raise error.ProgrammingError( |
43117
8ff1ecfadcd1
cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents:
43106
diff
changeset
|
1877 b'sends cannot be performed on this instance' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1878 ) |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1879 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1880 # If the instance only allows sending once, mark that we have fired |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1881 # our one shot. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1882 if not self._hasmultiplesend: |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1883 self._canissuecommands = False |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1884 self._cansend = False |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1885 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1886 def makeframes(): |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1887 while self._pendingrequests: |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1888 request = self._pendingrequests.popleft() |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
1889 yield from self._makecommandframes(request) |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1890 |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1891 return b'sendframes', { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1892 b'framegen': makeframes(), |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1893 } |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1894 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1895 def _makecommandframes(self, request): |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1896 """Emit frames to issue a command request. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1897 |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1898 As a side-effect, update request accounting to reflect its changed |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1899 state. |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1900 """ |
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1901 self._activerequests[request.requestid] = request |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1902 request.state = b'sending' |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1903 |
40133
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1904 if not self._protocolsettingssent and self._clientcontentencoders: |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1905 self._protocolsettingssent = True |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1906 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1907 payload = b''.join( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1908 cborutil.streamencode( |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1909 { |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1910 b'contentencodings': self._clientcontentencoders, |
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
43554
diff
changeset
|
1911 } |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1912 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1913 ) |
40133
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1914 |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1915 yield self._outgoingstream.makeframe( |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1916 requestid=request.requestid, |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1917 typeid=FRAME_TYPE_SENDER_PROTOCOL_SETTINGS, |
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1918 flags=FLAG_SENDER_PROTOCOL_SETTINGS_EOS, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1919 payload=payload, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1920 ) |
40133
762ef19a07e3
wireprotov2: send protocol settings frame from client
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40132
diff
changeset
|
1921 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1922 res = createcommandframes( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1923 self._outgoingstream, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1924 request.requestid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1925 request.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1926 request.args, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1927 datafh=request.datafh, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1928 redirect=request.redirect, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1929 ) |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1930 |
52669
e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents:
52668
diff
changeset
|
1931 yield from res |
37543
01361be9e2dc
wireproto: introduce a reactor for client-side state
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37485
diff
changeset
|
1932 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1933 request.state = b'sent' |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1934 |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1935 def onframerecv(self, frame): |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1936 """Process a frame that has been received off the wire. |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1937 |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1938 Returns a 2-tuple of (action, meta) describing further action the |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1939 caller needs to take as a result of receiving this frame. |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1940 """ |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1941 if frame.streamid % 2: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1942 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1943 b'error', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1944 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1945 b'message': ( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1946 _(b'received frame with odd numbered stream ID: %d') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1947 % frame.streamid |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1948 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1949 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1950 ) |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1951 |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1952 if frame.streamid not in self._incomingstreams: |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1953 if not frame.streamflags & STREAM_FLAG_BEGIN_STREAM: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1954 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1955 b'error', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1956 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1957 b'message': _( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1958 b'received frame on unknown stream ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1959 b'without beginning of stream flag set' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1960 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1961 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1962 ) |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1963 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1964 self._incomingstreams[frame.streamid] = inputstream(frame.streamid) |
37656
e6870bca1f47
wireprotoframing: record when new stream is encountered
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37655
diff
changeset
|
1965 |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1966 stream = self._incomingstreams[frame.streamid] |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1967 |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1968 # If the payload is encoded, ask the stream to decode it. We |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1969 # merely substitute the decoded result into the frame payload as |
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1970 # if it had been transferred all along. |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1971 if frame.streamflags & STREAM_FLAG_ENCODING_APPLIED: |
40132
e67522413ca8
wireprotov2: define and use stream encoders
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40131
diff
changeset
|
1972 frame.payload = stream.decode(frame.payload) |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1973 |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1974 if frame.streamflags & STREAM_FLAG_END_STREAM: |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1975 del self._incomingstreams[frame.streamid] |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1976 |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
1977 if frame.typeid == FRAME_TYPE_STREAM_SETTINGS: |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
1978 return self._onstreamsettingsframe(frame) |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
1979 |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1980 if frame.requestid not in self._activerequests: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1981 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1982 b'error', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1983 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1984 b'message': ( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1985 _(b'received frame for inactive request ID: %d') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1986 % frame.requestid |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1987 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1988 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
1989 ) |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1990 |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1991 request = self._activerequests[frame.requestid] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1992 request.state = b'receiving' |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1993 |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1994 handlers = { |
37724
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
1995 FRAME_TYPE_COMMAND_RESPONSE: self._oncommandresponseframe, |
37726
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
1996 FRAME_TYPE_ERROR_RESPONSE: self._onerrorresponseframe, |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1997 } |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1998 |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
1999 meth = handlers.get(frame.typeid) |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
2000 if not meth: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2001 raise error.ProgrammingError( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2002 b'unhandled frame type: %d' % frame.typeid |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2003 ) |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
2004 |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
2005 return meth(request, frame) |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
2006 |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2007 def _onstreamsettingsframe(self, frame): |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2008 assert frame.typeid == FRAME_TYPE_STREAM_SETTINGS |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2009 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2010 more = frame.flags & FLAG_STREAM_ENCODING_SETTINGS_CONTINUATION |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2011 eos = frame.flags & FLAG_STREAM_ENCODING_SETTINGS_EOS |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2012 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2013 if more and eos: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2014 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2015 b'error', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2016 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2017 b'message': ( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2018 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2019 b'stream encoding settings frame cannot have both ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2020 b'continuation and end of stream flags set' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2021 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2022 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2023 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2024 ) |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2025 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2026 if not more and not eos: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2027 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2028 b'error', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2029 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2030 b'message': _( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2031 b'stream encoding settings frame must have ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2032 b'continuation or end of stream flag set' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2033 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2034 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2035 ) |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2036 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2037 if frame.streamid not in self._streamsettingsdecoders: |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2038 decoder = cborutil.bufferingdecoder() |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2039 self._streamsettingsdecoders[frame.streamid] = decoder |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2040 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2041 decoder = self._streamsettingsdecoders[frame.streamid] |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2042 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2043 try: |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2044 decoder.decode(frame.payload) |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2045 except Exception as e: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2046 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2047 b'error', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2048 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2049 b'message': ( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2050 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2051 b'error decoding CBOR from stream encoding ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2052 b'settings frame: %s' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2053 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2054 % stringutil.forcebytestr(e) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2055 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2056 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2057 ) |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2058 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2059 if more: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2060 return b'noop', {} |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2061 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2062 assert eos |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2063 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2064 decoded = decoder.getavailable() |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2065 del self._streamsettingsdecoders[frame.streamid] |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2066 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2067 if not decoded: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2068 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2069 b'error', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2070 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2071 b'message': _( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2072 b'stream encoding settings frame did not contain ' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2073 b'CBOR data' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2074 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2075 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2076 ) |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2077 |
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2078 try: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2079 self._incomingstreams[frame.streamid].setdecoder( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2080 self._ui, decoded[0], decoded[1:] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2081 ) |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2082 except Exception as e: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2083 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2084 b'error', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2085 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2086 b'message': ( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2087 _(b'error setting stream decoder: %s') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2088 % stringutil.forcebytestr(e) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2089 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2090 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2091 ) |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2092 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2093 return b'noop', {} |
40129
57782791b7e9
wireprotov2: handle stream encoding settings frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40128
diff
changeset
|
2094 |
37724
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
2095 def _oncommandresponseframe(self, request, frame): |
deff7cf7eefd
wireprotov2: change frame type and name for command response
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37723
diff
changeset
|
2096 if frame.flags & FLAG_COMMAND_RESPONSE_EOS: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2097 request.state = b'received' |
37544
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
2098 del self._activerequests[request.requestid] |
55b5ba8d4e68
wireproto: client reactor support for receiving frames
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37543
diff
changeset
|
2099 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2100 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2101 b'responsedata', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2102 { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2103 b'request': request, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2104 b'expectmore': frame.flags & FLAG_COMMAND_RESPONSE_CONTINUATION, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2105 b'eos': frame.flags & FLAG_COMMAND_RESPONSE_EOS, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2106 b'data': frame.payload, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2107 }, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2108 ) |
37726
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
2109 |
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
2110 def _onerrorresponseframe(self, request, frame): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2111 request.state = b'errored' |
37726
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
2112 del self._activerequests[request.requestid] |
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
2113 |
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
2114 # The payload should be a CBOR map. |
39465
36f487a332ad
wireprotoframing: use our CBOR module
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37728
diff
changeset
|
2115 m = cborutil.decodeall(frame.payload)[0] |
37726
0c184ca594bb
wireprotov2: change behavior of error frame
Gregory Szorc <gregory.szorc@gmail.com>
parents:
37725
diff
changeset
|
2116 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2117 return ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2118 b'error', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2119 { |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2120 b'request': request, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2121 b'type': m[b'type'], |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2122 b'message': m[b'message'], |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
2123 }, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40328
diff
changeset
|
2124 ) |