1
0
mirror of https://github.com/payden/libwsclient synced 2024-10-27 17:54:01 +00:00

Add Ping frame handling

This commit is contained in:
Emil M George 2018-05-31 11:58:25 +05:30
parent d416fcb62c
commit 2266742c33

View File

@ -173,6 +173,36 @@ void libwsclient_handle_control_frame(wsclient *c, wsclient_frame *ctl_frame) {
} }
c->flags |= CLIENT_SHOULD_CLOSE; c->flags |= CLIENT_SHOULD_CLOSE;
break; break;
case 0x9:
// Received ping frame
if((c->flags & CLIENT_SHOULD_CLOSE) == 0) {
// Server sent ping. Send pong frame as acknowledgement.
for(i=0;i<ctl_frame->payload_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: default:
fprintf(stderr, "Unhandled control frame received. Opcode: %d\n", ctl_frame->opcode); fprintf(stderr, "Unhandled control frame received. Opcode: %d\n", ctl_frame->opcode);
break; break;