Exemple #1
0
def _receive_ReliablePacketStruct(msg, addr):
    if addr not in _channels:
        _channels[addr] = dict(
            queue=    [],
            timeout=  0,
            code_in=  0,
            code_out= 1,
        )
    channel = _channels[addr]

    if len(msg['content']): # This is a reliable message

        if msg['code'] == (channel['code_in'] + 1) % 256: # This message is new
            channel['code_in'] = msg['code']
            structure, data = cs.deserialize(msg['content'])
            _listeners[structure](data, addr)

        if msg['code'] == channel['code_in']: # Confirm reception
            _socket.sendto(cs.serialize(ReliablePacketStruct, {
                'code': channel['code_in'],
                'content': '',
                }), addr)

    else: # This is a received confirmation

        if msg['code'] == channel['code_out'] and channel['queue']:
            channel['code_out'] = (channel['code_out'] + 1) % 256
            channel['queue'].pop(0)
            channel['timeout'] = 0
Exemple #2
0
def echo(data, addr):
    print data
    raw = CustomStruct.serialize(MessageStruct, {
        'number': data['number'] + 1,
        'message': 'pong'
    })
    ReliableUDP.send_reliable(raw, addr)
Exemple #3
0
def update():
    while 1:
        try:
            string, addr = _socket.recvfrom(_buffer)
            structure, data = cs.deserialize(string)
            _listeners[structure](data, addr)
        except socket.error: break
    for addr, channel in _channels.items():
        if not channel['queue']: continue
        if not channel['timeout'] % _retry:
            _socket.sendto(cs.serialize(ReliablePacketStruct, {
                'code': channel['code_out'],
                'content': channel['queue'][0],
            }), addr)
        channel['timeout'] += 1
Exemple #4
0
def echo(data, addr):
    print data
    raw = CustomStruct.serialize(MessageStruct, {'number': data['number'] + 1, 'message': 'pong'})
    ReliableUDP.send_reliable(raw, addr)
Exemple #5
0
def echo(data, addr):
    print data
    raw = CustomStruct.serialize(MessageStruct, {"number": data["number"] + 1, "message": "ping"})
    ReliableUDP.send_reliable(raw, addr)
Exemple #6
0
__author__ = "pzucco"

import ReliableUDP
import CustomStruct
import random
import time

MessageStruct = CustomStruct.Structure(number=CustomStruct.Int, message=CustomStruct.String)


@ReliableUDP.listener(MessageStruct)
def echo(data, addr):
    print data
    raw = CustomStruct.serialize(MessageStruct, {"number": data["number"] + 1, "message": "ping"})
    ReliableUDP.send_reliable(raw, addr)


ReliableUDP.init(retry=100)

raw = CustomStruct.serialize(MessageStruct, {"number": 0, "message": "ping"})
ReliableUDP.send_reliable(raw, ("127.0.0.1", 1000))

while 1:
    ReliableUDP.update()
    time.sleep(0.01)
Exemple #7
0
__author__ = 'pzucco'

import ReliableUDP
import CustomStruct
import random
import time

MessageStruct = CustomStruct.Structure(
    number=CustomStruct.Int,
    message=CustomStruct.String,
)


@ReliableUDP.listener(MessageStruct)
def echo(data, addr):
    print data
    raw = CustomStruct.serialize(MessageStruct, {
        'number': data['number'] + 1,
        'message': 'ping'
    })
    ReliableUDP.send_reliable(raw, addr)


ReliableUDP.init(retry=100)

raw = CustomStruct.serialize(MessageStruct, {'number': 0, 'message': 'ping'})
ReliableUDP.send_reliable(raw, ('127.0.0.1', 1000))

while 1:
    ReliableUDP.update()
    time.sleep(0.01)
Exemple #8
0
    contacts= cs.List(ContactStruct),
)

TYPE_EMAIL, TYPE_PHONE = 0, 1


person = dict(
    name= 'John',
    birth= 1980,
    contacts= [
        dict(type= TYPE_EMAIL, data= '*****@*****.**'),
        dict(type= TYPE_PHONE, data= '12345678'),
    ],
)

serialized = cs.serialize(PersonStruct, person)
structure, data = cs.deserialize(serialized)
print data
print 'received data is a PersonStruct?', structure == PersonStruct


class Person:
    def __init__(self, name, birth, contacts):
        self.name = name
        self.birth = birth
        self.contacts = contacts

class Contact:
    def __init__(self, type, data):
        self.type = type
        self.data = data