worker.c
1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdlib.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "class.h"
#include "http/worker.h"
#include "http/parser.h"
#include "http/response/writer.h"
#include "interface/class.h"
#include "interface/stream_reader.h"
#include "interface/stream_writer.h"
#include "utils/memory.h"
static
int
httpWorkerCtor(void * _this, va_list * params)
{
HttpWorker this = _this;
char * id = va_arg(*params, char *);
int * val = va_arg(*params, int *);
char cbuf_id[100];
this->id = malloc(strlen(id) + 1);
strcpy(this->id, id);
this->val = val;
sprintf(cbuf_id, "%s_%s", "parser", id);
this->pbuf = new(Cbuf, cbuf_id, REQUEST_PARSER_BUFFER_MAX);
sprintf(cbuf_id, "%s_%s", "writer", id);
this->wbuf = new(Cbuf, cbuf_id, RESPONSE_WRITER_MAX_BUF);
this->parser = new(HttpParser, this->pbuf);
this->writer = new(HttpResponseWriter, this->wbuf);
return 0;
}
static
void
httpWorkerDtor(void * _this)
{
HttpWorker this = _this;
FREE(this->id);
delete(this->parser);
delete(this->writer);
if (NULL != this->pbuf) delete(this->pbuf);
if (NULL != this->wbuf) delete(this->wbuf);
}
static
void
_clone(void * _this, void * _base)
{
HttpWorker this = _this;
HttpWorker base = _base;
this->id = NULL;
this->val = base->val;
this->pbuf = NULL;
this->wbuf = NULL;
this->parser = new(HttpParser, base->pbuf);
this->writer = new(HttpResponseWriter, base->wbuf);
}
ssize_t httpWorkerProcess(void *, int);
ssize_t httpWorkerWrite(void *, int);
INIT_IFACE(Class, httpWorkerCtor, httpWorkerDtor, _clone);
INIT_IFACE(StreamReader, httpWorkerProcess);
INIT_IFACE(StreamWriter, httpWorkerWrite);
CREATE_CLASS(
HttpWorker,
NULL,
IFACE(Class),
IFACE(StreamReader),
IFACE(StreamWriter));
// vim: set ts=4 sw=4: