server 0.0.1
basicserverinfrastructure

include/server.h File Reference

#include <stdio.h>
#include <poll.h>
#include "class.h"
#include "socket.h"
#include "logger.h"
Include dependency graph for server.h:

Go to the source code of this file.

Data Structures

struct  Server

Defines

#define POLL_FD_NSIZE   1024
#define POLL_FD_SIZE   (sizeof(struct pollfd) * POLL_FD_NSIZE)
#define MOVE_SIZE(size, idx)   ((size) * (POLL_FD_NSIZE-((idx)+1)))
#define CLEAR_CONN(server, idx)

Functions

void serverRun (Server this)
void serverCloseConn (Server this, unsigned int handle)

Define Documentation

#define CLEAR_CONN (   server,
  idx 
)
Value:
memmove(&(((server)->fds)[(idx)]), \
                        &(((server)->fds)[(idx)+1]), \
                        MOVE_SIZE(sizeof(((server)->fds)[0]),(idx))); \
        memmove(&(((server)->conns)[(idx)]), \
                        &(((server)->conns)[(idx)+1]), \
                        MOVE_SIZE(sizeof(((server)->conns)[0]),(idx)))

Definition at line 15 of file server.h.

#define MOVE_SIZE (   size,
  idx 
)    ((size) * (POLL_FD_NSIZE-((idx)+1)))

Definition at line 14 of file server.h.

#define POLL_FD_NSIZE   1024

Definition at line 11 of file server.h.

#define POLL_FD_SIZE   (sizeof(struct pollfd) * POLL_FD_NSIZE)

Definition at line 12 of file server.h.


Function Documentation

void serverCloseConn ( Server  this,
unsigned int  handle 
)

Definition at line 7 of file close_conn.c.

{
        int fd = (this->fds)[i].fd;

        delete(&((this->conns)[fd].sock));
        delete(&((this->conns)[fd].reader));
        (this->fds)[i].events = 0;
        this->ndel++;
//      CLEAR_CONN(this, i);
//      this->nfds--;
}

Here is the caller graph for this function:

void serverRun ( Server  this)

: actually this is the main loop of my server. When stuff becomes more complicated it might be feasabible to split stuff into separate processes. This will definetly involve some IPC and syncing. Right now as this is actually only a simple HTTP server implementation we go on with this single process. What we can first do to get some processing between read/write cicles is to use the poll timeout.

handle accept

handle reads

do some other processing : actually this will hard assume that our stream reader is a http parser and it has its queue...think about more generalizing here.

: for now simply remove request and send not found. Make this sane.

: the complete response stuff have to be removed here.

: just to send an answer and be able to make some apache benchs i do it here...this definetly MUST BE moved

handle writes

Definition at line 30 of file run.c.

{
    loggerLog(this->logger, LOGGER_INFO, "service started");

    while (!doShutdown) /* until error or signal  */
    {
                int          events;
                unsigned int i;

                events = serverPoll(this);
                if (doShutdown) break;

                for (i=0; i < events; i++) {
                        int fd = (this->fds)[i].fd;
                        //int nreads = 0, nwrites = 0;

                        if (0 != ((this->fds)[i].revents & POLLIN)) {
                                if (this->sock->handle == (this->fds)[i].fd) {
                                        serverHandleAccept(this);
                                }

                                else {
                                        serverRead(this, i);

                                        {
                                                int              j;
                                                HttpRequestQueue queue =
                                                        ((HttpRequestParser)(this->conns)[fd].reader)->request_queue;

                                                for (j=0; j<queue->nrequests; j++) {
                                                        HttpRequest request = queue->requests[j];

                                                        delete(&request);

                                                        time_t t;
                                                        struct tm * tmp;
                                                        char timestr[200];

#define RESP_HEAD "HTTP/1.1 404 Not Found\r\n" \
                                                        "Content-Type: text/html\r\n" \
                                                        "Content-Length: %lu\r\n" \
                                                        "Date: %s\r\n" \
                                                        "Server: testserver\r\n"

#define RESP_DATA "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \
                                                        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \
                                                        " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" \
                                                        "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" \
                                                        "<head><title>404 - Not Found</title></head>" \
                                                        "<body><h1>404 - Not Found</h1></body>" \
                                                        "</html>"

                                                        t = time(NULL);
                                                        tmp = localtime(&t);
                                                        strftime(timestr, sizeof(timestr), "%a, %d %b %Y %T %Z", tmp);

                                                        sprintf((this->conns)[fd].wbuf, RESP_HEAD "\r\n" RESP_DATA, sizeof(RESP_DATA), timestr);
                                                        (this->fds)[i].events = (this->fds)[i].events | POLLOUT;
                                                }

                                                queue->nrequests = 0;
                                        }
                                }
                        }

                        if (0 != ((this->fds)[i].revents & POLLOUT)) {
                                write(
                                                (this->fds)[i].fd,
                                                (this->conns)[fd].wbuf,
                                                strlen((this->conns)[fd].wbuf));
                                (this->fds)[i].events = (this->fds)[i].events & ~POLLOUT;
                                serverCloseConn(this, i);
                        }
                }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines