| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "websocket.h" |
| 9 |
|
| 10 |
static WebSocketConnection *web_socket = NULL; |
| 11 |
static GString *buffer = NULL; |
| 12 |
static GMainLoop *loop = NULL; |
| 13 |
|
| 14 |
static void |
| 15 |
on_release_buffer (gpointer user_data) |
| 16 |
{ |
| 17 |
if (buffer == NULL) |
| 18 |
buffer = user_data; |
| 19 |
else |
| 20 |
g_string_free (user_data, TRUE); |
| 21 |
} |
| 22 |
|
| 23 |
static gboolean |
| 24 |
on_input_data (GIOChannel *channel, |
| 25 |
GIOCondition cond, |
| 26 |
gpointer user_data) |
| 27 |
{ |
| 28 |
GError *error = NULL; |
| 29 |
GIOStatus status; |
| 30 |
GBytes *msg; |
| 31 |
gsize line; |
| 32 |
|
| 33 |
if (buffer == NULL) |
| 34 |
buffer = g_string_sized_new (1024); |
| 35 |
status = g_io_channel_read_line_string (channel, buffer, &line, &error); |
| 36 |
switch (status) |
| 37 |
{ |
| 38 |
case G_IO_STATUS_ERROR: |
| 39 |
g_critical ("Failed to read input: %s", error->message); |
| 40 |
g_error_free (error); |
| 41 |
g_main_loop_quit (loop); |
| 42 |
return FALSE; |
| 43 |
case G_IO_STATUS_EOF: |
| 44 |
web_socket_connection_close (web_socket, WEB_SOCKET_CLOSE_GOING_AWAY, "going away"); |
| 45 |
return FALSE; |
| 46 |
case G_IO_STATUS_AGAIN: |
| 47 |
return TRUE; |
| 48 |
case G_IO_STATUS_NORMAL: |
| 49 |
msg = g_bytes_new_with_free_func (buffer->str, line, on_release_buffer, buffer); |
| 50 |
buffer = NULL; |
| 51 |
web_socket_connection_send (web_socket, WEB_SOCKET_DATA_TEXT, NULL, msg); |
| 52 |
g_bytes_unref (msg); |
| 53 |
return TRUE; |
| 54 |
default: |
| 55 |
g_assert_not_reached (); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
on_web_socket_open (WebSocketConnection *ws, |
| 61 |
gpointer unused) |
| 62 |
{ |
| 63 |
GIOChannel *channel; |
| 64 |
|
| 65 |
g_printerr ("WebSocket: opened %s with %s\n", |
| 66 |
web_socket_connection_get_protocol (ws), |
| 67 |
web_socket_connection_get_url (ws)); |
| 68 |
|
| 69 |
channel = g_io_channel_unix_new (0); |
| 70 |
g_io_add_watch (channel, G_IO_IN, on_input_data, NULL); |
| 71 |
g_io_channel_unref (channel); |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
on_web_socket_message (WebSocketConnection *ws, |
| 76 |
WebSocketDataType type, |
| 77 |
GBytes *message) |
| 78 |
{ |
| 79 |
const gchar *data; |
| 80 |
gsize len; |
| 81 |
|
| 82 |
g_printerr ("WebSocket: message 0x%x\n", (int)type); |
| 83 |
|
| 84 |
data = g_bytes_get_data (message, &len); |
| 85 |
g_print ("%.*s\n", (int)len, data); |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
on_web_socket_close (WebSocketConnection *ws) |
| 90 |
{ |
| 91 |
gushort code; |
| 92 |
|
| 93 |
code = web_socket_connection_get_close_code (ws); |
| 94 |
if (code != 0) |
| 95 |
g_printerr ("WebSocket: close: %d %s\n", code, |
| 96 |
web_socket_connection_get_close_data (ws)); |
| 97 |
else |
| 98 |
g_printerr ("WebSocket: close\n"); |
| 99 |
|
| 100 |
g_main_loop_quit (loop); |
| 101 |
} |
| 102 |
|
| 103 |
int |
| 104 |
main (int argc, |
| 105 |
char *argv[]) |
| 106 |
{ |
| 107 |
GOptionContext *options; |
| 108 |
gchar **protocols = NULL; |
| 109 |
gchar *origin = NULL; |
| 110 |
GError *error = NULL; |
| 111 |
|
| 112 |
GOptionEntry entries[] = { |
| 113 |
{ "origin", 0, 0, G_OPTION_ARG_STRING, &origin, "Web Socket Origin", "url" }, |
| 114 |
{ "protocol", 0, 0, G_OPTION_ARG_STRING_ARRAY, &protocols, "Web Socket Protocols", "proto" }, |
| 115 |
{ NULL } |
| 116 |
}; |
| 117 |
|
| 118 |
signal (SIGPIPE, SIG_IGN); |
| 119 |
options = g_option_context_new ("URL"); |
| 120 |
g_option_context_add_main_entries (options, entries, NULL); |
| 121 |
if (!g_option_context_parse (options, &argc, &argv, &error)) |
| 122 |
{ |
| 123 |
g_printerr ("frob-websocket: %s\n", error->message); |
| 124 |
return 2; |
| 125 |
} |
| 126 |
|
| 127 |
if (argc != 2) |
| 128 |
{ |
| 129 |
g_printerr ("frob-websocket: specify the url to connect to\n"); |
| 130 |
return 2; |
| 131 |
} |
| 132 |
|
| 133 |
loop = g_main_loop_new (NULL, FALSE); |
| 134 |
|
| 135 |
web_socket = web_socket_client_new (argv[1], origin, (const gchar **)protocols); |
| 136 |
g_signal_connect (web_socket, "open", G_CALLBACK (on_web_socket_open), NULL); |
| 137 |
g_signal_connect (web_socket, "message", G_CALLBACK (on_web_socket_message), NULL); |
| 138 |
g_signal_connect (web_socket, "close", G_CALLBACK (on_web_socket_close), NULL); |
| 139 |
|
| 140 |
g_main_loop_run (loop); |
| 141 |
|
| 142 |
g_option_context_free (options); |
| 143 |
g_object_unref (web_socket); |
| 144 |
g_free (origin); |
| 145 |
if (buffer) |
| 146 |
g_string_free (buffer, TRUE); |
| 147 |
g_strfreev (protocols); |
| 148 |
|
| 149 |
return 0; |
| 150 |
} |