Browse Source

now when a constructor returns -1 the new call will in turn call the destructor effectively freeing all resources. ATTENTION: now the destructor has to be aware that it might be called with a not completely initialized object. To make this more ease there is the FREE makro with the corresponding ffree that does NULL pointer checking and the destructor checks for NULL pointer too. Additionally the handle_accept now handles _SC_OPEN_MAX - 10 connections. The 10 are reserved for internal usage.

master
Georg Hopp 14 years ago
parent
commit
424297cd57
  1. 12
      ChangeLog
  2. 2
      include/interface/class.h
  3. 34
      include/server.h
  4. 2
      include/utils/memory.h
  5. 4
      src/cbuf.c
  6. 4
      src/http/header.c
  7. 4
      src/http/message.c
  8. 7
      src/http/message/queue.c
  9. 7
      src/http/request.c
  10. 4
      src/http/request/parser.c
  11. 4
      src/http/response.c
  12. 4
      src/http/response/writer.c
  13. 4
      src/http/worker.c
  14. 17
      src/interface/class.c
  15. 4
      src/logger.c
  16. 24
      src/server.c
  17. 6
      src/server/handle_accept.c
  18. 4
      src/socket.c
  19. 18
      src/testserver.c

12
ChangeLog

@ -1,6 +1,14 @@
2012-02-21 13:01:38 +0100 Georg Hopp
* now when a constructor returns -1 the new call will in turn call the destructor effectively freeing all resources. ATTENTION: now the destructor has to be aware that it might be called with a not completely initialized object. To make this more ease there is the FREE makro with the corresponding ffree that does NULL pointer checking and the destructor checks for NULL pointer too. Additionally the handle_accept now handles _SC_OPEN_MAX - 10 connections. The 10 are reserved for internal usage. (HEAD, master)
2012-02-21 09:45:01 +0100 Georg Hopp
* now a child is spawned and writes random values in a shared memory segment. These values will be shown in the me action (origin/master, origin/HEAD)
2012-02-20 21:36:55 +0100 Georg Hopp 2012-02-20 21:36:55 +0100 Georg Hopp
* some code cleanups...no changes in the logic (HEAD, master)
* some code cleanups...no changes in the logic
2012-02-20 18:08:23 +0100 Georg Hopp 2012-02-20 18:08:23 +0100 Georg Hopp
@ -8,7 +16,7 @@
2012-02-20 17:16:44 +0100 Georg Hopp 2012-02-20 17:16:44 +0100 Georg Hopp
* changed /**/ single line comments to // (origin/master, origin/HEAD)
* changed /**/ single line comments to //
2012-02-20 14:55:46 +0100 Georg Hopp 2012-02-20 14:55:46 +0100 Georg Hopp

2
include/interface/class.h

@ -31,7 +31,7 @@
#include "class.h" #include "class.h"
#include "interface.h" #include "interface.h"
typedef void (* fptr_ctor)(void *, va_list *);
typedef int (* fptr_ctor)(void *, va_list *);
typedef void (* fptr_dtor)(void *); typedef void (* fptr_dtor)(void *);
typedef void (* fptr_clone)(void *, void * const); typedef void (* fptr_clone)(void *, void * const);

34
include/server.h

@ -33,33 +33,21 @@
#include "socket.h" #include "socket.h"
#include "logger.h" #include "logger.h"
#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) \
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)))
struct conns {
Sock sock;
void * worker;
};
CLASS(Server) { CLASS(Server) {
Logger logger;
Sock sock;
void * worker;
Logger logger;
Sock sock;
void * worker;
nfds_t nfds;
struct pollfd fds[POLL_FD_NSIZE];
nfds_t nfds;
struct pollfd * fds;
long max_fds;
struct {
Sock sock;
void * worker;
} conns[POLL_FD_NSIZE];
struct conns * conns;
}; };
void serverRun(Server this); void serverRun(Server this);

2
include/utils/memory.h

@ -23,6 +23,8 @@
#ifndef __UTILS_MEMORY_H__ #ifndef __UTILS_MEMORY_H__
#define __UTILS_MEMORY_H__ #define __UTILS_MEMORY_H__
#define FREE(val) (ffree((void**)(val)))
void ffree(void **); void ffree(void **);
#endif // __UTILS_MEMORY_H__ #endif // __UTILS_MEMORY_H__

4
src/cbuf.c

@ -42,7 +42,7 @@
static void dtor(void*); static void dtor(void*);
static static
void
int
ctor(void * _this, va_list * params) ctor(void * _this, va_list * params)
{ {
Cbuf this = _this; Cbuf this = _this;
@ -104,6 +104,8 @@ ctor(void * _this, va_list * params)
if (1 != state) { if (1 != state) {
dtor(this); dtor(this);
} }
return 0;
} }
static static

4
src/http/header.c

