Browse Source

refactored CMClient disconnect handling

pull/18/merge
Rossen Georgiev 9 years ago
parent
commit
dbb698ca0e
  1. 36
      steam/core/cm.py
  2. 14
      steam/core/connection.py

36
steam/core/cm.py

@ -56,6 +56,8 @@ class CMClient(EventEmitter):
else: else:
raise ValueError("Only TCP is supported") raise ValueError("Only TCP is supported")
self.connection.event_connected.rawlink(self._handle_disconnect)
self.on(EMsg.ChannelEncryptRequest, self._handle_encrypt_request), self.on(EMsg.ChannelEncryptRequest, self._handle_encrypt_request),
self.on(EMsg.Multi, self._handle_multi), self.on(EMsg.Multi, self._handle_multi),
self.on(EMsg.ClientLogOnResponse, self._handle_logon), self.on(EMsg.ClientLogOnResponse, self._handle_logon),
@ -69,23 +71,25 @@ class CMClient(EventEmitter):
logger.debug("Connect initiated.") logger.debug("Connect initiated.")
while True: while True:
with gevent.Timeout(15, False): server_addr = random.choice(server_list)
server_addr = random.choice(server_list)
self.connection.connect(server_addr)
if not self.connection.event_connected.is_set(): if self.connection.connect(server_addr):
logger.debug("Failed to connect. Retrying...") break
continue
break logger.debug("Failed to connect. Retrying...")
self.connected = True self.connected = True
self.emit("connected") self.emit("connected")
self._recv_loop = gevent.spawn(self._recv_messages) self._recv_loop = gevent.spawn(self._recv_messages)
def _handle_disconnect(self, event):
if not event.is_set():
gevent.spawn(self.disconnect)
def disconnect(self, reconnect=False): def disconnect(self, reconnect=False):
if reconnect: if not self.connected:
gevent.spawn(self.connect) return
self.connected = False
self.connection.disconnect() self.connection.disconnect()
@ -96,6 +100,9 @@ class CMClient(EventEmitter):
self._init_attributes() self._init_attributes()
self.emit('disconnected') self.emit('disconnected')
if reconnect:
gevent.spawn(self.connect)
def _init_attributes(self): def _init_attributes(self):
self.connected = False self.connected = False
@ -133,14 +140,9 @@ class CMClient(EventEmitter):
self.connection.put_message(data) self.connection.put_message(data)
def _recv_messages(self): def _recv_messages(self):
while True: for message in self.connection:
try: if not self.connected:
message = self.connection.get_message(timeout=1) break
except queue.Empty:
if not self.connection.event_connected.is_set():
gevent.spawn(self.disconnect)
return
continue
if self.key: if self.key:
if self.hmac_secret: if self.hmac_secret:

14
steam/core/connection.py

@ -32,7 +32,11 @@ class Connection:
self._new_socket() self._new_socket()
logger.debug("Attempting connection to %s", str(server_addr)) logger.debug("Attempting connection to %s", str(server_addr))
self._connect(server_addr)
try:
self._connect(server_addr)
except socket.error:
return False
self.server_addr = server_addr self.server_addr = server_addr
@ -41,6 +45,7 @@ class Connection:
logger.debug("Connected.") logger.debug("Connected.")
self.event_connected.set() self.event_connected.set()
return True
def disconnect(self): def disconnect(self):
if not self.event_connected.is_set(): if not self.event_connected.is_set():
@ -62,8 +67,15 @@ class Connection:
self.socket.close() self.socket.close()
logger.debug("Disconnected.") logger.debug("Disconnected.")
# hack
# rawlink will only call the callback on set
self.event_connected.set()
self.event_connected.clear() self.event_connected.clear()
def __iter__(self):
return self.recv_queue
def get_message(self, block=True, timeout=None): def get_message(self, block=True, timeout=None):
return self.recv_queue.get(block, timeout) return self.recv_queue.get(block, timeout)

Loading…
Cancel
Save