Dear community partners, I'm a newbie from C#.
When writing a simple rust TCP server with Tokio, a confused problem occured :
Find nowhere to get client sockets.
Knowing it's unpleasant to show C# code in Rust community, I have to offer a clear view to the
difference of socket processing between C# and Rust.
In C#, programmer could do this:
pseudocode
main()
{
// A pool for storage of client connections
var connectionPool = new List<connection>();
// When client send something, CallBackMethod is invoked.
server.connect.receivedRaw += CallBackMethod;
}
void CallBackMethod(Connection conn)
{
// Save connection to pool
connectionPool.Add(conn);
}
bool SendToClient(string clientName, string message)
{
// Get client from pool
var client = PickClientFromPool(clientName);
client.Send(message);
}
With a pool, one could send message to any active client in C#.
When come to Rust, I have complete no idea how to keep client connections...
There's tcp server tutorial by tokio offical site, as a echo server, it works really well.
But, how to move client connections to a connection pool for further usage?
let done = socket.incoming()
.map_err(|e| println!("failed to accept socket; error = {:?}", e))
.for_each(move |socket|{
let (reader,writer) = socket.split();
let amt = io::copy(reader, writer);
let msg = amt.then(move |result|{
match result {
Ok((amt,_,_)) => println!("wrote {:?} bytes", amt),
Err(e) => println!("error: {:?}", e),
}
Ok(())
});
tokio::spawn(msg)
});
Errrr, I know it's a boring question.
Having look up answers for weeks, I still can't get any point to achieve my goal...
If there's any Lack of knowledge in my statement, please point out.
Any hint is welcomed.
Thanks.