2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 /***************************************************************************
19 * Description: Simple buffer object to handle buffered socket IO *
20 * Author: Gal Shachor <shachor@il.ibm.com> *
21 * Version: $Revision: 466585 $ *
22 ***************************************************************************/
24 #include "jk_global.h"
25 #include "jk_sockbuf.h"
27 static int fill_buffer(jk_sockbuf_t *sb);
29 int jk_sb_open(jk_sockbuf_t *sb, jk_sock_t sd)
41 int jk_sb_write(jk_sockbuf_t *sb, const void *buf, unsigned sz)
43 if (sb && buf && sz) {
44 if ((SOCKBUF_SIZE - sb->end) >= sz) {
45 memcpy(sb->buf + sb->end, buf, sz);
49 if (!jk_sb_flush(sb)) {
52 if (sz > SOCKBUF_SIZE) {
53 return (send(sb->sd, (char *)buf, sz, 0) == (int)sz);
56 memcpy(sb->buf + sb->end, buf, sz);
66 int jk_sb_flush(jk_sockbuf_t *sb)
69 int save_out = sb->end;
70 sb->end = sb->start = 0;
72 return send(sb->sd, sb->buf, save_out, 0) == save_out;
81 int jk_sb_read(jk_sockbuf_t *sb, char **buf, unsigned sz, unsigned *ac)
83 if (sb && buf && ac) {
89 if (sb->end == sb->start) {
90 sb->end = sb->start = 0;
91 if (fill_buffer(sb) < 0) {
96 *buf = sb->buf + sb->start;
97 avail = sb->end - sb->start;
112 int jk_sb_gets(jk_sockbuf_t *sb, char **ps)
118 for (i = sb->start; i < sb->end; i++) {
119 if (JK_LF == sb->buf[i]) {
120 if (i > sb->start && JK_CR == sb->buf[i - 1]) {
121 sb->buf[i - 1] = '\0';
126 *ps = sb->buf + sb->start;
131 if ((ret = fill_buffer(sb)) < 0) {
135 *ps = sb->buf + sb->start;
136 if ((SOCKBUF_SIZE - sb->end) > 0) {
137 sb->buf[sb->end] = '\0';
140 sb->buf[sb->end - 1] = '\0';
151 * Read data from the socket into the associated buffer, and update the
152 * start and end indices. May move the data currently in the buffer. If
153 * new data is read into the buffer (or if it is already full), returns 1.
154 * If EOF is received on the socket, returns 0. In case of error returns
157 static int fill_buffer(jk_sockbuf_t *sb)
162 * First move the current data to the beginning of the buffer
164 if (sb->start < sb->end) {
166 unsigned to_copy = sb->end - sb->start;
167 memmove(sb->buf, sb->buf + sb->start, to_copy);
173 sb->start = sb->end = 0;
177 * In the unlikely case where the buffer is already full, we won't be
178 * reading anything and we'd be calling recv with a 0 count.
180 if ((SOCKBUF_SIZE - sb->end) > 0) {
182 * Now, read more data
184 ret = recv(sb->sd, sb->buf + sb->end, SOCKBUF_SIZE - sb->end, 0);
186 /* 0 is EOF/SHUTDOWN, -1 is SOCK_ERROR */