taskrambler  0.1.9
Web server and task management solution.
run.c File Reference
#include "server.h"
#include "logger.h"
#include "utils/signalHandling.h"
+ Include dependency graph for run.c:

Go to the source code of this file.

Functions

int serverPoll (Server)
 
int serverHandleAccept (Server, unsigned int)
 
ssize_t serverRead (Server, unsigned int)
 
ssize_t serverWrite (Server, unsigned int)
 
void serverCloseConn (Server, unsigned int)
 
void serverRun (Server this)
 

Detailed Description

Author
Georg Hopp

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file run.c.

Function Documentation

void serverCloseConn ( Server  ,
unsigned  int 
)

Definition at line 31 of file close_conn.c.

Referenced by serverRun().

32 {
33  int fd = (this->fds)[i].fd;
34  Stream st = (this->conns[fd]).stream;
35 
36  delete((this->conns)[fd].sock);
37  delete((this->conns)[fd].worker);
38 
39  if (NULL != st && STREAM_SSL == st->type) {
40  SSL_shutdown((st->handle).ssl);
41  SSL_free((st->handle).ssl);
42  (st->handle).ssl = NULL;
43  }
44 
45  delete(st);
46 
47  memset(&(this->fds[i]), 0, sizeof(struct pollfd));
48 }
Definition: server.h:39

+ Here is the caller graph for this function:

int serverHandleAccept ( Server  ,
unsigned  int 
)

Definition at line 36 of file handle_accept.c.

References clone, logger, LOGGER_DEBUG, loggerLog(), socketAccept(), socketNonblock(), STREAM_FD, and STREAM_SSL.

Referenced by serverRun().

37 {
38  char remoteAddr[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
39  Sock acc = NULL;
40  Stream st;
41 
42  if (this->nfds >= this->max_fds) {
43  return -1;
44  }
45 
46  acc = socketAccept((0 == i)? this->sock : this->sockSSL, &remoteAddr);
47 
48  if (NULL != acc && -1 != acc->handle) {
49  socketNonblock(acc);
50 
51  switch(i) {
52  case 0:
53  // no SSL
54  st = new(Stream, STREAM_FD, acc->handle);
55  break;
56 
57  case 1:
58  // SSL
59  {
60  SSL * ssl = SSL_new(this->ctx);
61  SSL_set_fd(ssl, acc->handle);
62  SSL_accept(ssl);
63  st = new(Stream, STREAM_SSL, ssl);
64  }
65  break;
66 
67  default:
68  st = NULL;
69  break;
70  }
71 
72  // save the socket handle
73  (this->conns)[acc->handle].sock = acc;
74 
75  // clone worker
76  (this->conns)[acc->handle].worker = clone(this->worker);
77  (this->conns)[acc->handle].stream = st;
78 
79  (this->fds)[this->nfds].fd = acc->handle;
80  (this->fds)[this->nfds].events = POLLIN;
81  this->nfds++;
82  } else {
83  delete(acc);
84 
85  switch(errno) {
86  case EAGAIN|EWOULDBLOCK:
87  case EINTR:
88  loggerLog(this->logger,
90  "server accept blocks");
91  return -1;
92  break;
93 
94  default:
95  loggerLog(this->logger,
97  "server accept error");
98  return -2;
99  break;
100  }
101  }
102 
103  if (0 == this->nfds%200) {
104  loggerLog(this->logger,
105  LOGGER_DEBUG, "paralel connections: %lu", this->nfds);
106  }
107 
108  return acc->handle;
109 }
Logger logger
Definition: taskrambler.c:66
Sock socketAccept(Sock this, char(*remoteAddr)[16])
Definition: accept.c:32
void socketNonblock(Sock this)
Definition: nonblock.c:32
void loggerLog(void *, logger_level, const char *const,...)
Definition: i_logger.c:38
#define clone(object)
Definition: server.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int serverPoll ( Server  )

put all closed fds to end of array in O(this->nfds)

Definition at line 34 of file poll.c.

References doShutdown, logger, LOGGER_CRIT, and loggerLog().

Referenced by serverRun().

34  {
35  int events;
36 
37  /**
38  * put all closed fds to end of array in O(this->nfds)
39  */
40  struct pollfd * fda = &(this->fds[2]);
41  struct pollfd * fdb = &(this->fds[this->nfds-1]);
42 
43  while (fda <= fdb) {
44  while (0 == fdb->fd && fda <= fdb) {
45  fdb--;
46  this->nfds--;
47  }
48 
49  while (0 != fda->fd && fda <= fdb) fda++;
50 
51  if (fda < fdb) {
52  memcpy(fda, fdb, sizeof(struct pollfd));
53  fdb--;
54  this->nfds--;
55  }
56  }
57 
58  /*
59  * wait for handles to become ready
60  */
61  do {
62  if (-1 == (events = poll(this->fds, this->nfds, -1))) {
63  switch (errno) {
64  default:
65  case EBADF:
66  case EINVAL:
67  case ENOMEM:
68  doShutdown = 1;
69  // DROP THROUGH
70 
71  case EINTR:
73  "poll systemcall failed: [%s] - service terminated",
74  strerror(errno));
75  }
76  }
77  } while (! doShutdown && 0 >= events);
78 
79  return events;
80 }
Logger logger
Definition: taskrambler.c:66
void loggerLog(void *, logger_level, const char *const,...)
Definition: i_logger.c:38
volatile int doShutdown

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ssize_t serverRead ( Server  ,
unsigned  int 
)

