2

I am trying to implement a SCTP and asyncio based client using pysctp sockets. Ideally, I would like to do something like:

async def sctp_client():
    sock = sctp.sctpsocket_udp(socket.AF_INET)
    reader, writer = await asyncio.open_connection(sock=sock)
    writer.write(b'some data', ppid=5)
    await writer.drain()
    data = await reader.read(100)
    print(f'Received: {data.decode()}')
    writer.close()
    await writer.wait_closed()

In practice, there are two question:

  • Is there a way to pass a "transport factory" to open_connection or to loop.create_connection? I want the transport object contained by the streams to use sctp_recv instead of recv and sctp_send instead of send. Clearly the easiest way is to derive from the original transport class, but I couldn't find a good pythonic way to do it.
  • Is there a way to get a custom StreamWriter from open_connection? Obviously I could create my own one from the original, add a method to the original or just access the internal transport but these options are awfully ugly. The second best way I can think about is just to implement my own open_connection using the loop.create_connection.

Thanks a lot!

TJR
  • 454
  • 8
  • 24

1 Answers1

0

Looking at the source code, there is no way to provide a factory to asyncio.open_connection. That said, the code there isn't terribly complex. Note the last line of the docstring:

(If you want to customize the StreamReader and/or StreamReaderProtocol classes, just copy the code -- there's really nothing special here except some convenience.)

That's exactly what I'd recommend doing.

asthasr
  • 9,125
  • 1
  • 29
  • 43
  • Thanks! this is what I thought. Do you also have any insights about replacing `recv` and `send`? – TJR Nov 06 '20 at 07:20
  • Sure. Note that the first argument of [`loop.create_connection`](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_connection) is called `protocol_factory`. You'll need to find (or write) an implementation of the [`Protocol` interface](https://docs.python.org/3/library/asyncio-protocol.html#protocols) that uses the transport you need. – asthasr Nov 06 '20 at 13:14
  • The problem is that the transport is created independently from the protocol and the `recv` and `send` are used in the transport and not the protocol - so protocol factory will not help here. – TJR Nov 07 '20 at 17:12