Skip to content

Commit

Permalink
Merge pull request #14 from davehorton/feat/ws-pong
Browse files Browse the repository at this point in the history
Feat/ws pong
  • Loading branch information
davehorton authored Sep 4, 2024
2 parents f8ed764 + bc883fb commit 9effcc3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
46 changes: 41 additions & 5 deletions libsofia-sip-ua/tport/tport_type_ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ int tport_recv_stream_ws(tport_t *self)
return 0;
}

// DH: Check for "ping" message (CRLF)
if (N == 2 || N == 4) {
if ((data[0] == '\r' && data[1] == '\n') &&
(N == 2 || (data[2] == '\r' && data[3] == '\n'))) {
// "ping" message detected, send "pong"
tport_ws_send_crlf_text_frame(self);
return 1;
}
}

veclen = tport_recv_iovec(self, &self->tp_msg, iovec, N, 0);
if (veclen < 0)
return -1;
Expand Down Expand Up @@ -636,16 +646,42 @@ int tport_ws_ping(tport_t *self, su_time_t now)
/** Send pong */
int tport_ws_pong(tport_t *self)
{
ssize_t n;
self->tp_ping = 0;

if (tport_has_queued(self) || !self->tp_params->tpp_pong2ping)
return 0;

SU_DEBUG_7(("%s(%p): %s to " TPN_FORMAT "%s\n",
__func__, (void *)self,
"sending PONG", TPN_ARGS(self->tp_name), ""));
n = send(self->tp_socket, "\r\n", 2, 0);

SU_DEBUG_7(("%s(%p): %u bytes %s to " TPN_FORMAT "%s\n",
__func__, (void *)self, n,
"sent PONG", TPN_ARGS(self->tp_name), ""));

return n;
}

/** Send WebSocket text frame with payload CRLF */
int tport_ws_send_crlf_text_frame(tport_t *self)
{
tport_ws_t *wstp = (tport_ws_t *)self;
ssize_t n;
self->tp_ping = 0;

if (tport_has_queued(self) || !self->tp_params->tpp_pong2ping)
return 0;

return send(self->tp_socket, "\r\n", 2, 0);
// Prepare the CRLF payload
char crlf_payload[2] = { '\r', '\n' };

// Use ws_write_frame to send a text frame with the CRLF payload
n = ws_write_frame(&wstp->ws, 0x1 /* opcode for text frame */, crlf_payload, sizeof(crlf_payload));

SU_DEBUG_7(("%s(%p): %u bytes %s to " TPN_FORMAT "%s\n",
__func__, (void *)self, n,
"sent TEXT FRAME with CRLF", TPN_ARGS(self->tp_name), ""));

return n;
}

/** Calculate next timer for WS. */
Expand Down Expand Up @@ -696,4 +732,4 @@ void tport_ws_timer(tport_t *self, su_time_t now)
tport_keepalive_timer(self, now);
}
tport_base_timer(self, now);
}
}
1 change: 1 addition & 0 deletions libsofia-sip-ua/tport/tport_ws.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ ssize_t tport_send_stream_ws(tport_t const *self, msg_t *msg,

int tport_ws_ping(tport_t *self, su_time_t now);
int tport_ws_pong(tport_t *self);
int tport_ws_send_crlf_text_frame(tport_t *self);

int tport_ws_init_primary(tport_primary_t *,
tp_name_t tpn[1],
Expand Down

0 comments on commit 9effcc3

Please sign in to comment.