Browse Source

message debug; nozip multi fix

pull/18/merge
Rossen Georgiev 9 years ago
parent
commit
f65cb46261
  1. 64
      steam/client/cm.py
  2. 77
      steam/client/msg.py

64
steam/client/cm.py

@ -44,6 +44,7 @@ class CMClient:
UDP = 1 UDP = 1
def __init__(self, protocol=0): def __init__(self, protocol=0):
self.verbose_debug = False
self.reconnect = False self.reconnect = False
self._init_attributes() self._init_attributes()
@ -94,7 +95,9 @@ class CMClient:
self.connection.disconnect() self.connection.disconnect()
self._recv_loop.kill(block=False) self._recv_loop.kill(block=False)
self._heartbeat_loop.kill()
if self._heartbeat_loop:
self._heartbeat_loop.kill()
self._init_attributes() self._init_attributes()
@ -120,12 +123,16 @@ class CMClient:
if not isinstance(message, (Msg, MsgProto)): if not isinstance(message, (Msg, MsgProto)):
raise ValueError("Expected Msg or MsgProto, got %s" % message) raise ValueError("Expected Msg or MsgProto, got %s" % message)
if self.verbose_debug:
logger.debug("Outgoing: %s\n%s" % (repr(message), str(message)))
else:
logger.debug("Outgoing: %s", repr(message))
data = message.serialize() data = message.serialize()
if self.key: if self.key:
data = crypto.encrypt(data, self.key) data = crypto.encrypt(data, self.key)
logger.debug("Outgoing: %s", repr(message.msg))
self.connection.put_message(data) self.connection.put_message(data)
def _recv_messages(self): def _recv_messages(self):
@ -149,8 +156,6 @@ class CMClient:
emsg_id, = struct.unpack_from("<I", message) emsg_id, = struct.unpack_from("<I", message)
emsg = EMsg(clear_proto_bit(emsg_id)) emsg = EMsg(clear_proto_bit(emsg_id))
logger.debug("Incoming: %s", repr(emsg))
if emsg in (EMsg.ChannelEncryptRequest, if emsg in (EMsg.ChannelEncryptRequest,
EMsg.ChannelEncryptResponse, EMsg.ChannelEncryptResponse,
EMsg.ChannelEncryptResult, EMsg.ChannelEncryptResult,
@ -161,7 +166,6 @@ class CMClient:
try: try:
if is_proto(emsg_id): if is_proto(emsg_id):
msg = MsgProto(emsg, message) msg = MsgProto(emsg, message)
print str(msg)
else: else:
msg = Msg(emsg, message, extended=True) msg = Msg(emsg, message, extended=True)
except: except:
@ -174,6 +178,12 @@ class CMClient:
self.dispatch_message(emsg, msg) self.dispatch_message(emsg, msg)
def dispatch_message(self, emsg, msg): def dispatch_message(self, emsg, msg):
if self.verbose_debug:
logger.debug("Incoming: %s\n%s" % (repr(msg), str(msg)))
else:
logger.debug("Incoming: %s", repr(msg))
if emsg in self.registered_callbacks: if emsg in self.registered_callbacks:
for callback in list(self.registered_callbacks[emsg]): for callback in list(self.registered_callbacks[emsg]):
if isinstance(callback, event.AsyncResult): if isinstance(callback, event.AsyncResult):
@ -186,7 +196,7 @@ class CMClient:
if emsg not in self.registered_callbacks: if emsg not in self.registered_callbacks:
self.registered_callbacks[emsg] = [callback] self.registered_callbacks[emsg] = [callback]
else: else:
callbacks = self.registered_callbacks[emsg] allbacks = self.registered_callbacks[emsg]
if callback not in callbacks: if callback not in callbacks:
callbacks.append(callback) callbacks.append(callback)
@ -238,14 +248,19 @@ class CMClient:
logger.debug("Event: Ready") logger.debug("Event: Ready")
def _handle_multi(self, emsg, msg): def _handle_multi(self, emsg, msg):
logger.debug("Unzipping CMsgMulti") logger.debug("Unpacking CMsgMulti")
data = zipfile.ZipFile(StringIO(msg.body.message_body)).read('z') if msg.body.size_unzipped:
logger.debug("Unzipping body")
if len(data) != msg.body.size_unzipped: data = zipfile.ZipFile(StringIO(msg.body.message_body)).read('z')
logger.fatal("Unzipped size mismatch")
self.disconnect() if len(data) != msg.body.size_unzipped:
return logger.fatal("Unzipped size mismatch")
self.disconnect()
return
else:
data = msg.body.message_body
while len(data) > 0: while len(data) > 0:
size, = struct.unpack_from("<I", data) size, = struct.unpack_from("<I", data)
@ -260,19 +275,22 @@ class CMClient:
self.send_message(message) self.send_message(message)
def _handle_logon(self, emsg, msg): def _handle_logon(self, emsg, msg):
if msg.body.eresult == EResult.OK: if msg.body.eresult != EResult.OK:
logger.debug("Logon completed") self.disconnect()
return
logger.debug("Logon completed")
self.steam_id = SteamID(msg.header.steamid) self.steam_id = SteamID(msg.header.steamid)
self.session_id = msg.header.client_sessionid self.session_id = msg.header.client_sessionid
self.cell_id = msg.body.cell_id self.cell_id = msg.body.cell_id
self.webapi_nonce = msg.body.webapi_authenticate_user_nonce self.webapi_nonce = msg.body.webapi_authenticate_user_nonce
if self._heartbeat_loop: if self._heartbeat_loop:
self._heartbeat_loop.kill() self._heartbeat_loop.kill()
logger.debug("Heartbeat started.") logger.debug("Heartbeat started.")
interval = msg.body.out_of_game_heartbeat_seconds interval = msg.body.out_of_game_heartbeat_seconds
self._heartbeat_loop = gevent.spawn(self._heartbeat, interval) self._heartbeat_loop = gevent.spawn(self._heartbeat, interval)

77
steam/client/msg.py

@ -33,6 +33,12 @@ class MsgHdr:
self.msg = EMsg(msg) self.msg = EMsg(msg)
def __str__(self):
return '\n'.join(["msg: %s" % repr(self.msg),
"targetJobID: %s" % self.targetJobID,
"sourceJobID: %s" % self.sourceJobID,
])
class ExtendedMsgHdr: class ExtendedMsgHdr:
_size = struct.calcsize("<IBHqqBqi") _size = struct.calcsize("<IBHqqBqi")
@ -78,6 +84,17 @@ class ExtendedMsgHdr:
if self.headerSize != 36 or self.headerVersion != 2: if self.headerSize != 36 or self.headerVersion != 2:
raise RuntimeError("Failed to parse header") raise RuntimeError("Failed to parse header")
def __str__(self):
return '\n'.join(["msg: %s" % self.msg,
"headerSize: %s" % self.headerSize,
"headerVersion: %s" % self.headerVersion,
"targetJobID: %s" % self.targetJobID,
"sourceJobID: %s" % self.sourceJobID,
"headerCanary: %s" % self.headerCanary,
"steamID: %s" % self.steamID,
"sessionID: %s" % self.sessionID,
])
protobuf_mask = 0x80000000 protobuf_mask = 0x80000000
@ -142,8 +159,26 @@ class Msg:
def serialize(self): def serialize(self):
return self.header.serialize() + self.body.serialize() return self.header.serialize() + self.body.serialize()
def __repr__(self):
return "<Msg %s>" % repr(self.msg)
def __str__(self): def __str__(self):
return '' rows = ["Msg"]
header = str(self.header)
if header:
rows.append("-------------- header --")
rows.append(header)
body = str(self.body)
if body:
rows.append("---------------- body --")
rows.append(body)
if len(rows) == 1:
rows[0] += " (empty)"
return '\n'.join(rows)
cmsg_lookup = None cmsg_lookup = None
@ -195,13 +230,26 @@ class MsgProto:
def serialize(self): def serialize(self):
return self._header.serialize() + self.body.SerializeToString() return self._header.serialize() + self.body.SerializeToString()
def __repr__(self):
return "<MsgProto %s>" % repr(self.msg)
def __str__(self): def __str__(self):
return '\n'.join(['MsgProto', rows = ["MsgProto"]
'-' * 20,
str(self.header), header = str(self.header).rstrip()
'-' * 20, if header:
str(self.body), rows.append("-------------- header --")
]) rows.append(header)
body = str(self.body).rstrip()
if body:
rows.append("---------------- body --")
rows.append(body)
if len(rows) == 1:
rows[0] += " (empty)"
return '\n'.join(rows)
class ChannelEncryptRequest: class ChannelEncryptRequest:
@ -222,6 +270,11 @@ class ChannelEncryptRequest:
self.universe = EUniverse(universe) self.universe = EUniverse(universe)
def __str__(self):
return '\n'.join(["protocolVersion: %s" % self.protocolVersion,
"universe: %s" % repr(self.universe),
])
class ChannelEncryptResponse: class ChannelEncryptResponse:
def __init__(self, data=None): def __init__(self, data=None):
@ -250,6 +303,13 @@ class ChannelEncryptResponse:
_, _,
) = struct.unpack_from("<II128sII", data) ) = struct.unpack_from("<II128sII", data)
def __str__(self):
return '\n'.join(["protocolVersion: %s" % self.protocolVersion,
"keySize: %s" % self.keySize,
"key: %s" % repr(self.key),
"crc: %s" % self.crc,
])
class ChannelEncryptResult: class ChannelEncryptResult:
def __init__(self, data=None): def __init__(self, data=None):
@ -264,3 +324,6 @@ class ChannelEncryptResult:
def load(self, data): def load(self, data):
(result,) = struct.unpack_from("<I", data) (result,) = struct.unpack_from("<I", data)
self.result = EResult(result) self.result = EResult(result)
def __str__(self):
return "result: %s" % repr(self.result)

Loading…
Cancel
Save