|
|
|
@ -1,19 +1,19 @@ |
|
|
|
|
import struct |
|
|
|
|
import messages_pb2 |
|
|
|
|
import logging |
|
|
|
|
import messages_pb2 |
|
|
|
|
import struct |
|
|
|
|
|
|
|
|
|
from twisted.internet.protocol import Protocol |
|
|
|
|
from twisted.internet import reactor |
|
|
|
|
from h2.connection import H2Connection |
|
|
|
|
from h2.events import RequestReceived, DataReceived, WindowUpdated, RemoteSettingsChanged, PingAcknowledged |
|
|
|
|
from h2.exceptions import ProtocolError |
|
|
|
|
import h2 |
|
|
|
|
import h2.connection |
|
|
|
|
import twisted |
|
|
|
|
import twisted.internet |
|
|
|
|
import twisted.internet.protocol |
|
|
|
|
|
|
|
|
|
READ_CHUNK_SIZE = 16384 |
|
|
|
|
GRPC_HEADER_SIZE = 5 |
|
|
|
|
_READ_CHUNK_SIZE = 16384 |
|
|
|
|
_GRPC_HEADER_SIZE = 5 |
|
|
|
|
|
|
|
|
|
class H2ProtocolBaseServer(Protocol): |
|
|
|
|
class H2ProtocolBaseServer(twisted.internet.protocol.Protocol): |
|
|
|
|
def __init__(self): |
|
|
|
|
self._conn = H2Connection(client_side=False) |
|
|
|
|
self._conn = h2.connection.H2Connection(client_side=False) |
|
|
|
|
self._recv_buffer = {} |
|
|
|
|
self._handlers = {} |
|
|
|
|
self._handlers['ConnectionMade'] = self.on_connection_made_default |
|
|
|
@ -43,34 +43,35 @@ class H2ProtocolBaseServer(Protocol): |
|
|
|
|
self.transport.write(self._conn.data_to_send()) |
|
|
|
|
|
|
|
|
|
def on_connection_lost(self, reason): |
|
|
|
|
logging.info('Disconnected %s'%reason) |
|
|
|
|
reactor.callFromThread(reactor.stop) |
|
|
|
|
logging.info('Disconnected %s' % reason) |
|
|
|
|
twisted.internet.reactor.callFromThread(twisted.internet.reactor.stop) |
|
|
|
|
|
|
|
|
|
def dataReceived(self, data): |
|
|
|
|
try: |
|
|
|
|
events = self._conn.receive_data(data) |
|
|
|
|
except ProtocolError: |
|
|
|
|
except h2.exceptions.ProtocolError: |
|
|
|
|
# this try/except block catches exceptions due to race between sending |
|
|
|
|
# GOAWAY and processing a response in flight. |
|
|
|
|
return |
|
|
|
|
if self._conn.data_to_send: |
|
|
|
|
self.transport.write(self._conn.data_to_send()) |
|
|
|
|
for event in events: |
|
|
|
|
if isinstance(event, RequestReceived) and self._handlers.has_key('RequestReceived'): |
|
|
|
|
logging.info('RequestReceived Event for stream: %d'%event.stream_id) |
|
|
|
|
if isinstance(event, h2.events.RequestReceived) and self._handlers.has_key('RequestReceived'): |
|
|
|
|
logging.info('RequestReceived Event for stream: %d' % event.stream_id) |
|
|
|
|
self._handlers['RequestReceived'](event) |
|
|
|
|
elif isinstance(event, DataReceived) and self._handlers.has_key('DataReceived'): |
|
|
|
|
logging.info('DataReceived Event for stream: %d'%event.stream_id) |
|
|
|
|
elif isinstance(event, h2.events.DataReceived) and self._handlers.has_key('DataReceived'): |
|
|
|
|
logging.info('DataReceived Event for stream: %d' % event.stream_id) |
|
|
|
|
self._handlers['DataReceived'](event) |
|
|
|
|
elif isinstance(event, WindowUpdated) and self._handlers.has_key('WindowUpdated'): |
|
|
|
|
logging.info('WindowUpdated Event for stream: %d'%event.stream_id) |
|
|
|
|
elif isinstance(event, h2.events.WindowUpdated) and self._handlers.has_key('WindowUpdated'): |
|
|
|
|
logging.info('WindowUpdated Event for stream: %d' % event.stream_id) |
|
|
|
|
self._handlers['WindowUpdated'](event) |
|
|
|
|
elif isinstance(event, PingAcknowledged) and self._handlers.has_key('PingAcknowledged'): |
|
|
|
|
elif isinstance(event, h2.events.PingAcknowledged) and self._handlers.has_key('PingAcknowledged'): |
|
|
|
|
logging.info('PingAcknowledged Event') |
|
|
|
|
self._handlers['PingAcknowledged'](event) |
|
|
|
|
self.transport.write(self._conn.data_to_send()) |
|
|
|
|
|
|
|
|
|
def on_ping_acknowledged_default(self, event): |
|
|
|
|
logging.info('ping acknowledged') |
|
|
|
|
self._outstanding_pings -= 1 |
|
|
|
|
|
|
|
|
|
def on_data_received_default(self, event): |
|
|
|
@ -101,7 +102,7 @@ class H2ProtocolBaseServer(Protocol): |
|
|
|
|
self.transport.write(self._conn.data_to_send()) |
|
|
|
|
|
|
|
|
|
def setup_send(self, data_to_send, stream_id): |
|
|
|
|
logging.info('Setting up data to send for stream_id: %d'%stream_id) |
|
|
|
|
logging.info('Setting up data to send for stream_id: %d' % stream_id) |
|
|
|
|
self._send_remaining[stream_id] = len(data_to_send) |
|
|
|
|
self._send_offset = 0 |
|
|
|
|
self._data_to_send = data_to_send |
|
|
|
@ -116,16 +117,16 @@ class H2ProtocolBaseServer(Protocol): |
|
|
|
|
lfcw = self._conn.local_flow_control_window(stream_id) |
|
|
|
|
if lfcw == 0: |
|
|
|
|
break |
|
|
|
|
chunk_size = min(lfcw, READ_CHUNK_SIZE) |
|
|
|
|
chunk_size = min(lfcw, _READ_CHUNK_SIZE) |
|
|
|
|
bytes_to_send = min(chunk_size, self._send_remaining[stream_id]) |
|
|
|
|
logging.info('flow_control_window = %d. sending [%d:%d] stream_id %d'% |
|
|
|
|
logging.info('flow_control_window = %d. sending [%d:%d] stream_id %d' % |
|
|
|
|
(lfcw, self._send_offset, self._send_offset + bytes_to_send, |
|
|
|
|
stream_id)) |
|
|
|
|
data = self._data_to_send[self._send_offset : self._send_offset + bytes_to_send] |
|
|
|
|
try: |
|
|
|
|
self._conn.send_data(stream_id, data, False) |
|
|
|
|
except ProtocolError: |
|
|
|
|
logging.info('Stream %d is closed'%stream_id) |
|
|
|
|
except h2.exceptions.ProtocolError: |
|
|
|
|
logging.info('Stream %d is closed' % stream_id) |
|
|
|
|
break |
|
|
|
|
self._send_remaining[stream_id] -= bytes_to_send |
|
|
|
|
self._send_offset += bytes_to_send |
|
|
|
@ -133,6 +134,7 @@ class H2ProtocolBaseServer(Protocol): |
|
|
|
|
self._handlers['SendDone'](stream_id) |
|
|
|
|
|
|
|
|
|
def default_ping(self): |
|
|
|
|
logging.info('sending ping') |
|
|
|
|
self._outstanding_pings += 1 |
|
|
|
|
self._conn.ping(b'\x00'*8) |
|
|
|
|
self.transport.write(self._conn.data_to_send()) |
|
|
|
@ -141,9 +143,11 @@ class H2ProtocolBaseServer(Protocol): |
|
|
|
|
if self._stream_status[stream_id]: |
|
|
|
|
self._stream_status[stream_id] = False |
|
|
|
|
self.default_send_trailer(stream_id) |
|
|
|
|
else: |
|
|
|
|
logging.error('Stream %d is already closed' % stream_id) |
|
|
|
|
|
|
|
|
|
def default_send_trailer(self, stream_id): |
|
|
|
|
logging.info('Sending trailer for stream id %d'%stream_id) |
|
|
|
|
logging.info('Sending trailer for stream id %d' % stream_id) |
|
|
|
|
self._conn.send_headers(stream_id, |
|
|
|
|
headers=[ ('grpc-status', '0') ], |
|
|
|
|
end_stream=True |
|
|
|
@ -159,15 +163,14 @@ class H2ProtocolBaseServer(Protocol): |
|
|
|
|
return response_data |
|
|
|
|
|
|
|
|
|
def parse_received_data(self, stream_id): |
|
|
|
|
recv_buffer = self._recv_buffer[stream_id] |
|
|
|
|
""" returns a grpc framed string of bytes containing response proto of the size |
|
|
|
|
asked in request """ |
|
|
|
|
recv_buffer = self._recv_buffer[stream_id] |
|
|
|
|
grpc_msg_size = struct.unpack('i',recv_buffer[1:5][::-1])[0] |
|
|
|
|
if len(recv_buffer) != GRPC_HEADER_SIZE + grpc_msg_size: |
|
|
|
|
#logging.error('not enough data to decode req proto. size = %d, needed %s'%(len(recv_buffer), 5+grpc_msg_size)) |
|
|
|
|
if len(recv_buffer) != _GRPC_HEADER_SIZE + grpc_msg_size: |
|
|
|
|
return None |
|
|
|
|
req_proto_str = recv_buffer[5:5+grpc_msg_size] |
|
|
|
|
sr = messages_pb2.SimpleRequest() |
|
|
|
|
sr.ParseFromString(req_proto_str) |
|
|
|
|
logging.info('Parsed request for stream %d: response_size=%s'%(stream_id, sr.response_size)) |
|
|
|
|
logging.info('Parsed request for stream %d: response_size=%s' % (stream_id, sr.response_size)) |
|
|
|
|
return sr |
|
|
|
|