2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Definition of a class for writing and reading data from a file descriptor
27 #include "config/fdstore.hpp"
28 #include "config/exception.hpp"
40 void waitForEvent(int fd,
42 const std::chrono::high_resolution_clock::time_point deadline)
44 // Wait for the rest of the data
47 fds[0].events = event | POLLHUP;
50 std::chrono::milliseconds timeoutMS =
51 std::chrono::duration_cast<std::chrono::milliseconds>(deadline - std::chrono::high_resolution_clock::now());
52 if (timeoutMS.count() < 0) {
53 throw ConfigException("Timeout");
56 int ret = ::poll(fds, 1 /*fds size*/, timeoutMS.count());
62 throw ConfigException("Error in poll: " + std::string(strerror(errno)));
66 throw ConfigException("Timeout");
69 if (fds[0].revents & POLLHUP) {
70 throw ConfigException("Peer disconnected");
80 FDStore::FDStore(int fd)
85 FDStore::FDStore(const FDStore& store)
94 void FDStore::write(const void* bufferPtr, const size_t size, const unsigned int timeoutMS)
96 std::chrono::high_resolution_clock::time_point deadline = std::chrono::high_resolution_clock::now() +
97 std::chrono::milliseconds(timeoutMS);
102 reinterpret_cast<const char*>(bufferPtr) + nTotal,
106 if (nTotal == size) {
107 // All data is written, break loop
110 } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
113 throw ConfigException("Error during writing: " + std::string(strerror(errno)));
116 waitForEvent(mFD, POLLOUT, deadline);
120 void FDStore::read(void* bufferPtr, const size_t size, const unsigned int timeoutMS)
122 std::chrono::high_resolution_clock::time_point deadline = std::chrono::high_resolution_clock::now() +
123 std::chrono::milliseconds(timeoutMS);
128 reinterpret_cast<char*>(bufferPtr) + nTotal,
132 if (nTotal == size) {
133 // All data is read, break loop
137 throw ConfigException("Peer disconnected");
139 } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
142 throw ConfigException("Error during reading: " + std::string(strerror(errno)));
145 waitForEvent(mFD, POLLIN, deadline);
148 } // namespace config