Problem: I've been trying to get a simple, single-threaded server working under Rust, using the MIO (Metal I/O) abstraction over select/poll/epoll/kqueue
since that's what most closely tracks with my experience as a server writer. Unfortunately "my experience as a server writer" more or less came to halt with the collapse of Sprynet back in 1998. The objective here is to write a framework supporting older line-based protocols such as NNTP, POP3, and Gopher.
Attempted Solution: I've tried to abstract the sever out into several different layers: the server itself, the connection the server has to any given client, and the protocol. The connection marshals the input into lines, which are then placed in the protocol's queue. The protocol generates responses, which are then placed in the outbound queue. The connection sets itself writeable if the protocol says there's content to be delivered, and read-only if not.
Attempted Process: It looks more or less like any mid-grade developer's server, with ridiculous craptons of guard statements, automatic panics, and at least one weird construct to "get around" the borrower. Inside the event loop I had to use an indexed get()
into the array because the borrower didn't like me using an iterator, claiming I'd borrowed a mutable value twice. I'm still waiting for the Rust "borrower insight" everyone claims is coming but I have yet to see.
Result: Well, it works. The only protocol generated so far is an embedded line-based Echo protocol. It's still ugly. The number of additional libraries dragged in (net2, bytes, log, env_logger) balloon the server to almost 12MB in size; the equivalent C program, without all the niceties, comes out to about 18KB. Something is wrong here, but I'm not sure what.
Future work: Clean this thing the hell up. It's neither functional nor SOLID, which means it's just a heck of a mess. It's a manageable mess, I suppose, but still a mess. Investigate if the executable size is the result of static fat binaries, to which I don't object too strongly. Unembed the protocol.