2 * Copyright (c) 2001-2002 artofcode LLC.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 ijs_put_int (char *p, int val)
32 p[0] = (val >> 24) & 0xff;
33 p[1] = (val >> 16) & 0xff;
34 p[2] = (val >> 8) & 0xff;
39 ijs_send_init (IjsSendChan *ch, int fd)
46 ijs_send_int (IjsSendChan *ch, int val)
48 if ((ch->buf_size + 4) > (int)sizeof(ch->buf))
50 ijs_put_int (ch->buf + ch->buf_size, val);
56 ijs_send_begin (IjsSendChan *ch, IjsCommand cmd)
58 if (ch->buf_size != 0)
60 ijs_send_int (ch, cmd);
61 ch->buf_size += 4; /* leave room for size field */
66 ijs_send_block (IjsSendChan *ch, const char *buf, int len)
68 if ((ch->buf_size + len) > (int)sizeof(ch->buf))
70 memcpy (ch->buf + ch->buf_size, buf, len);
76 ijs_send_buf (IjsSendChan *ch)
80 ijs_put_int (ch->buf + 4, ch->buf_size);
81 status = write (ch->fd, ch->buf, ch->buf_size);
82 status = (status == ch->buf_size) ? 0 : IJS_EIO;
88 ijs_recv_init (IjsRecvChan *ch, int fd)
95 ijs_get_int (const char *p)
97 const unsigned char *up = (const unsigned char *)p;
98 return (up[0] << 24) | (up[1] << 16) | (up[2] << 8) | up[3];
101 /* This is a drop-in replacement for read(), but handles partial reads. */
103 ijs_recv_read (IjsRecvChan *ch, char *buf, int size)
110 nbytes = read (ch->fd, buf + ix, size - ix);
113 else if (nbytes == 0)
123 ijs_recv_buf (IjsRecvChan *ch)
128 nbytes = ijs_recv_read (ch, ch->buf, 8);
131 ch->buf_size = ijs_get_int (ch->buf + 4);
132 if (ch->buf_size < 8 || ch->buf_size > (int)sizeof(ch->buf))
134 data_size = ch->buf_size - 8;
137 nbytes = ijs_recv_read (ch, ch->buf + 8, data_size);
138 if (nbytes != data_size)
146 * ijs_recv_ack: Receive an acknowledgement.
149 * Receives an acknowledgement code (generally from server to client).
150 * Note that the distinction between local error and error generated
151 * by the remote peer is lost in this routine. If it is important to
152 * preserve this distinction, use ijs_recv_buf directly instead.
154 * Return value: 0 on success, negative on error.
157 ijs_recv_ack (IjsRecvChan *ch)
161 status = ijs_recv_buf (ch);
164 int cmd = ijs_get_int (ch->buf);
166 if (cmd == IJS_CMD_NAK)
168 if (ch->buf_size != 12)
171 status = ijs_get_int (ch->buf + 8);
178 ijs_recv_int (IjsRecvChan *ch, int *val)
180 if (ch->buf_idx + 4 > ch->buf_size)
182 *val = ijs_get_int (ch->buf + ch->buf_idx);
188 * Return value: data block size if nonnegative, or error code if
192 ijs_recv_block (IjsRecvChan *ch, char *buf, int buf_size)
194 int size = ch->buf_size - ch->buf_idx;
198 memcpy (buf, ch->buf + ch->buf_idx, size);
199 ch->buf_idx = ch->buf_size;