tcp_socket.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019, FZI Forschungszentrum Informatik (refactor)
3  *
4  * Copyright 2017, 2018 Simon Rasmussen (refactor)
5  *
6  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 #include <netdb.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <atomic>
26 #include <mutex>
27 #include <string>
28 #include <memory>
29 
30 namespace urcl
31 {
32 namespace comm
33 {
37 enum class SocketState
38 {
39  Invalid,
40  Connected,
41  Disconnected,
42  Closed
43 };
44 
48 class TCPSocket
49 {
50 private:
51  std::atomic<int> socket_fd_;
52  std::atomic<SocketState> state_;
53  std::chrono::seconds reconnection_time_;
54 
55 protected:
56  virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
57  {
58  return false;
59  }
60  virtual void setOptions(int socket_fd);
61 
62  bool setup(std::string& host, int port);
63 
64  std::unique_ptr<timeval> recv_timeout_;
65 
66 public:
70  TCPSocket();
71  virtual ~TCPSocket();
72 
79  {
80  return state_;
81  }
82 
89  {
90  return socket_fd_;
91  }
92 
98  std::string getIP() const;
99 
107  bool read(char* character);
108 
118  bool read(uint8_t* buf, const size_t buf_len, size_t& read);
119 
129  bool write(const uint8_t* buf, const size_t buf_len, size_t& written);
130 
134  void close();
135 
141  void setReceiveTimeout(const timeval& timeout);
142 
149  void setReconnectionTime(std::chrono::seconds reconnection_time)
150  {
151  reconnection_time_ = reconnection_time;
152  }
153 };
154 } // namespace comm
155 } // namespace urcl
std::atomic< SocketState > state_
Definition: tcp_socket.h:52
Connection to socket got closed.
std::atomic< int > socket_fd_
Definition: tcp_socket.h:51
SocketState
State the socket can be in.
Definition: tcp_socket.h:37
std::unique_ptr< timeval > recv_timeout_
Definition: tcp_socket.h:64
void setReconnectionTime(std::chrono::seconds reconnection_time)
Set reconnection time, if the server is unavailable during connection this will set the time before t...
Definition: tcp_socket.h:149
Socket is initialized or setup failed.
Socket is disconnected and cannot be used.
SocketState getState()
Getter for the state of the socket.
Definition: tcp_socket.h:78
Class for TCP socket abstraction.
Definition: tcp_socket.h:48
Socket is connected and ready to use.
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
Definition: tcp_socket.h:56
std::chrono::seconds reconnection_time_
Definition: tcp_socket.h:53
int getSocketFD()
Getter for the file descriptor of the socket.
Definition: tcp_socket.h:88


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Tue Jul 4 2023 02:09:47