1

I am new to Rust, and I am writing a library that creates background threads that listen and handle TCP communications. I want to store the latest n bytes for every TCP client and have Python to be able to fetch them. The way I am thinking about this is to have shared buffers, although I am not sure how to achieve that given Rust's memory model.

This is what I want to achieve at the end:

import tcp_server_pyo3

# How can I return something that would keep track of TCP connections?
listener = tcp_server_pyo3.start("127.0.0.1:6142")

# So I could print all the TCP clients that are currently connected
print(listener.connections()) 

# And this would return the latest n bytes received for the "client_id"
listener.read("client_id")

Below is what I have so far. It's currently able to create a listener and connection handler threads. What should I add/change to be able to keep track of connections and read latest bytes that were communicated from Python?

My code: tcp_server_pyo3.rs

I am not sure if I am thinking about this the right way. I though of global variables but people are saying not to use global variables in Rust.

  • Questions like this are really hard to answer. You'll need some datastructures to hold all the info you're interested in. To avoid globals you can pass the around as function parameters. But since you're learning go ahead and try it with globals, see why people recommend not to use them. (It will probably be harder than just passing the same argument around everywhere). – cafce25 Nov 23 '22 at 23:06

0 Answers0