StreamReader.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023
00024 #include "StreamReader.hpp"
00025 #include "IOManager.hpp"
00026 #include <ace/Asynch_IO.h>
00027 #include <ace/Message_Block.h>
00028 #include <ace/Proactor.h>
00029 #include <Exception.hpp>
00030
00031 using namespace mermaid::support::io;
00032
00033 using mermaid::support::errorhandling::Exception;
00034
00035 StreamReader::~StreamReader()
00036 {
00037
00038 }
00039
00040 StreamReader::StreamReader (Device* device) : device_ (device)
00041 {
00042 }
00043
00044 void StreamReader::onRead (size_t bytesRead, size_t requestedBytesRead, const char* data, bool success)
00045 {
00046 device_->callReadHandler (bytesRead, requestedBytesRead, data, success);
00047 doRead();
00048 }
00049
00050 void StreamReader::open (ACE_Handler* streamHandler, ACE_Proactor* proactor)
00051 {
00052 int ret = readStream_.open (*streamHandler, device_->getFileDescriptor(), 0, proactor);
00053 if (ret != 0)
00054 throw Exception ("StreamReader::open(): Error opening ACE_Asynch_Read_Stream.");
00055 }
00056
00057 void StreamReader::doRead()
00058 {
00059 ACE_Message_Block* mb;
00060 ACE_NEW_NORETURN (mb, ACE_Message_Block (device_->getExpectedDataSize()));
00061 int ret = readStream_.read (*mb, mb->space());
00062
00063 if (ret != 0) {
00064 mb->release();
00065 throw Exception ("StreamReader::doRead(): Error performing an asynch read operation.");
00066 }
00067 }