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 <netdb.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <atomic>
26 #include <mutex>
27 #include <string>
28 #include <memory>
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<int> socket_fd_;
52  std::atomic<SocketState> state_;
53 
54 protected:
55  virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
56  {
57  return false;
58  }
59  virtual void setOptions(int socket_fd);
60 
61  bool setup(std::string& host, int port);
62 
63  std::unique_ptr<timeval> recv_timeout_;
64 
65 public:
69  TCPSocket();
70  virtual ~TCPSocket();
71 
78  {
79  return state_;
80  }
81 
88  {
89  return socket_fd_;
90  }
91 
97  std::string getIP() const;
98 
106  bool read(char* character);
107 
117  bool read(uint8_t* buf, const size_t buf_len, size_t& read);
118 
128  bool write(const uint8_t* buf, const size_t buf_len, size_t& written);
129 
133  void close();
134 
140  void setReceiveTimeout(const timeval& timeout);
141 };
142 } // namespace comm
143 } // namespace urcl
std::atomic< SocketState > state_
Definition: tcp_socket.h:52
Connection to socket got closed.
std::atomic< int > socket_fd_
Definition: tcp_socket.h:51
SocketState
State the socket can be in.
Definition: tcp_socket.h:37
std::unique_ptr< timeval > recv_timeout_
Definition: tcp_socket.h:63
Socket is initialized or setup failed.
Socket is disconnected and cannot be used.
SocketState getState()
Getter for the state of the socket.
Definition: tcp_socket.h:77
Class for TCP socket abstraction.
Definition: tcp_socket.h:48
Socket is connected and ready to use.
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
Definition: tcp_socket.h:55
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