mirror of
https://github.com/nikdoof/python-ts3.git
synced 2025-12-16 19:42:22 +00:00
Added locking for IO, avoid call collisions on multithreaded applications
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
import time
|
import time
|
||||||
import telnetlib
|
import telnetlib
|
||||||
import logging
|
import logging
|
||||||
|
from threading import Lock
|
||||||
|
|
||||||
from defines import *
|
from defines import *
|
||||||
|
|
||||||
@@ -85,6 +86,8 @@ class TS3Response():
|
|||||||
|
|
||||||
class TS3Proto():
|
class TS3Proto():
|
||||||
|
|
||||||
|
io_lock = Lock()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def logger(self):
|
def logger(self):
|
||||||
if not hasattr(self, "_logger"):
|
if not hasattr(self, "_logger"):
|
||||||
@@ -92,6 +95,7 @@ class TS3Proto():
|
|||||||
return self._logger
|
return self._logger
|
||||||
|
|
||||||
def connect(self, ip, port=10011, timeout=5):
|
def connect(self, ip, port=10011, timeout=5):
|
||||||
|
self.io_lock.acquire()
|
||||||
try:
|
try:
|
||||||
self._telnet = telnetlib.Telnet(ip, port)
|
self._telnet = telnetlib.Telnet(ip, port)
|
||||||
except telnetlib.socket.error:
|
except telnetlib.socket.error:
|
||||||
@@ -99,9 +103,10 @@ class TS3Proto():
|
|||||||
|
|
||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
self._connected = False
|
self._connected = False
|
||||||
|
|
||||||
data = self._telnet.read_until("\n\r", self._timeout)
|
data = self._telnet.read_until("\n\r", self._timeout)
|
||||||
|
self.io_lock.release()
|
||||||
|
|
||||||
if data.endswith("TS3\n\r"):
|
if data.endswith("TS3\n\r"):
|
||||||
self._connected = True
|
self._connected = True
|
||||||
|
|
||||||
@@ -120,10 +125,13 @@ class TS3Proto():
|
|||||||
|
|
||||||
commandstr = self.construct_command(command, keys=keys, opts=opts)
|
commandstr = self.construct_command(command, keys=keys, opts=opts)
|
||||||
self.logger.debug("send_command - %s" % commandstr)
|
self.logger.debug("send_command - %s" % commandstr)
|
||||||
|
|
||||||
|
self.io_lock.acquire()
|
||||||
self._telnet.write("%s\n\r" % commandstr)
|
self._telnet.write("%s\n\r" % commandstr)
|
||||||
|
|
||||||
data = ""
|
data = ""
|
||||||
response = self._telnet.read_until("\n\r", self._timeout)
|
response = self._telnet.read_until("\n\r", self._timeout)
|
||||||
|
self.io_lock.release()
|
||||||
|
|
||||||
if not response.startswith("error"):
|
if not response.startswith("error"):
|
||||||
# what we just got was extra data
|
# what we just got was extra data
|
||||||
|
|||||||
Reference in New Issue
Block a user