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
* 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
@ -8,7 +16,7 @@
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

2
include/interface/class.h

@ -31,7 +31,7 @@
#include "class.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_clone)(void *, void * const);

34
include/server.h

@ -33,33 +33,21 @@
#include "socket.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) {
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);

2
include/utils/memory.h

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

4
src/cbuf.c

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

4
src/http/header.c

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

4
src/http/message.c

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

7
src/http/message/queue.c

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

7
src/http/request.c

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

4
src/http/request/parser.c

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

4
src/http/response.c

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

4
src/http/response/writer.c

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

4
src/http/worker.c

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

17
src/interface/class.c

@ -36,8 +36,9 @@ struct interface i_Class = {
void *
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;
int ret;
if (class->init) class->init();
@ -45,19 +46,25 @@ classNew(class_ptr class, ...)
object += sizeof(void*);
va_start(params, class);
CALL(object, Class, ctor, &params);
RETCALL(object, Class, ctor, ret, &params);
va_end(params);
if (-1 == ret) {
classDelete(&object);
}
return object;
}
void
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 *

4
src/logger.c

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

24
src/server.c

@ -21,6 +21,8 @@
*/
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include "class.h"
#include "server.h"
@ -28,8 +30,10 @@
#include "logger.h"
#include "interface/class.h"
#include "utils/memory.h"
static
void
int
ctor(void * _this, va_list * params)
{
Server this = _this;
@ -37,12 +41,23 @@ ctor(void * _this, va_list * params)
unsigned int backlog;
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->worker = va_arg(* params, void *);
port = va_arg(* params, 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);
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].events = POLLIN;
this->nfds = 1;
return 0;
}
static
@ -68,6 +85,9 @@ dtor(void * _this)
}
}
FREE(&(this->fds));
FREE(&(this->conns));
delete(&this->sock);
}

6
src/server/handle_accept.c

@ -33,7 +33,11 @@ int
serverHandleAccept(Server this)
{
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);

4
src/socket.c

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

18
src/testserver.c

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

Loading…
Cancel
Save