If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

.exe applications Hosting - ubuntu Linux

Started by kathleenrivero, Mar 14, 2023, 03:14 AM

Previous topic - Next topic

kathleenriveroTopic starter

I created a droplet on Digital Ocean, connected to the console, and installed Wine. The application runs successfully after launching it. However, the application unexpectedly shuts down after some time, which can vary from a month to just an hour.

Despite attempting to make changes in the code, none of them yielded any results. Here is the code snippet from the main function:

int main() {
    starting();
    add_log("Start new program: " + get_cur_time());//DEBUG
    while (1) {
        HINTERNET Session = WinHttpOpen(L"HTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
        if (!Session) {
            add_error("Session error: " + get_cur_time());
        }
        add_log("New longpoll: " + get_cur_time()); //DEBUG
        HINTERNET Connect = connect(L"api.vk.com", Session);
        if (!Connect) {
            add_error("Connect get long_poll error: " + get_cur_time());
        }
        HINTERNET Request = req(Connect, (L"/method/groups.getLongPollServer?group_id=" + group_id + L"&v=5.124&access_token=" + token).c_str());
        if (!Request) {
            add_error("Request get long_poll error: " + get_cur_time());
        }
        WinHttpSendRequest(Request, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
        WinHttpReceiveResponse(Request, NULL);
        string buffer1 = down(Request);
        //cout << buffer1 << endl;
        add_buffer(buffer1 + ": " + get_cur_time()); //BUFFER
        nlohmann::json serverjson = nlohmann::json::parse(buffer1);
        if (!serverjson["response"].is_null()) {
            string key = serverjson["response"]["key"];
            string server = serverjson["response"]["server"];
            string ts = serverjson["response"]["ts"];
            string wh = server.substr(18, server.size() - 1);
            server.erase(17, server.size() - 1);
            server.erase(0, 8);
            wstring s1 = wstring(server.begin(), server.end());
            LPCWSTR L7 = s1.c_str();
            WinHttpCloseHandle(Request);
            WinHttpCloseHandle(Connect);


            HINTERNET Connect1 = connect(L7, Session);
            if (!Connect1) {
                add_error("Connect long_poll error: " + get_cur_time());
            }
            int i = 0;
            while (1) {
                string longpoll = "//" + wh + "?act=a_check&key=" + key + "&ts=" + ts + "&wait=0";
                wstring s3 = wstring(longpoll.begin(), longpoll.end());
                LPCWSTR L8 = s3.c_str();
                HINTERNET Request1 = req(Connect1, L8);
                if (!Request1) {
                    add_error("Request long_poll error: " + get_cur_time());
                }
                WinHttpSendRequest(Request1, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
                WinHttpReceiveResponse(Request1, NULL);
                string buffer2 = down(Request1);
                //cout << buffer2 << endl;
                add_buffer(buffer2 + ": " + get_cur_time());//BUFFER
                WinHttpCloseHandle(Request1);
                nlohmann::json res = nlohmann::json::parse(buffer2);
                if (!res["updates"].is_null()) {
                    int n = res["updates"].size();
                    while (i < n) {
                        if (res["updates"][i]["type"] == "message_new") {
                            string text = res["updates"][i]["object"]["message"]["text"];
                            string id = to_string(res["updates"][i]["object"]["message"]["from_id"]);
                            string peerid = to_string(res["updates"][i]["object"]["message"]["peer_id"]);
                            string msgid = to_string(res["updates"][i]["object"]["message"]["conversation_message_id"]);
                            string kout = "Id = " + id + "   " + "Peerid = " + peerid + "   " + "Text = " + text;
                            add_log(kout + ": " + get_cur_time());//DEBUG
                            //cout << kout << endl;
                            analyze(id, str_wstr(peerid), str_wstr(msgid), text, Session);
                        }
                        i++;
                    }
                }
                else {
                    break;
                }
            }
            WinHttpCloseHandle(Connect1);
        }
        WinHttpCloseHandle(Session);
    }
    return 0;
}
  •  

Rita Jaiswal

Your code has an unusual error handling approach. Take a look at this snippet:

```
HINTERNET Connect1 = connect(L7, Session);
if (!Connect1) {
    add_error("Connect long_poll error: " + get_cur_time());
}
```

Here, an error occurs, it is handled, but the code continues to execute without proper preparation. This can lead to incorrect descriptors being passed to subsequent functions, resulting in unpredictable behavior. It's hard to say what will happen next.

To address this issue, it's important to implement proper error handling. For example, in the first case, using `continue;` to restart the loop may suffice. However, in the inner loop, more careful consideration is required, such as closing open descriptors if necessary.

In general, if I were to rewrite this code, I would consider using Go or Python instead. These languages offer more robust error handling mechanisms, and you wouldn't need Wine. Of course, the choice of language ultimately depends on personal preferences and project requirements.
  •  

johncruz

From what I gather, the issue lies within these lines of code: `nlohmann::json res = nlohmann::json::parse(buffer2);`. The JSON was parsed, but it attempted to parse an empty string, resulting in an exception and causing the program to crash. To address this, simply add the following check: `if (nlohmann::json::accept(buffer2))`.

This modification ensures that the program verifies if the JSON is acceptable for parsing before actually attempting to parse it. This way, it avoids trying to parse an empty string and reduces the risk of encountering exceptions and crashes. It's a simple yet effective addition that can prevent such errors from occurring.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...