Additional Include: stddef.h
[framework/web/wrt-commons.git] / modules / core / src / named_input_pipe.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        named_input_pipe.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of named input pipe
21  */
22 #include <stddef.h>
23 #include <dpl/named_input_pipe.h>
24 #include <dpl/binary_queue.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30
31 namespace DPL
32 {
33 namespace // anonymous
34 {
35 const size_t DEFAULT_READ_BUFFER_SIZE = 1024;
36 } // namespace anonymous
37
38 NamedInputPipe::NamedInputPipe()
39     : m_fifo(-1)
40 {
41 }
42
43 NamedInputPipe::~NamedInputPipe()
44 {
45     Close();
46 }
47
48 void NamedInputPipe::Open(const std::string& pipeName)
49 {
50     // Open pipe for reading
51     int fifo = TEMP_FAILURE_RETRY(open(pipeName.c_str(), O_RDONLY | O_NONBLOCK));
52
53     if (fifo == -1)
54         ThrowMsg(Exception::OpenFailed, pipeName);
55
56     m_fifo = fifo;
57 }
58
59 void NamedInputPipe::Close()
60 {
61     if (m_fifo == -1)
62         return;
63
64     if (TEMP_FAILURE_RETRY(close(m_fifo)) == -1)
65         Throw(Exception::CloseFailed);
66
67     m_fifo = -1;
68 }
69
70 BinaryQueueAutoPtr NamedInputPipe::Read(size_t size)
71 {
72     size_t bytesToRead = size > DEFAULT_READ_BUFFER_SIZE ? DEFAULT_READ_BUFFER_SIZE : size;
73
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);
77
78     if (buffer == NULL)
79         throw std::bad_alloc();
80
81     ssize_t result = TEMP_FAILURE_RETRY(read(m_fifo, buffer, bytesToRead));
82
83     if (result > 0)
84     {
85         // Succedded to read socket data
86         BinaryQueueAutoPtr binaryQueue(new BinaryQueue());
87
88         // Append unmanaged memory
89         binaryQueue->AppendUnmanaged(buffer, result, &BinaryQueue::BufferDeleterFree, NULL);
90
91         // Return buffer
92         return binaryQueue;
93     }
94     else if (result == 0)
95     {
96         // Socket was gracefuly closed
97         free(buffer);
98
99         // Return empty buffer
100         return BinaryQueueAutoPtr(new BinaryQueue());
101     }
102     else
103     {
104         // Must first save errno value, because it may be altered
105         int lastErrno = errno;
106
107         // Free buffer
108         free(buffer);
109
110         // Interpret error result
111         (void)lastErrno;
112
113         // FIXME: Handle specific errno
114         Throw(AbstractInput::Exception::ReadFailed);
115     }
116 }
117
118 int NamedInputPipe::WaitableReadHandle() const
119 {
120     return m_fifo;
121 }
122 } // namespace DPL