File: tcp-echo.c

package info (click to toggle)
libdex 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,136 kB
  • sloc: ansic: 13,114; asm: 226; makefile: 22; sh: 6; javascript: 5
file content (136 lines) | stat: -rw-r--r-- 4,157 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* tcp-echo.c
 *
 * Copyright 2022 Christian Hergert <chergert@redhat.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <libdex.h>

static DexScheduler *thread_pool;

static DexFuture *
socket_connection_fiber (gpointer user_data)
{
  GSocketConnection *connection = user_data;
  GOutputStream *output;
  GInputStream *input;
  GError *error = NULL;
  guint8 buffer[1024];

  g_assert (G_IS_SOCKET_CONNECTION (connection));

  input = g_io_stream_get_input_stream (G_IO_STREAM (connection));
  output = g_io_stream_get_output_stream (G_IO_STREAM (connection));

  for (;;)
    {
      gssize n_read;
      gssize to_write;
      gssize n_written;

      n_read = dex_await_int64 (dex_input_stream_read (input, buffer, sizeof buffer, G_PRIORITY_DEFAULT), &error);
      if (n_read == 0 || error != NULL)
        break;

      for (to_write = n_read; to_write > 0; to_write -= n_written)
        {
          n_written = dex_await_int64 (dex_output_stream_write (output, &buffer[n_read-to_write], to_write, G_PRIORITY_HIGH), &error);
          if (n_written == 0 || error != NULL)
            break;
        }
    }

  return error ? dex_future_new_for_error (error) : NULL;
}

static DexFuture *
socket_listener_fiber (gpointer user_data)
{
  GSocketListener *socket_listener = user_data;

  g_assert (G_IS_SOCKET_LISTENER (socket_listener));

  for (;;)
    {
      g_autoptr(GSocketConnection) connection = NULL;
      g_autoptr(DexFuture) fiber = NULL;
      g_autoptr(GError) error = NULL;

      /* Accept an incoming connection */
      if (!(connection = dex_await_object (dex_socket_listener_accept (socket_listener), &error)))
        return dex_future_new_for_error (g_steal_pointer (&error));

      /* Spawn a fiber to handle connection on thread pool */
      fiber = dex_scheduler_spawn (thread_pool, 0,
                                   socket_connection_fiber,
                                   g_steal_pointer (&connection),
                                   g_object_unref);
    }

  return NULL;
}

static DexFuture *
quit_cb (DexFuture *completed,
         gpointer   user_data)
{
  GMainLoop *main_loop = user_data;
  g_main_loop_quit (main_loop);
  return NULL;
}

int
main (int   argc,
      char *argv[])
{
  g_autoptr(GSocketListener) socket_listener = NULL;
  g_autoptr(GMainLoop) main_loop = NULL;
  g_autoptr(DexFuture) future = NULL;
  g_autoptr(GError) error = NULL;
  guint port = 8080;

  dex_init ();

  main_loop = g_main_loop_new (NULL, FALSE);
  thread_pool = dex_thread_pool_scheduler_new ();
  socket_listener = g_socket_listener_new ();

  /* Try to listen on configured port, or bail */
  if (!g_socket_listener_add_inet_port (socket_listener, port, NULL, &error))
    g_error ("Failed to listen on port %u: %s", port, error->message);

  g_print ("Listening on 0.0.0.0:%u\n", port);

  /* Spawn a fiber on current thread for socket loop */
  future = dex_scheduler_spawn (NULL, 0,
                                socket_listener_fiber,
                                g_object_ref (socket_listener),
                                g_object_unref);

  /* When it completes, call quit_cb to quit main loop */
  future = dex_future_finally (future,
                               quit_cb,
                               g_main_loop_ref (main_loop),
                               (GDestroyNotify) g_main_loop_unref);

  /* block on our main loop until quit_cb runs */
  g_main_loop_run (main_loop);

  return 0;
}