stream.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019, FZI Forschungszentrum Informatik (templating)
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 "ur_client_library/log.h"
28 
29 namespace urcl
30 {
31 namespace comm
32 {
39 template <typename T>
40 class URStream : public TCPSocket
41 {
42 public:
50  URStream(const std::string& host, int port) : host_(host), port_(port)
51  {
52  }
53 
63  bool connect(const size_t max_num_tries = 0,
64  const std::chrono::milliseconds reconnection_time = std::chrono::seconds(10))
65  {
66  return TCPSocket::setup(host_, port_, max_num_tries, reconnection_time);
67  }
68 
72  void disconnect()
73  {
74  URCL_LOG_DEBUG("Disconnecting from %s:%d", host_.c_str(), port_);
76  }
77 
81  bool closed()
82  {
83  return getState() == SocketState::Closed;
84  }
85 
97  bool read(uint8_t* buf, const size_t buf_len, size_t& read);
98 
108  bool write(const uint8_t* buf, const size_t buf_len, size_t& written);
109 
115  std::string getHost()
116  {
117  return host_;
118  }
119 
120 private:
121  std::string host_;
122  int port_;
124 };
125 
126 template <typename T>
127 bool URStream<T>::write(const uint8_t* buf, const size_t buf_len, size_t& written)
128 {
129  std::lock_guard<std::mutex> lock(write_mutex_);
130  return TCPSocket::write(buf, buf_len, written);
131 }
132 
133 template <typename T>
134 bool URStream<T>::read(uint8_t* buf, const size_t buf_len, size_t& total)
135 {
136  std::lock_guard<std::mutex> lock(read_mutex_);
137 
138  bool initial = true;
139  uint8_t* buf_pos = buf;
140  size_t remainder = sizeof(typename T::HeaderType::_package_size_type);
141  size_t read = 0;
142 
143  while (remainder > 0 && TCPSocket::read(buf_pos, remainder, read))
144  {
145  if (initial)
146  {
147  remainder = T::HeaderType::getPackageLength(buf);
148  if (remainder >= (buf_len - sizeof(typename T::HeaderType::_package_size_type)))
149  {
150  URCL_LOG_ERROR("Packet size %zd is larger than buffer %zu, discarding.", remainder, buf_len);
151  return false;
152  }
153  initial = false;
154  }
155 
156  total += read;
157  buf_pos += read;
158  remainder -= read;
159  }
160 
161  return remainder == 0;
162 }
163 } // namespace comm
164 } // namespace urcl
urcl::comm::TCPSocket
Class for TCP socket abstraction.
Definition: tcp_socket.h:48
urcl::comm::URStream
The stream is an abstraction of the TCPSocket that offers reading a full UR data package out of the s...
Definition: stream.h:40
urcl::comm::URStream::port_
int port_
Definition: stream.h:122
urcl::comm::URStream::connect
bool connect(const size_t max_num_tries=0, const std::chrono::milliseconds reconnection_time=std::chrono::seconds(10))
Connects to the configured socket.
Definition: stream.h:63
urcl::comm::URStream::disconnect
void disconnect()
Disconnects from the configured socket.
Definition: stream.h:72
urcl
Definition: bin_parser.h:36
urcl::comm::URStream::write
bool write(const uint8_t *buf, const size_t buf_len, size_t &written)
Writes directly to the underlying socket (with a mutex guard)
Definition: stream.h:127
urcl::comm::URStream::getHost
std::string getHost()
Get the host IP.
Definition: stream.h:115
urcl::comm::URStream::URStream
URStream(const std::string &host, int port)
Creates a new URStream object. Note, that this does not immediately open the socket,...
Definition: stream.h:50
URCL_LOG_ERROR
#define URCL_LOG_ERROR(...)
Definition: log.h:26
urcl::comm::SocketState::Closed
@ Closed
Connection to socket got closed.
urcl::comm::URStream::write_mutex_
std::mutex write_mutex_
Definition: stream.h:123
URCL_LOG_DEBUG
#define URCL_LOG_DEBUG(...)
Definition: log.h:23
urcl::comm::URStream::read
bool read(uint8_t *buf, const size_t buf_len, size_t &read)
Reads a full UR package out of a socket. For this, it looks into the package and reads the byte lengt...
Definition: stream.h:134
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::URStream::host_
std::string host_
Definition: stream.h:121
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::close
void close()
Closes the connection to the socket.
Definition: tcp_socket.cpp:151
log.h
urcl::comm::TCPSocket::read
bool read(char *character)
Reads one byte from the socket.
Definition: tcp_socket.cpp:178
urcl::comm::URStream::read_mutex_
std::mutex read_mutex_
Definition: stream.h:123
urcl::comm::URStream::closed
bool closed()
Returns whether the underlying socket is currently closed.
Definition: stream.h:81
tcp_socket.h
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