Add hand-shaking code when writing

There was a race condition where both the end of bytes and end of planes
messages could get queued and consumed as a single event by the C++
side.

This would lead to an infinite polling situation as the necessary
hand-shaking from Java would never come.

The C++ side will now acknowledge it has received hand-shaking signals
during writing, allowing the process to be better synchronized.