write_buf_count = 0;
write_buf_index = 0;
write_buf_last_sent_index = 0;
+ cl_payload = NULL;
+ is_header_parsed = false;
+ eventType = "";
}
Channel::Channel(Service *service1, string uri1) {
write_buf_count = 0;
write_buf_index = 0;
write_buf_last_sent_index = 0;
+ cl_payload = NULL;
+ is_header_parsed = false;
+ eventType = "";
}
Channel *Channel::create(Service *service, string uri) {
delete connectionHandler;
connectionHandler = NULL;
}
+
+ if (cl_payload != NULL) {
+ free(cl_payload);
+ cl_payload = NULL;
+ }
}
void Channel::init_json_key_map() {
}
connectionHandler->stopPing();
-
was_closed = true;
lws_callback_on_writable(wsi_mirror);
return -1;
} else {
}
-
- this_ptr->eventType = "";
-
if (lws_frame_is_binary(wsi)) {
- MSF_DBG("BINARY MESSAGE ARRIVED");
+ MSF_DBG("BINARY MESSAGE ARRIVED. len:%d", len);
+ // header needs to be parsed on first chunk
+ if (this_ptr->is_header_parsed == false) {
+ MSF_DBG("first chunk. parsing header");
+ int header_size = 0;
- int header_size = 0;
+ header_size = (int)((unsigned char *)in)[0];
+ header_size = header_size << 8;
+ header_size = header_size & 0xff00;
+ header_size = header_size | (int)((unsigned char *)in)[1];
+ MSF_DBG("header_size = %d", header_size);
- header_size = (int)((unsigned char *)in)[0];
- header_size = header_size << 8;
- header_size = header_size & 0xff00;
- header_size = header_size | (int)((unsigned char *)in)[1];
- MSF_DBG("header_size = %d", header_size);
+ char header_json[header_size + 1] = {0};
- char header_json[header_size + 1] = {0};
+ memcpy(&header_json[0], &(((unsigned char *)in)[2]), header_size);
+ header_json[header_size + 1] = 0;
- memcpy(&header_json[0], &(((unsigned char *)in)[2]), header_size);
- header_json[header_size + 1] = 0;
+ MSF_DBG("in = %s", &header_json[0]);
- MSF_DBG("in = %s", &header_json[0]);
+ this_ptr->json_parse(header_json);
- this_ptr->json_parse(header_json);
- memcpy(&(this_ptr->cl_payload),
- &(((unsigned char *)in)[2 + header_size]),
- len - 2 - header_size);
+ this_ptr->cl_payload = (unsigned char*)calloc(len - 2 - header_size, sizeof(unsigned char));
+ memcpy(this_ptr->cl_payload, &(((unsigned char*)in)[2 + header_size]), len - 2 - header_size);
- this_ptr->cl_payload[len - 2 - header_size] = 0;
- this_ptr->cl_payload_size = len - 2 - header_size;
+ this_ptr->cl_payload_size = len - 2 - header_size;
+ this_ptr->connectionHandler->resetLastPingReceived();
+ this_ptr->is_header_parsed = true;
-/*
- MSF_DBG("payload = %s",
- &(this_ptr->cl_payload[0]));
-*/
- this_ptr->connectionHandler->resetLastPingReceived();
+ MSF_DBG("type:%s", this_ptr->eventType.c_str());
- if (this_ptr->eventType == CONNECT_EVENT) {
- this_ptr->handleConnectMessage(this_ptr->UID);
+ if (lws_remaining_packet_payload(wsi) == 0) {
+ MSF_DBG("there's no remaining packet");
+ if (this_ptr->eventType == CONNECT_EVENT) {
+ MSF_DBG("handle connect message");
+ this_ptr->handleConnectMessage(this_ptr->UID);
+ } else {
+ MSF_DBG("handle user message");
+ this_ptr->handleMessage(this_ptr->UID, this_ptr->cl_payload);
+ }
+
+ if (this_ptr->cl_payload) {
+ free(this_ptr->cl_payload);
+ this_ptr->cl_payload = NULL;
+ this_ptr->cl_payload_size = 0;
+ this_ptr->is_header_parsed = false;
+ this_ptr->eventType.clear();
+ }
+ }
} else {
- // this_ptr->handleMessage(this_ptr->UID);
- this_ptr->handleMessage(this_ptr->UID, this_ptr->cl_payload);
+ MSF_DBG("reallocating to copy payload");
+
+ this_ptr->cl_payload = (unsigned char*)realloc(this_ptr->cl_payload, this_ptr->cl_payload_size + len);
+ memcpy(&(this_ptr->cl_payload[this_ptr->cl_payload_size]), (char*)in, len);
+
+ this_ptr->cl_payload_size += len;
+
+ this_ptr->connectionHandler->resetLastPingReceived();
+
+ if (lws_remaining_packet_payload(wsi) == 0) {
+ MSF_DBG("there's no remaining packet");
+ if (this_ptr->eventType == CONNECT_EVENT) {
+ MSF_DBG("handle connect message");
+ this_ptr->handleConnectMessage(this_ptr->UID);
+ } else {
+ MSF_DBG("handle user message");
+ this_ptr->handleMessage(this_ptr->UID, this_ptr->cl_payload);
+ }
+
+ if (this_ptr->cl_payload) {
+ free(this_ptr->cl_payload);
+ this_ptr->cl_payload = NULL;
+ this_ptr->cl_payload_size = 0;
+ this_ptr->is_header_parsed = false;
+ this_ptr->eventType.clear();
+ }
+ }
}
} else {
- MSF_DBG("TEXT MESSAGE ARRIVED");
+ MSF_DBG("TEXT MESSAGE ARRIVED. len:%d", len);
this_ptr->json_parse((char *)in);
this_ptr->connectionHandler->resetLastPingReceived();
this_ptr->handleMessage(this_ptr->UID, NULL);
}
}
-
break;
case LWS_CALLBACK_CLIENT_WRITEABLE:
// loop until socket closed
while (n >= 0 && !was_closed) {
- if (was_closed) {
- break;
- }
-
if (wsi_mirror == NULL) {
wsi_mirror = lws_client_connect_via_info(&connect_info);
if (wsi_mirror == NULL) {