📄 ijs_server.c
字号:
#ifdef VERBOSE fprintf (stderr, "ijs_server_proc_enum_param, key_size = %d\n", key_size);#endif code = ctx->enum_cb (ctx->enum_cb_data, ctx, job_id, key, 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_strnlen (const char *s, int size){ int i; for (i = 0; i < size; i++) if (s[i] == 0) return i; return size;}static intijs_server_parse_int (const char *value, int size, int *result){ int num = 0; int i; int sign = 1; i = 0; if (i == size) return IJS_ESYNTAX; if (value[i] == '-') { sign = -1; i++; } if (i == size) return IJS_ESYNTAX; for (; i < size; i++) { char c = value[i]; if (c < '0' || c > '9') return IJS_ESYNTAX; num = (num * 10) + (c - '0'); } *result = num; return 0;}static intijs_server_parse_float (const char *value, int size, double *result){ char buf[256]; char *tail; if (size + 1 > sizeof(buf)) return IJS_EBUF; memcpy (buf, value, size); buf[size] = 0; *result = strtod (buf, &tail); if (tail == buf) return IJS_ESYNTAX; return 0;}static intijs_server_set_param (IjsServerCtx *ctx, IjsJobId job_id, const char *key, const char *value, int value_size){ int code;#ifdef VERBOSE fprintf (stderr, "set_param %s = ", key); fwrite (value, 1, value_size, stderr); fputs ("\n", stderr);#endif if (!strcmp (key, "NumChan")) { code = ijs_server_parse_int (value, value_size, &ctx->ph->n_chan); if (code == 0) ctx->fields_set |= IJS_N_CHAN_SET; return code; } else if (!strcmp (key, "BitsPerSample")) { code = ijs_server_parse_int (value, value_size, &ctx->ph->bps); if (code == 0) ctx->fields_set |= IJS_BPS_SET; return code; } else if (!strcmp (key, "ColorSpace")) { int size = value_size; if (size > (int)sizeof(ctx->ph->cs) - 1) size = sizeof(ctx->ph->cs) - 1; memcpy (ctx->ph->cs, value, size); ctx->ph->cs[size] = 0; ctx->fields_set |= IJS_CS_SET; return 0; } else if (!strcmp (key, "Width")) { code = ijs_server_parse_int (value, value_size, &ctx->ph->width); if (code == 0) ctx->fields_set |= IJS_WIDTH_SET; return code; } else if (!strcmp (key, "Height")) { code = ijs_server_parse_int (value, value_size, &ctx->ph->height); if (code == 0) ctx->fields_set |= IJS_HEIGHT_SET; return code; } else if (!strcmp (key, "Dpi")) { int x_ix; for (x_ix = 0; x_ix < value_size; x_ix++) if (value[x_ix] == 'x') break; if (x_ix == value_size) return IJS_ESYNTAX; code = ijs_server_parse_float (value, x_ix, &ctx->ph->xres); if (code < 0) return code; code = ijs_server_parse_float (value + x_ix + 1, value_size - (x_ix + 1), &ctx->ph->yres); if (code < 0) return code; ctx->fields_set |= IJS_DPI_SET; return 0; } else { return ctx->set_cb (ctx->set_cb_data, ctx, job_id, key, value, value_size); }}static intijs_server_proc_set_param (IjsServerCtx *ctx){ const char *key, *value; int key_size, value_size; IjsJobId job_id; int param_size; int code; 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 = ijs_recv_int (&ctx->recv_chan, ¶m_size); if (code < 0) return code; if (param_size != ctx->recv_chan.buf_size - ctx->recv_chan.buf_idx) return IJS_EPROTO; key = ctx->recv_chan.buf + ctx->recv_chan.buf_idx; key_size = ijs_strnlen (key, ctx->recv_chan.buf_size); if (key_size == param_size) return IJS_EPROTO; value = key + key_size + 1; value_size = param_size - (key_size + 1); code = ijs_server_set_param (ctx, job_id, key, value, value_size); if (code) return ijs_server_nak (ctx, code); else return ijs_server_ack (ctx);}static intijs_server_get_param (IjsServerCtx *ctx, IjsJobId job_id, const char *key, char *value, int value_size){#ifdef VERBOSE fprintf (stderr, "ijs_server_get_param %s\n", key);#endif return ctx->get_cb (ctx->get_cb_data, ctx, job_id, key, value, value_size);}static intijs_server_proc_get_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;#ifdef VERBOSE fprintf (stderr, "ijs_server_proc_get_param, key_size = %d\n", key_size);#endif code = ijs_server_get_param (ctx, job_id, key, 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_begin_page (IjsServerCtx *ctx){ IjsPageHeader *ph = ctx->ph; int status = 0; if (ph == NULL) status = IJS_EPROTO; if ((ctx->fields_set & IJS_FIELDS_REQUIRED) != IJS_FIELDS_REQUIRED) status = IJS_EPROTO;#ifdef VERBOSE fprintf (stderr, "begin page %d %d %d %d %d\n", ph->n_chan, ph->bps, ph->cs, ph->width, ph->height);#endif if (!status) { ctx->in_page = TRUE; return ijs_server_ack (ctx); } else return ijs_server_nak (ctx, status);}static intijs_server_read_data (IjsServerCtx *ctx, char *buf, int size){ int status; status = ijs_recv_read (&ctx->recv_chan, buf, size); return (status == size) ? 0 : IJS_EIO;}static intijs_server_proc_send_data_block (IjsServerCtx *ctx){ int size; int status = 0; IjsJobId job_id; status = ijs_recv_int (&ctx->recv_chan, &job_id); if (status < 0) return status; if (!ctx->in_job || job_id != ctx->job_id) status = IJS_EJOBID; else if (ctx->buf == NULL) status = IJS_EPROTO; if (!status) status = ijs_recv_int (&ctx->recv_chan, &size);#ifdef VERBOSE fprintf (stderr, "status=%d, send data block id=%d, size=%d\n", status, job_id, size);#endif if (status) return ijs_server_nak (ctx, status); if (size <= ctx->buf_size - ctx->buf_ix) { status = ijs_server_read_data (ctx, ctx->buf + ctx->buf_ix, size); ctx->buf_ix += size; } else { ctx->overflow_buf_size = size - (ctx->buf_size - ctx->buf_ix); ctx->overflow_buf = (char *)malloc (ctx->overflow_buf_size); ctx->overflow_buf_ix = 0; status = ijs_server_read_data (ctx, ctx->buf + ctx->buf_ix, ctx->buf_size - ctx->buf_ix); ctx->buf_ix = ctx->buf_size; if (!status) { status = ijs_server_read_data (ctx, ctx->overflow_buf, ctx->overflow_buf_size); } } return ijs_server_ack (ctx);}static intijs_server_proc_end_page (IjsServerCtx *ctx){#ifdef VERBOSE fprintf (stderr, "end page\n");#endif return ijs_server_ack (ctx);}static intijs_server_proc_exit (IjsServerCtx *ctx){ return 1;}ijs_server_proc ijs_server_procs[] = { ijs_server_proc_ack, ijs_server_proc_nak, ijs_server_proc_ping, ijs_server_proc_pong, ijs_server_proc_open, ijs_server_proc_close, ijs_server_proc_begin_job, ijs_server_proc_end_job, ijs_server_proc_cancel_job, ijs_server_proc_query_status, ijs_server_proc_list_params, ijs_server_proc_enum_param, ijs_server_proc_set_param, ijs_server_proc_get_param, ijs_server_proc_begin_page, ijs_server_proc_send_data_block, ijs_server_proc_end_page, ijs_server_proc_exit};intijs_server_iter (IjsServerCtx *ctx){ int cmd_num; int status; status = ijs_recv_buf (&ctx->recv_chan); if (status < 0) return status; cmd_num = ijs_get_int (ctx->recv_chan.buf);#ifdef VERBOSE fprintf (stderr, "command %d, %d bytes\n", cmd_num, ctx->recv_chan.buf_size);#endif if (cmd_num < 0 || cmd_num >= (int)sizeof(ijs_server_procs) / sizeof(ijs_server_procs[0])) return -1; return ijs_server_procs[cmd_num] (ctx);}/** * ijs_server_get_page_header: Get the page header. * @ctx: The server context. * @ph: Where to store the page header. * * Return value: 0 on success, 1 on normal exit, negative on error. **/intijs_server_get_page_header (IjsServerCtx *ctx, IjsPageHeader *ph){ int status; ctx->ph = ph; ctx->in_page = FALSE; do { status = ijs_server_iter (ctx); } while (status == 0 && !ctx->in_page); ctx->ph = NULL; return status;}/** * ijs_server_get_data: Get data from client. * @ctx: The server context. * @buf: Buffer for data being read. * @size: Size of buf. * * Gets data from client. Data is stored in @buf or the * overflow_buf. * * Return value: Number of bytes read, -1 on end of page, or < 0 on * error. **/intijs_server_get_data (IjsServerCtx *ctx, char *buf, int size){ int buf_ix = 0; int status = 0;#ifdef VERBOSE fprintf (stderr, "ijs_server_get_data %d\n", size);#endif if (ctx->overflow_buf != NULL) { int n_bytes = ctx->overflow_buf_size - ctx->overflow_buf_ix; if (n_bytes > size) n_bytes = size; memcpy (buf, ctx->overflow_buf + ctx->overflow_buf_ix, n_bytes); ctx->overflow_buf_ix += n_bytes; buf_ix = n_bytes; if (ctx->overflow_buf_ix == ctx->overflow_buf_size) { free (ctx->overflow_buf); ctx->overflow_buf = NULL; ctx->overflow_buf_size = 0; ctx->overflow_buf_ix = 0; } } ctx->buf = buf; ctx->buf_size = size; ctx->buf_ix = buf_ix; while (!status && ctx->buf_ix < size) { status = ijs_server_iter (ctx); } ctx->buf = NULL; return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -