Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# data is a dict that will be converted to json
# wait_for_response only works if we have a request id
# If channel is not open yet, connect to it.
self._ensure_channel_connected(destination_id)
request_id = None
if not no_add_request_id:
request_id = self._gen_request_id()
data[REQUEST_ID] = request_id
if inc_session_id:
data[SESSION_ID] = self.session_id
# pylint: disable=no-member
msg = cast_channel_pb2.CastMessage()
msg.protocol_version = msg.CASTV2_1_0
msg.source_id = self.source_id
msg.destination_id = destination_id
msg.payload_type = cast_channel_pb2.CastMessage.STRING
msg.namespace = namespace
msg.payload_utf8 = _json_to_payload(data)
# prepend message with Big-Endian 4 byte payload size
be_size = pack(">I", msg.ByteSize())
# Log all messages except heartbeat
if msg.namespace != NS_HEARTBEAT:
self.logger.debug(
"[%s:%s] Sending: %s",
self.fn or self.host,
def _read_message(self):
""" Reads a message from the socket and converts it to a message. """
# first 4 bytes is Big-Endian payload length
payload_info = self._read_bytes_from_socket(4)
read_len = unpack(">I", payload_info)[0]
# now read the payload
payload = self._read_bytes_from_socket(read_len)
# pylint: disable=no-member
message = cast_channel_pb2.CastMessage()
message.ParseFromString(payload)
return message
request_id = None
if not no_add_request_id:
request_id = self._gen_request_id()
data[REQUEST_ID] = request_id
if inc_session_id:
data[SESSION_ID] = self.session_id
# pylint: disable=no-member
msg = cast_channel_pb2.CastMessage()
msg.protocol_version = msg.CASTV2_1_0
msg.source_id = self.source_id
msg.destination_id = destination_id
msg.payload_type = cast_channel_pb2.CastMessage.STRING
msg.namespace = namespace
msg.payload_utf8 = _json_to_payload(data)
# prepend message with Big-Endian 4 byte payload size
be_size = pack(">I", msg.ByteSize())
# Log all messages except heartbeat
if msg.namespace != NS_HEARTBEAT:
self.logger.debug(
"[%s:%s] Sending: %s",
self.fn or self.host,
self.port,
_message_to_string(msg, data),
)
if not force and self.stop.is_set():