Skip to content

Commit

Permalink
revert _recv_from_sock()
Browse files Browse the repository at this point in the history
  • Loading branch information
nakagami committed Feb 3, 2024
1 parent 23d359b commit f6e293c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
20 changes: 17 additions & 3 deletions drda/ddm.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,21 @@
from drda import codepoint as cp
from drda import consts
from drda import secmec9
from drda import utils


def _recv_from_sock(sock, nbytes, max_attempts=16):
n = nbytes
attempts = 0
received = b''
while n > 0 and attempts < max_attempts:
bs = sock.recv(n)
if len(bs) > 0:
received += bs
n -= len(bs)
attempts = 0
else:
attempts += 1
return received


def _send_to_sock(sock, b):
Expand Down Expand Up @@ -216,7 +230,7 @@ def parse_sqldard(obj, enc, endian, db_type):

def read_dds(sock):
"Read one DDS packet from socket"
b = utils.read_from_stream(sock, 6)
b = _recv_from_sock(sock, 6)

if len(b) != 6 or b[2] != 0xD0:
raise ConnectionError("invalid DDS packet from socket")
Expand All @@ -225,7 +239,7 @@ def read_dds(sock):
dds_type = b[3] & 0b1111
chained = b[3] & 0b01000000
number = int.from_bytes(b[4:6], byteorder='big')
obj = utils.read_from_stream(sock, ln-6)
obj = _recv_from_sock(sock, ln-6)

if len(obj) != ln - 6:
raise ConnectionError("invalid DDS packet from socket")
Expand Down
19 changes: 2 additions & 17 deletions drda/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,8 @@
DRDA_TYPE_NBOOLEAN = 0xBF


def read_from_stream(stream, nbytes, max_attempts=16):
if hasattr(stream, "read"):
return stream.read(nbytes)

n = nbytes
attempts = 0
received = b''
while n > 0 and attempts < max_attempts:
bs = stream.recv(n)
if len(bs) > 0:
received += bs
n -= len(bs)
attempts = 0
else:
attempts += 1
return received

def read_from_stream(stream, nbytes):
return stream.read(nbytes)

def read_field(t, ps, stream, endian):
"""
Expand Down

0 comments on commit f6e293c

Please sign in to comment.