2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file named_input_pipe.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of named input pipe
23 #include <dpl/named_input_pipe.h>
24 #include <dpl/binary_queue.h>
25 #include <sys/types.h>
33 namespace // anonymous
35 const size_t DEFAULT_READ_BUFFER_SIZE = 1024;
36 } // namespace anonymous
38 NamedInputPipe::NamedInputPipe()
43 NamedInputPipe::~NamedInputPipe()
48 void NamedInputPipe::Open(const std::string& pipeName)
50 // Open pipe for reading
51 int fifo = TEMP_FAILURE_RETRY(open(pipeName.c_str(), O_RDONLY | O_NONBLOCK));
54 ThrowMsg(Exception::OpenFailed, pipeName);
59 void NamedInputPipe::Close()
64 if (TEMP_FAILURE_RETRY(close(m_fifo)) == -1)
65 Throw(Exception::CloseFailed);
70 BinaryQueueAutoPtr NamedInputPipe::Read(size_t size)
72 size_t bytesToRead = size > DEFAULT_READ_BUFFER_SIZE ? DEFAULT_READ_BUFFER_SIZE : size;
74 // Malloc default read buffer size
75 // It is unmanaged, so it can be then attached directly to binary queue
76 void *buffer = malloc(bytesToRead);
79 throw std::bad_alloc();
81 ssize_t result = TEMP_FAILURE_RETRY(read(m_fifo, buffer, bytesToRead));
85 // Succedded to read socket data
86 BinaryQueueAutoPtr binaryQueue(new BinaryQueue());
88 // Append unmanaged memory
89 binaryQueue->AppendUnmanaged(buffer, result, &BinaryQueue::BufferDeleterFree, NULL);
96 // Socket was gracefuly closed
99 // Return empty buffer
100 return BinaryQueueAutoPtr(new BinaryQueue());
104 // Must first save errno value, because it may be altered
105 int lastErrno = errno;
110 // Interpret error result
113 // FIXME: Handle specific errno
114 Throw(AbstractInput::Exception::ReadFailed);
118 int NamedInputPipe::WaitableReadHandle() const