def _OnData(self, unused_websocket_app, binary_data, opcode, unused_finished): """Receive a single message from the server. Args: binary_data: str binary data of proto opcode: int signal value for whether data is binary or string unused_finished: bool whether this is the final message in a multi-part sequence """ if (opcode not in (websocket.ABNF.OPCODE_CONT, websocket.ABNF.OPCODE_BINARY)): log.warning('Unexpected WebSocket opcode [%r].', opcode) return subprotocol_tag, data = utils.ExtractSubprotocolData(binary_data) if data is not None: if subprotocol_tag == utils.SUBPROTOCOL_TAG_DATA: try: self._data_handler_callback(data) except (EnvironmentError, socket.error): log.exception('Error from WebSocket data handler callback') self.Close() raise elif subprotocol_tag == utils.SUBPROTOCOL_TAG_CONNECT_SUCCESS_SID: self._connection_sid = data else: log.warning( 'Unexpected subprotocol type [%r] with data length [%d].', subprotocol_tag, len(data))
def _HandleSubprotocolData(self, binary_data): """Handle Subprotocol DATA Frame.""" if not self._IsConnected(): raise UnexpectedWebSocketDataError( 'Received DATA before connected.') data, bytes_left = utils.ExtractSubprotocolData(binary_data) self._total_bytes_received += len(data) self._data_handler_callback(data) if bytes_left: log.warning('Discarding %d extra bytes after processing DATA', len(bytes_left))
def _HandleSubprotocolData(self, binary_data): """Handle Subprotocol DATA Frame.""" if not self._HasConnected(): self._StopConnectionAsync() raise SubprotocolEarlyDataError('Received DATA before connected.') data, bytes_left = utils.ExtractSubprotocolData(binary_data) self._total_bytes_received += len(data) try: self._data_handler_callback(data) except: # pylint: disable=bare-except self._StopConnectionAsync() raise if bytes_left: log.debug('Discarding [%d] extra bytes after processing DATA', len(bytes_left))
def testExtractSubprotocolDataIncompleteData(self, binary_data, expected_error): with self.AssertRaisesExceptionMatches(expected_error, ''): utils.ExtractSubprotocolData(binary_data)
def testExtractSubprotocolData(self, binary_data, expected_data, expected_bytes_left): data, bytes_left = utils.ExtractSubprotocolData(binary_data) self.assertEqual(data, expected_data) self.assertEqual(bytes_left, expected_bytes_left)