jeudi 2 avril 2020

Do i need boost strand

I modified echo example but I have issues understanding boost strand. Do I need or not? I yes please be kind and give code for. I searched examples but I can not understand (Im just stupid..) Thank you

#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include "mingw.thread.h"
#include <boost/bind.hpp>
class session
  : public std::enable_shared_from_this<session>
{
public:
  session(boost::asio::ip::tcp::socket socket)
    : socket_(std::move(socket))
  {
  }
  void start()
  {
    input_buffer_str = "[2J[H[A\r\n*ServerV8*\r\nSCAN_BADGE:\r\n";
    out_msg =input_buffer_str;
    do_write(out_msg.length());
  }
private:
  void do_read()
  {
     //  std::cout << "do_read" <<"" <<std::endl;
    auto self(shared_from_this());

//async_read_until for Telnet client

    async_read_until(socket_,input_buffer_,'\r',
        [this, self](boost::system::error_code ec, std::size_t length)
        {
          if (!ec)
          {
            do_write(length);
          }
        else
          {
               // std::cout<<ec <<"Disconection?"<<std::endl;
                input_buffer_str.clear();
                out_msg.clear();
                input_buffer_.consume(length);
                // Some Cleanup
          }
        });
  }

// async_write back to Telnet client based on received // will be result on SQL Query


  void do_write(std::size_t length)
  {
    auto self(shared_from_this());
    std::istream(&input_buffer_) >> input_buffer_str;
    input_buffer_.consume(length);
      std::cout << input_buffer_str  <<std::endl;
  if (input_buffer_str == "c") close();
    if (input_buffer_str == "a") out_msg = "test_a\r\n";
     if (input_buffer_str == "t")
        {
            Sleep(30000);
         out_msg = "test_t\r\n";
            }
     if(input_buffer_str.length()>0 && out_msg.length()>0)
     {
          input_buffer_str.clear();
        boost::asio::async_write(socket_, boost::asio::buffer(out_msg, out_msg.length()),
        [this, self](boost::system::error_code ec, std::size_t length)
        {
          if (!ec) do_read();
          else
          {
            out_msg.clear();
            input_buffer_.consume(length);
            // Some Cleanup
          }
          out_msg.clear();
        });
     }else  do_read();
  }

// close() Telnet client will be when user pushes F2 (Handheld scanner)


void close()
  {
    std::cout <<"Closing..."<<std::endl;
    boost::system::error_code ec;
    input_buffer_str.clear();
    out_msg.clear();
    socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
    Sleep(250);
    socket_.close(ec);
   // delete data_;
        if (ec)
        {
            std::cout<<ec << std::endl;
          // An error occurred.
        }
        std::cout <<"Closed..."<<std::endl;
  }
boost::asio::streambuf input_buffer_;
std::string input_buffer_str;
std::string out_msg ;
boost::asio::ip::tcp::socket socket_;
  //enum { max_length = 1024 };
  //char data_[max_length];
//std::string ClearScreen= "[2J[H[A"; "\r\n*ServerV8*\r\nSCAN_BADGE:\r\n";
};

Copy paste from example

class server
{
public:
  server(boost::asio::io_context& io_context, short port)
    : acceptor_(io_context, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
  {
    do_accept();
  }
private:
  void do_accept()
  {
    acceptor_.async_accept(
        [this](boost::system::error_code ec, boost::asio::ip::tcp::socket socket)
        {
          if (!ec)
          {
            std::make_shared<session>(std::move(socket))->start();
          }
          do_accept();
        });
  }
boost::asio::ip::tcp::acceptor acceptor_;
};

added Threads Some queries to sql might take depends on Network


int main(int argc, char* argv[])
{
    auto thread_count = std::thread::hardware_concurrency(); // for multi core
    boost::thread_group     m_Threads;
    boost::asio::io_context m_io_context;
    server srv(m_io_context, 23);
    for(unsigned int i = 0; i < thread_count; ++i)
        m_Threads.create_thread( [&](){m_io_context.run();});
    m_Threads.join_all();
  return 0;
}

Aucun commentaire:

Enregistrer un commentaire