正在显示
1 个修改的文件
包含
235 行增加
和
0 行删除
src/server/spserver/spwin32buffer.hpp
0 → 100644
1 | +/* | ||
2 | + * This file is adapted from libevent. | ||
3 | + */ | ||
4 | + | ||
5 | +/* | ||
6 | + * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> | ||
7 | + * All rights reserved. | ||
8 | + * | ||
9 | + * Redistribution and use in source and binary forms, with or without | ||
10 | + * modification, are permitted provided that the following conditions | ||
11 | + * are met: | ||
12 | + * 1. Redistributions of source code must retain the above copyright | ||
13 | + * notice, this list of conditions and the following disclaimer. | ||
14 | + * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | + * notice, this list of conditions and the following disclaimer in the | ||
16 | + * documentation and/or other materials provided with the distribution. | ||
17 | + * 3. The name of the author may not be used to endorse or promote products | ||
18 | + * derived from this software without specific prior written permission. | ||
19 | + * | ||
20 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
21 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
22 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
23 | + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
24 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
25 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
26 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
27 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
28 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
29 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
30 | + */ | ||
31 | + | ||
32 | +#ifndef __spwin32buffer_hpp__ | ||
33 | +#define __spwin32buffer_hpp__ | ||
34 | + | ||
35 | +#include <sys/types.h> | ||
36 | +#include <stdarg.h> | ||
37 | + | ||
38 | +#define sp_evbuffer_new spwin32buffer_new | ||
39 | +#define sp_evbuffer_free spwin32buffer_free | ||
40 | +#define sp_evbuffer_add spwin32buffer_add | ||
41 | +#define sp_evbuffer_drain spwin32buffer_drain | ||
42 | +#define sp_evbuffer_expand spwin32buffer_expand | ||
43 | +#define sp_evbuffer_readline spwin32buffer_readline | ||
44 | +#define sp_evbuffer_remove spwin32buffer_remove | ||
45 | +#define sp_evbuffer_add_vprintf spwin32buffer_add_vprintf | ||
46 | + | ||
47 | +typedef struct spwin32buffer sp_buffer_t; | ||
48 | + | ||
49 | +#ifdef __cplusplus | ||
50 | +extern "C" { | ||
51 | +#endif | ||
52 | + | ||
53 | +typedef unsigned char u_char; | ||
54 | + | ||
55 | +/* These functions deal with buffering input and output */ | ||
56 | + | ||
57 | +struct spwin32buffer { | ||
58 | + u_char *buffer; | ||
59 | + u_char *orig_buffer; | ||
60 | + | ||
61 | + size_t misalign; | ||
62 | + size_t totallen; | ||
63 | + size_t off; | ||
64 | + | ||
65 | + void (*cb)(struct spwin32buffer *, size_t, size_t, void *); | ||
66 | + void *cbarg; | ||
67 | +}; | ||
68 | + | ||
69 | +#define EVBUFFER_LENGTH(x) (x)->off | ||
70 | +#define EVBUFFER_DATA(x) (x)->buffer | ||
71 | +#define EVBUFFER_INPUT(x) (x)->input | ||
72 | +#define EVBUFFER_OUTPUT(x) (x)->output | ||
73 | + | ||
74 | +/** | ||
75 | + Allocate storage for a new spwin32buffer. | ||
76 | + | ||
77 | + @return a pointer to a newly allocated spwin32buffer struct, or NULL if an error | ||
78 | + occurred | ||
79 | + */ | ||
80 | +struct spwin32buffer *spwin32buffer_new(void); | ||
81 | + | ||
82 | + | ||
83 | +/** | ||
84 | + Deallocate storage for an spwin32buffer. | ||
85 | + | ||
86 | + @param pointer to the spwin32buffer to be freed | ||
87 | + */ | ||
88 | +void spwin32buffer_free(struct spwin32buffer *); | ||
89 | + | ||
90 | + | ||
91 | +/** | ||
92 | + Expands the available space in an event buffer. | ||
93 | + | ||
94 | + Expands the available space in the event buffer to at least datlen | ||
95 | + | ||
96 | + @param buf the event buffer to be expanded | ||
97 | + @param datlen the new minimum length requirement | ||
98 | + @return 0 if successful, or -1 if an error occurred | ||
99 | +*/ | ||
100 | +int spwin32buffer_expand(struct spwin32buffer *, size_t); | ||
101 | + | ||
102 | + | ||
103 | +/** | ||
104 | + Append data to the end of an spwin32buffer. | ||
105 | + | ||
106 | + @param buf the event buffer to be appended to | ||
107 | + @param data pointer to the beginning of the data buffer | ||
108 | + @param datlen the number of bytes to be copied from the data buffer | ||
109 | + */ | ||
110 | +int spwin32buffer_add(struct spwin32buffer *, const void *, size_t); | ||
111 | + | ||
112 | + | ||
113 | + | ||
114 | +/** | ||
115 | + Read data from an event buffer and drain the bytes read. | ||
116 | + | ||
117 | + @param buf the event buffer to be read from | ||
118 | + @param data the destination buffer to store the result | ||
119 | + @param datlen the maximum size of the destination buffer | ||
120 | + @return the number of bytes read | ||
121 | + */ | ||
122 | +int spwin32buffer_remove(struct spwin32buffer *, void *, size_t); | ||
123 | + | ||
124 | + | ||
125 | +/** | ||
126 | + * Read a single line from an event buffer. | ||
127 | + * | ||
128 | + * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'. | ||
129 | + * The returned buffer needs to be freed by the caller. | ||
130 | + * | ||
131 | + * @param buffer the spwin32buffer to read from | ||
132 | + * @return pointer to a single line, or NULL if an error occurred | ||
133 | + */ | ||
134 | +char *spwin32buffer_readline(struct spwin32buffer *); | ||
135 | + | ||
136 | + | ||
137 | +/** | ||
138 | + Move data from one spwin32buffer into another spwin32buffer. | ||
139 | + | ||
140 | + This is a destructive add. The data from one buffer moves into | ||
141 | + the other buffer. The destination buffer is expanded as needed. | ||
142 | + | ||
143 | + @param outbuf the output buffer | ||
144 | + @param inbuf the input buffer | ||
145 | + @return 0 if successful, or -1 if an error occurred | ||
146 | + */ | ||
147 | +int spwin32buffer_add_buffer(struct spwin32buffer *, struct spwin32buffer *); | ||
148 | + | ||
149 | + | ||
150 | +/** | ||
151 | + Append a formatted string to the end of an spwin32buffer. | ||
152 | + | ||
153 | + @param buf the spwin32buffer that will be appended to | ||
154 | + @param fmt a format string | ||
155 | + @param ... arguments that will be passed to printf(3) | ||
156 | + @return 0 if successful, or -1 if an error occurred | ||
157 | + */ | ||
158 | +int spwin32buffer_add_printf(struct spwin32buffer *, const char *fmt, ...) | ||
159 | +#ifdef __GNUC__ | ||
160 | + __attribute__((format(printf, 2, 3))) | ||
161 | +#endif | ||
162 | +; | ||
163 | + | ||
164 | + | ||
165 | +/** | ||
166 | + Append a va_list formatted string to the end of an spwin32buffer. | ||
167 | + | ||
168 | + @param buf the spwin32buffer that will be appended to | ||
169 | + @param fmt a format string | ||
170 | + @param ap a varargs va_list argument array that will be passed to vprintf(3) | ||
171 | + @return 0 if successful, or -1 if an error occurred | ||
172 | + */ | ||
173 | +int spwin32buffer_add_vprintf(struct spwin32buffer *, const char *fmt, va_list ap); | ||
174 | + | ||
175 | + | ||
176 | +/** | ||
177 | + Remove a specified number of bytes data from the beginning of an spwin32buffer. | ||
178 | + | ||
179 | + @param buf the spwin32buffer to be drained | ||
180 | + @param len the number of bytes to drain from the beginning of the buffer | ||
181 | + @return 0 if successful, or -1 if an error occurred | ||
182 | + */ | ||
183 | +void spwin32buffer_drain(struct spwin32buffer *, size_t); | ||
184 | + | ||
185 | + | ||
186 | +/** | ||
187 | + Write the contents of an spwin32buffer to a file descriptor. | ||
188 | + | ||
189 | + The spwin32buffer will be drained after the bytes have been successfully written. | ||
190 | + | ||
191 | + @param buffer the spwin32buffer to be written and drained | ||
192 | + @param fd the file descriptor to be written to | ||
193 | + @return the number of bytes written, or -1 if an error occurred | ||
194 | + @see spwin32buffer_read() | ||
195 | + */ | ||
196 | +int spwin32buffer_write(struct spwin32buffer *, int); | ||
197 | + | ||
198 | + | ||
199 | +/** | ||
200 | + Read from a file descriptor and store the result in an spwin32buffer. | ||
201 | + | ||
202 | + @param buf the spwin32buffer to store the result | ||
203 | + @param fd the file descriptor to read from | ||
204 | + @param howmuch the number of bytes to be read | ||
205 | + @return the number of bytes read, or -1 if an error occurred | ||
206 | + @see spwin32buffer_write() | ||
207 | + */ | ||
208 | +int spwin32buffer_read(struct spwin32buffer *, int, int); | ||
209 | + | ||
210 | + | ||
211 | +/** | ||
212 | + Find a string within an spwin32buffer. | ||
213 | + | ||
214 | + @param buffer the spwin32buffer to be searched | ||
215 | + @param what the string to be searched for | ||
216 | + @param len the length of the search string | ||
217 | + @return a pointer to the beginning of the search string, or NULL if the search failed. | ||
218 | + */ | ||
219 | +u_char *spwin32buffer_find(struct spwin32buffer *, const u_char *, size_t); | ||
220 | + | ||
221 | +/** | ||
222 | + Set a callback to invoke when the spwin32buffer is modified. | ||
223 | + | ||
224 | + @param buffer the spwin32buffer to be monitored | ||
225 | + @param cb the callback function to invoke when the spwin32buffer is modified | ||
226 | + @param cbarg an argument to be provided to the callback function | ||
227 | + */ | ||
228 | +void spwin32buffer_setcb(struct spwin32buffer *, void (*)(struct spwin32buffer *, size_t, size_t, void *), void *); | ||
229 | + | ||
230 | +#ifdef __cplusplus | ||
231 | +} | ||
232 | +#endif | ||
233 | + | ||
234 | +#endif | ||
235 | + |
请
注册
或
登录
后发表评论