예제 #1
0
    def run(self):
        """线程主函数

        循环运行,接受新的客户端的连接。
        """
        log.info('station server thread: start, port: %d' % self.port)
        try:
            server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            if os.name != 'nt':
                server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            server.bind(('0.0.0.0', self.port))
            server.listen(1)
            server.settimeout(3)    # timeout: 3s
            while self.running:
                try:
                    conn, address = server.accept()
                    conn.settimeout(3)
                    log.debug('new station connection from: %s' % str(address))
                    self.got_client(conn, str(address))
                except socket.timeout:
                    pass
                self.check_new_connections()
            # clean up
            server.close()
            self.disconnect()
            log.info('station server thread: bye')
        except Exception as e:
            log.error('station server thread error: %s' % e)
            self.running = False
        self.disconnect()
예제 #2
0
    def run(self):
        """线程主函数

        循环运行,接受新的客户端的连接。
        """
        log.info('server thread: start, port: %d' % self.port)
        try:
            server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            if os.name != 'nt':
                server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            server.bind(('0.0.0.0', self.port))
            server.listen(100)      # 并发
            server.settimeout(1)    # timeout: 1s
            while self.running:
                # 接受连接
                try:
                    conn, address = server.accept()
                    conn.settimeout(3)
                    self.dispatcher.add_client(conn, address)
                    log.debug('new client from: %s' % str(address))
                except socket.timeout:
                    pass
                # 分发数据
                self.dispatcher.dispatch()
            server.close()
            self.dispatcher.close_all_clients()
            log.info('server thread: bye')
        except Exception as e:
            log.error('server thread error: %s' % e)
            self.running = False
예제 #3
0
    def run(self):
        """线程主函数

        循环运行,接受新的客户端的连接。
        """
        log.info('station server thread: start, port: %d' % self.port)
        try:
            server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            if os.name != 'nt':
                server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            server.bind(('0.0.0.0', self.port))
            server.listen(1)
            server.settimeout(3)  # timeout: 3s
            while self.running:
                try:
                    conn, address = server.accept()
                    conn.settimeout(3)
                    log.debug('new station connection from: %s' % str(address))
                    self.got_client(conn, str(address))
                except socket.timeout:
                    pass
                self.check_new_connections()
            # clean up
            server.close()
            self.disconnect()
            log.info('station server thread: bye')
        except Exception as e:
            log.error('station server thread error: %s' % e)
            self.running = False
        self.disconnect()
예제 #4
0
    def run(self):
        """线程主函数

        循环运行,接受新的客户端的连接。
        """
        log.info('server thread: start, port: %d' % self.port)
        try:
            server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            if os.name != 'nt':
                server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            server.bind(('0.0.0.0', self.port))
            server.listen(100)  # 并发
            server.settimeout(1)  # timeout: 1s
            while self.running:
                # 接受连接
                try:
                    conn, address = server.accept()
                    conn.settimeout(3)
                    self.dispatcher.add_client(conn, address)
                    log.debug('new client from: %s' % str(address))
                except socket.timeout:
                    pass
                # 分发数据
                self.dispatcher.dispatch()
            server.close()
            self.dispatcher.close_all_clients()
            log.info('server thread: bye')
        except Exception as e:
            log.error('server thread error: %s' % e)
            self.running = False
예제 #5
0
    def receive_data(self):
        """接收一个数据包

        并追加到 self.buffer 中。
        """
        data = self.client.recv(BUFFER_SIZE)
        if len(data) == 0:
            raise RuntimeError('socket connection broken')
        self.buffer.extend(data)
        log.debug('control rcv %d bytes.' % len(data))
예제 #6
0
    def log_request(self, code='-', size='-'):
        """覆盖基类方法,不输出到屏幕

        This is called by send_response().

        Args:
            code (str): 状态码
            size (str):
        """
        log.debug('%s - - "%s" %s %s' % (self.address_string(), self.requestline, str(code), str(size)))
