Beispiel #1
0
    def recv_image_data(self, img_size, img_sel):
        imgView = (self.img_view_1 if img_sel else self.img_view_0)
        if img_size > self.img_max_size:
            raise RuntimeError(
                "Expected size {} exceeds buffer size {}".format(
                    img_size, self.img_max_size))
            return -1, imgView

        try:
            utils.recv_data_into(self.sock, imgView[:img_size], img_size)
        except:
            return -1, imgView

        # Read the final handshake
        try:
            cmd = utils.recv_data(self.sock, 5).decode('ascii')
        except:
            return -1, imgView

        if cmd != 'enod!':
            raise RuntimeError(
                "Unexpected server reply. Expected 'enod!', got '{}'".format(
                    cmd))
            return -1, imgView

        # # Transaction is done, we now process/display the received image
        # img = jpeg_decode_func(img_view[:img_size])

        return 0, imgView
Beispiel #2
0
def parse_cmddata():
    global tmp_view, conn, tmp_buf
    try:
        utils.recv_data_into(conn, tmp_view, 7)
    except:
        return -1
    cmddata = tmp_buf.decode('ascii')
    return cmddata
Beispiel #3
0
def parse_cmdcode():
    global tmp_view, conn, tmp_buf
    try:
        utils.recv_data_into(conn, tmp_view[:5], 5)
    except:
        return '12345'
    cmdcode = tmp_buf[:5].decode('ascii')
    return cmdcode
Beispiel #4
0
    def recv_int_data(self, cmdstr):
        ack_data = 0
        try:
            utils.recv_data_into(self.sock, self.tmp_view[:5], 5)
        except:
            return -1, ack_data

        cmd = self.tmp_buf[:5].decode('ascii')
        if cmd != cmdstr:
            raise RuntimeError("Inconsistent server reply {} != {}".format(
                cmd, cmdstr))
            return -1, ack_data

        try:
            utils.recv_data_into(self.sock, self.tmp_view, 7)
        except:
            return -1, ack_data

        ack_data = int(self.tmp_buf.decode('ascii'))
        return 0, ack_data
Beispiel #5
0
    def recv_reply_ack(self, cmdstr, intdata):
        try:
            utils.recv_data_into(self.sock, self.tmp_view[:5], 5)
        except:
            return -1

        cmd = self.tmp_buf[:5].decode('ascii')
        if cmd != cmdstr:
            raise RuntimeError("Inconsistent server reply {} != {}".format(
                cmd, cmdstr))
            return -1

        try:
            utils.recv_data_into(self.sock, self.tmp_view, 7)
        except:
            return -1

        ack_data = int(self.tmp_buf.decode('ascii'))
        if ack_data != intdata:
            raise RuntimeError("Inconsistent server reply {} != {}".format(
                ack_data, intdata))
            return -1

        return ack_data
Beispiel #6
0
s.bind((host, port))
s.listen(1)

print("Listening at port {} ...".format(port))

while True:
    conn, addr = s.accept()

    with conn:
        print('Connected by', addr)
        while True:
            #-------------------------------------
            #  接收命令訊息
            #-------------------------------------
            try:
                utils.recv_data_into(conn, tmp_view[:5], 5)
            except:
                continue
            # print("&")
            cmd = tmp_buf[:5].decode('ascii')
            #-----------------------------------------------
            # Client sends "imageXXXXXXX" to request new image
            # where XXXXXXX is image index
            #-----------------------------------------------
            if (cmd == 'image'):
                # Read the image index
                image_index = int(parse_cmddata())
                # Grab and encode the image
                #print("Received: {}-{}".format(cmd, image_index))
                n_NG = 5
                img_buffer = None
Beispiel #7
0
        # Grab and encode the image
        img_buffer = get_buffer()
        if img_buffer is None:
            continue

        # Prepare the message with the number of bytes going to be sent
        msg = bytes("image{:07}".format(len(img_buffer)), "ascii")

        utils.send_data(sock, msg)

        # Send the buffer
        utils.send_data(sock, img_buffer)

        # Read the reply command
        utils.recv_data_into(sock, tmp_view[:5], 5)
        cmd = tmp_buf[:5].decode('ascii')

        if cmd != 'image':
            raise RuntimeError("Unexpected server reply")

        # Read the image buffer size
        utils.recv_data_into(sock, tmp_view, 7)
        img_size = int(tmp_buf.decode('ascii'))

        # Read the image buffer
        utils.recv_data_into(sock, img_view[:img_size], img_size)

        # Read the final handshake
        cmd = utils.recv_data(sock, 5).decode('ascii')
        if cmd != 'enod!':
Beispiel #8
0
tmp_buf = bytearray(7)
tmp_view = memoryview(
    tmp_buf)  # this allows to get a reference to a slice of tmp_buf

# Creates a temporary buffer which can hold the largest image we can transmit
img_buf = bytearray(9999999)
img_view = memoryview(img_buf)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((host, port))
    s.listen(1)
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            utils.recv_data_into(conn, tmp_view[:5], 5)
            cmd = tmp_buf[:5].decode('ascii')
            if (cmd == 'image'):
                # Read the image buffer size
                utils.recv_data_into(conn, tmp_view, 7)
                img_size = int(tmp_buf.decode('ascii'))

                # Read the buffer content
                utils.recv_data_into(conn, img_view[:img_size], img_size)

                # Decode the image
                img = jpeg_decode_func(img_view[:img_size])

                # Process it
                res = image_process(img)