def handle_open_msg(self, channel: Channel, session: FBDPSession, msg: FBDPMessage) -> None: """Process OPEN message received from client. Arguments: channel: Channel that received the message. session: Session instance. msg: Received message. Note: All exceptions are handled by `handle_exception`. """ if session.pipe is not None: # Client already attached to data pipe, OPEN out of band raise StopError("Out of band OPEN message", code=ErrorCode.PROTOCOL_VIOLATION) socket = PipeSocket(msg.data_frame.pipe_socket) session.pipe = msg.data_frame.data_pipe session.socket = socket session.data_format = msg.data_frame.data_format session.params.update(struct2dict(msg.data_frame.parameters)) self.on_accept_client(channel, session) self._init_new_batch(channel, session) channel.on_output_ready = self._on_output_ready
def send_open(self, channel: Channel, session: FBDPSession, data_pipe: str, pipe_socket: PipeSocket, data_format: str, parameters: Dict = None) -> None: """Sends `OPEN` message. Arguments: channel: Channel associated with data pipe. session: Session associated with transmission. data_pipe: Data pipe identification. pipe_socket: Connected pipe socket. data_format: Required data format. parameters: Data pipe parameters. Raises: StopError: When sending message fails. """ msg = self.create_message_for(MsgType.OPEN) msg.data_frame.data_pipe = data_pipe msg.data_frame.pipe_socket = pipe_socket.value msg.data_frame.data_format = data_format if parameters: msg.data_frame.parameters.CopyFrom(dict2struct(parameters)) if channel.send(msg, session) != 0: raise StopError("Broken pipe, can't send OPEN message", code=ErrorCode.ERROR) channel.on_output_ready = self._on_output_ready session.pipe = data_pipe session.socket = pipe_socket session.data_format = data_format if parameters: session.params.update(parameters) self.on_init_session(channel, session)