struct per_session_data__keyboard {
int session_id;
int valid;
+ int need_init;
+ int initialized;
};
static int callback_keyboard(struct lws *wsi,
pss->session_id = ++last_session_id;
LOGD("LWS_CALLBACK_ESTABLISHED : %p %d", g_ws_server_context, pss->session_id);
pss->valid = false;
+ pss->need_init = false;
+ pss->initialized = false;
if (g_ws_server_context) {
lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]);
}
case LWS_CALLBACK_SERVER_WRITEABLE:
if (agent) {
+ /* Ignore valid check since the magic key is not used anymore */
+ if (!pss->valid) pss->valid = true;
+
/* We allow data tranmission only if this client is guaranteed to be valid */
if (pss->valid) {
pthread_mutex_lock(&g_ws_server_mutex);
std::queue<ISE_MESSAGE>& messages = agent->get_send_message_queue();
- if (messages.size() > 0) {
- ISE_MESSAGE &message = messages.front();
+
+ if (pss->need_init && !pss->initialized) {
+ ISE_MESSAGE message;
+ message.type = ISE_MESSAGE_TYPE_STRINGS[ISE_MESSAGE_TYPE_PLAIN];
+ message.command = ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_INIT];
std::string str = CISEMessageSerializer::serialize(message);
- LOGD("SEND_WEBSOCKET_MESSAGE : %s", str.c_str());
+ LOGD("SEND_WEBSOCKET_MESSAGE : %d %s", pss->session_id, str.c_str());
n = snprintf((char *)p, bufsize, "%s", str.c_str());
/* too small for partial */
n = lws_write(wsi, p, n, LWS_WRITE_TEXT);
- messages.pop();
+ pss->need_init = false;
+ pss->initialized = true;
+ } else {
+ if (messages.size() > 0) {
+ ISE_MESSAGE &message = messages.front();
+ bool drop = false;
+ if (message.command.compare(ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_INIT]) == 0) {
+ if (pss->initialized) {
+ drop = true;
+ } else {
+ pss->initialized = true;
+ }
+ }
+ if (!drop) {
+ std::string str = CISEMessageSerializer::serialize(message);
+ LOGD("SEND_WEBSOCKET_MESSAGE : %d %s", pss->session_id, str.c_str());
+ n = snprintf((char *)p, bufsize, "%s", str.c_str());
+ /* too small for partial */
+ n = lws_write(wsi, p, n, LWS_WRITE_TEXT);
+ }
+ messages.pop();
+ }
}
- pthread_mutex_unlock(&g_ws_server_mutex);
if (n < 0) {
- LOGE("ERROR %d writing to di socket\n", n);
+ LOGE("ERROR %d writing to di socket %d", n, pss->session_id);
return -1;
}
if (messages.size() > 0) {
lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]);
}
+ pthread_mutex_unlock(&g_ws_server_mutex);
} else {
- LOGD("Rejecting data transmission since client is not valid");
+ LOGD("Rejecting data transmission since client is not valid : %d", pss->session_id);
}
}
break;
}
*/
pss->valid = true;
+
+ if (agent->initalized()) {
+ pss->need_init = true;
+ lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]);
+ }
}
+ /* Ignore valid check since the magic key is not used anymore */
+ if (!pss->valid) pss->valid = true;
+
if (pss->valid) {
pthread_mutex_lock(&g_ws_server_mutex);
std::queue<ISE_MESSAGE>& messages = agent->get_recv_message_queue();
pthread_mutex_unlock(&g_ws_query_mutex);
}
} else {
- LOGD("Ignoring data received since client is not valid");
+ LOGD("Ignoring data received since client is not valid %d", pss->session_id);
}
}
}
m_current_instance = this;
m_recv_message_pipe = NULL;
+ m_initialized = false;
}
CWebHelperAgentWebSocket::~CWebHelperAgentWebSocket()
on_init();
+ m_initialized = true;
+
return ret;
}