def _read_smp_message(self):
        with self._lock:
            smid, flags, sid, l, seq_num, wnd = self._smp_header.unpack(readall(self._transport, self._smp_header.size))
            if smid != self._smid:
                self._bad_stm('Invalid SMP packet signature')
            #logger.debug('received smp packet t:%s sid:%s len:%s num:%s wnd:%s', self._type_to_str(flags), sid, l, seq_num, wnd)
            try:
                session = self._sessions[sid]
            except KeyError:
                self._bad_stm('Invalid SMP packet session id')
            if wnd < session._high_water_for_send:
                self._bad_stm('Invalid WNDW in packet from server')
            if seq_num > session._high_water_for_recv:
                self._bad_stm('Invalid SEQNUM in packet from server')
            session._last_recv_seq_num = seq_num
            if flags == self._ACK:
                if session._state in ('FIN RECEIVED', 'CLOSED'):
                    self._bad_stm('Unexpected SMP ACK packet from server')
                if seq_num != session._seq_num_for_recv:
                    self._bad_stm('Invalid SEQNUM in ACK packet from server')
                session._high_water_for_send = wnd
                self._send_queued_packets(session)
            elif flags == self._DATA:
                if session._state == 'SESSION ESTABLISHED':
                    if seq_num != self._add_one_wrap(session._seq_num_for_recv):
                        self._bad_stm('Invalid SEQNUM in ACK packet from server')
                    session._seq_num_for_recv = seq_num
                    data = readall(self._transport, l - self._smp_header.size)
                    session._recv_queue.append(data)
                    if wnd > session._high_water_for_send:
                        session._high_water_for_send = wnd
                        self._send_queued_packets(session)

                elif session._state == 'FIN SENT':
                    skipall(self._transport, l - self._smp_header.size)
                else:
                    self._bad_stm('Unexpected DATA packet from server')
            elif flags == self._FIN:
                if session._state == 'SESSION ESTABLISHED':
                    session._state = 'FIN RECEIVED'
                elif session._state == 'FIN SENT':
                    session._state = 'CLOSED'
                    del self._sessions[session._session_id]
                    self._used_ids_ba[session._session_id] = False
                elif session._state == 'FIN RECEIVED':
                    self._bad_stm('Unexpected SMP FIN packet from server')
                else:
                    self._bad_stm('Invalid state: ' + session._state)
            elif flags == self._SYN:
                self._bad_stm('Unexpected SMP SYN packet from server')
            else:
                self._bad_stm('Unexpected SMP flags in packet from server')
dataset/ETHPy150Open denisenkom/pytds/pytds/smp.py/SmpManager._read_smp_message/original