| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#include "cat-util.h" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
static DexFuture * |
| 37 |
cat_read_fiber (gpointer user_data) |
| 38 |
{ |
| 39 |
Cat *cat = user_data; |
| 40 |
Buffer *buffer = NULL; |
| 41 |
|
| 42 |
for (;;) |
| 43 |
{ |
| 44 |
Buffer *next; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
if (buffer != NULL) |
| 50 |
dex_await (dex_channel_send (cat->channel, |
| 51 |
dex_future_new_for_pointer (g_steal_pointer (&buffer))), |
| 52 |
NULL); |
| 53 |
|
| 54 |
|
| 55 |
next = cat_pop_buffer (cat); |
| 56 |
|
| 57 |
|
| 58 |
next->length = dex_await_int64 (dex_aio_read (NULL, |
| 59 |
cat->read_fd, |
| 60 |
next->data, |
| 61 |
next->capacity, |
| 62 |
-1), |
| 63 |
NULL); |
| 64 |
|
| 65 |
|
| 66 |
if (next->length <= 0) |
| 67 |
{ |
| 68 |
dex_channel_close_send (cat->channel); |
| 69 |
cat_push_buffer (cat, next); |
| 70 |
break; |
| 71 |
} |
| 72 |
|
| 73 |
buffer = next; |
| 74 |
} |
| 75 |
|
| 76 |
return dex_future_new_for_boolean (TRUE); |
| 77 |
} |
| 78 |
|
| 79 |
static DexFuture * |
| 80 |
cat_write_fiber (gpointer user_data) |
| 81 |
{ |
| 82 |
Cat *cat = user_data; |
| 83 |
|
| 84 |
for (;;) |
| 85 |
{ |
| 86 |
Buffer *buffer; |
| 87 |
gssize len; |
| 88 |
|
| 89 |
|
| 90 |
if (!(buffer = dex_await_pointer (dex_channel_receive (cat->channel), NULL))) |
| 91 |
break; |
| 92 |
|
| 93 |
|
| 94 |
len = dex_await_int64 (dex_aio_write (NULL, |
| 95 |
cat->write_fd, |
| 96 |
buffer->data, |
| 97 |
buffer->length, |
| 98 |
-1), |
| 99 |
NULL); |
| 100 |
|
| 101 |
|
| 102 |
cat_push_buffer (cat, buffer); |
| 103 |
|
| 104 |
|
| 105 |
if (len <= 0) |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
return dex_future_new_for_boolean (TRUE); |
| 110 |
} |
| 111 |
|
| 112 |
int |
| 113 |
main (int argc, |
| 114 |
char *argv[]) |
| 115 |
{ |
| 116 |
GError *error = NULL; |
| 117 |
Cat cat; |
| 118 |
int ret = EXIT_SUCCESS; |
| 119 |
|
| 120 |
dex_init (); |
| 121 |
|
| 122 |
if (!cat_init (&cat, &argc, &argv, &error) || |
| 123 |
!cat_run (&cat, |
| 124 |
dex_scheduler_spawn (NULL, 0, cat_read_fiber, &cat, NULL), |
| 125 |
dex_scheduler_spawn (NULL, 0, cat_write_fiber, &cat, NULL), |
| 126 |
&error)) |
| 127 |
{ |
| 128 |
g_printerr ("cat: %s\n", error->message); |
| 129 |
g_clear_error (&error); |
| 130 |
ret = EXIT_FAILURE; |
| 131 |
} |
| 132 |
|
| 133 |
cat_clear (&cat); |
| 134 |
|
| 135 |
return ret; |
| 136 |
} |