source: example/io/simple/server-nonblk.c @ 0640189e

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 0640189e was 0640189e, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Added example of a server using o_NONBLOCK, which does NOT work with io_uring

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2This is a simple server that users io_uring in blocking mode.
3It demonstrates the bare minimum needed to use io_uring.
4It uses liburing for simplicity.
5*/
6
7
8#include <assert.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#include <unistd.h>
14
15#include <sys/types.h>
16#include <sys/socket.h>
17#include <netinet/in.h>
18
19#include <liburing.h>
20
21extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
22
23struct io_uring ring;
24
25char data[256];
26struct iovec iov = { data, 256 };
27struct msghdr msg = { (void *)"", 0, &iov, 1, NULL, 0, 0 };
28static int async_read(int sock) {
29        int ret;
30
31        /* get an sqe and fill in a READ operation */
32      struct io_uring_sqe * sqe = io_uring_get_sqe(&ring);
33      io_uring_prep_recvmsg(sqe, sock, &msg, 0);
34      sqe->user_data = 0;
35
36      /* tell the kernel we have an sqe ready for consumption */
37      ret = io_uring_submit(&ring);
38      assert(ret == 1);
39
40        struct io_uring_cqe * cqe;
41        ret = io_uring_wait_cqe( &ring, &cqe );
42
43        if( ret < 0 ) {
44                printf( "Main Loop Error : %s\n", strerror(-ret) );
45                close( sock );
46                exit( EXIT_FAILURE );
47        }
48
49        // Problem ?
50        if(cqe->user_data != 0) {
51                printf("Unexpected user data : %llu", cqe->user_data);
52                exit( EXIT_FAILURE );
53        }
54
55        int res = cqe->res;
56        io_uring_cqe_seen( &ring, cqe );
57
58        if(res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
59                printf("io_uring returned EAGAIN\n");
60        }
61
62        return res;
63}
64
65static int do_read(int sock) {
66        int res = recvmsg(sock, &msg, 0);
67
68        if(res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
69                printf("Had EAGAIN\n");
70                return async_read(sock);
71        }
72
73        return res;
74}
75
76int main(int argc, char *argv[]) {
77        if(argc != 2) {
78            printf("usage:    %s portnumber\n", argv[0]);
79            exit( EXIT_FAILURE );
80      }
81      int port = atoi(argv[1]);
82      if(port < 1) {
83            printf("Invalid port : %d (from %s)\n", port, argv[1]);
84            exit( EXIT_FAILURE );
85      }
86
87        int sock = socket(AF_INET, SOCK_STREAM, 0);
88        if(sock < 0) {
89                perror( "socket" );
90                exit( EXIT_FAILURE );
91        }
92
93        struct sockaddr_in serv_addr;
94      memset(&serv_addr, 0, sizeof(serv_addr));
95      serv_addr.sin_family = AF_INET;
96      serv_addr.sin_addr.s_addr = INADDR_ANY;
97      serv_addr.sin_port = htons(port);
98
99        int ret = bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
100        if(ret < 0) {
101                perror( "bind" );
102                exit( EXIT_FAILURE );
103        }
104
105        listen(sock,1);
106
107        struct sockaddr_in cli_addr;
108        __socklen_t clilen = sizeof(cli_addr);
109        int newsock = accept4(sock, (struct sockaddr *) &cli_addr, &clilen, SOCK_NONBLOCK);
110        if (newsock < 0) {
111                perror( "accept" );
112                exit( EXIT_FAILURE );
113        }
114
115        io_uring_queue_init( 16, &ring, 0 );
116
117        while(1) {
118                int res = do_read(newsock);
119
120                // Did we get an error
121                if( res < 0 ) {
122                        perror( "Main Loop Error" );
123                        close( sock );
124                        exit( EXIT_FAILURE );
125                }
126
127                // If it is the end of file we are done
128                if( res == 0 ) {
129                        goto END;
130                }
131
132                // just echo the data
133                printf("'%.*s'\n", res - 1, data);
134        }
135END:
136
137        io_uring_queue_exit( &ring );
138
139        ret = close(newsock);
140      if(ret < 0) {
141            perror( "close new" );
142            exit( EXIT_FAILURE );
143      }
144
145        ret = close(sock);
146      if(ret < 0) {
147            perror( "close old" );
148            exit( EXIT_FAILURE );
149      }
150
151        return 0;
152}
Note: See TracBrowser for help on using the repository browser.