Skip to content

Commit

Permalink
bpo-41246: IOCP Proactor same socket overlapped callbacks
Browse files Browse the repository at this point in the history
Give the same callback function for when the overlapped operation is
done for the functions recv, recv_into, recvfrom, sendto, send
and sendfile inside IocpProactor.
  • Loading branch information
tontinton committed Jul 8, 2020
1 parent b26a0db commit 08df8fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 65 deletions.
82 changes: 17 additions & 65 deletions Lib/asyncio/windows_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,17 @@ def _result(self, value):
fut.set_result(value)
return fut

@staticmethod
def finish_socket_func(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
_overlapped.ERROR_OPERATION_ABORTED):
raise ConnectionResetError(*exc.args)
else:
raise

def recv(self, conn, nbytes, flags=0):
self._register_with_iocp(conn)
ov = _overlapped.Overlapped(NULL)
Expand All @@ -448,17 +459,7 @@ def recv(self, conn, nbytes, flags=0):
except BrokenPipeError:
return self._result(b'')

def finish_recv(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
_overlapped.ERROR_OPERATION_ABORTED):
raise ConnectionResetError(*exc.args)
else:
raise

return self._register(ov, conn, finish_recv)
return self._register(ov, conn, self.finish_socket_func)

def recv_into(self, conn, buf, flags=0):
self._register_with_iocp(conn)
Expand All @@ -471,17 +472,7 @@ def recv_into(self, conn, buf, flags=0):
except BrokenPipeError:
return self._result(b'')

def finish_recv(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
_overlapped.ERROR_OPERATION_ABORTED):
raise ConnectionResetError(*exc.args)
else:
raise

return self._register(ov, conn, finish_recv)
return self._register(ov, conn, self.finish_socket_func)

def recvfrom(self, conn, nbytes, flags=0):
self._register_with_iocp(conn)
Expand All @@ -491,35 +482,15 @@ def recvfrom(self, conn, nbytes, flags=0):
except BrokenPipeError:
return self._result((b'', None))

def finish_recv(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
_overlapped.ERROR_OPERATION_ABORTED):
raise ConnectionResetError(*exc.args)
else:
raise

return self._register(ov, conn, finish_recv)
return self._register(ov, conn, self.finish_socket_func)

def sendto(self, conn, buf, flags=0, addr=None):
self._register_with_iocp(conn)
ov = _overlapped.Overlapped(NULL)

ov.WSASendTo(conn.fileno(), buf, flags, addr)

def finish_send(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
_overlapped.ERROR_OPERATION_ABORTED):
raise ConnectionResetError(*exc.args)
else:
raise

return self._register(ov, conn, finish_send)
return self._register(ov, conn, self.finish_socket_func)

def send(self, conn, buf, flags=0):
self._register_with_iocp(conn)
Expand All @@ -529,17 +500,7 @@ def send(self, conn, buf, flags=0):
else:
ov.WriteFile(conn.fileno(), buf)

def finish_send(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
_overlapped.ERROR_OPERATION_ABORTED):
raise ConnectionResetError(*exc.args)
else:
raise

return self._register(ov, conn, finish_send)
return self._register(ov, conn, self.finish_socket_func)

def accept(self, listener):
self._register_with_iocp(listener)
Expand Down Expand Up @@ -610,16 +571,7 @@ def sendfile(self, sock, file, offset, count):
offset_low, offset_high,
count, 0, 0)

def finish_sendfile(trans, key, ov):
try:
return ov.getresult()
except OSError as exc:
if exc.winerror in (_overlapped.ERROR_NETNAME_DELETED,
_overlapped.ERROR_OPERATION_ABORTED):
raise ConnectionResetError(*exc.args)
else:
raise
return self._register(ov, sock, finish_sendfile)
return self._register(ov, sock, self.finish_socket_func)

def accept_pipe(self, pipe):
self._register_with_iocp(pipe)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Give the same callback function for when the overlapped operation is done to
the functions ``recv``, ``recv_into``, ``recvfrom``, ``sendto``, ``send``
and ``sendfile`` inside ``IocpProactor``.

0 comments on commit 08df8fc

Please sign in to comment.