Fix number of bytes when writing to disk.

Move protobuf message encoding and buffer logic to write thread. All
calls to f_write now write a fixed amount of bytes to disk.