• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import logging
16import messages_pb2
17import struct
18
19import h2
20import h2.connection
21import twisted
22import twisted.internet
23import twisted.internet.protocol
24
25_READ_CHUNK_SIZE = 16384
26_GRPC_HEADER_SIZE = 5
27_MIN_SETTINGS_MAX_FRAME_SIZE = 16384
28
29class H2ProtocolBaseServer(twisted.internet.protocol.Protocol):
30  def __init__(self):
31    self._conn = h2.connection.H2Connection(client_side=False)
32    self._recv_buffer = {}
33    self._handlers = {}
34    self._handlers['ConnectionMade'] = self.on_connection_made_default
35    self._handlers['DataReceived'] = self.on_data_received_default
36    self._handlers['WindowUpdated'] = self.on_window_update_default
37    self._handlers['RequestReceived'] = self.on_request_received_default
38    self._handlers['SendDone'] = self.on_send_done_default
39    self._handlers['ConnectionLost'] = self.on_connection_lost
40    self._handlers['PingAcknowledged'] = self.on_ping_acknowledged_default
41    self._stream_status = {}
42    self._send_remaining = {}
43    self._outstanding_pings = 0
44
45  def set_handlers(self, handlers):
46    self._handlers = handlers
47
48  def connectionMade(self):
49    self._handlers['ConnectionMade']()
50
51  def connectionLost(self, reason):
52    self._handlers['ConnectionLost'](reason)
53
54  def on_connection_made_default(self):
55    logging.info('Connection Made')
56    self._conn.initiate_connection()
57    self.transport.setTcpNoDelay(True)
58    self.transport.write(self._conn.data_to_send())
59
60  def on_connection_lost(self, reason):
61    logging.info('Disconnected %s' % reason)
62
63  def dataReceived(self, data):
64    try:
65      events = self._conn.receive_data(data)
66    except h2.exceptions.ProtocolError:
67      # this try/except block catches exceptions due to race between sending
68      # GOAWAY and processing a response in flight.
69      return
70    if self._conn.data_to_send:
71      self.transport.write(self._conn.data_to_send())
72    for event in events:
73      if isinstance(event, h2.events.RequestReceived) and self._handlers.has_key('RequestReceived'):
74        logging.info('RequestReceived Event for stream: %d' % event.stream_id)
75        self._handlers['RequestReceived'](event)
76      elif isinstance(event, h2.events.DataReceived) and self._handlers.has_key('DataReceived'):
77        logging.info('DataReceived Event for stream: %d' % event.stream_id)
78        self._handlers['DataReceived'](event)
79      elif isinstance(event, h2.events.WindowUpdated) and self._handlers.has_key('WindowUpdated'):
80        logging.info('WindowUpdated Event for stream: %d' % event.stream_id)
81        self._handlers['WindowUpdated'](event)
82      elif isinstance(event, h2.events.PingAcknowledged) and self._handlers.has_key('PingAcknowledged'):
83        logging.info('PingAcknowledged Event')
84        self._handlers['PingAcknowledged'](event)
85    self.transport.write(self._conn.data_to_send())
86
87  def on_ping_acknowledged_default(self, event):
88    logging.info('ping acknowledged')
89    self._outstanding_pings -= 1
90
91  def on_data_received_default(self, event):
92    self._conn.acknowledge_received_data(len(event.data), event.stream_id)
93    self._recv_buffer[event.stream_id] += event.data
94
95  def on_request_received_default(self, event):
96    self._recv_buffer[event.stream_id] = ''
97    self._stream_id = event.stream_id
98    self._stream_status[event.stream_id] = True
99    self._conn.send_headers(
100      stream_id=event.stream_id,
101      headers=[
102          (':status', '200'),
103          ('content-type', 'application/grpc'),
104          ('grpc-encoding', 'identity'),
105          ('grpc-accept-encoding', 'identity,deflate,gzip'),
106      ],
107    )
108    self.transport.write(self._conn.data_to_send())
109
110  def on_window_update_default(self, _, pad_length=None, read_chunk_size=_READ_CHUNK_SIZE):
111    # try to resume sending on all active streams (update might be for connection)
112    for stream_id in self._send_remaining:
113      self.default_send(stream_id, pad_length=pad_length, read_chunk_size=read_chunk_size)
114
115  def send_reset_stream(self):
116    self._conn.reset_stream(self._stream_id)
117    self.transport.write(self._conn.data_to_send())
118
119  def setup_send(self, data_to_send, stream_id, pad_length=None, read_chunk_size=_READ_CHUNK_SIZE):
120    logging.info('Setting up data to send for stream_id: %d' % stream_id)
121    self._send_remaining[stream_id] = len(data_to_send)
122    self._send_offset = 0
123    self._data_to_send = data_to_send
124    self.default_send(stream_id, pad_length=pad_length, read_chunk_size=read_chunk_size)
125
126  def default_send(self, stream_id, pad_length=None, read_chunk_size=_READ_CHUNK_SIZE):
127    if not self._send_remaining.has_key(stream_id):
128      # not setup to send data yet
129      return
130
131    while self._send_remaining[stream_id] > 0:
132      lfcw = self._conn.local_flow_control_window(stream_id)
133      padding_bytes = pad_length + 1 if pad_length is not None else 0
134      if lfcw - padding_bytes <= 0:
135        logging.info('Stream %d. lfcw: %d. padding bytes: %d. not enough quota yet' % (stream_id, lfcw, padding_bytes))
136        break
137      chunk_size = min(lfcw - padding_bytes, read_chunk_size)
138      bytes_to_send = min(chunk_size, self._send_remaining[stream_id])
139      logging.info('flow_control_window = %d. sending [%d:%d] stream_id %d. includes %d total padding bytes' %
140                    (lfcw, self._send_offset, self._send_offset + bytes_to_send + padding_bytes,
141                    stream_id, padding_bytes))
142      # The receiver might allow sending frames larger than the http2 minimum
143      # max frame size (16384), but this test should never send more than 16384
144      # for simplicity (which is always legal).
145      if bytes_to_send + padding_bytes > _MIN_SETTINGS_MAX_FRAME_SIZE:
146        raise ValueError("overload: sending %d" % (bytes_to_send + padding_bytes))
147      data = self._data_to_send[self._send_offset : self._send_offset + bytes_to_send]
148      try:
149        self._conn.send_data(stream_id, data, end_stream=False, pad_length=pad_length)
150      except h2.exceptions.ProtocolError:
151        logging.info('Stream %d is closed' % stream_id)
152        break
153      self._send_remaining[stream_id] -= bytes_to_send
154      self._send_offset += bytes_to_send
155      if self._send_remaining[stream_id] == 0:
156        self._handlers['SendDone'](stream_id)
157
158  def default_ping(self):
159    logging.info('sending ping')
160    self._outstanding_pings += 1
161    self._conn.ping(b'\x00'*8)
162    self.transport.write(self._conn.data_to_send())
163
164  def on_send_done_default(self, stream_id):
165    if self._stream_status[stream_id]:
166      self._stream_status[stream_id] = False
167      self.default_send_trailer(stream_id)
168    else:
169      logging.error('Stream %d is already closed' % stream_id)
170
171  def default_send_trailer(self, stream_id):
172    logging.info('Sending trailer for stream id %d' % stream_id)
173    self._conn.send_headers(stream_id,
174      headers=[ ('grpc-status', '0') ],
175      end_stream=True
176    )
177    self.transport.write(self._conn.data_to_send())
178
179  @staticmethod
180  def default_response_data(response_size):
181    sresp = messages_pb2.SimpleResponse()
182    sresp.payload.body = b'\x00'*response_size
183    serialized_resp_proto = sresp.SerializeToString()
184    response_data = b'\x00' + struct.pack('i', len(serialized_resp_proto))[::-1] + serialized_resp_proto
185    return response_data
186
187  def parse_received_data(self, stream_id):
188    """ returns a grpc framed string of bytes containing response proto of the size
189    asked in request """
190    recv_buffer = self._recv_buffer[stream_id]
191    grpc_msg_size = struct.unpack('i',recv_buffer[1:5][::-1])[0]
192    if len(recv_buffer) != _GRPC_HEADER_SIZE + grpc_msg_size:
193      return None
194    req_proto_str = recv_buffer[5:5+grpc_msg_size]
195    sr = messages_pb2.SimpleRequest()
196    sr.ParseFromString(req_proto_str)
197    logging.info('Parsed simple request for stream %d' % stream_id)
198    return sr
199