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 <atomic>
23 #include <chrono>
24 #include <mutex>
25 #include <string>
26 #include <memory>
27 
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<socket_t> socket_fd_;
52  std::atomic<SocketState> state_;
53  std::chrono::milliseconds reconnection_time_;
55 
56  void setupOptions();
57 
58 protected:
59  static bool open(socket_t socket_fd, struct sockaddr* address, size_t address_len)
60  {
61  return ::connect(socket_fd, address, static_cast<socklen_t>(address_len)) == 0;
62  }
63 
64  bool setup(const std::string& host, const int port, const size_t max_num_tries = 0,
65  const std::chrono::milliseconds reconnection_time = DEFAULT_RECONNECTION_TIME);
66 
67  std::unique_ptr<timeval> recv_timeout_;
68 
69 public:
70  static constexpr std::chrono::milliseconds DEFAULT_RECONNECTION_TIME{ 10000 };
74  TCPSocket();
75  virtual ~TCPSocket();
76 
83  {
84  return state_;
85  }
86 
93  {
94  return socket_fd_;
95  }
96 
102  std::string getIP() const;
103 
111  bool read(char* character);
112 
122  bool read(uint8_t* buf, const size_t buf_len, size_t& read);
123 
133  bool write(const uint8_t* buf, const size_t buf_len, size_t& written);
134 
138  void close();
139 
145  void setReceiveTimeout(const timeval& timeout);
146 
153  [[deprecated("Reconnection time is passed to setup directly now.")]] void
154  setReconnectionTime(const std::chrono::milliseconds reconnection_time);
155 };
156 } // namespace comm
157 } // namespace urcl
urcl::comm::TCPSocket
Class for TCP socket abstraction.
Definition: tcp_socket.h:48
urcl::comm::TCPSocket::setReceiveTimeout
void setReceiveTimeout(const timeval &timeout)
Setup Receive timeout used for this socket.
Definition: tcp_socket.cpp:259
socket_t
int socket_t
Definition: socket_t.h:57
urcl::comm::TCPSocket::TCPSocket
TCPSocket()
Creates a TCPSocket object.
Definition: tcp_socket.cpp:41
urcl::comm::TCPSocket::state_
std::atomic< SocketState > state_
Definition: tcp_socket.h:52
urcl
Definition: bin_parser.h:36
urcl::comm::TCPSocket::setupOptions
void setupOptions()
Definition: tcp_socket.cpp:54
urcl::comm::SocketState::Closed
@ Closed
Connection to socket got closed.
urcl::comm::TCPSocket::getIP
std::string getIP() const
Determines the local IP address of the currently configured socket.
Definition: tcp_socket.cpp:161
urcl::comm::SocketState::Disconnected
@ Disconnected
Socket is disconnected and cannot be used.
urcl::comm::SocketState::Invalid
@ Invalid
Socket is initialized or setup failed.
urcl::comm::TCPSocket::DEFAULT_RECONNECTION_TIME
static constexpr std::chrono::milliseconds DEFAULT_RECONNECTION_TIME
Definition: tcp_socket.h:70
socket_t.h
urcl::comm::TCPSocket::setup
bool setup(const std::string &host, const int port, const size_t max_num_tries=0, const std::chrono::milliseconds reconnection_time=DEFAULT_RECONNECTION_TIME)
Definition: tcp_socket.cpp:72
urcl::comm::TCPSocket::write
bool write(const uint8_t *buf, const size_t buf_len, size_t &written)
Writes to the socket.
Definition: tcp_socket.cpp:228
urcl::comm::TCPSocket::socket_fd_
std::atomic< socket_t > socket_fd_
Definition: tcp_socket.h:51
urcl::comm::TCPSocket::close
void close()
Closes the connection to the socket.
Definition: tcp_socket.cpp:151
urcl::comm::TCPSocket::reconnection_time_modified_deprecated_
bool reconnection_time_modified_deprecated_
Definition: tcp_socket.h:54
urcl::comm::TCPSocket::read
bool read(char *character)
Reads one byte from the socket.
Definition: tcp_socket.cpp:178
urcl::comm::SocketState::Connected
@ Connected
Socket is connected and ready to use.
urcl::comm::TCPSocket::open
static bool open(socket_t socket_fd, struct sockaddr *address, size_t address_len)
Definition: tcp_socket.h:59
urcl::comm::TCPSocket::setReconnectionTime
void setReconnectionTime(const std::chrono::milliseconds reconnection_time)
Set reconnection time, if the server is unavailable during connection this will set the time before t...
Definition: tcp_socket.cpp:269
urcl::comm::TCPSocket::getSocketFD
socket_t getSocketFD()
Getter for the file descriptor of the socket.
Definition: tcp_socket.h:92
urcl::comm::TCPSocket::~TCPSocket
virtual ~TCPSocket()
Definition: tcp_socket.cpp:49
urcl::comm::TCPSocket::recv_timeout_
std::unique_ptr< timeval > recv_timeout_
Definition: tcp_socket.h:67
urcl::comm::SocketState
SocketState
State the socket can be in.
Definition: tcp_socket.h:37
urcl::comm::TCPSocket::reconnection_time_
std::chrono::milliseconds reconnection_time_
Definition: tcp_socket.h:53
urcl::comm::TCPSocket::getState
SocketState getState()
Getter for the state of the socket.
Definition: tcp_socket.h:82


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Mon May 26 2025 02:35:58