tcp_socket.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 
27 enum class SocketState
28 {
29  Invalid,
30  Connected,
32  Closed
33 };
34 
35 class TCPSocket
36 {
37 private:
38  std::atomic<int> socket_fd_;
39  std::atomic<SocketState> state_;
40 
41 protected:
42  virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
43  {
44  return false;
45  }
46  virtual void setOptions(int socket_fd);
47 
48  bool setup(std::string &host, int port);
49 
50 public:
51  TCPSocket();
52  virtual ~TCPSocket();
53 
55  {
56  return state_;
57  }
58 
60  {
61  return socket_fd_;
62  }
63  bool setSocketFD(int socket_fd);
64 
65  std::string getIP();
66 
67  bool read(char *character);
68  bool read(uint8_t *buf, size_t buf_len, size_t &read);
69  bool write(const uint8_t *buf, size_t buf_len, size_t &written);
70 
71  void close();
72 };
SocketState getState()
Definition: tcp_socket.h:54
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
Definition: tcp_socket.h:42
std::atomic< int > socket_fd_
Definition: tcp_socket.h:38
std::atomic< SocketState > state_
Definition: tcp_socket.h:39
SocketState
Definition: tcp_socket.h:27
int getSocketFD()
Definition: tcp_socket.h:59


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