jeudi 28 novembre 2019

How to make a TCP/IP sequencer

I have a OpenGL , QT application in which i am sending remote commands from a client , Based on the incoming commands the application is rendering.

The process works fine but i am not very proficient with networking and would like to know how i can improve the concept of the sequencer for the incoming commands that i have built.

This is the Main function.

int main(int argc, char *argv[])
{   
    QApplication a(argc, argv);
    cont.SetName("RootItem");
    TreeModel* model = new TreeModel("RootElement", &cont);
    WavefrontRenderer w(model);
    w.show();    // Show the QT GUI
    glfwInit();  // Initialioze the GLFW
    window = glfwCreateWindow(SCR_WIDTH , SCR_HEIGHT, "Renderer", nullptr, nullptr);   // Create the render window
    glfwMakeContextCurrent(window);
    GLenum GlewInitResult;
    glewExperimental = GL_TRUE;
    GlewInitResult = glewInit();    


          boost::asio::io_service io_service;
    server server1(io_service, 1980);
    boost::thread t(boost::bind(&io_service::run, &io_service)); // Start The service in a new thread so that it does not block the GUI

         while(!glfwWindowShouldClose(window))
    {
             // Process incoming commands
        processCommands()
             //Drawing Stuff here
                    Draw();

          }

    auto raii = makeCleaner( [&]() { io_service.stop(); });  // Stop the thread when the UI of QT closes
    return a.exec();
}

I create a server on a new thread so that the GUI is not blocked.

Than in my rendering loop i first collect the incoming commands , process them and than render the scene.

To collect the commands from the server i have a global extern std::vector , whenever a new message comes to the server i pushback the string into this vector.

To process these commands before drawing i have a class named CommandInterface in which i run a loop based on vector.size() of the same extern std::vector.

The server when receiving commands from telnet is not able to to receive all the incoming commands.

Also i have a question when i am running the std::vector loop in Command interface and at that time server receives a message and adds it to the the same vector would that cause a crash ?

This is the code for the server.

#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <qdebug.h>
#include <global.h>
using boost::asio::ip::tcp;
class session
{
public:
    session(boost::asio::io_service& io_service)
        : socket_(io_service)
    {
    }

    tcp::socket& socket()
    {
        return socket_;
    }

    void start()
    {       
        boost::asio::streambuf buf;
        boost::asio::read_until(socket_, buf, ";");
        std::string data = boost::asio::buffer_cast<const char*>(buf.data());
        incomingCommands.push_back(data); // Fill the the vector with incoming commands

    }

private:
    void handle_read(const boost::system::error_code& error,
        size_t bytes_transferred)
    {
        if (!error)
        {
            boost::asio::async_write(socket_,
                boost::asio::buffer("Wavefront_Hello", bytes_transferred),
                boost::bind(&session::handle_write, this,
                    boost::asio::placeholders::error));

        }
        else
        {
            delete this;
        }
    }

    void handle_write(const boost::system::error_code& error)
    {
        if (!error)
        {
            socket_.async_read_some(boost::asio::buffer(data_, max_length),
                boost::bind(&session::handle_read, this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
        }
        else
        {
            delete this;
        }
    }

    tcp::socket socket_;
    enum { max_length = 2048 };
    char data_[max_length];
};

class server
{
public:
    server(boost::asio::io_service& io_service, short port)
        : io_service_(io_service),
        acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
    {
        start_accept();
    }

private:
    void start_accept()
    {
        session* new_session = new session(io_service_);
        acceptor_.async_accept(new_session->socket(),
            boost::bind(&server::handle_accept, this, new_session,
                boost::asio::placeholders::error));
    }

    void handle_accept(session* new_session,
        const boost::system::error_code& error)
    {
        if (!error)
        {
            new_session->start();
        }
        else
        {
            delete new_session;
        }

        start_accept();
    }

    boost::asio::io_service& io_service_;
    tcp::acceptor acceptor_;
};

Aucun commentaire:

Enregistrer un commentaire