Add mg_ws_printf()

This commit is contained in:
Sergey Lyubka 2022-06-05 14:59:50 +01:00
parent 219428c249
commit cac7f653c9
4 changed files with 40 additions and 0 deletions

View File

@ -5702,6 +5702,24 @@ struct ws_msg {
size_t data_len; size_t data_len;
}; };
size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt,
va_list ap) {
char mem[256], *buf = mem;
size_t len = mg_vasprintf(&buf, sizeof(mem), fmt, ap);
len = mg_ws_send(c, buf, len, op);
if (buf != mem) free(buf);
return len;
}
size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...) {
size_t len = 0;
va_list ap;
va_start(ap, fmt);
len = mg_ws_vprintf(c, op, fmt, ap);
va_end(ap);
return len;
}
static void ws_handshake(struct mg_connection *c, const struct mg_str *wskey, static void ws_handshake(struct mg_connection *c, const struct mg_str *wskey,
const struct mg_str *wsproto, const char *fmt, const struct mg_str *wsproto, const char *fmt,
va_list ap) { va_list ap) {

View File

@ -1178,6 +1178,8 @@ void mg_ws_upgrade(struct mg_connection *, struct mg_http_message *,
const char *fmt, ...); const char *fmt, ...);
size_t mg_ws_send(struct mg_connection *, const char *buf, size_t len, int op); size_t mg_ws_send(struct mg_connection *, const char *buf, size_t len, int op);
size_t mg_ws_wrap(struct mg_connection *, size_t len, int op); size_t mg_ws_wrap(struct mg_connection *, size_t len, int op);
size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...);
size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt, va_list);

View File

@ -13,6 +13,24 @@ struct ws_msg {
size_t data_len; size_t data_len;
}; };
size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt,
va_list ap) {
char mem[256], *buf = mem;
size_t len = mg_vasprintf(&buf, sizeof(mem), fmt, ap);
len = mg_ws_send(c, buf, len, op);
if (buf != mem) free(buf);
return len;
}
size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...) {
size_t len = 0;
va_list ap;
va_start(ap, fmt);
len = mg_ws_vprintf(c, op, fmt, ap);
va_end(ap);
return len;
}
static void ws_handshake(struct mg_connection *c, const struct mg_str *wskey, static void ws_handshake(struct mg_connection *c, const struct mg_str *wskey,
const struct mg_str *wsproto, const char *fmt, const struct mg_str *wsproto, const char *fmt,
va_list ap) { va_list ap) {

View File

@ -21,3 +21,5 @@ void mg_ws_upgrade(struct mg_connection *, struct mg_http_message *,
const char *fmt, ...); const char *fmt, ...);
size_t mg_ws_send(struct mg_connection *, const char *buf, size_t len, int op); size_t mg_ws_send(struct mg_connection *, const char *buf, size_t len, int op);
size_t mg_ws_wrap(struct mg_connection *, size_t len, int op); size_t mg_ws_wrap(struct mg_connection *, size_t len, int op);
size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...);
size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt, va_list);