def rx_rst_frame(protocol: int, pipe: int, src_ia: int, frame: Frame): """ 接收到RST帧时处理函数 """ _lock.acquire() log.warn('rx rst frame.src ia:0x%x', src_ia) for item in _items: _deal_rst_frame(protocol, pipe, src_ia, frame, item) _lock.release()
def service_callback(protocol: int, pipe: int, src_ia: int, rid: int, req: bytearray) -> (bytearray, int): """ 回调资源号rid对应的函数 """ log.info('service callback.rid:%d', rid) rid += protocol << 16 if rid not in _services: log.warn('service callback failed!can not find new rid:%d', rid) return None, SYSTEM_ERROR_INVALID_RID return _services[rid](pipe, src_ia, req)
def send(protocol: int, pipe: int, dst_ia: int, frame: Frame): """ 发送数据 """ if frame is None: return load_param = get_load_param() if not load_param.is_allow_send(pipe): log.warn('send failed!pipe:0x%x is not allow send.token:%d', pipe, frame.control_word.token) return log.info('send frame.token:%d protocol:%d pipe:0x%x dst ia:0x%x', frame.control_word.token, protocol, pipe, dst_ia) load_param.send(protocol, pipe, dst_ia, frame_to_bytes(frame))
def block_send(protocol: int, pipe: int, dst_ia: int, frame: BlockFrame): """ 块传输发送数据 """ if frame is None: return load_param = get_load_param() if not load_param.is_allow_send(pipe): log.warn('block send failed!pipe:0x%x is not allow send.token:%d', pipe, frame.control_word.token) return log.info('block send frame.token:%d protocol:%d pipe:0x%x dst ia:0x%x offset:%d', frame.control_word.token, protocol, pipe, dst_ia, frame.block_header.offset) load_param.send(protocol, pipe, dst_ia, block_frame_to_bytes(frame))
def _deal_rst_frame(protocol: int, pipe: int, src_ia: int, frame: Frame, item: _Item): if item.protocol != protocol or item.pipe != pipe or item.dst_ia != src_ia or item.rid != frame.control_word.rid \ or item.token != frame.control_word.token: return False result = frame.payload[0] log.warn('deal rst frame.token:%d result:0x%x', item.token, result) _items.remove(item) if item.ack_callback: # 回调方式 item.ack_callback(bytearray(), result) else: # 同步调用 item.is_rx_ack = True item.result = result return True
def _retry_send(item: _Item): t = get_time() if t - item.start_time > item.timeout: log.warn('wait ack timeout!task failed!token:%d', item.token) _items.remove(item) if len(item.req) > SINGLE_FRAME_SIZE_MAX: block_remove(item.protocol, item.pipe, item.dst_ia, item.code, item.rid, item.token) if item.ack_callback: # 回调方式 item.ack_callback(bytearray(), SYSTEM_ERROR_RX_TIMEOUT) else: # 同步调用 item.is_rx_ack = True item.result = SYSTEM_ERROR_RX_TIMEOUT return # 块传输不用此处重传.块传输模块自己负责 if len(item.req) > SINGLE_FRAME_SIZE_MAX: return load_param = get_load_param() if t - item.last_retry_timestamp < load_param.block_retry_interval * 1000: return # 重传 item.retry_num += 1 if item.retry_num >= load_param.block_retry_max_num: log.warn('retry too many!task failed!token:%d', item.token) _items.remove(item) if item.ack_callback: # 回调方式 item.ack_callback(bytearray(), SYSTEM_ERROR_RX_TIMEOUT) else: # 同步调用 item.is_rx_ack = True item.result = SYSTEM_ERROR_RX_TIMEOUT return item.last_retry_timestamp = t log.warn('retry send.token:%d retry num:%d', item.token, item.retry_num) _send_frame(item.protocol, item.pipe, item.dst_ia, item.code, item.rid, item.token, item.req)