Home  >  Article  >  Web Front-end  >  javascript - Problems and practical codes in the interaction between C++ and nodejs

javascript - Problems and practical codes in the interaction between C++ and nodejs

php是最好的语言
php是最好的语言Original
2018-08-02 15:00:161498browse

1. Question

First of all, the big background is the interaction between C and js (addon), which can be data, functions, etc. . Among them, js passes a json data to C, which is a C http network request. Then C opens a sub-thread, establishes a tcp connection, and then makes an http request. Finally, the response information of the http request is passed to the js layer, or callback nodejs It is also possible to process response information.

Among them, C establishes tcp connection and http request is implemented by a class, and the process is asynchronous. There is a callback function on_read() that will get the response information from the server and process it.
C After getting the json request of js, we start the network work.

The problem is:
a. I don’t know when the response information will be returned, and I can’t wait for the response like synchronization.
b. The v8 api cannot be used outside the main thread of js, which means that the response information cannot be directly returned to js in the on_read() callback function

2. Code

class TCP_CLIENT : public IConnectionHandler
{
    bool isdone;
public:
    TCP_CLIENT(const char* host, int port, const char* data, Isolate *isolate, const FunctionCallbackInfo<Value> args)
    {
        this->host_ = host;
        this->port_ = port;
        this->request_data_ = data;
        isolate_ = isolate;
    }
    ~TCP_CLIENT()
    {
        std::cout << "~tcp_client" << std::endl;
    }
    
    /*callback handler implement回调接口实现*/

    //建立连接的回调
    void on_established(IBaseConnection*  conn) override_final
    {
        std::cout << "Connect Successful" << std::endl;
        this->send_request(conn);
    }

    。。。。。。。。。
    
    //服务器响应得到数据的回调
    int on_read(int err, int bytes_transfered, IBaseConnection* conn, SharedBufferPtr buffer) override_final
    {

        if (err == 0 && bytes_transfered)
        {
            std::cout << "In read, thread id is ----" << GetCurrentThreadId() << std::endl;
            buffer->erase(bytes_transfered);
            if (conn->is_established())
            {
                conn->stop();
            }
                
        }
        return 0;
    }

    //发送数据的回调
    void on_write(int err, int bytes_transferred, IBaseConnection* conn, SharedBufferPtr buffer) override_final
    {
        if (err == 0 && bytes_transferred > 0)
        {
            std::cout << "Send Request Successful------>" << buffer->length() << std::endl;
            buffer->erase(bytes_transferred);
        }
        else {}
    }

private:
    const char* host_;
    int port_;
    const char* request_data_;
    http::ParserPtr response_parser_;
    Isolate *isolate_;
};


const char* ToCString(const v8::String::Utf8Value& value)
{
    return *value ? *value : "<string conversion failed>";
}

void entry(const FunctionCallbackInfo<Value>& args)
{
    std::cout << "In entry, process id is ----" << GetCurrentProcessId() << std::endl;
    std::cout << "In entry, thread id is ----" << GetCurrentThreadId() << std::endl;

    //启动一个线程执行tcp连接
    global_http_transfer_context_pool.run();

    Isolate* isolate = args.GetIsolate();

    if (args.Length() != 2)
    {
        isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "参数数量必须为二")));
        return;
    }
    if (!args[0]->IsString() || !args[1]->IsNumber())
    {
        isolate->ThrowException(Exception::TypeError(
            v8::String::NewFromUtf8(isolate, "参数类型错误")
        ));
    }

    v8::String::Utf8Value str_temp(args[0]);
    const char* data = ToCString(str_temp);

    TCP_CLIENT* tcp_client;
    if (args[1]->NumberValue() == 1)
    {
        tcp_client = new TCP_CLIENT(cms_host, cms_port, data, isolate, args);
        tcp_client->perform(1);

    }
    else
    {
        tcp_client = new TCP_CLIENT(liteapp_host, liteapp_port, data, isolate, args);
        tcp_client->perform(0);
    }
}

void Init(Local<Object> exports)
{
    NODE_SET_METHOD(exports, "http_request", entry);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

Methods to try:
a. Set up a while loop to wait in the main thread. After on_read() returns data to js, ​​the sub-thread sets a flag to indicate the end. This can It ends correctly, but C's asynchronous request becomes a synchronization, which is meaningless.
b. I saw the libuv method on the Internet, but there is still a problem that the libuv working function cannot get the C asynchronous response message. The main issue is how to handle the asynchronous message and C return to the node, and how to handle it between threads.

Related articles:

NodeJS and Mysql interaction sample code_javascript skills

Detailed introduction to C# code and javaScript functions Mutual calls

Related videos:

Nodejs mongoDB practical development Weibo system video tutorial

The above is the detailed content of javascript - Problems and practical codes in the interaction between C++ and nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn