修复mpd在线检测,mpd掉线时死循环的bug

master
yutent 2023-08-24 14:49:18 +08:00
parent 271a8838fb
commit 8fd907e805
1 changed files with 14 additions and 13 deletions

27
mpd.py
View File

@ -447,7 +447,8 @@ class MPDClient(MPDClientBase, GObject.Object):
'offline': (GObject.SignalFlags.RUN_FIRST, None, ()),
'playing': (GObject.SignalFlags.RUN_FIRST, None, (object, object)),
'song_changed': (GObject.SignalFlags.RUN_FIRST, None, (object, object)),
'state_changed': (GObject.SignalFlags.RUN_FIRST, None, (object,))
'state_changed': (GObject.SignalFlags.RUN_FIRST, None, (object,)),
'error': (GObject.SignalFlags.RUN_FIRST, None, (object,))
}
@ -543,7 +544,7 @@ class MPDClient(MPDClientBase, GObject.Object):
def _read_line(self):
line = self._rbfile.readline().decode("utf-8")
if not line.endswith("\n"):
self.end()
self.destroy()
raise ConnectionError("Connection lost while reading line")
line = line.rstrip("\n")
if line.startswith(ERROR_PREFIX):
@ -597,7 +598,7 @@ class MPDClient(MPDClientBase, GObject.Object):
value = self._read_chunk(chunk_size)
if len(value) != chunk_size:
self.end()
self.destroy()
raise ConnectionError(
"Connection lost while reading binary data: "
"expected %d bytes, got %d" % (chunk_size, len(value))
@ -605,7 +606,7 @@ class MPDClient(MPDClientBase, GObject.Object):
if self._rbfile.read(1) != b"\n":
# newline after binary content
self.end()
self.destroy()
raise ConnectionError("Connection lost while reading line")
obj[key] = value
@ -685,7 +686,7 @@ class MPDClient(MPDClientBase, GObject.Object):
def _check_is_mpd_server(self, line):
if not line.endswith("\n"):
self.end()
self.destroy()
return
line = line.rstrip("\n")
@ -725,7 +726,7 @@ class MPDClient(MPDClientBase, GObject.Object):
except Exception as e:
if e.strerror == 'Connection refused':
self.emit('offline', True)
self.emit('offline')
else:
self.emit('error', e)
@ -772,10 +773,13 @@ class MPDClient(MPDClientBase, GObject.Object):
self._check_is_mpd_server(helloline)
if self.connected:
break
except Exception as e:
self.connected = False
self.end()
self.destroy()
time.sleep(2)
def heart_beat(self):
@ -809,7 +813,7 @@ class MPDClient(MPDClientBase, GObject.Object):
else:
break
self.end()
self.destroy()
self.start()
@ -819,11 +823,8 @@ class MPDClient(MPDClientBase, GObject.Object):
self.emit('online')
self.heart_beat()
def destroy(self):
self.end()
def end(self):
if self._rbfile is not None:
self._rbfile.close()
if self._wfile is not None: