本文整理汇总了Python中packet.Packet.unpack方法的典型用法代码示例。如果您正苦于以下问题:Python Packet.unpack方法的具体用法?Python Packet.unpack怎么用?Python Packet.unpack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类packet.Packet
的用法示例。
在下文中一共展示了Packet.unpack方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from packet import Packet [as 别名]
# 或者: from packet.Packet import unpack [as 别名]
def run(self):
while True:
data, addr = self.server.recv()
pkg = Packet.unpack(data)
# print "got packet: %s, %s, %s" % (pkg.tick, pkg.players, pkg.input)
with self.lock:
net_tick = self.parent.net_tick
if addr not in self.clients:
if pkg.tick == 0 and len(pkg.players) == 0 and len(pkg.input) == 0:
self.clients[addr] = Client(addr, self.next_id, Player(50 + self.next_id * 30, 50))
self.next_id += 1
pkg_response = Packet(net_tick)
pkg_response.add_player(self.clients[addr].id, self.clients[addr].player)
self.server.send(pkg_response, addr)
print "authenticating new client: %s -> %s" % (addr, self.clients[addr].id)
else:
print "invalid authentication attempt: %s, %s, %s" % (pkg.tick, pkg.players, pkg.input)
else:
client = self.clients[addr]
client.ack()
if pkg.tick >= net_tick:
if len(pkg.input) > 0:
client.events[pkg.tick] = pkg.input
# if net_tick not in self.pkg_queue:
# self.pkg_queue[net_tick] = []
# self.pkg_queue[net_tick].insert(0, pkg)
else:
print "discard packet (too old: %d < %d)" % (pkg.tick, net_tick)
示例2: authenticate
# 需要导入模块: from packet import Packet [as 别名]
# 或者: from packet.Packet import unpack [as 别名]
def authenticate(self):
""" call this before opening the game
gets the current tick from the server
and synchronizes the game state """
def threaded_recv(retlist):
response, addr = self.client.recv()
retlist.append(response)
pkg_request = Packet(0)
self.client.send(pkg_request)
retlist = []
t = Thread(target=lambda: threaded_recv(retlist))
t.daemon = True
t.start()
wait_start = time.get_ticks()
wait_end = wait_start + 1000
while len(retlist) <= 0 and time.get_ticks() < wait_end:
time.wait(1)
if len(retlist) > 0:
response = retlist[0]
pkg_response = Packet.unpack(response)
self.start_tick = pkg_response.tick
if len(pkg_response.players) <= 0:
raise RuntimeError("Invalid response: %s" % pkg_response)
self.id = pkg_response.players[0][0]
self.set_state(pkg_response, backtrack=False)
else:
raise RuntimeError("Server not responding")
示例3: run
# 需要导入模块: from packet import Packet [as 别名]
# 或者: from packet.Packet import unpack [as 别名]
def run(self):
while True:
data, addr = self.client.recv()
try:
pkg = Packet.unpack(data)
except:
print "invalid package from %s" % addr
continue
with self.lock:
self.parent.set_state(pkg, backtrack=True)
示例4: while
# 需要导入模块: from packet import Packet [as 别名]
# 或者: from packet.Packet import unpack [as 别名]
import socket
from packet import Packet
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#sock.connect(("68.62.87.97",48011))
sock.connect(("127.0.0.1",8022))
data1={}
data2={}
data1["request_type"]=3
data1["room"]="Hall"
msg1=Packet.pack(data1)
data2["request_type"]=4
data2["target"]="room"
data2["room"]="Hall"
data2["message"]="hello world"
msg2=Packet.pack(data2)
sock.send("%8s"%(hex(len(msg1))[2:]));
sock.send(msg1)
sock.send("%8s"%(hex(len(msg2))[2:]));
sock.send(msg2)
while(1):
head = sock.recv(8);
if head:
size=int("0x"+head.strip(),16);
data = sock.recv(size);
ary=Packet.unpack(data)
print ary