Ejemplo n.º 1
0
__author__ = 'pzucco'

import ReliableUDP
import CustomStruct
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': 'pong'
    })
    ReliableUDP.send_reliable(raw, addr)


ReliableUDP.init(port=1000, retry=100)

while 1:
    ReliableUDP.update()
    time.sleep(0.01)
Ejemplo n.º 2
0
__author__ = 'pzucco'

import socket
import CustomStruct as cs

ReliablePacketStruct = cs.Structure(
    code= cs.Byte,
    content= cs.RawData,
)

_channels = {}
_socket = None
_buffer = 4096
_retry = 60

def init(**args):
    global _socket, _retry
    if _socket: _socket.close()
    _socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    if 'port' in args: _socket.bind(('', args['port']))
    _socket.setblocking(0)
    _retry = args.get('retry', 60)

def send(string, addr):
    _socket.sendto(string, addr)

def send_reliable(string, addr):
    if not string: return
    if addr not in _channels:
        _channels[addr] = dict(
            queue=    [],