From d4216dff7780b6b239520d1555114f5fd762cd50 Mon Sep 17 00:00:00 2001 From: InHong Han Date: Tue, 31 May 2016 14:33:44 +0900 Subject: [PATCH] Replace old libwebsocket functions with a new API Change-Id: Ie3ab79a758ac5669f68c8ce16f7cb9661cdc97a3 --- ism/extras/efl_panel/websocketserver.cpp | 195 ++++++++++++++++++++----------- 1 file changed, 125 insertions(+), 70 deletions(-) diff --git a/ism/extras/efl_panel/websocketserver.cpp b/ism/extras/efl_panel/websocketserver.cpp index c4b5bbd..62a49f4 100644 --- a/ism/extras/efl_panel/websocketserver.cpp +++ b/ism/extras/efl_panel/websocketserver.cpp @@ -47,7 +47,7 @@ pthread_cond_t g_ws_query_condition = PTHREAD_COND_INITIALIZER; pthread_mutex_t g_ws_query_mutex = PTHREAD_MUTEX_INITIALIZER; bool g_ws_server_exit = false; -struct libwebsocket_context *g_ws_server_context = NULL; +struct lws_context *g_ws_server_context = NULL; WebSocketServer* WebSocketServer::m_current_instance = NULL; static Remote_Input* remote_input_impl= NULL; @@ -160,10 +160,9 @@ struct per_session_data__http { /* this protocol server (always the first one) just knows how to do HTTP */ -static int callback_http(struct libwebsocket_context *context, - struct libwebsocket *wsi, - enum libwebsocket_callback_reasons reason, void *user, - void *in, size_t len) +static int callback_http(struct lws *wsi, + enum lws_callback_reasons reason, + void *user, void *in, size_t len) { LOGD(" "); #if 0 @@ -217,7 +216,7 @@ static int callback_http(struct libwebsocket_context *context, * (too small for partial) */ - n = libwebsocket_write(wsi, buffer, + n = lws_write(wsi, buffer, p - buffer, LWS_WRITE_HTTP); if (n < 0) { @@ -227,7 +226,7 @@ static int callback_http(struct libwebsocket_context *context, /* * book us a LWS_CALLBACK_HTTP_WRITEABLE callback */ - libwebsocket_callback_on_writable(context, wsi); + lws_callback_on_writable(wsi); break; } @@ -239,7 +238,7 @@ static int callback_http(struct libwebsocket_context *context, sprintf(buf, LOCAL_RESOURCE_PATH"%s", whitelist[n].urlpath); - if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype)) + if (lws_serve_http_file(wsi, buf, whitelist[n].mimetype, NULL, NULL)) return -1; /* through completion or error, close the socket */ /* @@ -272,7 +271,7 @@ static int callback_http(struct libwebsocket_context *context, * because it's HTTP and not websocket, don't need to take * care about pre and postamble */ - m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP); + m = lws_write(wsi, buffer, n, LWS_WRITE_HTTP); if (m < 0) /* write failed, close conn */ goto bail; @@ -281,7 +280,7 @@ static int callback_http(struct libwebsocket_context *context, lseek(pss->fd, m - n, SEEK_CUR); } while (!lws_send_pipe_choked(wsi)); - libwebsocket_callback_on_writable(context, wsi); + lws_callback_on_writable(wsi); break; bail: @@ -359,14 +358,13 @@ struct per_session_data__keyboard { int valid; }; -static int -callback_keyboard(struct libwebsocket_context *context, -struct libwebsocket *wsi, - enum libwebsocket_callback_reasons reason, +static int +callback_keyboard(struct lws *wsi, + enum lws_callback_reasons reason, void *user, void *in, size_t len); /* list of supported protocols and callbacks */ -static struct libwebsocket_protocols protocols[] = { +static struct lws_protocols protocols[] = { /* first protocol must always be HTTP handler */ { "http-only", /* name */ @@ -384,10 +382,9 @@ static struct libwebsocket_protocols protocols[] = { }; static int -callback_keyboard(struct libwebsocket_context *context, - struct libwebsocket *wsi, - enum libwebsocket_callback_reasons reason, - void *user, void *in, size_t len) +callback_keyboard(struct lws *wsi, + enum lws_callback_reasons reason, + void *user, void *in, size_t len) { LOGD(" %d",reason); static int last_session_id = 0; @@ -404,7 +401,9 @@ callback_keyboard(struct libwebsocket_context *context, pss->session_id = ++last_session_id; LOGD("LWS_CALLBACK_ESTABLISHED : %d", pss->session_id); //pss->valid = false; - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } break; case LWS_CALLBACK_CLOSED: @@ -419,12 +418,12 @@ callback_keyboard(struct libwebsocket_context *context, std::queue& messages = agent->get_send_message_queue(); while (messages.size() > 0) { ISE_MESSAGE &message = messages.front(); - //n = sprintf((char *)p, "%s %s", message.command.c_str(), message.value.c_str()); + std::string str = CISEMessageSerializer::serialize(message); LOGD("SEND_WEBSOCKET_MESSAGE : %s", str.c_str()); n = snprintf((char *)p, bufsize, "%s", str.c_str()); /* too small for partial */ - n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT); + n = lws_write(wsi, p, n, LWS_WRITE_TEXT); messages.pop(); } pthread_mutex_unlock(&g_ws_server_mutex); @@ -508,7 +507,7 @@ void *process_ws_server(void *data) * the number of ms in the second argument. */ - libwebsocket_service(g_ws_server_context, 50); + lws_service(g_ws_server_context, 50); } return NULL; } @@ -568,7 +567,7 @@ bool WebSocketServer::init() info.iface = NULL; info.protocols = protocols; #ifndef LWS_NO_EXTENSIONS - info.extensions = libwebsocket_get_internal_extensions(); + info.extensions = lws_get_internal_extensions(); #endif info.ssl_cert_filepath = NULL; info.ssl_private_key_filepath = NULL; @@ -587,7 +586,7 @@ bool WebSocketServer::init() int retry_num = 0; do { - g_ws_server_context = libwebsocket_create_context(&info); + g_ws_server_context = lws_create_context(&info); LOGD("libwebsocket context : %p", g_ws_server_context); sleep(1); } while (g_ws_server_context == NULL && retry_num++ < max_retry_num); @@ -598,7 +597,10 @@ bool WebSocketServer::init() pthread_cond_init(&g_ws_query_condition, NULL); if (g_ws_server_context) { - pthread_create(&g_ws_server_thread, NULL, &process_ws_server, NULL); + if (pthread_create(&g_ws_server_thread, NULL, &process_ws_server, NULL) != 0) { + g_ws_server_thread = (pthread_t)NULL; + ret = false; + } } else { ret = false; } @@ -633,7 +635,7 @@ bool WebSocketServer::exit() pthread_mutex_destroy(&g_ws_server_mutex); - libwebsocket_context_destroy(g_ws_server_context); + lws_context_destroy(g_ws_server_context); return true; } @@ -666,7 +668,11 @@ void WebSocketServer::on_init() m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_exit() @@ -680,7 +686,11 @@ void WebSocketServer::on_exit() m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_focus_in(int ic) @@ -695,7 +705,11 @@ void WebSocketServer::on_focus_in(int ic) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_focus_out(int ic) @@ -710,7 +724,11 @@ void WebSocketServer::on_focus_out(int ic) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_show(int ic) @@ -725,9 +743,11 @@ void WebSocketServer::on_show(int ic) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); - - LOGD("put into send message buffer"); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_hide(int ic) @@ -742,7 +762,11 @@ void WebSocketServer::on_hide(int ic) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_set_rotation(int degree) @@ -757,7 +781,11 @@ void WebSocketServer::on_set_rotation(int degree) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_update_cursor_position(int ic, int cursor_pos) @@ -773,7 +801,11 @@ void WebSocketServer::on_update_cursor_position(int ic, int cursor_pos) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_set_language(unsigned int language) @@ -796,7 +828,11 @@ void WebSocketServer::on_set_language(unsigned int language) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } } @@ -812,7 +848,11 @@ void WebSocketServer::on_set_imdata(char *buf, unsigned int len) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_get_imdata(char **buf, unsigned int *len) @@ -826,7 +866,11 @@ void WebSocketServer::on_get_imdata(char **buf, unsigned int *len) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } wait_for_reply_message(); @@ -871,7 +915,11 @@ void WebSocketServer::on_set_return_key_type(unsigned int type) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } } @@ -886,12 +934,14 @@ void WebSocketServer::on_get_return_key_type(unsigned int *type) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } wait_for_reply_message(); - /* Since we are accessing recved buffer, lock the server mutex again */ - pthread_mutex_lock(&g_ws_server_mutex); std::vector values; /* Check if we received reply for GET_RETURN_KEY_TYPE message */ if (process_recved_messages_until_reply_found( @@ -906,7 +956,6 @@ void WebSocketServer::on_get_return_key_type(unsigned int *type) } /* Now process the rest in the recv buffer */ process_recved_messages(); - pthread_mutex_unlock(&g_ws_server_mutex); } void WebSocketServer::on_set_return_key_disable(unsigned int disabled) @@ -929,13 +978,16 @@ void WebSocketServer::on_set_return_key_disable(unsigned int disabled) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } } void WebSocketServer::on_get_return_key_disable(unsigned int *disabled) { - LOGD(" "); ISE_MESSAGE message; message.type = ISE_MESSAGE_TYPE_STRINGS[ISE_MESSAGE_TYPE_QUERY]; message.command = ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_GET_RETURN_KEY_DISABLE]; @@ -944,12 +996,14 @@ void WebSocketServer::on_get_return_key_disable(unsigned int *disabled) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } wait_for_reply_message(); - /* Since we are accessing recved buffer, lock the server mutex again */ - pthread_mutex_lock(&g_ws_server_mutex); std::vector values; /* Check if we received reply for GET_RETURN_KEY_DISABLE message */ if (process_recved_messages_until_reply_found( @@ -964,19 +1018,17 @@ void WebSocketServer::on_get_return_key_disable(unsigned int *disabled) } /* Now process the rest in the recv buffer */ process_recved_messages(); - pthread_mutex_unlock(&g_ws_server_mutex); } void WebSocketServer::on_set_layout(unsigned int layout) { - LOGD(" "); ISE_MESSAGE message; message.type = ISE_MESSAGE_TYPE_STRINGS[ISE_MESSAGE_TYPE_PLAIN]; message.command = ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_SET_LAYOUT]; bool found = false; for (unsigned int loop = 0;loop < sizeof(ISE_LAYOUT_TYPES) / sizeof(ISE_TYPE_VALUE_STRING);loop++) { - if (layout == ISE_LAYOUT_TYPES[loop].type_value) { + if (layout == (unsigned int)ISE_LAYOUT_TYPES[loop].type_value) { message.values.push_back(ISE_LAYOUT_TYPES[loop].type_string); found = true; } @@ -987,13 +1039,16 @@ void WebSocketServer::on_set_layout(unsigned int layout) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } } void WebSocketServer::on_get_layout(unsigned int *layout) { - LOGD(" "); ISE_MESSAGE message; message.type = ISE_MESSAGE_TYPE_STRINGS[ISE_MESSAGE_TYPE_QUERY]; message.command = ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_GET_LAYOUT]; @@ -1002,12 +1057,14 @@ void WebSocketServer::on_get_layout(unsigned int *layout) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } wait_for_reply_message(); - /* Since we are accessing recved buffer, lock the server mutex again */ - pthread_mutex_lock(&g_ws_server_mutex); std::vector values; /* Check if we received reply for GET_LAYOUT message */ if (process_recved_messages_until_reply_found( @@ -1022,12 +1079,10 @@ void WebSocketServer::on_get_layout(unsigned int *layout) } /* Now process the rest in the recv buffer */ process_recved_messages(); - pthread_mutex_unlock(&g_ws_server_mutex); } void WebSocketServer::on_reset_input_context(int ic) { - LOGD(" "); ISE_MESSAGE message; message.type = ISE_MESSAGE_TYPE_STRINGS[ISE_MESSAGE_TYPE_PLAIN]; message.command = ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_RESET_INPUT_CONTEXT]; @@ -1037,12 +1092,15 @@ void WebSocketServer::on_reset_input_context(int ic) m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } } void WebSocketServer::on_process_key_event(unsigned int code, unsigned int mask, unsigned int layout, unsigned int *ret) { - LOGD(" "); ISE_MESSAGE message; message.type = ISE_MESSAGE_TYPE_STRINGS[ISE_MESSAGE_TYPE_QUERY]; message.command = ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_PROCESS_KEY_EVENT]; @@ -1054,12 +1112,14 @@ void WebSocketServer::on_process_key_event(unsigned int code, unsigned int mask, m_send_message_queue.push(message); pthread_mutex_unlock(&g_ws_server_mutex); - libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_KEYBOARD]); + if (g_ws_server_context) { + lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]); + } else { + LOGW("WARNING : g_ws_server_context is NULL"); + } wait_for_reply_message(); - /* Since we are accessing recved buffer, lock the server mutex again */ - pthread_mutex_lock(&g_ws_server_mutex); std::vector values; /* Check if we received reply for PROCESS_KEY_EVENT message */ if (process_recved_messages_until_reply_found( @@ -1074,30 +1134,25 @@ void WebSocketServer::on_process_key_event(unsigned int code, unsigned int mask, } /* Now process the rest in the recv buffer */ process_recved_messages(); - pthread_mutex_unlock(&g_ws_server_mutex); } WebSocketServer* WebSocketServer::get_current_instance() { - LOGD(" "); return m_current_instance; } std::queue& WebSocketServer::get_send_message_queue() { - LOGD(" "); return m_send_message_queue; } std::queue& WebSocketServer::get_recv_message_queue() { - LOGD(" "); return m_recv_message_queue; } Ecore_Pipe* WebSocketServer::get_recv_message_pipe() { - LOGD(" "); return m_recv_message_pipe; } -- 2.7.4