diff options
author | Daniel Høyer Iversen <mail@dahoiv.net> | 2016-12-22T08·51+0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-22T08·51+0100 |
commit | 1296c3da483f37b677c7811c182132af7f5aa77e (patch) | |
tree | d8023665c716343b86f26a1cd49314fc9b5e646e /broadlink | |
parent | 83f1c3fc93344c0264223fbf37ac0198626e006b (diff) |
Make communication thread safe
Diffstat (limited to 'broadlink')
-rwxr-xr-x | broadlink/__init__.py | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py index f4bb1069caa1..1ec3c329eae0 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 @@ -138,6 +139,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) @@ -228,16 +230,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]) |