1

我正在使用 libwebsockets 开发一个小型服务,用于连接浏览器和我的服务。当它在 http 中工作时,一切正常。但是当我使用 openssl 切换到 https 时,它的效果很差。例如,当我在浏览器中单击超链接时,我的服务应该返回一张图片。但它的工作非常缓慢。执行此操作时,我的 cpu 运行大约 50%。我只是很困惑,需要你的帮助。非常感谢~主要代码如下:

case LWS_CALLBACK_HTTP:

    /* check for the "send a big file by hand" example case */
    sprintf(cache_file, "%s\\%s", g_cache_path, (char*)in+1);
    n = GetLastIndex(cache_file, '/');
    if (n >= 0)
    {
        cache_file[n] = '\\';
    }
    p = buffer;

            // cache_file is my file name , should be transfer
    pss->hFile = CreateFile(cache_file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);

    if (pss->hFile == NULL || pss->hFile == INVALID_HANDLE_VALUE)
        return -1;

    //fstat(pss->hFile, &stat_buf);

    /*
     * we will send a big jpeg file, but it could be
     * anything.  Set the Content-Type: appropriately
     * so the browser knows what to do with it.
     */

    p += sprintf((char *)p,
        "HTTP/1.0 200 OK\x0d\x0a"
        "Server: libwebsockets\x0d\x0a"
        "Content-Type: image/png\x0d\x0a"
            "Content-Length: %u\x0d\x0a\x0d\x0a",
            GetFileSize(pss->hFile, 0));

    /*
     * send the http headers...
     * this won't block since it's the first payload sent
     * on the connection since it was established
     * (too small for partial)
     */

    n = libwebsocket_write(wsi, buffer,
           p - buffer, LWS_WRITE_HTTP);

    if (n < 0) {
        CloseHandle(pss->hFile);
        return -1;
    }
    /*
     * book us a LWS_CALLBACK_HTTP_WRITEABLE callback
     */
    libwebsocket_callback_on_writable(context, wsi);
    break;

case LWS_CALLBACK_HTTP_WRITEABLE:

    do {
        ReadFile(pss->hFile, buffer, sizeof buffer, &readLen, NULL);

        if (readLen <= 0)
            goto bail;

        m = libwebsocket_write(wsi, buffer, readLen, LWS_WRITE_HTTP);
        if (m < 0)
            /* write failed, close conn */
            goto bail;
        if (m != readLen)
            /* partial write, adjust */
            SetFilePointer(pss->hFile, m - readLen, 0, FILE_CURRENT);

    } while (!lws_send_pipe_choked(wsi));
    libwebsocket_callback_on_writable(context, wsi);
    break;
4

0 回答 0