Skip to content

Commit

Permalink
Fix ordering of popping messages, and prevent excessive timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
cameroncros committed Jul 10, 2023
1 parent bec4c31 commit ad173d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions octoprint_discordremote/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from octoprint_discordremote.command import Command
from .discordlink import DiscordLink
from .libs import ipgetter
from .proto.messages_pb2 import EmbedContent, ProtoFile, Response
from .proto.messages_pb2 import EmbedContent, ProtoFile, Response, Presence


class DiscordRemotePlugin(octoprint.plugin.EventHandlerPlugin,
Expand Down Expand Up @@ -702,7 +702,8 @@ def periodic_reporting(self):

def update_presence(self, param: str):
if self.discord is not None:
self.discord.update_presence(param)
response = Response(presence=Presence(presence=param))
self.discord.send(response)


# If you want your plugin to be registered within OctoPrint under a different name than what you defined in setup.py
Expand Down
20 changes: 14 additions & 6 deletions octoprint_discordremote/genericforeversocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def sendsafe(self, data: bytes):

def peek(self, length: int) -> bytes:
try:
self.socket.settimeout(0.1)
tmp = self.socket.recv(length, socket.MSG_PEEK + socket.MSG_DONTWAIT)
if len(tmp) == 0:
raise GenericForeverSocket.ConnectionClosed()
Expand All @@ -39,6 +40,8 @@ def peek(self, length: int) -> bytes:
raise GenericForeverSocket.ConnectionClosed()
except BlockingIOError:
raise TimeoutError("Data not ready yet - Would block")
finally:
self.socket.settimeout(10)

def recvblocking(self, length: int) -> bytes:
"""
Expand All @@ -49,7 +52,7 @@ def recvblocking(self, length: int) -> bytes:
data = b''
while len(data) < length:
try:
tmp = self.socket.recv(length-len(data))
tmp = self.socket.recv(length - len(data))
if len(tmp) == 0:
raise GenericForeverSocket.ConnectionClosed()
data += tmp
Expand Down Expand Up @@ -104,7 +107,7 @@ def thread_fn(self):
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 300)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 2)
except Exception as e:
print(e)
self.logger.error(f"Exception: [{e}]")
time.sleep(2)
continue

Expand All @@ -123,10 +126,15 @@ def thread_fn(self):
except socket.timeout:
pass

if len(self.queued_messages) != 0:
with self.queued_messages_lock:
self.write_fn(safe, self.queued_messages[0])
self.queued_messages.pop()
try:
if len(self.queued_messages) > 0:
with self.queued_messages_lock:
self.write_fn(safe, self.queued_messages[0])
self.queued_messages.pop(0)
except TimeoutError:
pass
except socket.timeout:
pass

if not self.running:
s.close()
Expand Down

0 comments on commit ad173d2

Please sign in to comment.