Skip to content

Commit 9631d28

Browse files
committed
added thread safety
1 parent c13a2bf commit 9631d28

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

openrgb/network.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, update_callback: Callable, address: str = "127.0.0.1", port:
3131
:param port: the port of the SDK server
3232
:param name: the string that will be displayed on the OpenRGB SDK tab's list of clients
3333
'''
34+
self.lock = threading.Lock()
3435
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3536
self.sock.connect((address, port))
3637

@@ -44,7 +45,7 @@ def __init__(self, update_callback: Callable, address: str = "127.0.0.1", port:
4445
# Sending the client name
4546
name = bytes(f"{name}\0", 'utf-8')
4647
self.send_header(0, utils.PacketType.NET_PACKET_ID_SET_CLIENT_NAME, len(name))
47-
self.sock.send(name, NOSIGNAL)
48+
self.send_data(name)
4849

4950
# Requesting the number of devices
5051
self.send_header(0, utils.PacketType.NET_PACKET_ID_REQUEST_CONTROLLER_COUNT, 0)
@@ -99,8 +100,14 @@ def send_header(self, device_id: int, packet_type: int, packet_size: int):
99100
:param packet_type: a utils.PacketType
100101
:param packet_size: the full size of the data to be send after the header
101102
'''
103+
if packet_size > 0:
104+
self.lock.acquire()
102105
self.sock.send(struct.pack('ccccIII', b'O', b'R', b'G', b'B', device_id, packet_type, packet_size), NOSIGNAL)
103106

107+
def send_data(self, data: bytes):
108+
self.sock.send(data, NOSIGNAL)
109+
self.lock.release()
110+
104111
def timeout(self):
105112
while self.state == Status.WAITING:
106113
sleep(.05)

openrgb/orgb.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def set_color(self, color: utils.RGBColor, fast: bool = False):
2828
'''
2929
self.comms.send_header(self.device_id, utils.PacketType.NET_PACKET_ID_RGBCONTROLLER_UPDATESINGLELED, struct.calcsize("i3bx"))
3030
buff = struct.pack("i", self.id) + color.pack()
31-
self.comms.sock.send(buff)
31+
self.comms.send_data(buff)
3232
if not fast:
3333
self.comms.requestDeviceData(self.device_id)
3434

@@ -64,7 +64,7 @@ def set_color(self, color: utils.RGBColor, start: int = 0, end: int = 0, fast: b
6464
self.comms.send_header(self.device_id, utils.PacketType.NET_PACKET_ID_RGBCONTROLLER_UPDATEZONELEDS, struct.calcsize(f"IIH{3*(end - start)}b{(end - start)}x"))
6565
buff = struct.pack("IH", self.id, end - start) + (color.pack())*(end - start)
6666
buff = struct.pack("I", len(buff)) + buff
67-
self.comms.sock.send(buff)
67+
self.comms.send_data(buff)
6868
if not fast:
6969
self.comms.requestDeviceData(self.device_id)
7070

@@ -84,7 +84,7 @@ def set_colors(self, colors: List[utils.RGBColor], start: int = 0, end: int = 0,
8484
self.comms.send_header(self.device_id, utils.PacketType.NET_PACKET_ID_RGBCONTROLLER_UPDATEZONELEDS, struct.calcsize(f"IIH{3*(end - start)}b{(end - start)}x"))
8585
buff = struct.pack("IH", self.id, end - start) + b''.join((color.pack() for color in colors))
8686
buff = struct.pack("I", len(buff)) + buff
87-
self.comms.sock.send(buff)
87+
self.comms.send_data(buff)
8888
if not fast:
8989
self.comms.requestDeviceData(self.device_id)
9090

@@ -128,7 +128,7 @@ def set_color(self, color: utils.RGBColor, start: int = 0, end: int = 0, fast: b
128128
)
129129
buff = struct.pack("H", end - start) + (color.pack())*(end - start)
130130
buff = struct.pack("I", len(buff)) + buff
131-
self.comms.sock.send(buff)
131+
self.comms.send_data(buff)
132132
if not fast:
133133
self.comms.requestDeviceData(self.id)
134134

@@ -152,7 +152,7 @@ def set_colors(self, colors: List[utils.RGBColor], start: int = 0, end: int = 0,
152152
)
153153
buff = struct.pack("H", end - start) + b''.join((color.pack() for color in colors))
154154
buff = struct.pack("I", len(buff)) + buff
155-
self.comms.sock.send(buff)
155+
self.comms.send_data(buff)
156156
if not fast:
157157
self.comms.requestDeviceData(self.id)
158158

@@ -174,7 +174,7 @@ def set_mode(self, mode: Union[int, str, utils.ModeData]):
174174
utils.PacketType.NET_PACKET_ID_RGBCONTROLLER_UPDATEMODE,
175175
len(data)
176176
)
177-
self.comms.sock.send(data)
177+
self.comms.send_data(data)
178178
self.active_mode = mode.id
179179
self.data.active_mode = self.active_mode
180180
if len(mode.colors) == len(self.colors):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="openrgb-python",
8-
version='0.1.1',
8+
version='0.1.2',
99
author="jath03",
1010
description="A python client for the OpenRGB SDK",
1111
long_description=long_description,

0 commit comments

Comments
 (0)