aboutsummaryrefslogtreecommitdiff
path: root/src/examples/suspend_resume_epoll.c
blob: d25c3b21194634f43cb434ff74ceb27c9ee5d23d (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
     This file is part of libmicrohttpd
     Copyright (C) 2018 Christian Grothoff (and other contributing authors)

     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 Lesser General Public
     License along with this library; if not, write to the Free Software
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
/**
 * @file suspend_resume_epoll.c
 * @brief example for how to use libmicrohttpd with epoll() and
 *        resume a suspended connection
 * @author Robert D Kocisko
 * @author Christian Grothoff
 */
#include "platform.h"
#include <microhttpd.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <limits.h>

#define TIMEOUT_INFINITE -1

struct Request
{
  struct MHD_Connection *connection;
  int timerfd;
};


static int epfd;

static struct epoll_event evt;


static enum MHD_Result
ahc_echo (void *cls,
          struct MHD_Connection *connection,
          const char *url,
          const char *method,
          const char *version,
          const char *upload_data, size_t *upload_data_size, void **ptr)
{
  struct MHD_Response *response;
  enum MHD_Result ret;
  struct Request *req;
  struct itimerspec ts;

  (void) cls;
  (void) url;               /* Unused. Silence compiler warning. */
  (void) method;
  (void) version;           /* Unused. Silence compiler warning. */
  (void) upload_data;       /* Unused. Silence compiler warning. */
  (void) upload_data_size;  /* Unused. Silence compiler warning. */
  req = *ptr;
  if (NULL == req)
  {

    req = malloc (sizeof(struct Request));
    if (NULL == req)
      return MHD_NO;
    req->connection = connection;
    req->timerfd = -1;
    *ptr = req;
    return MHD_YES;
  }

  if (-1 != req->timerfd)
  {
    /* send response (echo request url) */
    response = MHD_create_response_from_buffer (strlen (url),
                                                (void *) url,
                                                MHD_RESPMEM_MUST_COPY);
    if (NULL == response)
      return MHD_NO;
    ret = MHD_queue_response (connection,
                              MHD_HTTP_OK,
                              response);
    MHD_destroy_response (response);
    return ret;
  }
  /* create timer and suspend connection */
  req->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK);
  if (-1 == req->timerfd)
  {
    printf ("timerfd_create: %s", strerror (errno));
    return MHD_NO;
  }
  evt.events = EPOLLIN;
  evt.data.ptr = req;
  if (-1 == epoll_ctl (epfd, EPOLL_CTL_ADD, req->timerfd, &evt))
  {
    printf ("epoll_ctl: %s", strerror (errno));
    return MHD_NO;
  }
  ts.it_value.tv_sec = 1;
  ts.it_value.tv_nsec = 0;
  ts.it_interval.tv_sec = 0;
  ts.it_interval.tv_nsec = 0;
  if (-1 == timerfd_settime (req->timerfd, 0, &ts, NULL))
  {
    printf ("timerfd_settime: %s", strerror (errno));
    return MHD_NO;
  }
  MHD_suspend_connection (connection);
  return MHD_YES;
}


static void
connection_done (void *cls,
                 struct MHD_Connection *connection,
                 void **con_cls,
                 enum MHD_RequestTerminationCode toe)
{
  struct Request *req = *con_cls;

  (void) cls;
  (void) connection;
  (void) toe;
  if (-1 != req->timerfd)
    if (0 != close (req->timerfd))
      abort ();
  free (req);
}


int
main (int argc,
      char *const *argv)
{
  struct MHD_Daemon *d;
  const union MHD_DaemonInfo *info;
  int current_event_count;
  struct epoll_event events_list[1];
  struct Request *req;
  uint64_t timer_expirations;

  if (argc != 2)
  {
    printf ("%s PORT\n", argv[0]);
    return 1;
  }
  d = MHD_start_daemon (MHD_USE_EPOLL | MHD_ALLOW_SUSPEND_RESUME,
                        atoi (argv[1]),
                        NULL, NULL, &ahc_echo, NULL,
                        MHD_OPTION_NOTIFY_COMPLETED, &connection_done, NULL,
                        MHD_OPTION_END);
  if (d == NULL)
    return 1;

  info = MHD_get_daemon_info (d, MHD_DAEMON_INFO_EPOLL_FD);
  if (info == NULL)
    return 1;

  epfd = epoll_create1 (EPOLL_CLOEXEC);
  if (-1 == epfd)
    return 1;

  evt.events = EPOLLIN;
  evt.data.ptr = NULL;
  if (-1 == epoll_ctl (epfd, EPOLL_CTL_ADD, info->epoll_fd, &evt))
    return 1;

  while (1)
  {
    int timeout;
    MHD_UNSIGNED_LONG_LONG to;

    if (MHD_YES !=
        MHD_get_timeout (d,
                         &to))
      timeout = TIMEOUT_INFINITE;
    else
      timeout = (to < INT_MAX - 1) ? (int) to : (INT_MAX - 1);
    current_event_count = epoll_wait (epfd, events_list, 1, timeout);

    if (1 == current_event_count)
    {
      if (events_list[0].data.ptr)
      {
        /*  A timer has timed out */
        req = events_list[0].data.ptr;
        /* read from the fd so the system knows we heard the notice */
        if (-1 == read (req->timerfd, &timer_expirations,
                        sizeof(timer_expirations)))
        {
          return 1;
        }
        /*  Now resume the connection */
        MHD_resume_connection (req->connection);
      }
    }
    else if (0 == current_event_count)
    {
      /* no events: continue */
    }
    else
    {
      /* error */
      return 1;
    }
    if (! MHD_run (d))
      return 1;
  }

  return 0;
}