[gnome-keysign: 41/65] bluetoothreceive: cleaner check for end of connection



commit a7974694bb7ee2f7f8df1fcae491b1da8246e57a
Author: RyuzakiKK <aasonykk gmail com>
Date:   Mon Aug 21 11:55:06 2017 +0200

    bluetoothreceive: cleaner check for end of connection

 keysign/bluetoothreceive.py | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
---
diff --git a/keysign/bluetoothreceive.py b/keysign/bluetoothreceive.py
index cce0338..b6fa1a2 100644
--- a/keysign/bluetoothreceive.py
+++ b/keysign/bluetoothreceive.py
@@ -53,14 +53,21 @@ class BluetoothReceive:
                     raise be
             success = False
             while not self.stopped and not success:
-                r, w, e = yield threads.deferToThread(select.select, [self.client_socket], [], [], True)
+                r, w, e = yield threads.deferToThread(select.select, [self.client_socket], [], [], 0.5)
                 if r:
                     log.info("Connection established")
                     self.client_socket.setblocking(True)
                     success = True
-                    while len(message) < 35 or message[-35:] != b"-----END PGP PUBLIC KEY BLOCK-----\n":
-                        part_message = yield threads.deferToThread(self.client_socket.recv, self.size)
-                        message += part_message
+                    # try to receive until the sender closes the connection
+                    try:
+                        while True:
+                            part_message = yield threads.deferToThread(self.client_socket.recv, self.size)
+                            message += part_message
+                    except BluetoothError as be:
+                        if be.args[0] == "(104, 'Connection reset by peer')":
+                            log.info("Bluetooth connection closed, let's check if we downloaded the key")
+                        else:
+                            raise be
             mac_key = fingerprint_from_keydata(message)
             verified = None
             if mac:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]