Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
channel.hpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Contact: Jan Olszak <j.olszak@samsung.com>
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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
17 */
18 
25 #ifndef COMMON_UTILS_CHANNEL_HPP
26 #define COMMON_UTILS_CHANNEL_HPP
27 
28 #include "utils/fd-utils.hpp"
29 
30 #include <array>
31 #include <cassert>
32 
33 namespace utils {
34 
38 class Channel {
39 public:
40  explicit Channel(const bool closeOnExec = true);
41  explicit Channel(const int fd);
42  ~Channel();
43 
44  Channel(const Channel&) = delete;
45  Channel& operator=(const Channel&) = delete;
46 
51  void setLeft();
52 
57  void setRight();
58 
62  void shutdown();
63 
69  template<typename Data>
70  void write(const Data& data);
71 
75  template<typename Data>
76  Data read();
77 
81  int getFD();
82 
86  int getLeftFD();
87 
91  int getRightFD();
92 
96  void setCloseOnExec(const bool closeOnExec);
97 
98 private:
99 
100  void closeSocket(int socketIndex);
101 
103  std::array<int, 2> mSockets;
104 };
105 
106 template<typename Data>
107 void Channel::write(const Data& data)
108 {
109  assert(mSocketIndex != -1 && "Channel's end isn't set");
110 
111  utils::write(mSockets[mSocketIndex], &data, sizeof(Data));
112 }
113 
114 template<typename Data>
116 {
117  assert(mSocketIndex != -1 && "Channel's end isn't set");
118 
119  Data data;
120  utils::read(mSockets[mSocketIndex], &data, sizeof(Data));
121  return data;
122 }
123 
124 } // namespace utils
125 
126 #endif // COMMON_UTILS_CHANNEL_HPP
int mSocketIndex
Definition: channel.hpp:102
void setLeft()
Use the "left" end of the channel Closes the "right" end.
Definition: channel.cpp:73
int getRightFD()
Gen the right file descriptor.
Definition: channel.cpp:107
int getLeftFD()
Gen the left file descriptor.
Definition: channel.cpp:102
void write(int fd, const void *bufferPtr, const size_t size, int timeoutMS)
Write to a file descriptor, throw on error.
Definition: fd-utils.cpp:208
int getFD()
Get an active file descriptor.
Definition: channel.cpp:96
void closeSocket(int socketIndex)
Definition: channel.cpp:119
~Channel()
Definition: channel.cpp:64
char data[368]
Definition: initctl.cpp:41
Channel & operator=(const Channel &)=delete
void setRight()
Use the "right" end of the channel Closes the "left" end.
Definition: channel.cpp:83
Channel(const bool closeOnExec=true)
Definition: channel.cpp:41
void shutdown()
Gracefully shutdown the used end of the channel.
Definition: channel.cpp:90
void setCloseOnExec(const bool closeOnExec)
Sets close on exec on an active fd to either true or false.
Definition: channel.cpp:112
Data read()
Receive data of a given type (size)
Definition: channel.hpp:115
Channel is implemented with a pair of anonymous sockets.
Definition: channel.hpp:38
std::array< int, 2 > mSockets
Definition: channel.hpp:103
void read(int fd, void *bufferPtr, const size_t size, int timeoutMS)
Read from a file descriptor, throw on error.
Definition: fd-utils.cpp:237
void write(const Data &data)
Send the data to the other end of the channel.
Definition: channel.hpp:107
File descriptor utility functions.