about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthew Garrett <mjg59@coreos.com>2016-12-25T11·07+0000
committerGitHub <noreply@github.com>2016-12-25T11·07+0000
commita0a1c1b7a1726ee3bf96c1f2afd197ecad00d5e1 (patch)
tree7bf327b521feb5a219a1635487c40c4e22b13879
parent1cd4834fef027ddbed4e58dd597e36eae4744ecd (diff)
parent1296c3da483f37b677c7811c182132af7f5aa77e (diff)
Merge pull request #37 from Danielhiversen/patch-9
Make communication thread safe
-rwxr-xr-xbroadlink/__init__.py23
1 files changed, 13 insertions, 10 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py
index 1ebaf9e50f1a..c00f79a8ce8d 100755
--- a/broadlink/__init__.py
+++ b/broadlink/__init__.py
@@ -5,6 +5,7 @@ from Crypto.Cipher import AES
 import time
 import random
 import socket
+import threading
 
 def gendevice(devtype, host, mac):
   if devtype == 0: # SP1
@@ -140,6 +141,7 @@ class device:
     self.cs.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
     self.cs.bind(('',0))
     self.type = "Unknown"
+    self.lock = threading.Lock()
 
   def auth(self):
     payload = bytearray(0x50)
@@ -230,16 +232,17 @@ class device:
     packet[0x21] = checksum >> 8
 
     starttime = time.time()
-    while True:
-      try:
-        self.cs.sendto(packet, self.host)
-        self.cs.settimeout(1)
-        response = self.cs.recvfrom(1024)
-        break
-      except socket.timeout:
-        if (time.time() - starttime) < self.timeout:
-          pass
-        raise
+    with self.lock:
+      while True:
+        try:
+          self.cs.sendto(packet, self.host)
+          self.cs.settimeout(1)
+          response = self.cs.recvfrom(1024)
+          break
+        except socket.timeout:
+          if (time.time() - starttime) < self.timeout:
+            pass
+          raise
     return bytearray(response[0])