gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:16:41 27 4
gpt4 key购买 nike

如何在 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;
}

最佳答案

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

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_info 有用户空间的 user 变量。您必须在创建上下文之前设置它

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
*/
}

关于c - 如何在 libwebsockets 回调中设置用户自定义指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51353478/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com