Program Listing for File tcp_server.h

Return to documentation for file (include/ur_client_library/comm/tcp_server.h)

// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-

// -- BEGIN LICENSE BLOCK ----------------------------------------------
// Copyright 2021 FZI Forschungszentrum Informatik
// Created on behalf of Universal Robots A/S
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -- END LICENSE BLOCK ------------------------------------------------

//----------------------------------------------------------------------
//----------------------------------------------------------------------

#ifndef UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
#define UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED

#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include <atomic>
#include <chrono>
#include <functional>
#include <thread>

namespace urcl
{
namespace comm
{
class TCPServer
{
public:
  TCPServer() = delete;
  explicit TCPServer(const int port, const size_t max_num_tries = 0,
                     const std::chrono::milliseconds reconnection_time = std::chrono::seconds(1));
  virtual ~TCPServer();

  void setConnectCallback(std::function<void(const int)> func)
  {
    new_connection_callback_ = func;
  }

  void setDisconnectCallback(std::function<void(const int)> func)
  {
    disconnect_callback_ = func;
  }

  void setMessageCallback(std::function<void(const int, char*, int)> func)
  {
    message_callback_ = func;
  }

  void start();

  void shutdown();

  bool write(const int fd, const uint8_t* buf, const size_t buf_len, size_t& written);

  uint32_t getMaxClientsAllowed() const
  {
    return max_clients_allowed_;
  }

  void setMaxClientsAllowed(const uint32_t& max_clients_allowed)
  {
    max_clients_allowed_ = max_clients_allowed;
  }

private:
  void init();
  void bind(const size_t max_num_tries, const std::chrono::milliseconds reconnection_time);
  void startListen();

  void handleConnect();

  void handleDisconnect(const int fd);

  void readData(const int fd);

  void spin();

  void worker();

  std::atomic<bool> keep_running_;
  std::thread worker_thread_;

  std::atomic<int> listen_fd_;
  int port_;

  int maxfd_;
  fd_set masterfds_;
  fd_set tempfds_;

  uint32_t max_clients_allowed_;
  std::vector<int> client_fds_;

  // Pipe for the self-pipe trick (https://cr.yp.to/docs/selfpipe.html)
  int self_pipe_[2];

  static const int INPUT_BUFFER_SIZE = 100;
  char input_buffer_[INPUT_BUFFER_SIZE];

  std::function<void(const int)> new_connection_callback_;
  std::function<void(const int)> disconnect_callback_;
  std::function<void(const int, char* buffer, int nbytesrecv)> message_callback_;
};

}  // namespace comm
}  // namespace urcl

#endif  // ifndef UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED