tcp_server.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // Copyright 2021 FZI Forschungszentrum Informatik
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 // -- END LICENSE BLOCK ------------------------------------------------
18 
19 //----------------------------------------------------------------------
26 //----------------------------------------------------------------------
27 
28 #ifndef UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
29 #define UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
30 
31 #include <netdb.h>
32 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 
36 #include <atomic>
37 #include <functional>
38 #include <thread>
39 #include <vector>
40 
41 namespace urcl
42 {
43 namespace comm
44 {
59 class TCPServer
60 {
61 public:
62  TCPServer() = delete;
63  TCPServer(const int port);
64  virtual ~TCPServer();
65 
72  void setConnectCallback(std::function<void(const int)> func)
73  {
75  }
76 
83  void setDisconnectCallback(std::function<void(const int)> func)
84  {
85  disconnect_callback_ = func;
86  }
87 
94  void setMessageCallback(std::function<void(const int, char*)> func)
95  {
96  message_callback_ = func;
97  }
98 
105  void start();
106 
112  void shutdown();
113 
125  bool write(const int fd, const uint8_t* buf, const size_t buf_len, size_t& written);
126 
132  uint32_t getMaxClientsAllowed() const
133  {
134  return max_clients_allowed_;
135  }
136 
143  void setMaxClientsAllowed(const uint32_t& max_clients_allowed)
144  {
145  max_clients_allowed_ = max_clients_allowed;
146  }
147 
148 private:
149  void init();
150  void bind();
151  void startListen();
152 
154  void handleConnect();
155 
156  void handleDisconnect(const int fd);
157 
159  void readData(const int fd);
160 
162  void spin();
163 
165  void worker();
166 
167  std::atomic<bool> keep_running_;
168  std::thread worker_thread_;
169 
170  std::atomic<int> listen_fd_;
171  int port_;
172 
173  int maxfd_;
174  fd_set masterfds_;
175  fd_set tempfds_;
176 
178  std::vector<int> client_fds_;
179 
180  // Pipe for the self-pipe trick (https://cr.yp.to/docs/selfpipe.html)
181  int self_pipe_[2];
182 
183  static const int INPUT_BUFFER_SIZE = 100;
185 
186  std::function<void(const int)> new_connection_callback_;
187  std::function<void(const int)> disconnect_callback_;
188  std::function<void(const int, char* buffer)> message_callback_;
189 };
190 
191 } // namespace comm
192 } // namespace urcl
193 
194 #endif // ifndef UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
void setDisconnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients disconnecting from the server.
Definition: tcp_server.h:83
void setMaxClientsAllowed(const uint32_t &max_clients_allowed)
Set the maximum number of clients allowed to connect to this server.
Definition: tcp_server.h:143
std::thread worker_thread_
Definition: tcp_server.h:168
void setConnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients connecting to the server.
Definition: tcp_server.h:72
std::vector< int > client_fds_
Definition: tcp_server.h:178
Wrapper class for a TCP socket server.
Definition: tcp_server.h:59
void spin()
Event handler. Blocks until activity on any client or connection attempt.
Definition: tcp_server.cpp:193
std::atomic< int > listen_fd_
Definition: tcp_server.h:170
void start()
Start event handling.
Definition: tcp_server.cpp:307
void handleDisconnect(const int fd)
Definition: tcp_server.cpp:247
void readData(const int fd)
read data from socket
Definition: tcp_server.cpp:267
void shutdown()
Shut down the event listener thread. After calling this, no events will be handled anymore...
Definition: tcp_server.cpp:107
char input_buffer_[INPUT_BUFFER_SIZE]
Definition: tcp_server.h:184
std::function< void(const int)> disconnect_callback_
Definition: tcp_server.h:187
std::atomic< bool > keep_running_
Definition: tcp_server.h:167
uint32_t getMaxClientsAllowed() const
Get the maximum number of clients allowed to connect to this server.
Definition: tcp_server.h:132
void worker()
Runs spin() as long as keep_running_ is set to true.
Definition: tcp_server.cpp:298
uint32_t max_clients_allowed_
Definition: tcp_server.h:177
bool write(const int fd, const uint8_t *buf, const size_t buf_len, size_t &written)
Writes to a client.
Definition: tcp_server.cpp:314
static const int INPUT_BUFFER_SIZE
Definition: tcp_server.h:183
void handleConnect()
Handles connection events.
Definition: tcp_server.cpp:159
void setMessageCallback(std::function< void(const int, char *)> func)
This callback will be triggered on messages received on the socket.
Definition: tcp_server.h:94
std::function< void(const int, char *buffer)> message_callback_
Definition: tcp_server.h:188
std::function< void(const int)> new_connection_callback_
Definition: tcp_server.h:186


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