本文整理汇总了Python中cuwo.packet.PacketHandler类的典型用法代码示例。如果您正苦于以下问题:Python PacketHandler类的具体用法?Python PacketHandler怎么用?Python PacketHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PacketHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, server):
self.server = server
self.client_handler = PacketHandler(CS_PACKETS,
self.on_client_packet)
self.server_handler = PacketHandler(SC_PACKETS,
self.on_server_packet)
self.relay_packets = []
self.print_stats()
示例2: __init__
def __init__(self, server):
self.server = server
self.start_time = reactor.seconds()
self.client_handler = PacketHandler(CS_PACKETS,
self.on_client_packet)
self.server_handler = PacketHandler(SC_PACKETS,
self.on_server_packet)
self.entities = {}
self.relay_packets = []
self.print_stats()
示例3: __init__
def __init__(self, loop):
self.loop = loop
self.start_time = self.loop.time()
self.client_handler = PacketHandler(CS_PACKETS,
self.on_client_packet)
self.server_handler = PacketHandler(SC_PACKETS,
self.on_server_packet)
self.entities = {}
self.relay_packets = []
self.print_stats()
self.times = []
示例4: connectionMade
def connectionMade(self):
if self.connection_state != 0:
self.disconnect('Unexpected data')
return
self.connection_state = 1
server = self.server
if len(server.connections) >= server.config.base.max_players:
# For being able to allow joining by external scritps although server is full
ret = self.scripts.call('on_join_full_server').result
if ret is not True:
self.send_packet(server_full_packet)
self.disconnect()
self.connection_state = -1
print '[INFO] %s tried to join full server' % self.address.host
return
self.packet_handlers = {
ClientVersion.packet_id: self.on_version_packet,
EntityUpdate.packet_id: self.on_entity_packet,
ClientChatMessage.packet_id: self.on_chat_packet,
InteractPacket.packet_id: self.on_interact_packet,
HitPacket.packet_id: self.on_hit_packet,
ShootPacket.packet_id: self.on_shoot_packet
}
self.packet_handler = PacketHandler(CS_PACKETS, self.on_packet)
server.connections.add(self)
self.rights = AttributeSet()
self.scripts = ScriptManager()
server.scripts.call('on_new_connection', connection=self)
示例5: connectionMade
def connectionMade(self):
self.transport.setTcpNoDelay(True)
server = self.server
self.client_packet_handler = PacketHandler(CS_PACKETS,
self.on_client_packet)
self.server_packet_handler = PacketHandler(SC_PACKETS,
self.on_server_packet)
server.connections.add(self)
self.rights = AttributeSet()
self.scripts = ScriptManager()
server.scripts.call('on_new_connection', connection=self)
point = TCP4ClientEndpoint(reactor, self.server.config.base.mitm_ip, self.server.config.base.mitm_port)
d = point.connect(RelayFactory(self))
d.addCallback(self.got_relay_client)
示例6: connectionMade
def connectionMade(self):
self.packet_handlers = {
ClientVersion.packet_id : self.on_version_packet,
EntityUpdate.packet_id : self.on_entity_packet,
ClientChatMessage.packet_id : self.on_chat_packet,
InteractPacket.packet_id : self.on_interact_packet,
HitPacket.packet_id : self.on_hit_packet
}
self.scripts = []
self.factory.call_scripts('on_new_connection', self)
self.packet_handler = PacketHandler(CS_PACKETS, self.on_packet)
self.rights = AttributeSet()
示例7: connectionMade
def connectionMade(self):
server = self.server
if len(server.connections) >= server.config.base.max_players:
self.send_packet(server_full_packet)
self.disconnect()
return
self.packet_handlers = {
ClientVersion.packet_id: self.on_version_packet,
EntityUpdate.packet_id: self.on_entity_packet,
ClientChatMessage.packet_id: self.on_chat_packet,
InteractPacket.packet_id: self.on_interact_packet,
HitPacket.packet_id: self.on_hit_packet,
ShootPacket.packet_id: self.on_shoot_packet
}
server.connections.add(self)
self.scripts = []
self.server.call_scripts('on_new_connection', self)
self.packet_handler = PacketHandler(CS_PACKETS, self.on_packet)
self.rights = AttributeSet()
示例8: CubeWorldProtocol
class CubeWorldProtocol(Protocol):
relay_client = None
relay_packets = None
def __init__(self, server):
self.server = server
self.client_handler = PacketHandler(CS_PACKETS,
self.on_client_packet)
self.server_handler = PacketHandler(SC_PACKETS,
self.on_server_packet)
self.relay_packets = []
self.print_stats()
def print_stats(self):
return
reactor.callLater(3, self.print_stats)
print 'Server packets:', server_ids
print 'Client packets:', client_ids
def send_welcome(self):
lines = [u'Hello %s! Welcome to the server!' % self.name,
u'(server powered by cuwo)']
for line in reversed(lines):
self.send_chat(line)
def send_chat(self, value):
packet = ServerChatMessage()
packet.entity_id = 0
packet.value = value
self.transport.write(write_packet(packet))
def on_client_packet(self, packet):
if packet.packet_id in (12, 11):
return
if self.relay_client is None:
self.relay_packets.append(write_packet(packet))
return
self.relay_client.transport.write(write_packet(packet))
client_ids[packet.packet_id] += 1
if packet.packet_id == EntityUpdate.packet_id:
try:
name = packet.entity.name
if not name:
return
self.name = name
reactor.callLater(10, self.send_welcome)
except AttributeError:
pass
return
print 'Got client packet:', packet.packet_id
def on_server_packet(self, packet):
self.transport.write(write_packet(packet))
server_ids[packet.packet_id] += 1
if packet.packet_id in (0, 2, 4, 5):
return
print 'Got server packet:', packet.packet_id
def got_relay_client(self, p):
self.relay_client = p
for data in self.relay_packets:
self.relay_client.transport.write(data)
self.relay_packets = None
def connectionMade(self):
point = TCP4ClientEndpoint(reactor, "127.0.0.1", 12346)
d = point.connect(RelayFactory(self))
d.addCallback(self.got_relay_client)
print 'Connected'
def connectionLost(self, reason):
print 'Lost connection'
if self.relay_client is not None:
self.relay_client.transport.loseConnection()
def serverDataReceived(self, data):
self.server_handler.feed(data)
def dataReceived(self, data):
self.client_handler.feed(data)
示例9: CubeWorldProtocol
class CubeWorldProtocol(Protocol):
has_joined = False
entity_id = None
entity_data = None
disconnected = False
def __init__(self, factory):
self.factory = factory
def send_packet(self, packet):
self.transport.write(write_packet(packet))
def on_packet(self, packet):
packet_id = packet.packet_id
if packet_id == ClientVersion.packet_id:
if packet.version != constants.CLIENT_VERSION:
self.disconnect()
return
self.entity_id = self.factory.entity_ids.pop()
self.entity_data = create_entity_data()
self.factory.entities[self.entity_id] = self.entity_data
self.factory.connections[(self.entity_id,)] = self
server_data.entity_id = self.entity_id
self.send_packet(server_data)
seed_data.seed = self.factory.config.seed
self.send_packet(seed_data)
elif packet_id == EntityUpdate.packet_id:
if packet.entity_id != self.entity_id:
raise NotImplementedError()
packet.update_entity(self.entity_data)
if not self.has_joined and self.entity_data.name:
self.on_join()
self.has_joined = True
self.factory.broadcast_packet(packet)
elif packet_id == ClientChatMessage.packet_id:
message = packet.value
if self.on_chat(message) is False:
return
chat_message.entity_id = self.entity_id
chat_message.value = message
self.factory.broadcast_packet(chat_message)
else:
print 'Got client packet:', packet.packet_id
def on_join(self):
print 'Player %r joined' % self.entity_data.name
for connection in self.factory.connections.values():
if not connection.has_joined:
continue
entity_update.set_entity(connection.entity_data,
connection.entity_id)
self.send_packet(entity_update)
self.call_scripts('on_join')
def on_command(self, command, parameters):
self.call_scripts('on_command', command, parameters)
def on_chat(self, message):
if message.startswith('/'):
try:
splitted = shlex.split(message[1:])
except ValueError:
# shlex failed. let's just split per space
splitted = value.split(' ')
if splitted:
command = splitted.pop(0)
else:
command = ''
self.on_command(command, splitted)
return False
return self.call_scripts('on_chat', message)
def send_chat(self, value):
packet = ServerChatMessage()
packet.entity_id = 0
packet.value = value
self.send_packet(packet)
def send_lines(self, lines):
current_time = 0
for line in lines:
reactor.callLater(current_time, self.send_chat, line)
current_time += 2
def disconnect(self):
self.transport.loseConnection()
self.connectionLost('Kicked by server')
def connectionMade(self):
self.scripts = []
self.factory.call_scripts('on_new_connection', self)
self.packet_handler = PacketHandler(CS_PACKETS, self.on_packet)
self.rights = AttributeSet()
def connectionLost(self, reason):
if self.disconnected:
return
self.disconnected = True
try:
del self.factory.connections[self]
#.........这里部分代码省略.........
示例10: connectionMade
def connectionMade(self):
self.scripts = []
self.factory.call_scripts('on_new_connection', self)
self.packet_handler = PacketHandler(CS_PACKETS, self.on_packet)
self.rights = AttributeSet()
示例11: FrontendProtocol
class FrontendProtocol(asyncio.Protocol):
relay_client = None
relay_packets = None
entity_id = None
disconnected = False
update_index = 0
def __init__(self, loop):
self.loop = loop
self.start_time = self.loop.time()
self.client_handler = PacketHandler(CS_PACKETS,
self.on_client_packet)
self.server_handler = PacketHandler(SC_PACKETS,
self.on_server_packet)
self.entities = {}
self.relay_packets = []
self.print_stats()
self.times = []
def print_stats(self):
if self.disconnected:
return
self.loop.call_later(15, self.print_stats)
if self.entity_id is None:
return
entity = self.entities[self.entity_id]
print('Info:')
print('Pos:', entity.pos.x, entity.pos.y, entity.pos.z)
def send_chat(self, value):
packet = ServerChatMessage()
packet.entity_id = 0
packet.value = value
self.transport.write(write_packet(packet))
def send_client_chat(self, value):
packet = ClientChatMessage()
packet.value = value
self.relay_client.transport.write(write_packet(packet))
def on_entity_update(self, packet):
return
if packet.entity_id not in self.entities:
entity = entitydata.EntityData()
self.entities[packet.entity_id] = entity
print('Created new entity:', packet.entity_id)
else:
entity = self.entities[packet.entity_id]
packet.update_entity(entity)
def on_server_update(self, packet):
if packet.items_8:
print(packet.items_8)
if packet.missions:
print(packet.missions)
packet.missions = []
# for static_entity in packet.static_entities:
# print(vars(static_entity.header))
def on_client_packet(self, packet):
if packet.packet_id == EntityUpdate.packet_id:
self.on_entity_update(packet)
if self.relay_client is None:
self.relay_packets.append(write_packet(packet))
return
if packet.packet_id == InteractPacket.packet_id:
print(vars(packet))
self.relay_client.transport.write(write_packet(packet))
if packet.packet_id not in (0,):
print('Got client packet:', packet.packet_id)
def on_server_packet(self, packet):
if packet.packet_id == EntityUpdate.packet_id:
self.on_entity_update(packet)
elif packet.packet_id == JoinPacket.packet_id:
self.entity_id = packet.entity_id
elif packet.packet_id == CurrentTime.packet_id:
# I hate darkness
packet.time = constants.MAX_TIME / 2
elif packet.packet_id == ServerUpdate.packet_id:
self.on_server_update(packet)
self.transport.write(write_packet(packet))
if packet.packet_id not in (EntityUpdate.packet_id,
UpdateFinished.packet_id,
CurrentTime.packet_id,
ServerUpdate.packet_id):
print('Got server packet:', packet.packet_id)
def got_relay_client(self):
for data in self.relay_packets:
self.relay_client.transport.write(data)
self.relay_packets = None
def connection_made(self, transport):
self.transport = transport
print('On connection')
co = self.loop.create_connection(lambda: BackendProtocol(self),
'127.0.0.1', 12346)
self.loop.create_task(co)
#.........这里部分代码省略.........
示例12: CubeWorldProtocol
class CubeWorldProtocol(Protocol):
relay_client = None
relay_packets = None
entity_id = None
disconnected = True
def __init__(self, server):
self.server = server
self.start_time = reactor.seconds()
self.client_handler = PacketHandler(CS_PACKETS,
self.on_client_packet)
self.server_handler = PacketHandler(SC_PACKETS,
self.on_server_packet)
self.entities = {}
self.relay_packets = []
self.print_stats()
def print_stats(self):
if self.disconnected:
return
reactor.callLater(4, self.print_stats)
if self.entity_id is None:
return
entity = self.entities[self.entity_id]
print 'Info:'
print 'Pos:', entity.x, entity.y, entity.z
def send_chat(self, value):
packet = ServerChatMessage()
packet.entity_id = 0
packet.value = value
self.transport.write(write_packet(packet))
def on_entity_update(self, packet):
if packet.entity_id not in self.entities:
entity = create_entity_data()
self.entities[packet.entity_id] = entity
else:
entity = self.entities[packet.entity_id]
packet.update_entity(entity)
def on_client_packet(self, packet):
if packet.packet_id == EntityUpdate.packet_id:
self.on_entity_update(packet)
if self.relay_client is None:
self.relay_packets.append(write_packet(packet))
return
self.relay_client.transport.write(write_packet(packet))
if packet.packet_id not in (0,):
print 'Got client packet:', packet.packet_id
def on_server_packet(self, packet):
if packet.packet_id == EntityUpdate.packet_id:
self.on_entity_update(packet)
elif packet.packet_id == ServerData.packet_id:
self.entity_id = packet.entity_id
elif packet.packet_id == CurrentTime.packet_id:
# I hate darkness
packet.time = constants.MAX_TIME / 2
self.transport.write(write_packet(packet))
if packet.packet_id not in (0, 2, 4, 5):
print 'Got server packet:', packet.packet_id
def got_relay_client(self, p):
self.relay_client = p
for data in self.relay_packets:
self.relay_client.transport.write(data)
self.relay_packets = None
def connectionMade(self):
point = TCP4ClientEndpoint(reactor, "127.0.0.1", 12346)
d = point.connect(RelayFactory(self))
d.addCallback(self.got_relay_client)
print 'Connected'
def connectionLost(self, reason):
self.disconnected = True
print 'Lost connection'
if self.relay_client is not None:
self.relay_client.transport.loseConnection()
def serverDataReceived(self, data):
self.server_handler.feed(data)
def dataReceived(self, data):
self.client_handler.feed(data)
示例13: CubeWorldConnection
class CubeWorldConnection(Protocol):
"""
Protocol used for players
"""
connection_state = 0
entity_id = None
entity_data = None
login_id = None
change_index = -1
scripts = None
chunk = None
old_name = None
old_pos = None
old_health = None
old_level = None
old_xp = None
# used for anti chat spamming
time_last_chat = 0
chat_messages_burst = 0
# used for detecting dead connections
time_last_packet = 0
time_last_rate = 0
packet_count = 0
packet_rate = 0
# used for basic DoS protection
packet_burst = 0
def __init__(self, server, addr):
self.address = addr
self.server = server
# connection methods
def connectionMade(self):
if self.connection_state != 0:
self.disconnect('Unexpected data')
return
self.connection_state = 1
server = self.server
if len(server.connections) >= server.config.base.max_players:
# For being able to allow joining by external scritps although server is full
ret = self.scripts.call('on_join_full_server').result
if ret is not True:
self.send_packet(server_full_packet)
self.disconnect()
self.connection_state = -1
print '[INFO] %s tried to join full server' % self.address.host
return
self.packet_handlers = {
ClientVersion.packet_id: self.on_version_packet,
EntityUpdate.packet_id: self.on_entity_packet,
ClientChatMessage.packet_id: self.on_chat_packet,
InteractPacket.packet_id: self.on_interact_packet,
HitPacket.packet_id: self.on_hit_packet,
ShootPacket.packet_id: self.on_shoot_packet
}
self.packet_handler = PacketHandler(CS_PACKETS, self.on_packet)
server.connections.add(self)
self.rights = AttributeSet()
self.scripts = ScriptManager()
server.scripts.call('on_new_connection', connection=self)
def dataReceived(self, data):
self.packet_handler.feed(data)
def disconnect(self, reason=None):
self.transport.loseConnection()
self.connectionLost(reason)
def connectionLost(self, reason):
if self.connection_state < 0:
return
self.server.connections.discard(self)
if self.connection_state >= 3:
del self.server.players[self]
print '[INFO] Player %s #%s left the game.' % (self.name, self.entity_id)
self.server.send_chat('<<< %s #%s left the game' % (self.name, self.entity_id))
self.connection_state = -1
if self.entity_id is not None:
self.server.world.unregister(self.entity_id)
self.server.entity_ids.put_back(self.entity_id)
if self.scripts is not None:
self.scripts.unload()
# packet methods
def send_packet(self, packet):
self.transport.write(write_packet(packet))
def on_packet(self, packet):
if self.connection_state < 0:
return
if packet is None:
#.........这里部分代码省略.........
示例14: CubeWorldConnection
class CubeWorldConnection(Protocol):
"""
Protocol used for players
"""
relay_client = None
relay_packets = None
has_joined = False
entity_id = None
entity_data = None
login_id = None
rank = None
disconnected = False
scripts = None
chunk = None
old_pos = None
old_health = None
old_level = None
old_xp = None
def __init__(self, server, addr):
self.address = addr
self.server = server
self.relay_packets = []
# connection methods
def got_relay_client(self, p):
self.relay_client = p
for data in self.relay_packets:
self.relay_client.transport.write(data)
self.relay_packets = None
print 'Relaying Client Packets.'
def connectionMade(self):
self.transport.setTcpNoDelay(True)
server = self.server
self.client_packet_handler = PacketHandler(CS_PACKETS,
self.on_client_packet)
self.server_packet_handler = PacketHandler(SC_PACKETS,
self.on_server_packet)
server.connections.add(self)
self.rights = AttributeSet()
self.scripts = ScriptManager()
server.scripts.call('on_new_connection', connection=self)
point = TCP4ClientEndpoint(reactor, self.server.config.base.mitm_ip, self.server.config.base.mitm_port)
d = point.connect(RelayFactory(self))
d.addCallback(self.got_relay_client)
def serverDataReceived(self, data):
self.server_packet_handler.feed(data)
def dataReceived(self, data):
self.client_packet_handler.feed(data)
def disconnect(self, reason=None):
self.transport.loseConnection()
self.connectionLost(reason)
def connectionLost(self, reason):
if self.relay_client is not None:
self.relay_client.transport.loseConnection()
if self.disconnected:
return
self.disconnected = True
if self.login_id is not None:
database.update_online_seconds(self.server.db_con, self.login_id)
self.server.connections.discard(self)
if self.has_joined:
del self.server.players[self]
print '[INFO] Player %s #%s left the game.' % (self.name, self.entity_id)
self.server.send_chat('<<< %s #%s left the game' % (self.name, self.entity_id))
if self.entity_data is not None:
del self.server.entities[self.entity_id]
if self.scripts is not None:
self.scripts.unload()
# packet methods
def send_packet(self, packet):
self.transport.write(write_packet(packet))
def relay_packet(self, packet):
if self.relay_client is None:
self.relay_packets.append(write_packet(packet))
else:
self.relay_client.transport.write(write_packet(packet))
def on_server_packet(self, packet):
if packet.packet_id == EntityUpdate.packet_id:
if packet.entity_id == self.entity_id:
self.on_entity_packet(packet)
elif packet.packet_id == JoinPacket.packet_id:
#.........这里部分代码省略.........
示例15: CubeWorldConnection
class CubeWorldConnection(Protocol):
"""
Protocol used for players
"""
has_joined = False
entity_id = None
entity_data = None
disconnected = False
def __init__(self, server, addr):
self.address = addr
self.server = server
# connection methods
def connectionMade(self):
self.packet_handlers = {
ClientVersion.packet_id: self.on_version_packet,
EntityUpdate.packet_id: self.on_entity_packet,
ClientChatMessage.packet_id: self.on_chat_packet,
InteractPacket.packet_id: self.on_interact_packet,
HitPacket.packet_id: self.on_hit_packet,
ShootPacket.packet_id: self.on_shoot_packet
}
self.scripts = []
self.server.call_scripts('on_new_connection', self)
self.packet_handler = PacketHandler(CS_PACKETS, self.on_packet)
self.rights = AttributeSet()
def dataReceived(self, data):
self.packet_handler.feed(data)
def disconnect(self, reason=None):
self.transport.loseConnection()
self.connectionLost(reason)
def connectionLost(self, reason):
if self.disconnected:
return
self.disconnected = True
try:
del self.server.connections[self]
except KeyError:
pass
if self.has_joined:
print 'Player %s left' % self.name
if self.entity_data is not None:
del self.server.entities[self.entity_id]
if self.entity_id is not None:
self.server.entity_ids.put_back(self.entity_id)
for script in self.scripts[:]:
script.unload()
# packet methods
def send_packet(self, packet):
self.transport.write(write_packet(packet))
def on_packet(self, packet):
handler = self.packet_handlers.get(packet.packet_id, None)
if handler is None:
# print 'Unhandled client packet: %s' % packet.packet_id
return
handler(packet)
def on_version_packet(self, packet):
if packet.version != constants.CLIENT_VERSION:
mismatch_packet.version = constants.CLIENT_VERSION
self.send_packet(mismatch_packet)
self.disconnect()
return
server = self.server
if len(server.connections) >= server.config.base.max_players:
self.send_packet(server_full_packet)
self.disconnect()
return
self.entity_id = server.entity_ids.pop()
server.connections[(self.entity_id,)] = self
join_packet.entity_id = self.entity_id
self.send_packet(join_packet)
seed_packet.seed = server.config.base.seed
self.send_packet(seed_packet)
def on_entity_packet(self, packet):
if self.entity_data is None:
self.entity_data = create_entity_data()
self.server.entities[self.entity_id] = self.entity_data
self.entity_data.mask |= packet.update_entity(self.entity_data)
if not self.has_joined and getattr(self.entity_data, 'name', None):
self.on_join()
self.has_joined = True
def on_chat_packet(self, packet):
message = filter_string(packet.value).strip()
if not message:
return
if self.on_chat(message) is False:
return
chat_packet.entity_id = self.entity_id
#.........这里部分代码省略.........