|
|
|
@ -35,7 +35,6 @@ cdef class _AsyncioSocket: |
|
|
|
|
self._server = None |
|
|
|
|
self._py_socket = None |
|
|
|
|
self._peername = None |
|
|
|
|
self._loop = asyncio.get_event_loop() |
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
cdef _AsyncioSocket create(grpc_custom_socket * grpc_socket, |
|
|
|
@ -62,27 +61,37 @@ cdef class _AsyncioSocket: |
|
|
|
|
connected = self.is_connected() |
|
|
|
|
return f"<{class_name} {id_} connected={connected}>" |
|
|
|
|
|
|
|
|
|
def _connect_cb(self, future): |
|
|
|
|
async def _async_connect(self, object host, object port,): |
|
|
|
|
self._task_connect = None |
|
|
|
|
try: |
|
|
|
|
self._reader, self._writer = future.result() |
|
|
|
|
self._reader, self._writer = await asyncio.open_connection(host, port) |
|
|
|
|
except Exception as e: |
|
|
|
|
self._grpc_connect_cb( |
|
|
|
|
<grpc_custom_socket*>self._grpc_socket, |
|
|
|
|
grpc_socket_error("Socket connect failed: {}".format(e).encode()) |
|
|
|
|
grpc_socket_error("Socket connect failed: {}: {}".format(type(e), str(e)).encode()) |
|
|
|
|
) |
|
|
|
|
return |
|
|
|
|
finally: |
|
|
|
|
self._task_connect = None |
|
|
|
|
else: |
|
|
|
|
# gRPC default posix implementation disables nagle |
|
|
|
|
# algorithm. |
|
|
|
|
sock = self._writer.transport.get_extra_info('socket') |
|
|
|
|
sock.setsockopt(native_socket.IPPROTO_TCP, native_socket.TCP_NODELAY, True) |
|
|
|
|
|
|
|
|
|
# gRPC default posix implementation disables nagle |
|
|
|
|
# algorithm. |
|
|
|
|
sock = self._writer.transport.get_extra_info('socket') |
|
|
|
|
sock.setsockopt(native_socket.IPPROTO_TCP, native_socket.TCP_NODELAY, True) |
|
|
|
|
self._grpc_connect_cb( |
|
|
|
|
<grpc_custom_socket*>self._grpc_socket, |
|
|
|
|
<grpc_error*>0 |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self._grpc_connect_cb( |
|
|
|
|
<grpc_custom_socket*>self._grpc_socket, |
|
|
|
|
<grpc_error*>0 |
|
|
|
|
cdef void connect(self, |
|
|
|
|
object host, |
|
|
|
|
object port, |
|
|
|
|
grpc_custom_connect_callback grpc_connect_cb): |
|
|
|
|
assert not self._reader |
|
|
|
|
assert not self._task_connect |
|
|
|
|
|
|
|
|
|
self._task_connect = grpc_aio_loop().create_task( |
|
|
|
|
self._async_connect(host, port) |
|
|
|
|
) |
|
|
|
|
self._grpc_connect_cb = grpc_connect_cb |
|
|
|
|
|
|
|
|
|
async def _async_read(self, size_t length): |
|
|
|
|
self._task_read = None |
|
|
|
@ -106,25 +115,12 @@ cdef class _AsyncioSocket: |
|
|
|
|
<grpc_error*>0 |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
cdef void connect(self, |
|
|
|
|
object host, |
|
|
|
|
object port, |
|
|
|
|
grpc_custom_connect_callback grpc_connect_cb): |
|
|
|
|
assert not self._reader |
|
|
|
|
assert not self._task_connect |
|
|
|
|
|
|
|
|
|
self._task_connect = asyncio.ensure_future( |
|
|
|
|
asyncio.open_connection(host, port) |
|
|
|
|
) |
|
|
|
|
self._grpc_connect_cb = grpc_connect_cb |
|
|
|
|
self._task_connect.add_done_callback(self._connect_cb) |
|
|
|
|
|
|
|
|
|
cdef void read(self, char * buffer_, size_t length, grpc_custom_read_callback grpc_read_cb): |
|
|
|
|
assert not self._task_read |
|
|
|
|
|
|
|
|
|
self._grpc_read_cb = grpc_read_cb |
|
|
|
|
self._read_buffer = buffer_ |
|
|
|
|
self._task_read = self._loop.create_task(self._async_read(length)) |
|
|
|
|
self._task_read = grpc_aio_loop().create_task(self._async_read(length)) |
|
|
|
|
|
|
|
|
|
async def _async_write(self, bytearray outbound_buffer): |
|
|
|
|
self._writer.write(outbound_buffer) |
|
|
|
@ -157,7 +153,7 @@ cdef class _AsyncioSocket: |
|
|
|
|
outbound_buffer.extend(<bytes>start[:length]) |
|
|
|
|
|
|
|
|
|
self._grpc_write_cb = grpc_write_cb |
|
|
|
|
self._task_write = self._loop.create_task(self._async_write(outbound_buffer)) |
|
|
|
|
self._task_write = grpc_aio_loop().create_task(self._async_write(outbound_buffer)) |
|
|
|
|
|
|
|
|
|
cdef bint is_connected(self): |
|
|
|
|
return self._reader and not self._reader._transport.is_closing() |
|
|
|
@ -201,7 +197,7 @@ cdef class _AsyncioSocket: |
|
|
|
|
sock=self._py_socket, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self._loop.create_task(create_asyncio_server()) |
|
|
|
|
grpc_aio_loop().create_task(create_asyncio_server()) |
|
|
|
|
|
|
|
|
|
cdef accept(self, |
|
|
|
|
grpc_custom_socket* grpc_socket_client, |
|
|
|
|