This commit is contained in:
Олег Бородин
2023-09-05 11:30:10 +02:00
parent 77bd507ae8
commit e9b9df0356
15 changed files with 423 additions and 562 deletions

View File

@@ -5,15 +5,20 @@
*/
#ifdef __linux__
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/event.h>
//#include <sys/event.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
@@ -211,9 +216,8 @@ int cworker_build(cworker_t* worker) {
paddr->sin_family = AF_INET;
paddr->sin_addr.s_addr = INADDR_ANY;
paddr->sin_port = htons(worker->port);
paddr->sin_len = sizeof(struct sockaddr_in);
if (bind(worker->socket, (struct sockaddr*)paddr, paddr->sin_len) < 0) {
if (bind(worker->socket, (struct sockaddr*)paddr, sizeof(struct sockaddr_in)) < 0) {
log_debug("Socket binding error %d: %s ", errno, strerror(errno));
return -1;
}
@@ -240,7 +244,7 @@ int cworker_run(const cworker_t* worker) {
continue;
}
if (childpid == 0) {
// child
/* Child */
log_debug("Service %d forked", getpid());
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, SIG_IGN);
@@ -250,12 +254,12 @@ int cworker_run(const cworker_t* worker) {
log_debug("Socket option setting error %d: %s ", errno, strerror(errno));
return -1;
}
cworker_handler(worker, newsocket);
log_debug("Handler %d done", getpid());
int res = cworker_handler(worker, newsocket);
log_debug("Handler %d done with res code %d", getpid(), res);
close(newsocket);
return 0;
} else {
// parent
/* Parent */
close(newsocket);
}
}
@@ -269,13 +273,13 @@ int cworker_run(const cworker_t* worker) {
int cworker_handler(const cworker_t* worker, int socket) {
int err = 0;
//rcache_t cache;
//jlexer_t lexer;
//jparser_t parser;
rcache_t cache;
jlexer_t lexer;
jparser_t parser;
//rcache_init(&cache, socket);
//jlexer_init(&lexer, &cache);
//jparser_init(&parser, &lexer);
rcache_init(&cache, socket);
jlexer_init(&lexer, &cache);
jparser_init(&parser, &lexer);
//if (jparser_parse(&parser) < 0) {
//log_error("Cannot parse json");
@@ -283,8 +287,8 @@ int cworker_handler(const cworker_t* worker, int socket) {
//goto exit;
//}
//int64_t id = 0;
//char* name = "none";
int64_t id = 0;
char* name = "none";
//if (jparser_bind(&parser, JVALTYPE_NUM, "id", (void *)&id) < 0) {
//log_error("Cannot bind id");
@@ -294,29 +298,30 @@ int cworker_handler(const cworker_t* worker, int socket) {
//}
//char* msg = NULL;
//asprintf(&msg, "hello, %s!", name);
char* msg = NULL;
asprintf(&msg, "hello, %s!", name);
//jblock_t jb;
//jblock_init(&jb);
//jblock_addstr(&jb, "message", msg);
//jblock_addbool(&jb, "error", false);
jblock_t jb;
jblock_init(&jb);
jblock_addstr(&jb, "message", msg);
jblock_addbool(&jb, "error", false);
jblock_addint(&jb, "id", 12);
//char* jsonstr = NULL;
//jblock_outjson(&jb, &jsonstr);
//jblock_destroy(&jb);
//write(socket, jsonstr, strlen(jsonstr));
//free(jsonstr);
char* jsonstr = NULL;
jblock_outjson(&jb, &jsonstr);
jblock_destroy(&jb);
write(socket, jsonstr, strlen(jsonstr));
free(jsonstr);
//free(name);
//free(msg);
free(msg);
//exit:
//jparser_destroy(&parser);
//jlexer_destroy(&lexer);
//rcache_destroy(&cache);
jparser_destroy(&parser);
jlexer_destroy(&lexer);
rcache_destroy(&cache);
close(socket);
return err;
}