1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/syscall/usermode.h"
4
5#include <libipc/ipc.h>
6#include <pthread.h>
7#include <stdio.h>
8#include <stdlib.h>
9
10void *ipc_do_echo(void *arg)
11{
12 fd_t client_fd = (fd_t) (ptr_t) arg;
13 while (true)
14 {
15 ipc_msg_t *msg = ipc_read_msg(fd: client_fd);
16
17 if (msg == NULL)
18 return NULL; // EOF
19
20 if (!ipc_write_msg(fd: client_fd, buffer: msg))
21 {
22 puts(string: "Failed to send IPC message");
23 return NULL;
24 }
25
26 ipc_msg_destroy(buffer: msg);
27 }
28}
29
30int main()
31{
32 fd_t server_fd = syscall_ipc_create(name: "echo-server", max_pending_connections: 30);
33 if (IS_ERR_VALUE(server_fd))
34 {
35 puts(string: "Failed to create IPC server");
36 return 1;
37 }
38
39 while (true)
40 {
41 const fd_t client_fd = syscall_ipc_accept(fd: server_fd);
42
43 if (client_fd == 0)
44 break; // closed
45
46 if (IS_ERR_VALUE(client_fd))
47 {
48 puts(string: "Failed to accept IPC client");
49 return 1;
50 }
51
52 pthread_t thread = { 0 };
53 pthread_create(&thread, NULL, ipc_do_echo, (void *) (ptr_t) client_fd);
54 }
55
56 return 0;
57}
58