Definition at line 31 of file server/read.c.

References logger, LOGGER_INFO, loggerLog(), and streamReaderRead().

Referenced by serverRun().

32 {
33  int fd = (this->fds)[i].fd;
34 
35  if (NULL == (this->conns)[fd].worker) {
36  loggerLog(
37  this->logger,
39  "initialization error: NULL reader");
40  return -2;
41  }
42 
43  return streamReaderRead(
44  (this->conns)[fd].worker,
45  (this->conns)[fd].stream);
46 }
ssize_t streamReaderRead(void *, Stream)
Definition: reader.c:34
Logger logger
Definition: taskrambler.c:66
void loggerLog(void *, logger_level, const char *const,...)
Definition: i_logger.c:38
Definition: server.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void serverRun ( Server  this)

until error or signal

handle accept

handle accept SSL

handle reads

handle writes

Definition at line 36 of file run.c.

References doShutdown, logger, LOGGER_INFO, loggerLog(), serverCloseConn(), serverHandleAccept(), serverPoll(), serverRead(), and serverWrite().

Referenced by main().

37 {
38  int events = 0;
39 
40  loggerLog(this->logger, LOGGER_INFO, "service started");
41 
42  while (!doShutdown) //! until error or signal
43  {
44  unsigned int i;
45 
46  if (0 <= events) {
47  /*
48  * TODO check why sometimes events is less than 0
49  * There is still a misshandling here.
50  */
51  events = serverPoll(this);
52  }
53 
54  /**
55  * handle accept
56  */
57  if (0 != ((this->fds)[0].revents & POLLIN)) {
58  while (0 < serverHandleAccept(this, 0)) {}
59  events--;
60  }
61 
62  if (events == 0) {
63  continue;
64  }
65 
66  /**
67  * handle accept SSL
68  */
69  if (0 != ((this->fds)[1].revents & POLLIN)) {
70  while (0 < serverHandleAccept(this, 1)) {}
71  events--;
72  }
73 
74  if (events == 0) {
75  continue;
76  }
77 
78  for (i=2; i < this->nfds; i++) {
79  /**
80  * handle reads
81  */
82  if (0 != ((this->fds)[i].revents & POLLIN)) {
83  ssize_t processed = serverRead(this, i);
84 
85  if (0 > processed) {
86  events--;
87 
88  switch (processed) {
89  case -2: // close me...
90  serverCloseConn(this, i);
91 
92  case -1: // poll me again
93  break;
94  }
95  }
96 
97  if (0 < processed) {
98  (this->fds)[i].events |= POLLOUT;
99  }
100  }
101 
102  if (events == 0) {
103  break;
104  }
105 
106  /**
107  * handle writes
108  */
109  if (0 != ((this->fds)[i].revents & POLLOUT)) {
110  ssize_t remaining = serverWrite(this, i);
111 
112  if (0 >= remaining) {
113  /*
114  * 0 means queue was empty...try again next
115  * time...no need to poll again.
116  * Anyway, most likely we need to read again
117  * so lets finish this event for now.
118  */
119  events--;
120 
121  switch (remaining) {
122  case 0: // nothing more to write stop polling
123  (this->fds)[i].events &= ~POLLOUT;
124  break;
125 
126  case -2: // close me...
127  serverCloseConn(this, i);
128 
129  case -1: // poll me again
130  break;
131  }
132  }
133  }
134 
135  if (events == 0) {
136  break; // no more events to handle
137  }
138  }
139  }
140 }
Logger logger
Definition: taskrambler.c:66
ssize_t serverRead(Server, unsigned int)
Definition: server/read.c:31
void loggerLog(void *, logger_level, const char *const,...)
Definition: i_logger.c:38
void serverCloseConn(Server, unsigned int)
Definition: close_conn.c:31
int serverHandleAccept(Server, unsigned int)
Definition: handle_accept.c:36
ssize_t serverWrite(Server, unsigned int)
Definition: server/write.c:30
int serverPoll(Server)
Definition: poll.c:34
volatile int doShutdown

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ssize_t serverWrite ( Server  ,
unsigned  int 
)

Definition at line 30 of file server/write.c.

References logger, LOGGER_INFO, loggerLog(), and streamWriterWrite().

Referenced by serverRun().

31 {
32  int fd = (this->fds)[i].fd;
33 
34  if (NULL == (this->conns)[fd].worker) {
35  loggerLog(
36  this->logger,
38  "initialization error: NULL worker");
39  return -2;
40  }
41 
42  return streamWriterWrite(
43  (this->conns)[fd].worker,
44  (this->conns)[fd].stream);
45 }
Logger logger
Definition: taskrambler.c:66
void loggerLog(void *, logger_level, const char *const,...)
Definition: i_logger.c:38
Definition: server.h:39
ssize_t streamWriterWrite(void *, Stream)

+ Here is the call graph for this function:

+ Here is the caller graph for this function: