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 
107 protected:
108  virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
109  {
110  return ::connect(socket_fd, address, address_len) == 0;
111  }
112 
113 private:
114  std::string host_;
115  int port_;
117 };
118 
119 template <typename T>
120 bool URStream<T>::write(const uint8_t* buf, const size_t buf_len, size_t& written)
121 {
122  std::lock_guard<std::mutex> lock(write_mutex_);
123  return TCPSocket::write(buf, buf_len, written);
124 }
125 
126 template <typename T>
127 bool URStream<T>::read(uint8_t* buf, const size_t buf_len, size_t& total)
128 {
129  std::lock_guard<std::mutex> lock(read_mutex_);
130 
131  bool initial = true;
132  uint8_t* buf_pos = buf;
133  size_t remainder = sizeof(typename T::HeaderType::_package_size_type);
134  size_t read = 0;
135 
136  while (remainder > 0 && TCPSocket::read(buf_pos, remainder, read))
137  {
139  if (initial)
140  {
141  remainder = T::HeaderType::getPackageLength(buf);
142  if (remainder >= (buf_len - sizeof(typename T::HeaderType::_package_size_type)))
143  {
144  URCL_LOG_ERROR("Packet size %zd is larger than buffer %zu, discarding.", remainder, buf_len);
145  return false;
146  }
147  initial = false;
148  }
149 
150  total += read;
151  buf_pos += read;
152  remainder -= read;
153  }
154 
155  return remainder == 0;
156 }
157 } // namespace comm
158 } // namespace urcl
#define URCL_LOG_ERROR(...)
Definition: log.h:37
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:127
bool write(const uint8_t *buf, const size_t buf_len, size_t &written)
Writes to the socket.
Definition: tcp_socket.cpp:167
std::mutex read_mutex_
Definition: stream.h:116
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:120
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
Definition: stream.h:108
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:137
void disconnect()
Disconnects from the configured socket.
Definition: stream.h:69
SocketState getState()
Getter for the state of the socket.
Definition: tcp_socket.h:77
#define URCL_LOG_DEBUG(...)
Definition: log.h:34
std::string host_
Definition: stream.h:114
std::mutex write_mutex_
Definition: stream.h:116
bool setup(std::string &host, int port)
Definition: tcp_socket.cpp:56
void close()
Closes the connection to the socket.
Definition: tcp_socket.cpp:110
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:44
int getSocketFD()
Getter for the file descriptor of the socket.
Definition: tcp_socket.h:87


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Sun May 9 2021 02:16:26