Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow server to receive files more than initial receive buffer size #3

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/mujinasync/asynchttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def _TryReceiveHttpRequest(self, connection):
if 'content-length' in headers:
length = int(headers['content-length'])
if len(bufferData) < length + bufferConsumed:
log.warn('failed to parse http request because it is too long %d > %d', length + bufferConsumed, len(bufferData))
return None
body = bytearray(bufferData[bufferConsumed:bufferConsumed + length])
bufferConsumed += length
Expand Down Expand Up @@ -231,6 +232,7 @@ def _TryReceiveHttpResponse(self, connection):
if 'content-length' in headers:
length = int(headers['content-length'])
if len(bufferData) < length + bufferConsumed:
log.warn('failed to parse http response because it is too long %d > %d', length + bufferConsumed, len(bufferData))
justism marked this conversation as resolved.
Show resolved Hide resolved
return None
body = bytearray(bufferData[bufferConsumed:bufferConsumed + length])
bufferConsumed += length
Expand Down
25 changes: 18 additions & 7 deletions python/mujinasync/asynctcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,30 @@ def SpinOnce(self, timeout=0):
continue

serverClient, connection = socketConnections[rsocket]
try:
received = rsocket.recv_into(connection.receiveBuffer.writeView)
except Exception as e:
justism marked this conversation as resolved.
Show resolved Hide resolved
connection.closeType = 'Immediate'
log.exception('error while trying to receive from connection %s: %s', connection, e)
totalReceived = 0
while True:
try:
received = rsocket.recv_into(connection.receiveBuffer.writeView)
if received == 0:
break
connection.receiveBuffer.size += received
totalReceived += received
if connection.receiveBuffer.capacity - connection.receiveBuffer.size < connection.receiveBuffer.capacity:
connection.receiveBuffer.capacity *= 2
except socket.error as e:
if e.errno not in (errno.EAGAIN, errno.EWOULDBLOCK):
connection.closeType = 'Immediate'
log.exception('error while trying to receive from connection %s: %s', connection, e)
break

if connection.closeType == 'Immediate':
continue

if received == 0:
if totalReceived == 0:
connection.closeType = 'AfterSend'
log.debug('received nothing from connection, maybe closed: %s', connection)
continue

connection.receiveBuffer.size += received
receivedConnections.append((serverClient, connection))

# handle sockets that can write
Expand Down
Loading