r/rust•
Shadow0133

[Code Review Request] Simple async PixelFlood server

Pixelflut is a very simple (and inefficient) ASCII based network protocol to draw pixels on a screen. You can write a basic client in a single line of shell code if you want, but you only get to change a single pixel at a time.

I decided to learn async by implementing a simple Pixelflut server. Here is the repo. Constructive criticism and patches welcome.

For parsing messages, I tried using tokio_codec (which has outdated deps) and tokio_util with "codec" feature (which didn't work correctly with timeout), so I ended up using AsyncBufRead instead. There is still an issue with updating window in async, because I couldn't make it work with spawn_blocking or block_in_place (which panicks with "can only call blocking when on Tokio runtime"). It also once blow-up with 300 line error message about !Send future when I tried async_writer_ext.write_all(&format("{} {}", x, y)), but binding format! result to variable fixed the issue.

Overall, async still has some rough edges with error messages and docs, but already feels quite nice to write and read.

Posted Dec 30, 2019, 9:59 PM UTC

72
pcpthm•

You are updating the window in a busy loop. You probably want to limit fps limit_update_rate.

Then, an issue is that updating the window can be blocking while you are holding a lock of buffer. Instead of using Mutex and locking, I would use Arc<Vec<AtomicU32>> for the shared resource (pixels). The main thread copies pixels to a separate buffer Vec<u32> each frame.

2
Shadow0133OP•

Thanks for reply. I just implemented the non-locking buffer and simple netcat testing shows it's much faster now.

I didn't used limit_update_rate because minifb by default sets it to 4ms which is probably good enough.

2