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 // Created on behalf of Universal Robots A/S
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // -- END LICENSE BLOCK ------------------------------------------------
19 
20 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 
29 #ifndef UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
30 #define UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
31 
32 #include <atomic>
33 #include <chrono>
34 #include <functional>
35 #include <thread>
36 #include <vector>
37 
39 
40 namespace urcl
41 {
42 namespace comm
43 {
58 class TCPServer
59 {
60 public:
61  TCPServer() = delete;
71  explicit TCPServer(const int port, const size_t max_num_tries = 0,
72  const std::chrono::milliseconds reconnection_time = std::chrono::seconds(1));
73  virtual ~TCPServer();
74 
81  void setConnectCallback(std::function<void(const socket_t)> func)
82  {
84  }
85 
92  void setDisconnectCallback(std::function<void(const socket_t)> func)
93  {
94  disconnect_callback_ = func;
95  }
96 
103  void setMessageCallback(std::function<void(const socket_t, char*, int)> func)
104  {
105  message_callback_ = func;
106  }
107 
114  void start();
115 
121  void shutdown();
122 
134  bool write(const socket_t fd, const uint8_t* buf, const size_t buf_len, size_t& written);
135 
141  uint32_t getMaxClientsAllowed() const
142  {
143  return max_clients_allowed_;
144  }
145 
152  void setMaxClientsAllowed(const uint32_t& max_clients_allowed)
153  {
154  max_clients_allowed_ = max_clients_allowed;
155  }
156 
157 private:
158  void init();
159  void bind(const size_t max_num_tries, const std::chrono::milliseconds reconnection_time);
160  void startListen();
161 
163  void handleConnect();
164 
165  void handleDisconnect(const socket_t fd);
166 
168  void readData(const socket_t fd);
169 
171  void spin();
172 
174  void worker();
175 
176  std::atomic<bool> keep_running_;
177  std::thread worker_thread_;
178 
179  std::atomic<socket_t> listen_fd_;
180  int port_;
181 
183  fd_set masterfds_;
184  fd_set tempfds_;
185 
187  std::vector<socket_t> client_fds_;
188 
189  static const int INPUT_BUFFER_SIZE = 100;
191 
192  std::function<void(const socket_t)> new_connection_callback_;
193  std::function<void(const socket_t)> disconnect_callback_;
194  std::function<void(const socket_t, char* buffer, int nbytesrecv)> message_callback_;
195 };
196 
197 } // namespace comm
198 } // namespace urcl
199 
200 #endif // ifndef UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
urcl::comm::TCPServer::masterfds_
fd_set masterfds_
Definition: tcp_server.h:183
socket_t
int socket_t
Definition: socket_t.h:57
urcl::comm::TCPServer::shutdown
void shutdown()
Shut down the event listener thread. After calling this, no events will be handled anymore,...
Definition: tcp_server.cpp:83
urcl::comm::TCPServer::start
void start()
Start event handling.
Definition: tcp_server.cpp:302
urcl::comm::TCPServer::INPUT_BUFFER_SIZE
static const int INPUT_BUFFER_SIZE
Definition: tcp_server.h:189
urcl::comm::TCPServer::handleConnect
void handleConnect()
Handles connection events.
Definition: tcp_server.cpp:170
urcl::comm::TCPServer::input_buffer_
char input_buffer_[INPUT_BUFFER_SIZE]
Definition: tcp_server.h:190
urcl::comm::TCPServer::max_clients_allowed_
uint32_t max_clients_allowed_
Definition: tcp_server.h:186
urcl
Definition: bin_parser.h:36
urcl::comm::TCPServer::message_callback_
std::function< void(const socket_t, char *buffer, int nbytesrecv)> message_callback_
Definition: tcp_server.h:194
urcl::comm::TCPServer::keep_running_
std::atomic< bool > keep_running_
Definition: tcp_server.h:176
urcl::comm::TCPServer::port_
int port_
Definition: tcp_server.h:180
urcl::comm::TCPServer::setMaxClientsAllowed
void setMaxClientsAllowed(const uint32_t &max_clients_allowed)
Set the maximum number of clients allowed to connect to this server.
Definition: tcp_server.h:152
urcl::comm::TCPServer::client_fds_
std::vector< socket_t > client_fds_
Definition: tcp_server.h:187
urcl::comm::TCPServer::setDisconnectCallback
void setDisconnectCallback(std::function< void(const socket_t)> func)
This callback will be triggered on clients disconnecting from the server.
Definition: tcp_server.h:92
urcl::comm::TCPServer::tempfds_
fd_set tempfds_
Definition: tcp_server.h:184
urcl::comm::TCPServer::getMaxClientsAllowed
uint32_t getMaxClientsAllowed() const
Get the maximum number of clients allowed to connect to this server.
Definition: tcp_server.h:141
urcl::comm::TCPServer::setMessageCallback
void setMessageCallback(std::function< void(const socket_t, char *, int)> func)
This callback will be triggered on messages received on the socket.
Definition: tcp_server.h:103
socket_t.h
urcl::comm::TCPServer
Wrapper class for a TCP socket server.
Definition: tcp_server.h:58
urcl::comm::TCPServer::readData
void readData(const socket_t fd)
read data from socket
Definition: tcp_server.cpp:261
urcl::comm::TCPServer::write
bool write(const socket_t fd, const uint8_t *buf, const size_t buf_len, size_t &written)
Writes to a client.
Definition: tcp_server.cpp:309
urcl::comm::TCPServer::handleDisconnect
void handleDisconnect(const socket_t fd)
Definition: tcp_server.cpp:241
urcl::comm::TCPServer::startListen
void startListen()
Definition: tcp_server.cpp:158
urcl::comm::TCPServer::worker_thread_
std::thread worker_thread_
Definition: tcp_server.h:177
urcl::comm::TCPServer::spin
void spin()
Event handler. Blocks until activity on any client or connection attempt.
Definition: tcp_server.cpp:204
urcl::comm::TCPServer::bind
void bind(const size_t max_num_tries, const std::chrono::milliseconds reconnection_time)
Definition: tcp_server.cpp:120
urcl::comm::TCPServer::init
void init()
Definition: tcp_server.cpp:64
urcl::comm::TCPServer::worker
void worker()
Runs spin() as long as keep_running_ is set to true.
Definition: tcp_server.cpp:293
urcl::comm::TCPServer::TCPServer
TCPServer()=delete
urcl::comm::TCPServer::disconnect_callback_
std::function< void(const socket_t)> disconnect_callback_
Definition: tcp_server.h:193
urcl::comm::TCPServer::new_connection_callback_
std::function< void(const socket_t)> new_connection_callback_
Definition: tcp_server.h:192
urcl::comm::TCPServer::listen_fd_
std::atomic< socket_t > listen_fd_
Definition: tcp_server.h:179
urcl::comm::TCPServer::setConnectCallback
void setConnectCallback(std::function< void(const socket_t)> func)
This callback will be triggered on clients connecting to the server.
Definition: tcp_server.h:81
urcl::comm::TCPServer::maxfd_
socket_t maxfd_
Definition: tcp_server.h:182
urcl::comm::TCPServer::~TCPServer
virtual ~TCPServer()
Definition: tcp_server.cpp:57


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Mon May 26 2025 02:35:58