示例#1
0
文件: client.py 项目: smallka/piablo
def send_msg(sock, msg_id, msg_func):
    writer = BitWriter()
    writer.write_int(9, msg_id)
    msg_func(writer)
    msg = writer.get_bytes()
    header = BitStream(uint=len(msg) + 4, length=32).tobytes()
    sock.send(header + msg)
示例#2
0
文件: test.py 项目: smallka/piablo
def test_char_array():
    writer = BitWriter()
    for i in xrange(len(WORDS)):
        writer.write_char_array(len(WORDS) + i, WORDS)
    reader = BitReader(writer.get_bytes())
    for i in xrange(len(WORDS)):
        assert WORDS == reader.read_char_array(len(WORDS) + i)

    for i in xrange(1000):
        start = random.randint(0, len(WORDS)-1)
        end = random.randint(start+1, len(WORDS))
        sample = WORDS[start:end]
        max_length = random.randint(len(sample), len(sample) * 2)

        writer = BitWriter()
        writer.write_char_array(max_length, sample)
        reader = BitReader(writer.get_bytes())
        assert sample == reader.read_char_array(max_length)