Commit 5ba21d28 authored by Alexander Korolkov's avatar Alexander Korolkov

Receive data from socket in a more reliable way

socket.recv() may return partial answer, so it must be called in loop.
parent 5c1474c7
......@@ -7,6 +7,7 @@ import socket
from threading import Thread, Condition, Lock
import opcua.uaprotocol as ua
import opcua.utils as utils
class Buffer(object):
"""
......@@ -107,7 +108,7 @@ class BinaryClient(object):
def _receive_body(self, size):
self.logger.debug("reading body of message (%s bytes)", size)
data = self._socket.recv(size)
data = utils.recv_all(self._socket, size)
if size != len(data):
raise Exception("Error, did not received expected number of bytes, got {}, asked for {}".format(len(data), size))
#return io.BytesIO(data)
......
......@@ -4,6 +4,7 @@ import logging
import opcua.uaprotocol_auto as auto
import opcua.uatypes as uatypes
import opcua.utils as utils
logger = logging.getLogger(__name__)
......@@ -11,7 +12,7 @@ class SocketClosedException(Exception):
pass
def get_bytes_from_sock(sock, size):
data = sock.recv(size)
data = utils.recv_all(sock, size)
if len(data) < size: #socket has closed!
raise SocketClosedException("Server socket has closed")
return io.BytesIO(data)
......
import socket
def recv_all(socket, size):
"""
Receive up to size bytes from socket
"""
data = b''
while size > 0:
chunk = socket.recv(size)
if not chunk:
break
data += chunk
size -= len(chunk)
return data
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment