stream.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017, 2018 Simon Rasmussen (refactor)
3  *
4  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 #include <netdb.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <atomic>
24 #include <mutex>
25 #include <string>
26 #include "ur_modern_driver/log.h"
28 
29 class URStream : public TCPSocket
30 {
31 private:
32  std::string host_;
33  int port_;
35 
36 protected:
37  virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
38  {
39  return ::connect(socket_fd, address, address_len) == 0;
40  }
41 
42 public:
43  URStream(std::string& host, int port) : host_(host), port_(port)
44  {
45  }
46 
47  bool connect()
48  {
49  return TCPSocket::setup(host_, port_);
50  }
51  void disconnect()
52  {
53  LOG_INFO("Disconnecting from %s:%d", host_.c_str(), port_);
55  }
56 
57  bool closed()
58  {
59  return getState() == SocketState::Closed;
60  }
61 
62  bool read(uint8_t* buf, size_t buf_len, size_t& read);
63  bool write(const uint8_t* buf, size_t buf_len, size_t& written);
64 };
bool connect()
Definition: stream.h:47
SocketState getState()
Definition: tcp_socket.h:54
void disconnect()
Definition: stream.h:51
std::mutex write_mutex_
Definition: stream.h:34
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
Definition: stream.h:37
URStream(std::string &host, int port)
Definition: stream.h:43
bool closed()
Definition: stream.h:57
#define LOG_INFO(format,...)
Definition: log.h:35
std::mutex read_mutex_
Definition: stream.h:34
bool setup(std::string &host, int port)
Definition: tcp_socket.cpp:45
bool read(uint8_t *buf, size_t buf_len, size_t &read)
Definition: stream.cpp:33
int port_
Definition: stream.h:33
bool write(const uint8_t *buf, size_t buf_len, size_t &written)
Definition: stream.cpp:27
void close()
Definition: tcp_socket.cpp:108
std::string host_
Definition: stream.h:32


ur_modern_driver
Author(s): Thomas Timm Andersen, Simon Rasmussen
autogenerated on Fri Jun 26 2020 03:37:00