Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
socket.hpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2014 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 CARGO_IPC_INTERNALS_SOCKET_HPP
26 #define CARGO_IPC_INTERNALS_SOCKET_HPP
27 
28 #include <string>
29 #include <mutex>
30 #include <memory>
31 
32 namespace cargo {
33 namespace ipc {
34 
40 class Socket {
41 public:
42 
43  typedef std::unique_lock<std::recursive_mutex> Guard;
44 
51  explicit Socket(int socketFD = -1);
52  Socket(Socket&& socket) noexcept;
53  ~Socket() noexcept;
54 
55  Socket(const Socket&) = delete;
56  Socket& operator=(const Socket&) = delete;
57  Socket& operator=(Socket&&) = delete;
58 
62  int getFD() const;
63 
70  void write(const void* bufferPtr, const size_t size) const;
71 
78  void read(void* bufferPtr, const size_t size) const;
79 
84  std::shared_ptr<Socket> accept() ;
85 
88  Socket::Guard getGuard() const;
89 
90 
98  static Socket createSocket(const std::string& path);
99 
106  static Socket connectSocket(const std::string& path, const int timeoutMs = 1000);
107 
108 private:
109  int mFD;
110  mutable std::recursive_mutex mCommunicationMutex;
111 
112  static int createSocketInternal(const std::string& path);
113  static int getSystemdSocketInternal(const std::string& path);
114 };
115 
116 } // namespace ipc
117 } // namespace cargo
118 
119 #endif // CARGO_IPC_INTERNALS_SOCKET_HPP
This class wraps all operations possible to do with a socket.
Definition: socket.hpp:40
int mFD
Definition: socket.hpp:109
void write(const void *bufferPtr, const size_t size) const
Write data using the file descriptor.
Definition: socket.cpp:154
std::shared_ptr< Socket > accept()
Accepts connection.
Definition: socket.cpp:142
static int getSystemdSocketInternal(const std::string &path)
static Socket createSocket(const std::string &path)
Prepares socket for accepting connections.
Definition: socket.cpp:233
int getFD() const
Definition: socket.cpp:137
static int createSocketInternal(const std::string &path)
Definition: socket.cpp:189
std::recursive_mutex mCommunicationMutex
Definition: socket.hpp:110
static Socket connectSocket(const std::string &path, const int timeoutMs=1000)
Connects to a socket.
Definition: socket.cpp:249
Socket::Guard getGuard() const
Definition: socket.cpp:132
Socket(int socketFD=-1)
Default constructor.
Definition: socket.cpp:112
std::unique_lock< std::recursive_mutex > Guard
Definition: socket.hpp:43
void read(void *bufferPtr, const size_t size) const
Reads a value of the given type.
Definition: socket.cpp:160