aboutsummaryrefslogtreecommitdiffstats
path: root/thirdparty/apps/testapp/cps/cps_s.c
blob: 57d41c728c89f7d49769be4f02e768cc4e80cb38 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "lb.h"
#include "cps.h"

inline static void
cps_s_close (struct cps_thread *thread)
{
  int fd;
  struct epoll_event ev = { 0 };

  for (fd = thread->server; fd >= 0; fd = CPS_CONN (fd)->next)
    {
      (void) _epoll_ctl (thread->epfd, EPOLL_CTL_DEL, fd, &ev);
      _close (fd);
    }

  thread->server = -1;
}

int
cps_s_listen (struct cps_thread *thread)
{
  int i;

  for (i = 0; i < thread->server_num; ++i)
    {
      int fd, ret;
      struct timespec dummy;
      struct epoll_event event;
      struct sockaddr_in *server = &thread->s_addr[i];

      fd = _socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
      ERR_RETURN (fd < 0, -1, "socket()=%d:%d\n", fd, errno);

      ret = _bind (fd, (struct sockaddr *) server, sizeof (*server));
      ERR_RETURN (ret, -1, "bind(%d)=%d:%d\n", fd, ret, errno);

      ret = set_nonblock (fd);
      ERR_RETURN (ret, -1, "set_nonblock(%d)=%d:%d\n", fd, ret, errno);

      ret = _listen (fd, SOMAXCONN);
      ERR_RETURN (ret, -1, "listen(%d)=%d:%d\n", fd, ret, errno);

      event.events = EPOLLIN | EPOLLET;
      event.data.u64 = CPS_EV_DATA (i, fd);
      ret = _epoll_ctl (thread->epfd, EPOLL_CTL_ADD, fd, &event);
      ERR_RETURN (ret, -1, "epoll_ctl(%d, %d)=%d:%d\n", thread->epfd, fd, ret,
                  errno);

      out ("[%d.%d:%d] listen on %s\n", thread->index, i, fd,
           f_inaddr (server));

      cps_add_server (thread, fd, i);
    }

  return 0;
}

int
cps_s_accept (struct cps_thread *thread, int server_fd)
{
  while (cps.run_state == CPS_RUNNING)
    {
      int fd, ret;
      struct timespec begin;
      struct epoll_event event;
#if defined(DEBUG)
      struct sockaddr_in addr;
      socklen_t len = sizeof (addr);
#endif

      LB_TIME (begin);

#if defined(DEBUG) && 0
      fd =
        _accept4 (server_fd, (struct sockaddr *) &addr, &len, SOCK_NONBLOCK);
#else
      fd = _accept4 (server_fd, NULL, NULL, SOCK_NONBLOCK);
#endif
      if (fd < 0)
        {
          int e = errno;
          if (e == EAGAIN || e == EWOULDBLOCK)
            return 0;
          DBG ("->accept4(%d)=%d:%d\n", server_fd, fd, e);
          CPS_CNT_INC_E (thread, CPS_CNT_ACCEPT_ERR, errno);
          return -1;
        }

      CPS_REC_INC (thread, CPS_REC_INIT);
//                DBG("(%d, %d) -> accepted(%d) %d: %s", thread->index, sid, server->fd, fd, f_inaddr(&addr));

      if (fd >= CPS_MAX_FD)
        {
          _close (fd);
          CPS_REC_INC (thread, CPS_REC_FAIL);
          continue;
        }

      ret = set_nodelay (fd, 1);
      if (ret)
        CPS_CNT_INC_E (thread, CPS_CNT_NODELAY_ERR, errno);

      event.events = EPOLLIN | EPOLLET;
      event.data.u64 = CPS_EV_DATA (CPS_CONN_SID, fd);
      ret = _epoll_ctl (thread->epfd, EPOLL_CTL_ADD, fd, &event);
      if (ret)
        {
          _close (fd);
          CPS_CNT_INC_E (thread, CPS_CNT_EPOLL_ERR, errno);
          CPS_REC_INC (thread, CPS_REC_FAIL);
          DBG ("epoll_ctl(%d, %d)=%d:%d\n", thread->epfd, fd, ret, errno);
          continue;
        }

      cps_add_conn (thread, fd, -cps.req_len, &begin);
    }

  return 0;
}

