📄 ijs_server.c
字号:
/** * Copyright (c) 2001-2004 artofcode LLC. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE.**/#include <stdlib.h>#include <stdio.h>#include "unistd_.h"#include <string.h>#include "ijs.h"#include "ijs_server.h"#define noVERBOSEtypedef enum { IJS_N_CHAN_SET = 1, IJS_BPS_SET = 2, IJS_CS_SET = 4, IJS_WIDTH_SET = 8, IJS_HEIGHT_SET = 16, IJS_DPI_SET = 32} IjsFieldsSet;#define IJS_FIELDS_REQUIRED (IJS_N_CHAN_SET | IJS_BPS_SET | IJS_CS_SET | \ IJS_WIDTH_SET | IJS_HEIGHT_SET | IJS_DPI_SET)struct _IjsServerCtx { int fd_from; int child_pid; IjsSendChan send_chan; IjsRecvChan recv_chan; int version; /* callbacks */ IjsBeginJobCb *begin_cb; void *begin_cb_data; IjsEndJobCb *end_cb; void *end_cb_data; IjsQueryStatusCb *status_cb; void *status_cb_data; IjsListParamsCb *list_cb; void *list_cb_data; IjsEnumParamCb *enum_cb; void *enum_cb_data; IjsSetParamCb *set_cb; void *set_cb_data; IjsGetParamCb *get_cb; void *get_cb_data; ijs_bool in_job; IjsJobId job_id; IjsPageHeader *ph; /* This should be IjsFieldsSet, but David Suffield reports that this causes problems when compiling with g++. */ int fields_set; ijs_bool in_page; char *buf; int buf_size; int buf_ix; char *overflow_buf; int overflow_buf_size; int overflow_buf_ix;};static intijs_server_dummy_begin_cb (void *begin_cb_data, IjsServerCtx *ctx, IjsJobId job_id){ return 0;}static intijs_server_dummy_end_cb (void *end_cb_data, IjsServerCtx *ctx, IjsJobId job_id){ return 0;}IjsServerCtx *ijs_server_init (void){ ijs_bool ok = TRUE; char helo_buf[8]; char resp_buf[8]; int nbytes; IjsServerCtx *ctx = (IjsServerCtx *)malloc (sizeof(IjsServerCtx)); int fd_from, fd_to; memcpy (resp_buf, IJS_RESP_STR, sizeof(resp_buf)); fd_from = 0; fd_to = 1;#ifdef _MSC_VER _setmode(fd_from, _O_BINARY); _setmode(fd_to, _O_BINARY);#endif#ifdef VERBOSE fprintf (stderr, "fd_from = %d, fd_to = %d\n", fd_from, fd_to);#endif ijs_recv_init (&ctx->recv_chan, fd_from); ijs_send_init (&ctx->send_chan, fd_to); nbytes = read (ctx->recv_chan.fd, helo_buf, sizeof(helo_buf)); if (nbytes != sizeof(helo_buf)) ok = FALSE; if (ok) nbytes = write (ctx->send_chan.fd, resp_buf, sizeof(resp_buf)); if (nbytes != sizeof(resp_buf)) ok = FALSE; ctx->in_job = FALSE; ctx->job_id = -1; ctx->ph = NULL; ctx->in_page = FALSE; ctx->buf = NULL; ctx->overflow_buf = NULL; ctx->begin_cb = ijs_server_dummy_begin_cb; ctx->end_cb = ijs_server_dummy_end_cb; if (ok) return ctx; else { ijs_server_done (ctx); return NULL; }}intijs_server_install_begin_cb (IjsServerCtx *ctx, IjsBeginJobCb *begin_cb, void *begin_cb_data){ ctx->begin_cb = begin_cb; ctx->begin_cb_data = begin_cb_data; return 0;}intijs_server_install_end_cb (IjsServerCtx *ctx, IjsEndJobCb *end_cb, void *end_cb_data){ ctx->end_cb = end_cb; ctx->end_cb_data = end_cb_data; return 0;}intijs_server_install_status_cb (IjsServerCtx *ctx, IjsQueryStatusCb *status_cb, void *status_cb_data){ ctx->status_cb = status_cb; ctx->status_cb_data = status_cb_data; return 0;}intijs_server_install_list_cb (IjsServerCtx *ctx, IjsListParamsCb *list_cb, void *list_cb_data){ ctx->list_cb = list_cb; ctx->list_cb_data = list_cb_data; return 0;}intijs_server_install_enum_cb (IjsServerCtx *ctx, IjsEnumParamCb *enum_cb, void *enum_cb_data){ ctx->enum_cb = enum_cb; ctx->enum_cb_data = enum_cb_data; return 0;}intijs_server_install_set_cb (IjsServerCtx *ctx, IjsSetParamCb *set_cb, void *set_cb_data){ ctx->set_cb = set_cb; ctx->set_cb_data = set_cb_data; return 0;}intijs_server_install_get_cb (IjsServerCtx *ctx, IjsGetParamCb *get_cb, void *get_cb_data){ ctx->get_cb = get_cb; ctx->get_cb_data = get_cb_data; return 0;}static intijs_server_ack (IjsServerCtx *ctx){ int status; status = ijs_send_begin (&ctx->send_chan, IJS_CMD_ACK); if (status < 0) return status; return ijs_send_buf (&ctx->send_chan);}voidijs_server_done (IjsServerCtx *ctx){ /* todo: close channels */ ijs_server_ack (ctx); free (ctx);}static intijs_server_nak (IjsServerCtx *ctx, int errorcode){ int status; status = ijs_send_begin (&ctx->send_chan, IJS_CMD_NAK); if (status < 0) return status; status = ijs_send_int (&ctx->send_chan, errorcode); if (status < 0) return status; return ijs_send_buf (&ctx->send_chan);}/* The return code is: 0 if ok, positive on normal exit, negative on error */typedef int (*ijs_server_proc) (IjsServerCtx *ctx);static intijs_server_proc_ack (IjsServerCtx *ctx){ /* servers should not get ack commands */ return IJS_EPROTO;}static intijs_server_proc_nak (IjsServerCtx *ctx){ /* servers should not get nak commands */ return IJS_EPROTO;}static intijs_server_proc_ping (IjsServerCtx *ctx){ int status; int version; status = ijs_recv_int (&ctx->recv_chan, &version); if (status < 0) return status; if (version > IJS_VERSION) version = IJS_VERSION; ctx->version = version;#ifdef VERBOSE fprintf (stderr, "ping version=%d\n", version);#endif status = ijs_send_begin (&ctx->send_chan, IJS_CMD_PONG); if (status < 0) return status; status = ijs_send_int (&ctx->send_chan, IJS_VERSION); if (status < 0) return status; return ijs_send_buf (&ctx->send_chan);}static intijs_server_proc_pong (IjsServerCtx *ctx){ /* servers should not get pong commands */ return IJS_EPROTO;}static intijs_server_proc_open (IjsServerCtx *ctx){ /* A server might allocate tables here. */ return ijs_server_ack (ctx);}static intijs_server_proc_close (IjsServerCtx *ctx){ /* A server might deallocate memory here. */ return ijs_server_ack (ctx);}static intijs_server_proc_begin_job (IjsServerCtx *ctx){ int code; IjsJobId job_id; code = ijs_recv_int (&ctx->recv_chan, &job_id); if (code < 0) return code; if (ctx->in_job) return ijs_server_nak (ctx, IJS_ETOOMANYJOBS); ctx->in_job = TRUE; ctx->job_id = job_id; return ijs_server_ack (ctx);}static intijs_server_proc_end_job (IjsServerCtx *ctx){ int code; IjsJobId job_id; code = ijs_recv_int (&ctx->recv_chan, &job_id); if (code < 0) return code; if (!ctx->in_job || job_id != ctx->job_id) return ijs_server_nak (ctx, IJS_EJOBID); ctx->in_job = FALSE; return ijs_server_ack (ctx);}static intijs_server_proc_cancel_job (IjsServerCtx *ctx){ int code; IjsJobId job_id; code = ijs_recv_int (&ctx->recv_chan, &job_id); if (code < 0) return code; if (!ctx->in_job || job_id != ctx->job_id) return ijs_server_nak (ctx, IJS_EJOBID); /* todo: call cancel callback here */ ctx->in_job = FALSE; return ijs_server_ack (ctx);}static intijs_server_proc_query_status (IjsServerCtx *ctx){ int code; IjsJobId job_id; code = ijs_recv_int (&ctx->recv_chan, &job_id); if (code < 0) return code; if (!ctx->in_job || ctx->job_id != job_id) return ijs_server_nak (ctx, IJS_EJOBID); code = ctx->status_cb (ctx->list_cb_data, ctx, job_id); if (code < 0) return ijs_server_nak (ctx, code); else { int status; status = ijs_send_begin (&ctx->send_chan, IJS_CMD_ACK); if (status < 0) return status; status = ijs_send_int (&ctx->send_chan, code); if (status < 0) return status; return ijs_send_buf (&ctx->send_chan); }}static intijs_server_proc_list_params (IjsServerCtx *ctx){ int code; char buf[4096]; IjsJobId job_id; code = ijs_recv_int (&ctx->recv_chan, &job_id); if (code < 0) return code; if (!ctx->in_job || ctx->job_id != job_id) return ijs_server_nak (ctx, IJS_EJOBID); code = ctx->list_cb (ctx->list_cb_data, ctx, job_id, buf, sizeof(buf)); if (code < 0) return ijs_server_nak (ctx, code); else { int status; status = ijs_send_begin (&ctx->send_chan, IJS_CMD_ACK); if (status < 0) return status; status = ijs_send_block (&ctx->send_chan, buf, code); if (status < 0) return status; return ijs_send_buf (&ctx->send_chan); }}static intijs_server_proc_enum_param (IjsServerCtx *ctx){ const char *key; int key_size; int code; char buf[4096]; IjsJobId job_id; code = ijs_recv_int (&ctx->recv_chan, &job_id); if (code < 0) return code; if (!ctx->in_job || ctx->job_id != job_id) return ijs_server_nak (ctx, IJS_EJOBID); key = ctx->recv_chan.buf + ctx->recv_chan.buf_idx; key_size = ctx->recv_chan.buf_size - ctx->recv_chan.buf_idx; if (key_size == 0 || key[key_size - 1]) return IJS_ESYNTAX;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -