Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ws pong #14

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading