Make communication thread safe
This commit is contained in:
		
							parent
							
								
									83f1c3fc93
								
							
						
					
					
						commit
						1296c3da48
					
				
					 1 changed files with 13 additions and 10 deletions
				
			
		| 
						 | 
					@ -5,6 +5,7 @@ from Crypto.Cipher import AES
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
import random
 | 
					import random
 | 
				
			||||||
import socket
 | 
					import socket
 | 
				
			||||||
 | 
					import threading
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def gendevice(devtype, host, mac):
 | 
					def gendevice(devtype, host, mac):
 | 
				
			||||||
  if devtype == 0: # SP1
 | 
					  if devtype == 0: # SP1
 | 
				
			||||||
| 
						 | 
					@ -138,6 +139,7 @@ class device:
 | 
				
			||||||
    self.cs.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
 | 
					    self.cs.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
 | 
				
			||||||
    self.cs.bind(('',0))
 | 
					    self.cs.bind(('',0))
 | 
				
			||||||
    self.type = "Unknown"
 | 
					    self.type = "Unknown"
 | 
				
			||||||
 | 
					    self.lock = threading.Lock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def auth(self):
 | 
					  def auth(self):
 | 
				
			||||||
    payload = bytearray(0x50)
 | 
					    payload = bytearray(0x50)
 | 
				
			||||||
| 
						 | 
					@ -228,6 +230,7 @@ class device:
 | 
				
			||||||
    packet[0x21] = checksum >> 8
 | 
					    packet[0x21] = checksum >> 8
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    starttime = time.time()
 | 
					    starttime = time.time()
 | 
				
			||||||
 | 
					    with self.lock:
 | 
				
			||||||
      while True:
 | 
					      while True:
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
          self.cs.sendto(packet, self.host)
 | 
					          self.cs.sendto(packet, self.host)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue