The full implementation
- Here's is the condensed interface file for the OutboundFile class.
class OutboundFile {
public:
OutboundFile();
void initialize(const std::string& source, int sink);
bool sendMoreData();
private:
int source, sink;
static const size_t kBufferSize = 128;
char buffer[kBufferSize];
size_t numBytesAvailable;
size_t numBytesSent;
bool isSending;
bool dataReadyToBeSent() const;
void readMoreData();
void writeMoreData();
bool allDataFlushed();
};
- This was presented in a previous slide deck, except now I'm exposing the private data members.
- source and sink are descriptors bound to the data source and the data recipient, and both are nonblocking.
- buffer is a reasonably sized character array that helps shovel bytes lifted from the source via calls to read over to the sink via calls to write. We shouldn't be surprised that read and write come in to play.
- numBytesAvailable stores the number of meaningful characters residing in buffer.
- numBytesSent tracks the number of bytes residing in buffer that have been written to the recipient.
- When numBytesAvailable and numBytesSent are equal, we know that buffer is effectively empty, and that perhaps another call to read is in order.
- isSending tracks whether all data has been pulled from the source and pushed to the recipient sink.