Skip to content

reficul0/cliver

Repository files navigation

Cliver

Build status Codacy Badge CodeFactor MIT License FOSSA Status
language c++

Overview

Cliver is a C++ network client-server framework.

Builds upon Boost.ASIO to provide a simple API for developers.

Features

Features:

  • Server can send packets to clients
  • Clients can handle server packets
  • Server can accept many clients connections
  • Cross-platform compatible

Installation

Requirements

  • [Required] - Conan.
  • [Required] - pthread(Linux only).
  • [Installing by conan] - Boost 1.70.

Building on Windows

- git clone [email protected]:reficul0/cliver.git
- mkdir build && cd build
- conan install ..
- cmake -A %platform% -G "Visual Studio 15 2017" -DBUILD_TESTS=OFF ..
- cmake --build .

Building on Linux

$ git clone [email protected]:reficul0/cliver.git
$ mkdir build && cd build
$ conan install ..
$ cmake -DBUILD_TESTS=OFF ..
$ cmake --build .

Getting Started

Create server, start connections acception and send to all connected clients specific packet:

#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <tools/asio/ip/tcp_server.hpp>

int main(int argc, char const* argv[]) {
  if (argc < 3)
  {
    std::cout << "Usage <port> <wait_for_first_connection_timout_milliseconds>" << std::endl;
    return EXIT_SUCCESS;
  }
  
  auto const port = std::stoul(argv[1]);
  boost::chrono::milliseconds const wait_for_first_connection_timout_ms{std::stoul(argv[2])};
  
  boost::asio::io_service io_service;
  boost::asio::io_service::work work{ io_service };
  std::thread io_thread{
    [&io_service]()
    {
      io_service.run();
    }
  };

  ip::tcp::server server{ io_service, port_number };
  
  server.start_acception();
  std::vector<uint8_t> packet = {1, 2, 3, 4};

  while (true)
  {
    if (!server.get_connections_count())
    {
      boost::this_thread::sleep_for(wait_for_first_connection_timout_ms);
      continue;
    }

    server.write(packet.data(), packet.size());
  }
  
  return (EXIT_SUCCESS);
}

Create client, connect to server and read its packet:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <tools/asio/ip/tcp_server.hpp>

int main(int argc, char const* argv[]) {
  if (argc < 3)
  {
    std::cout << "Usage <host_ip> <port>" << std::endl;
    return EXIT_SUCCESS;
  }
  
  auto host_ip = argv[1];
  unsigned short port_number = std::stoul(argv[2]);
  
  boost::asio::io_service io_service;
  ip::tcp::client client{ io_service };
  
  auto connect_to_server = [host_ip, port_number](decltype(client) &connect_me)
  {
    while (true)
    {
      connect_me.connect(host_ip, std::to_string(port_number));
      if (connect_me.is_connected())
        break;

      // todo customize wait timeout
      boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
    }
  };
  std::vector<uint8_t> packet;
  packet.resize(4);
  
  while (true)
  {
    if (!client.is_connected())
      connect_to_server(client);
		
    if(client.read(packet.data(), packet.size()) != packet.size())
      std::cout << "Accepted packet is less than expected" << std:: endl;
      
    // process packet...
  }
  
  return (EXIT_SUCCESS);
}

System Compatibility

OS Compiler Status
Windows msvc15 ✅ Working
Linux gcc ✅ Working

Contributing

Pull requests are welcome.

Authors

License

FOSSA Status