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 <netdb.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <atomic>
26 #include <mutex>
27 #include <string>
28 #include "ur_client_library/log.h"
30 
31 namespace urcl
32 {
33 namespace comm
34 {
41 template <typename T>
42 class URStream : public TCPSocket
43 {
44 public:
52  URStream(const std::string& host, int port) : host_(host), port_(port)
53  {
54  }
55 
61  bool connect()
62  {
63  return TCPSocket::setup(host_, port_);
64  }
65 
69  void disconnect()
70  {
71  URCL_LOG_DEBUG("Disconnecting from %s:%d", host_.c_str(), port_);
73  }
74 
78  bool closed()
79  {
80  return getState() == SocketState::Closed;
81  }
82 
94  bool read(uint8_t* buf, const size_t buf_len, size_t& read);
95 
105  bool write(const uint8_t* buf, const size_t buf_len, size_t& written);
106 
112  std::string getHost()
113  {
114  return host_;
115  }
116 
117 protected:
118  virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
119  {
120  return ::connect(socket_fd, address, address_len) == 0;
121  }
122 
123 private:
124  std::string host_;
125  int port_;
127 };
128 
129 template <typename T>
130 bool URStream<T>::write(const uint8_t* buf, const size_t buf_len, size_t& written)
131 {
132  std::lock_guard<std::mutex> lock(write_mutex_);
133  return TCPSocket::write(buf, buf_len, written);
134 }
135 
136 template <typename T>
137 bool URStream<T>::read(uint8_t* buf, const size_t buf_len, size_t& total)
138 {
139  std::lock_guard<std::mutex> lock(read_mutex_);
140 
141  bool initial = true;
142  uint8_t* buf_pos = buf;
143  size_t remainder = sizeof(typename T::HeaderType::_package_size_type);
144  size_t read = 0;
145 
146  while (remainder > 0 && TCPSocket::read(buf_pos, remainder, read))
147  {
149  if (initial)
150  {
151  remainder = T::HeaderType::getPackageLength(buf);
152  if (remainder >= (buf_len - sizeof(typename T::HeaderType::_package_size_type)))
153  {
154  URCL_LOG_ERROR("Packet size %zd is larger than buffer %zu, discarding.", remainder, buf_len);
155  return false;
156  }
157  initial = false;
158  }
159 
160  total += read;
161  buf_pos += read;
162  remainder -= read;
163  }
164 
165  return remainder == 0;
166 }
167 } // namespace comm
168 } // namespace urcl
std::string getHost()
Get the host IP.
Definition: stream.h:112
#define URCL_LOG_ERROR(...)
Definition: log.h:26
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:137
bool write(const uint8_t *buf, const size_t buf_len, size_t &written)
Writes to the socket.
Definition: tcp_socket.cpp:173
std::mutex read_mutex_
Definition: stream.h:126
Connection to socket got closed.
The stream is an abstraction of the TCPSocket that offers reading a full UR data package out of the s...
Definition: stream.h:42
bool closed()
Returns whether the underlying socket is currently closed.
Definition: stream.h:78
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:130
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
Definition: stream.h:118
bool connect()
Connects to the configured socket.
Definition: stream.h:61
bool read(char *character)
Reads one byte from the socket.
Definition: tcp_socket.cpp:143
void disconnect()
Disconnects from the configured socket.
Definition: stream.h:69
SocketState getState()
Getter for the state of the socket.
Definition: tcp_socket.h:78
#define URCL_LOG_DEBUG(...)
Definition: log.h:23
std::string host_
Definition: stream.h:124
std::mutex write_mutex_
Definition: stream.h:126
bool setup(std::string &host, int port)
Definition: tcp_socket.cpp:58
void close()
Closes the connection to the socket.
Definition: tcp_socket.cpp:116
Class for TCP socket abstraction.
Definition: tcp_socket.h:48
URStream(const std::string &host, int port)
Creates a new URStream object. Note, that this does not immediately open the socket, that has to be done separately by calling the connect() function.
Definition: stream.h:52
virtual void setOptions(int socket_fd)
Definition: tcp_socket.cpp:46
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