@ -31,7 +31,7 @@
#include "utils/hash.h" #include "utils/hash.h"
static static
void
int
ctor(void * _this, va_list * params) { ctor(void * _this, va_list * params) {
HttpHeader this = _this; HttpHeader this = _this;
char * name; char * name;
@ -47,6 +47,8 @@ ctor(void * _this, va_list * params) {
this->value = malloc(strlen(value) + 1); this->value = malloc(strlen(value) + 1);
strcpy(this->value, value); strcpy(this->value, value);
return 0;
} }
static static

4
src/http/message.c

@ -46,7 +46,7 @@ tDelete(void * node)
} }
static static
void
int
ctor(void * _this, va_list * params) ctor(void * _this, va_list * params)
{ {
HttpMessage this = _this; HttpMessage this = _this;
@ -54,6 +54,8 @@ ctor(void * _this, va_list * params)
this->version = calloc(1, strlen(version)+1); this->version = calloc(1, strlen(version)+1);
strcpy(this->version, version); strcpy(this->version, version);
return 0;
} }
static static

7
src/http/message/queue.c

@ -28,8 +28,11 @@
#include "http/message/queue.h" #include "http/message/queue.h"
static static
void
ctor(void * _this, va_list * params) {}
int
ctor(void * _this, va_list * params)
{
return 0;
}
static static
void void

7
src/http/request.c

@ -34,8 +34,11 @@
static static
void
ctor(void * _this, va_list * params) {}
int
ctor(void * _this, va_list * params)
{
return 0;
}
static static
void void

4
src/http/request/parser.c

@ -35,13 +35,15 @@
static static
void
int
ctor(void * _this, va_list * params) ctor(void * _this, va_list * params)
{ {
HttpRequestParser this = _this; HttpRequestParser this = _this;
this->buffer = va_arg(* params, Cbuf); this->buffer = va_arg(* params, Cbuf);
this->request_queue = new(HttpMessageQueue); this->request_queue = new(HttpMessageQueue);
return 0;
} }
static static

4
src/http/response.c

@ -35,7 +35,7 @@
static static
void
int
ctor(void * _this, va_list * params) ctor(void * _this, va_list * params)
{ {
HttpResponse this = _this; HttpResponse this = _this;
@ -48,6 +48,8 @@ ctor(void * _this, va_list * params)
this->reason = calloc(1, strlen(reason)+1); this->reason = calloc(1, strlen(reason)+1);
strcpy(this->reason, reason); strcpy(this->reason, reason);
return 0;
} }
static static

4
src/http/response/writer.c

@ -30,13 +30,15 @@
#include "http/response/writer.h" #include "http/response/writer.h"
static static
void
int
ctor(void * _this, va_list * params) ctor(void * _this, va_list * params)
{ {
HttpResponseWriter this = _this; HttpResponseWriter this = _this;
this->buffer = va_arg(*params, Cbuf); this->buffer = va_arg(*params, Cbuf);
this->response_queue = new(HttpMessageQueue); this->response_queue = new(HttpMessageQueue);
return 0;
} }
static static

4
src/http/worker.c

@ -15,7 +15,7 @@
#define SHMN "/worker_" #define SHMN "/worker_"
static static
void
int
ctor(void * _this, va_list * params) ctor(void * _this, va_list * params)
{ {
HttpWorker this = _this; HttpWorker this = _this;
@ -35,6 +35,8 @@ ctor(void * _this, va_list * params)
this->parser = new(HttpRequestParser, this->pbuf); this->parser = new(HttpRequestParser, this->pbuf);
this->writer = new(HttpResponseWriter, this->wbuf); this->writer = new(HttpResponseWriter, this->wbuf);
return 0;
} }
static static

17
src/interface/class.c

@ -36,8 +36,9 @@ struct interface i_Class = {
void * void *
classNew(class_ptr class, ...) classNew(class_ptr class, ...)
{ {
void * object = calloc(1, class->object_size + sizeof(void*));
void * object = calloc(1, class->object_size + sizeof(void*));
va_list params; va_list params;
int ret;
if (class->init) class->init(); if (class->init) class->init();
@ -45,19 +46,25 @@ classNew(class_ptr class, ...)
object += sizeof(void*); object += sizeof(void*);
va_start(params, class); va_start(params, class);
CALL(object, Class, ctor, &params);
RETCALL(object, Class, ctor, ret, &params);
va_end(params); va_end(params);
if (-1 == ret) {
classDelete(&object);
}
return object; return object;
} }
void void
classDelete(void ** object) classDelete(void ** object)
{ {
CALL(*object, Class, dtor);
if (NULL != *object) {
CALL(*object, Class, dtor);
free(*object - sizeof(void*));
*object = NULL;
free(*object - sizeof(void*));
*object = NULL;
}
} }
void * void *

4
src/logger.c

@ -40,11 +40,13 @@ logger_level_str[] = {
}; };
static static
void
int
ctor(void * _this, va_list * params) ctor(void * _this, va_list * params)
{ {
Logger this = _this; Logger this = _this;
this->min_level = va_arg(*params, int); this->min_level = va_arg(*params, int);
return 0;
} }
static void dtor(void * _this) {} static void dtor(void * _this) {}

