22 import twisted.internet
23 import twisted.internet.protocol
25 _READ_CHUNK_SIZE = 16384
27 _MIN_SETTINGS_MAX_FRAME_SIZE = 16384
33 self.
_conn = h2.connection.H2Connection(client_side=
False)
57 logging.info(
'Connection Made')
58 self.
_conn.initiate_connection()
59 self.transport.setTcpNoDelay(
True)
60 self.transport.
write(self.
_conn.data_to_send())
63 logging.info(
'Disconnected %s' % reason)
67 events = self.
_conn.receive_data(data)
68 except h2.exceptions.ProtocolError:
72 if self.
_conn.data_to_send:
73 self.transport.
write(self.
_conn.data_to_send())
75 if isinstance(event, h2.events.RequestReceived
76 )
and self.
_handlers.has_key(
'RequestReceived'):
77 logging.info(
'RequestReceived Event for stream: %d' %
80 elif isinstance(event, h2.events.DataReceived
81 )
and self.
_handlers.has_key(
'DataReceived'):
82 logging.info(
'DataReceived Event for stream: %d' %
85 elif isinstance(event, h2.events.WindowUpdated
86 )
and self.
_handlers.has_key(
'WindowUpdated'):
87 logging.info(
'WindowUpdated Event for stream: %d' %
90 elif isinstance(event, h2.events.PingAcknowledged
91 )
and self.
_handlers.has_key(
'PingAcknowledged'):
92 logging.info(
'PingAcknowledged Event')
94 self.transport.
write(self.
_conn.data_to_send())
97 logging.info(
'ping acknowledged')
101 self.
_conn.acknowledge_received_data(
len(event.data), event.stream_id)
108 self.
_conn.send_headers(
109 stream_id=event.stream_id,
112 (
'content-type',
'application/grpc'),
113 (
'grpc-encoding',
'identity'),
114 (
'grpc-accept-encoding',
'identity,deflate,gzip'),
117 self.transport.
write(self.
_conn.data_to_send())
122 read_chunk_size=_READ_CHUNK_SIZE):
126 pad_length=pad_length,
127 read_chunk_size=read_chunk_size)
131 self.transport.
write(self.
_conn.data_to_send())
137 read_chunk_size=_READ_CHUNK_SIZE):
138 logging.info(
'Setting up data to send for stream_id: %d' % stream_id)
143 pad_length=pad_length,
144 read_chunk_size=read_chunk_size)
149 read_chunk_size=_READ_CHUNK_SIZE):
155 lfcw = self.
_conn.local_flow_control_window(stream_id)
156 padding_bytes = pad_length + 1
if pad_length
is not None else 0
157 if lfcw - padding_bytes <= 0:
159 'Stream %d. lfcw: %d. padding bytes: %d. not enough quota yet'
160 % (stream_id, lfcw, padding_bytes))
162 chunk_size =
min(lfcw - padding_bytes, read_chunk_size)
165 'flow_control_window = %d. sending [%d:%d] stream_id %d. includes %d total padding bytes'
167 padding_bytes, stream_id, padding_bytes))
171 if bytes_to_send + padding_bytes > _MIN_SETTINGS_MAX_FRAME_SIZE:
172 raise ValueError(
"overload: sending %d" %
173 (bytes_to_send + padding_bytes))
180 pad_length=pad_length)
181 except h2.exceptions.ProtocolError:
182 logging.info(
'Stream %d is closed' % stream_id)
190 logging.info(
'sending ping')
193 self.transport.
write(self.
_conn.data_to_send())
200 logging.error(
'Stream %d is already closed' % stream_id)
203 logging.info(
'Sending trailer for stream id %d' % stream_id)
204 self.
_conn.send_headers(stream_id,
205 headers=[(
'grpc-status',
'0')],
207 self.transport.
write(self.
_conn.data_to_send())
212 sresp.payload.body = b
'\x00' * response_size
213 serialized_resp_proto = sresp.SerializeToString()
214 response_data = b
'\x00' + struct.pack(
215 'i',
len(serialized_resp_proto))[::-1] + serialized_resp_proto
219 """ returns a grpc framed string of bytes containing response proto of the size
222 grpc_msg_size = struct.unpack(
'i', recv_buffer[1:5][::-1])[0]
223 if len(recv_buffer) != _GRPC_HEADER_SIZE + grpc_msg_size:
225 req_proto_str = recv_buffer[5:5 + grpc_msg_size]
227 sr.ParseFromString(req_proto_str)
228 logging.info(
'Parsed simple request for stream %d' % stream_id)