bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / native / common / jk_sockbuf.c
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /***************************************************************************
19  * Description: Simple buffer object to handle buffered socket IO          *
20  * Author:      Gal Shachor <shachor@il.ibm.com>                           *
21  * Version:     $Revision: 466585 $                                           *
22  ***************************************************************************/
23
24 #include "jk_global.h"
25 #include "jk_sockbuf.h"
26
27 static int fill_buffer(jk_sockbuf_t *sb);
28
29 int jk_sb_open(jk_sockbuf_t *sb, jk_sock_t sd)
30 {
31     if (sb && sd >= 0) {
32         sb->end = 0;
33         sb->start = 0;
34         sb->sd = sd;
35         return JK_TRUE;
36     }
37
38     return JK_FALSE;
39 }
40
41 int jk_sb_write(jk_sockbuf_t *sb, const void *buf, unsigned sz)
42 {
43     if (sb && buf && sz) {
44         if ((SOCKBUF_SIZE - sb->end) >= sz) {
45             memcpy(sb->buf + sb->end, buf, sz);
46             sb->end += sz;
47         }
48         else {
49             if (!jk_sb_flush(sb)) {
50                 return JK_FALSE;
51             }
52             if (sz > SOCKBUF_SIZE) {
53                 return (send(sb->sd, (char *)buf, sz, 0) == (int)sz);
54             }
55
56             memcpy(sb->buf + sb->end, buf, sz);
57             sb->end += sz;
58         }
59
60         return JK_TRUE;
61     }
62
63     return JK_FALSE;
64 }
65
66 int jk_sb_flush(jk_sockbuf_t *sb)
67 {
68     if (sb) {
69         int save_out = sb->end;
70         sb->end = sb->start = 0;
71         if (save_out) {
72             return send(sb->sd, sb->buf, save_out, 0) == save_out;
73         }
74         return JK_TRUE;
75     }
76
77     return JK_FALSE;
78 }
79
80
81 int jk_sb_read(jk_sockbuf_t *sb, char **buf, unsigned sz, unsigned *ac)
82 {
83     if (sb && buf && ac) {
84         unsigned avail;
85
86         *ac = 0;
87         *buf = NULL;
88
89         if (sb->end == sb->start) {
90             sb->end = sb->start = 0;
91             if (fill_buffer(sb) < 0) {
92                 return JK_FALSE;
93             }
94         }
95
96         *buf = sb->buf + sb->start;
97         avail = sb->end - sb->start;
98         if (avail > sz) {
99             *ac = sz;
100         }
101         else {
102             *ac = avail;
103         }
104         sb->start += *ac;
105
106         return JK_TRUE;
107     }
108
109     return JK_FALSE;
110 }
111
112 int jk_sb_gets(jk_sockbuf_t *sb, char **ps)
113 {
114     int ret;
115     if (sb) {
116         while (1) {
117             unsigned i;
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';
122                     }
123                     else {
124                         sb->buf[i] = '\0';
125                     }
126                     *ps = sb->buf + sb->start;
127                     sb->start = (i + 1);
128                     return JK_TRUE;
129                 }
130             }
131             if ((ret = fill_buffer(sb)) < 0) {
132                 return JK_FALSE;
133             }
134             else if (ret == 0) {
135                 *ps = sb->buf + sb->start;
136                 if ((SOCKBUF_SIZE - sb->end) > 0) {
137                     sb->buf[sb->end] = '\0';
138                 }
139                 else {
140                     sb->buf[sb->end - 1] = '\0';
141                 }
142                 return JK_TRUE;
143             }
144         }
145     }
146
147     return JK_FALSE;
148 }
149
150 /*
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
155  * -1.  
156  */
157 static int fill_buffer(jk_sockbuf_t *sb)
158 {
159     int ret;
160
161     /*
162      * First move the current data to the beginning of the buffer
163      */
164     if (sb->start < sb->end) {
165         if (sb->start > 0) {
166             unsigned to_copy = sb->end - sb->start;
167             memmove(sb->buf, sb->buf + sb->start, to_copy);
168             sb->start = 0;
169             sb->end = to_copy;
170         }
171     }
172     else {
173         sb->start = sb->end = 0;
174     }
175
176     /*
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.  
179      */
180     if ((SOCKBUF_SIZE - sb->end) > 0) {
181         /*
182          * Now, read more data
183          */
184         ret = recv(sb->sd, sb->buf + sb->end, SOCKBUF_SIZE - sb->end, 0);
185
186         /* 0 is EOF/SHUTDOWN, -1 is SOCK_ERROR */
187         if (ret <= 0) {
188             return ret;
189         }
190
191         sb->end += ret;
192     }
193
194     return 1;
195 }