network.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:python-ardrone 作者: fkmclane 项目源码 文件源码
def run(self):
        video_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        video_socket.connect((self.host, ardrone.constant.VIDEO_PORT))

        nav_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        nav_socket.setblocking(False)
        nav_socket.bind(('', ardrone.constant.NAVDATA_PORT))
        nav_socket.sendto(b'\x01\x00\x00\x00', (self.host, ardrone.constant.NAVDATA_PORT))

        stopping = False
        while not stopping:
            inputready, outputready, exceptready = select.select([nav_socket, video_socket, self.com_pipe], [], [])
            for i in inputready:
                if i == video_socket:
                    # get first few bytes of header
                    data = video_socket.recv(12, socket.MSG_WAITALL)
                    if len(data) != 12:
                        continue

                    # decode relevant portions of the header
                    sig_p, sig_a, sig_v, sig_e, version, codec, header, payload = struct.unpack('4cBBHI', data)

                    # check signature (and ignore packet otherwise)
                    if sig_p != b'P' or sig_a != b'a' or sig_v != b'V' or sig_e != b'E':
                        continue

                    # get remaining frame
                    data += video_socket.recv(header - 12 + payload, socket.MSG_WAITALL)

                    try:
                        # decode the frame
                        image = ardrone.video.decode(data)
                        self.video_pipe.send(image)
                    except ardrone.video.DecodeError:
                        pass
                elif i == nav_socket:
                    while 1:
                        try:
                            data = nav_socket.recv(65535)
                        except IOError:
                            # we consumed every packet from the socket and
                            # continue with the last one
                            break
                    navdata = ardrone.navdata.decode(data)
                    self.nav_pipe.send(navdata)
                elif i == self.com_pipe:
                    _ = self.com_pipe.recv()
                    stopping = True
                    break
        video_socket.close()
        nav_socket.close()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号