3

如何在 libwebsockets 回调中设置用户自定义指针?

我在 lws_protocol 中添加了一个指针变量。

调用回调函数时,用户指针始终为 NULL。

我使用 libwebsockets v3.0。

static int interrupted, rx_seen, test;
int ws_callback(struct lws *ws, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
  // user is NULL

  return lws_callback_http_dummy(ws, reason, user, in, len);
}

int main() {
    struct lws *ws;
    struct lws_context_creation_info context_info;
    struct lws_client_connect_info client_info;
    struct lws_context *context;
    struct lws_protocols protocols[] = {
            { "ws_callback", ws_callback, 0, 0, 0, POINTER_VARIABLE /* HERE */, 0 }
    };

    int n = 0;

    // context creation info
    memset(&context_info, 0, sizeof(context_info));
    context_info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
    context_info.port = CONTEXT_PORT_NO_LISTEN;
    context_info.protocols = protocols;

    context = lws_create_context(&context_info);
    if (!context) {
        return;
    }

    memset(&client_info, 0, sizeof(client_info));
    client_info.context = context;
    client_info.port = 8080;
    client_info.address = "192.168.1.1";
    client_info.path = "/";
    client_info.host = client_info.address;
    client_info.origin = client_info.address;
    client_info.protocol = protocols[0].name;
    client_info.pwsi = &ws;

    lws_client_connect_via_info(&client_info);

    while (n >= 0 && ws && !interrupted) {
        n = lws_service(context, 1000);
    }

    lws_context_destroy(context);

  return 0;
}
4

1 回答 1

2

您必须指定每个会话数据结构的大小,而不是指针本身。每个连接的每个会话数据将不同。

typedef struct per_session_data {
    void *user_space;
} per_session_data;

int ws_callback(struct lws *ws, enum lws_callback_reasons reason, void *user, void *in, size_t len) 
{
    /* This will be different for every connected peer */
    per_session_data *data = (per_session_data*)user;
}

struct lws_protocols protocols[] = {
        { "ws_callback", ws_callback, sizeof(per_session_data), 0 }
        { NULL, NULL, 0, 0 } /* terminator */
};

您还可以设置将与上下文关联的可选用户指针。lws_context_creation_infouser用户空间的变量。您必须在创建上下文之前设置它

typedef struct per_session_data {
    void *user_space;

    /*
    Same other variables 
     */
} per_session_data;

int ws_callback(struct lws *ws, enum lws_callback_reasons reason, void *user, void *in, size_t len) 
{
    /* This will be different for every connected peer */
    per_session_data *data = (per_session_data*)user;

    /* This will be same for every connected peer */
    void *userdata = lws_context_user(lws_get_context(ws));
    /* userdata is POINTER_VARIABLE specified before context creating */

    switch (reason)
    {
        case LWS_CALLBACK_ESTABLISHED:
            /* Initialize per session data here */
            break;
        case LWS_CALLBACK_CLOSED:
            /* Destroy per session data here */;
            break;
        default:
            break;
    }

}

struct lws_protocols protocols[] = {
        { "ws_callback", ws_callback, sizeof(per_session_data), 0 }
        { NULL, NULL, 0, 0 } /* terminator */
};


int main() 
{
    /*
        your code here 
     */

    // context creation info
    memset(&context_info, 0, sizeof(context_info));
    context_info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
    context_info.port = CONTEXT_PORT_NO_LISTEN;
    context_info.protocols = protocols;
    context_info.user = POINTER_VARIABLE; /* HERE */

    context = lws_create_context(&context_info);
    if (!context) {
        return;
    }

    /*
        your code here 
     */
}
于 2019-07-29T12:15:01.760 回答