예제 #7
0
    def log_request(self, code="-", size="-"):
        """覆盖基类方法,不输出到屏幕

        This is called by send_response().

        Args:
            code (str): 状态码
            size (str):
        """
        log.debug('%s - - "%s" %s %s' % (self.address_string(), self.requestline, str(code), str(size)))
예제 #8
0
 def dispatch(self):
     """每次执行时,把 self.data_queue 中的所有数据包合并,再分发给各 SenderThread"""
     data = b''
     try:
         while self.data_queue.qsize() > 0:
             data += self.data_queue.get(block=False)
             self.data_queue.task_done()
     except queue.Empty:
         pass
     if len(data) > 0:
         num_of_sender = self.send_data(bytes(data))
         log.debug('send %d bytes to %d clients.' % (len(data), num_of_sender))
예제 #9
0
 def dispatch(self):
     """每次执行时,把 self.data_queue 中的所有数据包合并,再分发给各 SenderThread"""
     data = b''
     try:
         while self.data_queue.qsize() > 0:
             data += self.data_queue.get(block=False)
             self.data_queue.task_done()
     except queue.Empty:
         pass
     if len(data) > 0:
         num_of_sender = self.send_data(bytes(data))
         log.debug('send %d bytes to %d clients.' %
                   (len(data), num_of_sender))
예제 #10
0
    def parse_data(self, data):
        """收到数据后的处理

        Args:
            data (list): rtk 服务名
        """
        self.rcv_count += 1
        log.debug('rcv %d bytes. id: %d' % (len(data), self.rcv_count))
        self.protocol_handler.push_back(data)

        # 握手
        if not self.handshake_ok:
            self.handshake_ok = self.protocol_handler.handshake()
        # 处理
        while self.running and self.handshake_ok:
            data = self.protocol_handler.get_parsed_data()
            if data is None or len(data) <= 0:
                break
            self.got_data_cb(data)
예제 #11
0
    def parse_data(self, data):
        """收到数据后的处理

        Args:
            data (list): 新收到的数据
        """
        self.rcv_count += 1
        log.debug('rcv %d bytes. id: %d' % (len(data), self.rcv_count))
        base64_log.raw(bytes(data))
        self.protocol_handler.push_back(data)

        # 握手
        if not self.handshake_ok:
            self.handshake_ok = self.protocol_handler.handshake()
        # 处理
        while self.running and self.handshake_ok:
            data = self.protocol_handler.get_parsed_data()
            if data is None or len(data) <= 0:
                break
            self.got_data_cb(data)
예제 #12
0
    def get_parsed_data(self):
        """解析数据

        Returns:
            return (bytes): 解析了的完整报文
        """

        if len(self.data) <= 0:
            return None
        # 拷贝
        data = self.data[:]
        if self.is_acceptable_msg_type is None:
            # 如果不用解析
            self.data.clear()
            return bytes(data)

        try:
            # 解析
            index, len_message, msg_type = try_parse(data)
            if index > 0:
                # 删除无法解析的数据
                log.debug('unknown data size: %d' % index)
                # print unknown data
                # print([hex(x) for x in data[:index]])
                # print(bytes(data[:index]).decode('utf-8', errors='ignore'))
                self.pop_front(index)
            if len_message > 0:
                # 删除解析后的数据
                log.debug('pkg size: %d, msg size: %d, msg type: %d' % (len_message, len_message - 6, msg_type))
                # print([hex(x) for x in data[:index + len_message]])
                # print(bytes(data[:index + len_message]).decode('utf-8', errors='ignore'))
                parsed_data = bytes(self.pop_front(len_message))
                if self.is_acceptable_msg_type(msg_type):
                    return parsed_data
        except Exception as e:
            log.error('checker error when parse msg: %s' % e)
        return None
예제 #13
0
 def log_error(self, format, *args):
     """覆盖基类方法,不输出到屏幕"""
     log.debug('%s - - %s' % (self.address_string(), format % args))
예제 #14
0
 def log_error(self, format, *args):
     """覆盖基类方法,不输出到屏幕"""
     log.debug("%s - - %s" % (self.address_string(), format % args))