server_8h.tex 9.68 KB
\hypertarget{server_8h}{
\section{include/server.h File Reference}
\label{server_8h}\index{include/server.h@{include/server.h}}
}
{\ttfamily \#include $<$stdio.h$>$}\par
{\ttfamily \#include $<$poll.h$>$}\par
{\ttfamily \#include \char`\"{}class.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}socket.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}logger.h\char`\"{}}\par
Include dependency graph for server.h:
\subsection*{Classes}
\begin{DoxyCompactItemize}
\item 
struct \hyperlink{structServer}{Server}
\end{DoxyCompactItemize}
\subsection*{Defines}
\begin{DoxyCompactItemize}
\item 
\#define \hyperlink{server_8h_a6371240ebfb3156b737ddd83e1e142bd}{POLL\_\-FD\_\-NSIZE}~1024
\item 
\#define \hyperlink{server_8h_ade0a08dce19efc4f1ec7dc2ab0120ab9}{POLL\_\-FD\_\-SIZE}~(sizeof(struct pollfd) $\ast$ POLL\_\-FD\_\-NSIZE)
\item 
\#define \hyperlink{server_8h_a4a694160d07797872cb3861f94b95c94}{MOVE\_\-SIZE}(size, idx)~((size) $\ast$ (POLL\_\-FD\_\-NSIZE-\/((idx)+1)))
\item 
\#define \hyperlink{server_8h_a11d5c89fb77bc941d085e8f38d014665}{CLEAR\_\-CONN}(server, idx)
\end{DoxyCompactItemize}
\subsection*{Functions}
\begin{DoxyCompactItemize}
\item 
void \hyperlink{server_8h_a67a4d9073b80d8ae1459fb11ddd23675}{serverRun} (\hyperlink{structServer}{Server} this)
\item 
void \hyperlink{server_8h_a81f787254ea87cbc851d292e5a7d195d}{serverCloseConn} (\hyperlink{structServer}{Server} this, unsigned int handle)
\end{DoxyCompactItemize}


\subsection{Define Documentation}
\hypertarget{server_8h_a11d5c89fb77bc941d085e8f38d014665}{
\index{server.h@{server.h}!CLEAR\_\-CONN@{CLEAR\_\-CONN}}
\index{CLEAR\_\-CONN@{CLEAR\_\-CONN}!server.h@{server.h}}
\subsubsection[{CLEAR\_\-CONN}]{\setlength{\rightskip}{0pt plus 5cm}\#define CLEAR\_\-CONN(
\begin{DoxyParamCaption}
\item[{}]{server, }
\item[{}]{idx}
\end{DoxyParamCaption}
)}}
\label{server_8h_a11d5c89fb77bc941d085e8f38d014665}
{\bfseries Value:}
\begin{DoxyCode}
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)))
\end{DoxyCode}


Definition at line 15 of file server.h.

\hypertarget{server_8h_a4a694160d07797872cb3861f94b95c94}{
\index{server.h@{server.h}!MOVE\_\-SIZE@{MOVE\_\-SIZE}}
\index{MOVE\_\-SIZE@{MOVE\_\-SIZE}!server.h@{server.h}}
\subsubsection[{MOVE\_\-SIZE}]{\setlength{\rightskip}{0pt plus 5cm}\#define MOVE\_\-SIZE(
\begin{DoxyParamCaption}
\item[{}]{size, }
\item[{}]{idx}
\end{DoxyParamCaption}
)~((size) $\ast$ (POLL\_\-FD\_\-NSIZE-\/((idx)+1)))}}
\label{server_8h_a4a694160d07797872cb3861f94b95c94}


Definition at line 14 of file server.h.

\hypertarget{server_8h_a6371240ebfb3156b737ddd83e1e142bd}{
\index{server.h@{server.h}!POLL\_\-FD\_\-NSIZE@{POLL\_\-FD\_\-NSIZE}}
\index{POLL\_\-FD\_\-NSIZE@{POLL\_\-FD\_\-NSIZE}!server.h@{server.h}}
\subsubsection[{POLL\_\-FD\_\-NSIZE}]{\setlength{\rightskip}{0pt plus 5cm}\#define POLL\_\-FD\_\-NSIZE~1024}}
\label{server_8h_a6371240ebfb3156b737ddd83e1e142bd}


Definition at line 11 of file server.h.

\hypertarget{server_8h_ade0a08dce19efc4f1ec7dc2ab0120ab9}{
\index{server.h@{server.h}!POLL\_\-FD\_\-SIZE@{POLL\_\-FD\_\-SIZE}}
\index{POLL\_\-FD\_\-SIZE@{POLL\_\-FD\_\-SIZE}!server.h@{server.h}}
\subsubsection[{POLL\_\-FD\_\-SIZE}]{\setlength{\rightskip}{0pt plus 5cm}\#define POLL\_\-FD\_\-SIZE~(sizeof(struct pollfd) $\ast$ POLL\_\-FD\_\-NSIZE)}}
\label{server_8h_ade0a08dce19efc4f1ec7dc2ab0120ab9}


Definition at line 12 of file server.h.



\subsection{Function Documentation}
\hypertarget{server_8h_a81f787254ea87cbc851d292e5a7d195d}{
\index{server.h@{server.h}!serverCloseConn@{serverCloseConn}}
\index{serverCloseConn@{serverCloseConn}!server.h@{server.h}}
\subsubsection[{serverCloseConn}]{\setlength{\rightskip}{0pt plus 5cm}void serverCloseConn (
\begin{DoxyParamCaption}
\item[{{\bf Server}}]{this, }
\item[{unsigned int}]{handle}
\end{DoxyParamCaption}
)}}
\label{server_8h_a81f787254ea87cbc851d292e5a7d195d}


Definition at line 7 of file close\_\-conn.c.


\begin{DoxyCode}
{
        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--;
}
\end{DoxyCode}


Here is the caller graph for this function:


\hypertarget{server_8h_a67a4d9073b80d8ae1459fb11ddd23675}{
\index{server.h@{server.h}!serverRun@{serverRun}}
\index{serverRun@{serverRun}!server.h@{server.h}}
\subsubsection[{serverRun}]{\setlength{\rightskip}{0pt plus 5cm}void serverRun (
\begin{DoxyParamCaption}
\item[{{\bf Server}}]{this}
\end{DoxyParamCaption}
)}}
\label{server_8h_a67a4d9073b80d8ae1459fb11ddd23675}


: 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.


\begin{DoxyCode}
{
    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 = que
      ue->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 F
      ound</title></head>" \
                                                        "<body><h1>404 - Not Foun
      d</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);
                        }
                }
    }
}
\end{DoxyCode}


Here is the call graph for this function:




Here is the caller graph for this function: