Hello,
I'm learning the basics of the Tokio library and at the same time moving my first steps into the async world.
My task is to implement the following workflow:
- A client connects to a TCP proxy, sends one ASCII string
- the proxy reads the payload
- based on the content, the proxy opens a connection to server A or B, forwards the payload
- the proxy receives the response from the server and forward it to the client
- the connection is closed on both ends
I've started rewriting the Tokio proxy example using the tokio_io::AsyncRead::framed
method in order to parse the payload received (as suggested on the Tokio Gitter channel) before forwarding the the server.
My prototype has almost implemented all the points above and "works". The only thing missing is the payload parsing before connecting to the server (currently I connect and blindly forward the payload).
You can see the work in progress in this gist.
I have a couple of questions:
- In the original Tokio code the TcpStream socket is wrapped by a MyTcpStream to implement the socket
shutdown
when done. If I don't do that, the client socket hangs.
From my newbie point of view I'd like to simplify the code shutting down explicitly the socket around here but the compiler borrow checker blocks me from using theclient
down there. Is this possible any way or another? - style: nesting Futures looks to me ugly. I tried splitting them but I still don't understand how the logic blocks should be splitted (I am facing various errors in my trial-and-errors). Any hints? AFAICS each block should return a Future chainable with another code block, until the final
tokio::run(...)
statement.
The next step is to actually implement the payload evaluation right in the middle of the various Future processing, right before connecting to a server. I'll be working on that.
Thank you for evaluting my efforts :^)