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
22 #include <dpl/named_input_pipe.h>
23 #include <dpl/binary_queue.h>
24 #include <sys/types.h>
32 namespace // anonymous
34 const size_t DEFAULT_READ_BUFFER_SIZE = 1024;
35 } // namespace anonymous
37 NamedInputPipe::NamedInputPipe()
42 NamedInputPipe::~NamedInputPipe()
47 void NamedInputPipe::Open(const std::string& pipeName)
49 // Open pipe for reading
50 int fifo = TEMP_FAILURE_RETRY(open(pipeName.c_str(), O_RDONLY | O_NONBLOCK));
53 ThrowMsg(Exception::OpenFailed, pipeName);
58 void NamedInputPipe::Close()
63 if (TEMP_FAILURE_RETRY(close(m_fifo)) == -1)
64 Throw(Exception::CloseFailed);
69 BinaryQueueAutoPtr NamedInputPipe::Read(size_t size)
71 size_t bytesToRead = size > DEFAULT_READ_BUFFER_SIZE ? DEFAULT_READ_BUFFER_SIZE : size;
73 // Malloc default read buffer size
74 // It is unmanaged, so it can be then attached directly to binary queue
75 void *buffer = malloc(bytesToRead);
78 throw std::bad_alloc();
80 ssize_t result = TEMP_FAILURE_RETRY(read(m_fifo, buffer, bytesToRead));
84 // Succedded to read socket data
85 BinaryQueueAutoPtr binaryQueue(new BinaryQueue());
87 // Append unmanaged memory
88 binaryQueue->AppendUnmanaged(buffer, result, &BinaryQueue::BufferDeleterFree, NULL);
95 // Socket was gracefuly closed
98 // Return empty buffer
99 return BinaryQueueAutoPtr(new BinaryQueue());
103 // Must first save errno value, because it may be altered
104 int lastErrno = errno;
109 // Interpret error result
112 // FIXME: Handle specific errno
113 Throw(AbstractInput::Exception::ReadFailed);
117 int NamedInputPipe::WaitableReadHandle() const