From 2266742c3315c40fee07a6d12be3c39190cd5f1c Mon Sep 17 00:00:00 2001 From: Emil M George Date: Thu, 31 May 2018 11:58:25 +0530 Subject: [PATCH] Add Ping frame handling --- wsclient.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/wsclient.c b/wsclient.c index 4847a9f..3d73273 100644 --- a/wsclient.c +++ b/wsclient.c @@ -173,6 +173,36 @@ void libwsclient_handle_control_frame(wsclient *c, wsclient_frame *ctl_frame) { } c->flags |= CLIENT_SHOULD_CLOSE; break; + case 0x9: + // Received ping frame + if((c->flags & CLIENT_SHOULD_CLOSE) == 0) { + // Server sent ping. Send pong frame as acknowledgement. + for(i=0;ipayload_len;i++) + *(ctl_frame->rawdata + ctl_frame->payload_offset + i) ^= (mask[i % 4] & 0xff); //mask payload + *(ctl_frame->rawdata + 1) |= 0x80; //turn mask bit on + i = 0; + // change opcode to 0xA (Pong Frame) + *(ctl_frame->rawdata) = (*(ctl_frame->rawdata) & 0xf0) | 0xA; + pthread_mutex_lock(&c->send_lock); + while(i < ctl_frame->payload_offset + ctl_frame->payload_len && n >= 0) { + n = _libwsclient_write(c, ctl_frame->rawdata + i, ctl_frame->payload_offset + ctl_frame->payload_len - i); + i += n; + } + pthread_mutex_unlock(&c->send_lock); + if(n < 0) { + if(c->onerror) { + err = libwsclient_new_error(WS_HANDLE_CTL_FRAME_SEND_ERR); + err->extra_code = n; + c->onerror(c, err); + free(err); + err = NULL; + } + } + } + break; + case 0xA: + // Received pong frame. Nothing to do. + break; default: fprintf(stderr, "Unhandled control frame received. Opcode: %d\n", ctl_frame->opcode); break;