24
src/server.c

@ -21,6 +21,8 @@
*/ */
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include "class.h" #include "class.h"
#include "server.h" #include "server.h"
@ -28,8 +30,10 @@
#include "logger.h" #include "logger.h"
#include "interface/class.h" #include "interface/class.h"
#include "utils/memory.h"
static static
void
int
ctor(void * _this, va_list * params) ctor(void * _this, va_list * params)
{ {
Server this = _this; Server this = _this;
@ -37,12 +41,23 @@ ctor(void * _this, va_list * params)
unsigned int backlog; unsigned int backlog;
int flags; int flags;
this->max_fds = sysconf(_SC_OPEN_MAX);
if (this->max_fds <= 10) { // reserve 10 handles for internal use.
/**
* \todo some logging would be appropriate :)
*/
return -1;
}
this->max_fds -= 10;
this->logger = va_arg(* params, Logger); this->logger = va_arg(* params, Logger);
this->worker = va_arg(* params, void *); this->worker = va_arg(* params, void *);
port = va_arg(* params, int); port = va_arg(* params, int);
backlog = va_arg(* params, unsigned int); backlog = va_arg(* params, unsigned int);
this->sock = new(Sock, this->logger, port);
this->fds = calloc(sizeof(struct pollfd), this->max_fds);
this->conns = calloc(sizeof(struct conns), this->max_fds);
this->sock = new(Sock, this->logger, port);
flags = fcntl(this->sock->handle, F_GETFL, 0); flags = fcntl(this->sock->handle, F_GETFL, 0);
fcntl(this->sock->handle, F_SETFL, flags | O_NONBLOCK); fcntl(this->sock->handle, F_SETFL, flags | O_NONBLOCK);
@ -52,6 +67,8 @@ ctor(void * _this, va_list * params)
(this->fds)[0].fd = this->sock->handle; (this->fds)[0].fd = this->sock->handle;
(this->fds)[0].events = POLLIN; (this->fds)[0].events = POLLIN;
this->nfds = 1; this->nfds = 1;
return 0;
} }
static static
@ -68,6 +85,9 @@ dtor(void * _this)
} }
} }
FREE(&(this->fds));
FREE(&(this->conns));
delete(&this->sock); delete(&this->sock);
} }

6
src/server/handle_accept.c

@ -33,7 +33,11 @@ int
serverHandleAccept(Server this) serverHandleAccept(Server this)
{ {
char remoteAddr[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; char remoteAddr[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
Sock acc;
Sock acc = NULL;
if (this->nfds >= this->max_fds) {
return -1;
}
acc = socketAccept(this->sock, &remoteAddr); acc = socketAccept(this->sock, &remoteAddr);

4
src/socket.c

@ -30,7 +30,7 @@
#include "interface/logger.h" #include "interface/logger.h"
static static
void
int
ctor(void * _this, va_list * params) ctor(void * _this, va_list * params)
{ {
Sock this = _this; Sock this = _this;
@ -49,6 +49,8 @@ ctor(void * _this, va_list * params)
//! Make the socket REUSE a TIME_WAIT socket //! Make the socket REUSE a TIME_WAIT socket
setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof (reUse)); setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof (reUse));
return 0;
} }
static static

18
src/testserver.c

@ -63,6 +63,10 @@ main()
struct rlimit limit = {RLIM_INFINITY, RLIM_INFINITY}; struct rlimit limit = {RLIM_INFINITY, RLIM_INFINITY};
setrlimit(RLIMIT_CPU, &limit); setrlimit(RLIMIT_CPU, &limit);
getrlimit(RLIMIT_NOFILE, &limit);
limit.rlim_cur = limit.rlim_max;
setrlimit(RLIMIT_NOFILE, &limit);
init_signals(); init_signals();
shm = shm_open("/fooshm", O_RDWR|O_CREAT, S_IRWXU); shm = shm_open("/fooshm", O_RDWR|O_CREAT, S_IRWXU);
@ -132,7 +136,13 @@ main()
server = new(Server, logger, worker, 11212, SOMAXCONN); server = new(Server, logger, worker, 11212, SOMAXCONN);
//daemonize(); //daemonize();
serverRun(server);
if (NULL != server) {
serverRun(server);
}
else {
doShutdown = 1;
kill(pid, SIGINT);
}
do { do {
pid_t w; pid_t w;
@ -155,9 +165,9 @@ main()
} }
} while (!WIFEXITED(status) && !WIFSIGNALED(status)); } while (!WIFEXITED(status) && !WIFSIGNALED(status));
delete(&server);
delete(&worker);
delete(&logger);
if (NULL != server) delete(&server);
if (NULL != worker) delete(&worker);
if (NULL != logger) delete(&logger);
} }
break; break;

Loading…
Cancel
Save