std::size_t PipeIODevice::onEndRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof) { loop.selector().disable(_ioh); DWORD readBytes = 0; DWORD flags = 0; eof = false; // TODO: can we receive EOF? if (_bufferSize) { readBytes = _bufferSize; } else if ( FALSE == ReadMsgQueue(handle(), &_buffer[0], _msgSize, &readBytes, INFINITE, &flags) ) { throw IOError( PT_ERROR_MSG("Could not read from message queue handle") ); } DWORD bytesToCopy = std::min<DWORD>(_rbuflen, readBytes); memcpy(_rbuf, &_buffer[0], bytesToCopy); _bufferSize = 0; if (_rbuflen >= readBytes) return readBytes; std::vector<char>::iterator beginData = (_buffer.begin() + bytesToCopy); std::vector<char>::iterator endData = (_buffer.begin() + readBytes); std::copy(beginData, endData, _buffer.begin()); _bufferSize = (readBytes - bytesToCopy); return bytesToCopy; }
std::size_t PipeIODevice::onBeginWrite(EventLoop& loop, const char* buffer, std::size_t n) { if( Write != _mode ) { throw IOError( PT_ERROR_MSG("Could not write on a read only pipe") ); } loop.selector().enable(_ioh); return 0; }
std::size_t PipeIODevice::onBeginRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof) { if( Read != _mode ) throw IOError( PT_ERROR_MSG("Could not read from write only pipe") ); if(_bufferSize) return std::min(_bufferSize, n); loop.selector().enable(_ioh); return 0; }
std::size_t PipeIODevice::onEndWrite(EventLoop& loop, const char* buffer, std::size_t n) { loop.selector().disable(_ioh); DWORD bytesToWrite = std::min<DWORD>(_wbuflen, _msgSize); if ( FALSE == WriteMsgQueue(handle(), (LPVOID) _wbuf, bytesToWrite, 0, 0)) { throw IOError( PT_ERROR_MSG("WriteMsgQueue failed") ); } return bytesToWrite; }