| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <libdex.h> |
| 24 |
|
| 25 |
static DexScheduler *thread_pool; |
| 26 |
|
| 27 |
static DexFuture * |
| 28 |
socket_connection_fiber (gpointer user_data) |
| 29 |
{ |
| 30 |
GSocketConnection *connection = user_data; |
| 31 |
GOutputStream *output; |
| 32 |
GInputStream *input; |
| 33 |
GError *error = NULL; |
| 34 |
guint8 buffer[1024]; |
| 35 |
|
| 36 |
g_assert (G_IS_SOCKET_CONNECTION (connection)); |
| 37 |
|
| 38 |
input = g_io_stream_get_input_stream (G_IO_STREAM (connection)); |
| 39 |
output = g_io_stream_get_output_stream (G_IO_STREAM (connection)); |
| 40 |
|
| 41 |
for (;;) |
| 42 |
{ |
| 43 |
gssize n_read; |
| 44 |
gssize to_write; |
| 45 |
gssize n_written; |
| 46 |
|
| 47 |
n_read = dex_await_int64 (dex_input_stream_read (input, buffer, sizeof buffer, G_PRIORITY_DEFAULT), &error); |
| 48 |
if (n_read == 0 || error != NULL) |
| 49 |
break; |
| 50 |
|
| 51 |
for (to_write = n_read; to_write > 0; to_write -= n_written) |
| 52 |
{ |
| 53 |
n_written = dex_await_int64 (dex_output_stream_write (output, &buffer[n_read-to_write], to_write, G_PRIORITY_HIGH), &error); |
| 54 |
if (n_written == 0 || error != NULL) |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return error ? dex_future_new_for_error (error) : NULL; |
| 60 |
} |
| 61 |
|
| 62 |
static DexFuture * |
| 63 |
socket_listener_fiber (gpointer user_data) |
| 64 |
{ |
| 65 |
GSocketListener *socket_listener = user_data; |
| 66 |
|
| 67 |
g_assert (G_IS_SOCKET_LISTENER (socket_listener)); |
| 68 |
|
| 69 |
for (;;) |
| 70 |
{ |
| 71 |
g_autoptr(GSocketConnection) connection = NULL; |
| 72 |
g_autoptr(DexFuture) fiber = NULL; |
| 73 |
g_autoptr(GError) error = NULL; |
| 74 |
|
| 75 |
|
| 76 |
if (!(connection = dex_await_object (dex_socket_listener_accept (socket_listener), &error))) |
| 77 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 78 |
|
| 79 |
|
| 80 |
fiber = dex_scheduler_spawn (thread_pool, 0, |
| 81 |
socket_connection_fiber, |
| 82 |
g_steal_pointer (&connection), |
| 83 |
g_object_unref); |
| 84 |
} |
| 85 |
|
| 86 |
return NULL; |
| 87 |
} |
| 88 |
|
| 89 |
static DexFuture * |
| 90 |
quit_cb (DexFuture *completed, |
| 91 |
gpointer user_data) |
| 92 |
{ |
| 93 |
GMainLoop *main_loop = user_data; |
| 94 |
g_main_loop_quit (main_loop); |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
int |
| 99 |
main (int argc, |
| 100 |
char *argv[]) |
| 101 |
{ |
| 102 |
g_autoptr(GSocketListener) socket_listener = NULL; |
| 103 |
g_autoptr(GMainLoop) main_loop = NULL; |
| 104 |
g_autoptr(DexFuture) future = NULL; |
| 105 |
g_autoptr(GError) error = NULL; |
| 106 |
guint port = 8080; |
| 107 |
|
| 108 |
dex_init (); |
| 109 |
|
| 110 |
main_loop = g_main_loop_new (NULL, FALSE); |
| 111 |
thread_pool = dex_thread_pool_scheduler_new (); |
| 112 |
socket_listener = g_socket_listener_new (); |
| 113 |
|
| 114 |
|
| 115 |
if (!g_socket_listener_add_inet_port (socket_listener, port, NULL, &error)) |
| 116 |
g_error ("Failed to listen on port %u: %s", port, error->message); |
| 117 |
|
| 118 |
g_print ("Listening on 0.0.0.0:%u\n", port); |
| 119 |
|
| 120 |
|
| 121 |
future = dex_scheduler_spawn (NULL, 0, |
| 122 |
socket_listener_fiber, |
| 123 |
g_object_ref (socket_listener), |
| 124 |
g_object_unref); |
| 125 |
|
| 126 |
|
| 127 |
future = dex_future_finally (future, |
| 128 |
quit_cb, |
| 129 |
g_main_loop_ref (main_loop), |
| 130 |
(GDestroyNotify) g_main_loop_unref); |
| 131 |
|
| 132 |
|
| 133 |
g_main_loop_run (main_loop); |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |