Esempio n. 1
0
def recv_event(cfg, conn):
    # Always receive the socket data, or else the poll would loop.
    mbuf = conn.sock.recv(4096)
    if mbuf == None:
        # Curious - does it happen? XXX
        raise AppError("Received None")
    if len(mbuf) == 0:
        # EOF - we do nothing and hope for a proper event in the main loop.
        return
    conn.mbufs.append(mbuf)
    conn.rcvd += len(mbuf)

    if conn.rcvd < 4:
        return
    hdr = forumlib.skb_pull_copy(conn.mbufs, 4)

    # This produces a string if hdr is an str. Works great for bytearray.
    length = hdr[1]*256*256 + hdr[2]*256 + hdr[3]
    # This works if hdr is a string, fails for bytearray with struct.error.
    # length = struct.unpack("!I", hdr)[0] & 0xFFFFFF

    if conn.rcvd < 4 + length:
        return
    buf = forumlib.skb_pull(conn.mbufs, 4 + length)
    conn.rcvd -= len(buf)

    recv_msg(cfg, conn, str(buf[4:]))
    return
Esempio n. 2
0
def rec_msg(sock):
    mbufs = []
    rcvd = 0

    while rcvd < 4:
        mbuf = sock.recv(4096)
        if mbuf == None:
            # Curious - does it happen? EOF does, but not this? XXX
            print >>sys.stderr, "Received None"
            sys.exit(1)
        if len(mbuf) == 0:
            print >>sys.stderr, "Received EOF"
            sys.exit(1)
        mbufs.append(mbuf)
        rcvd += len(mbuf)

    hdr = forumlib.skb_pull_copy(mbufs, 4)

    # This produces a string if hdr is an str. Works great for bytearray.
    length = hdr[1]*256*256 + hdr[2]*256 + hdr[3]
    # This works if hdr is a string, fails for bytearray with struct.error.
    # length = struct.unpack("!I", hdr)[0] & 0xFFFFFF

    while rcvd < 4 + length:
        mbuf = sock.recv(4096)
        if mbuf == None:
            print >>sys.stderr, "Received None"
            sys.exit(1)
        if len(mbuf) == 0:
            print >>sys.stderr, "Received EOF"
            sys.exit(1)
        mbufs.append(mbuf)
        rcvd += len(mbuf)

    buf = forumlib.skb_pull(mbufs, 4 + length)

    # Is this a double copy? Not very efficient, if so.
    return str(buf[4:])