2
* Copyright 1999-2004 The Apache Software Foundation
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
/***************************************************************************
18
* Description: Simple buffer object to handle buffered socket IO *
19
* Author: Gal Shachor <shachor@il.ibm.com> *
20
* Version: $Revision: 1.10 $ *
21
***************************************************************************/
23
#include "jk_global.h"
24
#include "jk_sockbuf.h"
26
static int fill_buffer(jk_sockbuf_t *sb);
28
int jk_sb_open(jk_sockbuf_t *sb, int sd)
40
int jk_sb_write(jk_sockbuf_t *sb, const void *buf, unsigned sz)
42
if (sb && buf && sz) {
43
if ((SOCKBUF_SIZE - sb->end) >= sz) {
44
memcpy(sb->buf + sb->end, buf, sz);
48
if (!jk_sb_flush(sb)) {
51
if (sz > SOCKBUF_SIZE) {
52
return (send(sb->sd, (char *)buf, sz, 0) == (int)sz);
55
memcpy(sb->buf + sb->end, buf, sz);
65
int jk_sb_flush(jk_sockbuf_t *sb)
68
int save_out = sb->end;
69
sb->end = sb->start = 0;
71
return send(sb->sd, sb->buf, save_out, 0) == save_out;
80
int jk_sb_read(jk_sockbuf_t *sb, char **buf, unsigned sz, unsigned *ac)
82
if (sb && buf && ac) {
88
if (sb->end == sb->start) {
89
sb->end = sb->start = 0;
90
if (fill_buffer(sb) < 0) {
95
*buf = sb->buf + sb->start;
96
avail = sb->end - sb->start;
111
int jk_sb_gets(jk_sockbuf_t *sb, char **ps)
117
for (i = sb->start; i < sb->end; i++) {
118
if (JK_LF == sb->buf[i]) {
119
if (i > sb->start && JK_CR == sb->buf[i - 1]) {
120
sb->buf[i - 1] = '\0';
125
*ps = sb->buf + sb->start;
130
if ((ret = fill_buffer(sb)) < 0) {
134
*ps = sb->buf + sb->start;
135
if ((SOCKBUF_SIZE - sb->end) > 0) {
136
sb->buf[sb->end] = '\0';
139
sb->buf[sb->end - 1] = '\0';
150
* Read data from the socket into the associated buffer, and update the
151
* start and end indices. May move the data currently in the buffer. If
152
* new data is read into the buffer (or if it is already full), returns 1.
153
* If EOF is received on the socket, returns 0. In case of error returns
156
static int fill_buffer(jk_sockbuf_t *sb)
161
* First move the current data to the beginning of the buffer
163
if (sb->start < sb->end) {
165
unsigned to_copy = sb->end - sb->start;
166
memmove(sb->buf, sb->buf + sb->start, to_copy);
172
sb->start = sb->end = 0;
176
* In the unlikely case where the buffer is already full, we won't be
177
* reading anything and we'd be calling recv with a 0 count.
179
if ((SOCKBUF_SIZE - sb->end) > 0) {
181
* Now, read more data
183
ret = recv(sb->sd, sb->buf + sb->end, SOCKBUF_SIZE - sb->end, 0);
185
/* 0 is EOF/SHUTDOWN, -1 is SOCK_ERROR */