Debian Codegraph @

examples/tcp-echo.c

raw symbols SCIP
1 /* tcp-echo.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
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 /* Accept an incoming connection */
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 /* Spawn a fiber to handle connection on thread pool */
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 /* Try to listen on configured port, or bail */
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 /* Spawn a fiber on current thread for socket loop */
121 future = dex_scheduler_spawn (NULL, 0,
122 socket_listener_fiber,
123 g_object_ref (socket_listener),
124 g_object_unref);
125
126 /* When it completes, call quit_cb to quit main loop */
127 future = dex_future_finally (future,
128 quit_cb,
129 g_main_loop_ref (main_loop),
130 (GDestroyNotify) g_main_loop_unref);
131
132 /* block on our main loop until quit_cb runs */
133 g_main_loop_run (main_loop);
134
135 return 0;
136 }