diff --git a/Makefile.am b/Makefile.am index 8991d5d..206fbce 100644 --- a/Makefile.am +++ b/Makefile.am @@ -13,19 +13,19 @@ noinst_PROGRAMS = gen_tile_test man_MANS = docs/renderd.8 docs/render_expired.1 docs/render_list.1 docs/render_old.1 docs/render_speedtest.1 renderddir = $(sysconfdir) -renderd_SOURCES = src/daemon.c src/daemon_compat.c src/gen_tile.cpp src/sys_utils.c src/request_queue.c src/cache_expire.c src/metatile.cpp src/parameterize_style.cpp $(STORE_SOURCES) iniparser3.0b/libiniparser.la +renderd_SOURCES = src/daemon.c src/daemon_compat.c src/gen_tile.cpp src/sys_utils.c src/request_queue.c src/cache_expire.c src/metatile.cpp src/parameterize_style.cpp src/protocol_helper.c $(STORE_SOURCES) iniparser3.0b/libiniparser.la renderd_LDADD = $(FT2_LIBS) $(PTHREAD_CFLAGS) $(MAPNIK_LDFLAGS) $(BOOST_LDFLAGS) $(ICU_LDFLAGS) $(STORE_LDFLAGS) -Liniparser3.0b/.libs -liniparser renderd_DATA = renderd.conf -render_speedtest_SOURCES = src/speedtest.cpp src/render_submit_queue.c src/sys_utils.c +render_speedtest_SOURCES = src/speedtest.cpp src/protocol_helper.c src/render_submit_queue.c src/sys_utils.c render_speedtest_LDADD = $(PTHREAD_CFLAGS) -render_list_SOURCES = src/render_list.c src/sys_utils.c src/render_submit_queue.c $(STORE_SOURCES) +render_list_SOURCES = src/render_list.c src/sys_utils.c src/protocol_helper.c src/render_submit_queue.c $(STORE_SOURCES) render_list_LDADD = $(PTHREAD_CFLAGS) $(STORE_LDFLAGS) -render_expired_SOURCES = src/render_expired.c src/render_submit_queue.c src/sys_utils.c $(STORE_SOURCES) +render_expired_SOURCES = src/render_expired.c src/protocol_helper.c src/render_submit_queue.c src/sys_utils.c $(STORE_SOURCES) render_expired_LDADD = $(PTHREAD_CFLAGS) $(STORE_LDFLAGS) -render_old_SOURCES = src/store_file_utils.c src/render_old.c src/sys_utils.c src/render_submit_queue.c +render_old_SOURCES = src/store_file_utils.c src/render_old.c src/sys_utils.c src/protocol_helper.c src/render_submit_queue.c render_old_LDADD = $(PTHREAD_CFLAGS) #convert_meta_SOURCES = src/dir_utils.c src/store.c src/convert_meta.c -gen_tile_test_SOURCES = src/gen_tile_test.cpp src/metatile.cpp src/request_queue.c src/daemon.c src/daemon_compat.c src/gen_tile.cpp src/sys_utils.c src/cache_expire.c src/parameterize_style.cpp $(STORE_SOURCES) iniparser3.0b/libiniparser.la +gen_tile_test_SOURCES = src/gen_tile_test.cpp src/metatile.cpp src/request_queue.c src/protocol_helper.c src/daemon.c src/daemon_compat.c src/gen_tile.cpp src/sys_utils.c src/cache_expire.c src/parameterize_style.cpp $(STORE_SOURCES) iniparser3.0b/libiniparser.la gen_tile_test_CFLAGS = -DMAIN_ALREADY_DEFINED $(PTHREAD_CFLAGS) gen_tile_test_LDADD = $(FT2_LIBS) $(MAPNIK_LDFLAGS) $(BOOST_LDFLAGS) $(ICU_LDFLAGS) $(STORE_LDFLAGS) -Liniparser3.0b/.libs -liniparser diff --git a/includes/protocol_helper.h b/includes/protocol_helper.h new file mode 100644 index 0000000..38933d8 --- /dev/null +++ b/includes/protocol_helper.h @@ -0,0 +1,17 @@ +#ifndef DAEMONHELPER_H +#define DAEMONHELPER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "protocol.h" + +int send_cmd(struct protocol * cmd, int fd); +int recv_cmd(struct protocol * cmd, int fd); + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/daemon.c b/src/daemon.c index 2031b05..e7233fb 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -23,6 +23,7 @@ #include "daemon.h" #include "gen_tile.h" #include "protocol.h" +#include "protocol_helper.h" #include "request_queue.h" #define PIDFILE "/var/run/renderd/renderd.pid" @@ -59,6 +60,9 @@ static const char *cmdStr(enum protoCmd c) } } + + + void send_response(struct item *item, enum protoCmd rsp, int render_time) { struct protocol *req = &item->req; struct item *prev; @@ -71,24 +75,8 @@ void send_response(struct item *item, enum protoCmd rsp, int render_time) { if ((item->fd != FD_INVALID) && ((req->cmd == cmdRender) || (req->cmd == cmdRenderPrio) || (req->cmd == cmdRenderBulk))) { req->cmd = rsp; //fprintf(stderr, "Sending message %s to %d\n", cmdStr(rsp), item->fd); - syslog(LOG_DEBUG, "DEBUG: Sending reply with protocol version %i\n", req->ver); - switch (req->ver) { - case 1: - ret = send(item->fd, req, sizeof(struct protocol_v1), 0); - if (ret != sizeof(struct protocol_v1)) - perror("send error during send_done"); - break; - case 2: - ret = send(item->fd, req, sizeof(struct protocol_v2), 0); - if (ret != sizeof(struct protocol_v2)) - perror("send error during send_done"); - break; - case 3: - ret = send(item->fd, req, sizeof(*req), 0); - if (ret != sizeof(*req)) - perror("send error during send_done"); - break; - } + + send_cmd(req, item->fd); } prev = item; @@ -226,7 +214,7 @@ void process_loop(int listen_fd) memset(&cmd,0,sizeof(cmd)); // TODO: to get highest performance we should loop here until we get EAGAIN - ret = recv(fd, &cmd, sizeof(struct protocol_v1), MSG_DONTWAIT); + ret = recv_cmd(&cmd, fd); if (ret < 1) { int j; @@ -236,37 +224,13 @@ void process_loop(int listen_fd) connections[j] = connections[j+1]; request_queue_clear_requests_by_fd(render_request_queue, fd); close(fd); - } else { - syslog(LOG_DEBUG, "DEBUG: Got incoming request with protocol version %i\n", cmd.ver); - switch (cmd.ver) { - case 2: ret += recv(fd, ((void*)&cmd) + sizeof(struct protocol_v1), sizeof(struct protocol_v2) + 1 - sizeof(struct protocol_v1), MSG_DONTWAIT); - break; - case 3: ret += recv(fd, ((void*)&cmd) + sizeof(struct protocol_v1), sizeof(struct protocol) - sizeof(struct protocol_v1), MSG_DONTWAIT); - break; - } - if ((ret == sizeof(cmd)) || (ret == sizeof(struct protocol_v1)) || (ret == sizeof(struct protocol_v2))) { - enum protoCmd rsp = rx_request(&cmd, fd); + } else { + enum protoCmd rsp = rx_request(&cmd, fd); - if (rsp == cmdNotDone) { - cmd.cmd = rsp; - syslog(LOG_DEBUG, "DEBUG: Sending NotDone response(%d)\n", rsp); - ret = send(fd, &cmd, sizeof(cmd), 0); - if (ret != sizeof(cmd)) - perror("response send error"); - } - } else if (!ret) { - int j; - - num_connections--; - syslog(LOG_DEBUG, "DEBUG: Connection %d, fd %d closed, now %d left\n", i, fd, num_connections); - for (j=i; j < num_connections; j++) - connections[j] = connections[j+1]; - request_queue_clear_requests_by_fd(render_request_queue, fd); - close(fd); - } else { - - syslog(LOG_ERR, "Recv Error on fd %d (%s). Read %i bytes", fd, strerror(errno), ret); - break; + if (rsp == cmdNotDone) { + cmd.cmd = rsp; + syslog(LOG_DEBUG, "DEBUG: Sending NotDone response(%d)\n", rsp); + ret = send_cmd(&cmd, fd); } } } @@ -555,7 +519,7 @@ void *slave_thread(void * arg) { syslog(LOG_INFO, "Dispatching request to slave thread on fd %i", pfd); do { - ret_size = send(pfd, req_slave, sizeof(struct protocol), 0); + ret_size = send_cmd(req_slave, pfd); if (ret_size == sizeof(struct protocol)) { //correctly sent command to slave diff --git a/src/mod_tile.c b/src/mod_tile.c index 264884c..e668bc7 100644 --- a/src/mod_tile.c +++ b/src/mod_tile.c @@ -257,6 +257,9 @@ static int request_tile(request_rec *r, struct protocol *cmd, int renderImmediat strerror(errno)); break; } + if (resp.ver == 3) { + ret += recv(fd, ((void*)&resp) + sizeof(struct protocol_v2), sizeof(struct protocol) - sizeof(struct protocol_v2), 0); + } if (cmd->x == resp.x && cmd->y == resp.y && cmd->z == resp.z && !strcmp(cmd->xmlname, resp.xmlname)) { close(fd); diff --git a/src/protocol_helper.c b/src/protocol_helper.c new file mode 100644 index 0000000..88bbd9e --- /dev/null +++ b/src/protocol_helper.c @@ -0,0 +1,57 @@ +#include "protocol.h" +#include +#include +#include +#include +#include + +int send_cmd(struct protocol * cmd, int fd) { + int ret; + syslog(LOG_DEBUG, "DEBUG: Sending render cmd with protocol version %i\n", cmd->ver); + switch (cmd->ver) { + case 1: + ret = send(fd, cmd, sizeof(struct protocol_v1), 0); + break; + case 2: + ret = send(fd, cmd, sizeof(struct protocol_v2), 0); + break; + case 3: + ret = send(fd, cmd, sizeof(struct protocol), 0); + break; + } + if ((ret != sizeof(struct protocol)) && (ret != sizeof(struct protocol_v2)) && (ret != sizeof(struct protocol_v1))) { + perror("send error"); + } + return ret; +} + +int recv_cmd(struct protocol * cmd, int fd) { + int ret, ret2; + memset(cmd,0,sizeof(*cmd)); + ret = recv(fd, cmd, sizeof(struct protocol_v1), MSG_DONTWAIT); + if (ret < 1) { + return -1; + } else if (ret < sizeof(struct protocol_v1)) { + return 0; + } + syslog(LOG_DEBUG, "DEBUG: Got incoming request with protocol version %i\n", cmd->ver); + switch (cmd->ver) { + case 2: ret2 = recv(fd, ((void*)cmd) + sizeof(struct protocol_v1), sizeof(struct protocol_v2) - sizeof(struct protocol_v1), MSG_DONTWAIT); + break; + case 3: ret2 = recv(fd, ((void*)cmd) + sizeof(struct protocol_v1), sizeof(struct protocol) - sizeof(struct protocol_v1), MSG_DONTWAIT); + break; + } + + if (ret2 < 1) { + syslog(LOG_WARNING, "WARNING: Socket prematurely closed: %i\n", fd); + return -1; + } + + ret += ret2; + + if ((ret == sizeof(struct protocol)) || (ret == sizeof(struct protocol_v1)) || (ret == sizeof(struct protocol_v2))) { + return ret; + } + syslog(LOG_WARNING, "WARNING: Socket read wrong number of bytes: %i -> %li, %li\n", ret, sizeof(struct protocol_v2), sizeof(struct protocol)); + return 0; +} diff --git a/src/render_submit_queue.c b/src/render_submit_queue.c index 86547e0..c7259a0 100644 --- a/src/render_submit_queue.c +++ b/src/render_submit_queue.c @@ -11,6 +11,7 @@ #include "render_submit_queue.h" #include "sys_utils.h" #include "protocol.h" +#include "protocol_helper.h" #include "render_config.h" #define QMAX 32 @@ -70,19 +71,14 @@ static int process(struct protocol * cmd, int fd) t1 = tim.tv_sec*1000+(tim.tv_usec/1000); //printf("Sending request\n"); - ret = send(fd, cmd, sizeof(*cmd), 0); - if (ret != sizeof(*cmd)) { + if (send_cmd(cmd, fd) < 1) { perror("send error"); - } + }; //printf("Waiting for response\n"); bzero(&rsp, sizeof(rsp)); - ret = recv(fd, &rsp, sizeof(rsp), 0); - if (ret != sizeof(rsp)) - { - perror("recv error"); - return 0; - } + ret = recv_cmd(&rsp, fd); + if (ret < 1) return 0; //printf("Got response\n"); if (rsp.cmd != cmdDone)