From 0b2e1c7a54b7322226f26f6f2243fd71b3874771 Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Wed, 23 Mar 2022 15:56:07 +0100
Subject: [PATCH] L2SS-703: Use socket.recv to read from sockets, as Windows
 cannot use os.read for that

---
 .../statistics_writer/receiver.py                    | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/tangostationcontrol/tangostationcontrol/statistics_writer/receiver.py b/tangostationcontrol/tangostationcontrol/statistics_writer/receiver.py
index c481d085e..b70446c30 100644
--- a/tangostationcontrol/tangostationcontrol/statistics_writer/receiver.py
+++ b/tangostationcontrol/tangostationcontrol/statistics_writer/receiver.py
@@ -27,6 +27,12 @@ class receiver:
         # add payload to the header, and return the full packet
         return header + payload
 
+    def _read(self, length: int) -> bytes:
+        """ Low-level read function to fetch at most "length" (>1) bytes. Returns
+            nothing if there is no data left. """
+
+        return os.read(self.fd, length)
+
     def read_data(self, data_length: int) -> bytes:
         """ Read exactly data_length bytes from the TCP connection. """
 
@@ -35,7 +41,7 @@ class receiver:
             # try to read the remainder.
             # NOTE: recv() may return less data than requested, and returns 0
             # if there is nothing left to read (end of stream)
-            more_data = os.read(self.fd, data_length - len(data))
+            more_data = self._read(data_length - len(data))
             if not more_data:
                 # connection got dropped
                 raise EOFError("End of stream")
@@ -54,6 +60,10 @@ class tcp_receiver(receiver):
 
         super().__init__(fd=self.sock.fileno())
 
+    def _read(self, length):
+        # On Windows, we cannot use os.read to read from sockets
+        return self.sock.recv(length)
+
     def reconnect(self):
         self.fd = None
         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-- 
GitLab