int
cps_s_io (struct cps_thread *thread, int fd, uint32_t events)
{
  static char buf[CPS_DATA_MAX];

  int ret;
  struct cps_conn *conn = CPS_CONN (fd);

  DBG ("(%d, %d, %x) conn:{size:%d next:%d prev:%p:%d}\n",
       thread->index, fd, events, conn->size, conn->next, conn->prev,
       *conn->prev);

  if (events & EPOLLERR)
    {
      CPS_CNT_INC (thread, CPS_CNT_ERR_EVENT);
      DBG ("(%d, %d, 0x%x)\n", thread->index, fd, events);
      goto ERR;
    }

  if (0 == (events & EPOLLIN))
    return 0;

  while (1)
    {
      if (cps.run_state <= CPS_INIT)
        goto ERR;

      ret = _recv (fd, buf, sizeof (buf), 0);
      if (ret > 0)
        {
          conn->size += ret;
          if (conn->size >= 0)
            {
              CPS_REC_TIMED_INC (thread, CPS_REC_RECV, conn->last);
              break;            /* receive success */
            }
        }
      else
        {
          if (ret < 0)
            {
              const int e = errno;
              if (e == EWOULDBLOCK || e == EAGAIN)
                return 0;
              if (e == EINTR)
                continue;
              CPS_CNT_INC_E (thread, CPS_CNT_RECV_ERR, e);
            }
          else
            {
              CPS_CNT_INC (thread, CPS_CNT_RECV_ERR);
            }
          DBG ("->recv(%d,, %ld)=%d:%d\n", fd, sizeof (buf), ret, errno);
          goto ERR;
        }
    }

  conn->size = 0;

  while (cps.run_state > CPS_INIT)
    {
      ret = _send (fd, buf, cps.res_len - conn->size, 0);
      if (ret > 0)
        {
          conn->size += ret;
          if (conn->size >= cps.res_len)
            {
              _close (fd);
              CPS_REC_TIMED_INC (thread, CPS_REC_SEND, conn->last);
              cps_rem_conn (thread, fd, conn);
              return 0;
            }
        }
      else
        {
          if (ret < 0)
            {
              const int e = errno;
              if (e == EWOULDBLOCK || e == EAGAIN || e == EINTR)
                continue;
              CPS_CNT_INC_E (thread, CPS_CNT_SEND_ERR, e);
            }
          else
            {
              CPS_CNT_INC (thread, CPS_CNT_SEND_ERR);
            }
          DBG ("->send(%d,, %d)=%d:%d\n", fd, cps.res_len - conn->size, ret,
               errno);
          goto ERR;
        }
    }

  return 0;

ERR:
  _close (fd);
  cps_rem_conn (thread, fd, conn);
  CPS_REC_INC (thread, CPS_REC_FAIL);
  return -1;
}

void *
cps_s_thread (void *arg)
{
  int num = 0;
  struct cps_thread *thread = (struct cps_thread *) arg;
  struct epoll_event *event = thread->event;

  out ("[%d] initialize thread %ld server:%d core:%d epfd:%d\n",
       thread->index, pthread_self (), thread->server_num, thread->core,
       thread->epfd);

  if (cps_s_listen (thread))
    {
      cps.run_state = CPS_ERROR;
      return NULL;
    }

  futex_wait (&cps.run_state, CPS_INIT);

  while (1)
    {

      if (num > 0)
        {
          int sid = CPS_EV_SID (event->data.u64);
          int fd = CPS_EV_FD (event->data.u64);
          DBG ("epoll evnet{sid:%d fd:%d event:%x}\n", sid, fd,
               event->events);
          if (sid >= 0)
            {
              if (event->events & EPOLLIN)
                {
                  (void) cps_s_accept (thread, fd);
                }
              if (event->events & EPOLLERR)
                {
                  wrn ("Error event for server %d\n", fd);
                }
            }
          else
            {
              if (fd >= CPS_MAX_FD)
                {
                  err ("Error connection index %d\n", fd);
                }
              else
                {
                  (void) cps_s_io (thread, fd, event->events);
                }
            }

          num--;
          event++;
        }

      if (num <= 0)
        {
          if (cps.run_state == CPS_CLOSING)
            {
              if (thread->server >= 0)
                cps_s_close (thread);
              if (thread->conn_num <= 0)
                break;
            }
          else if (cps.run_state <= CPS_INIT)
            {
              break;
            }

          event = thread->event;
          num = _epoll_wait (thread->epfd, event, cps.evnum, CPS_EPWAIT_MS);
          if (num < 0)
            {
              int e = errno;
              if (e != EINTR && e != ETIMEDOUT)
                CPS_CNT_INC_E (thread, CPS_CNT_EPOLL_ERR, e);
            }
        }
    }

  __sync_fetch_and_sub (&cps.active_thread, 1);
  return NULL;
}