Updated net skeleton code

This commit is contained in:
Sergey Lyubka committed 2014-04-09 11:32:23 +01:00
1 parent 3a60c5764d
commit 6bb49c780c
1 file changed
+14 -17
+14 -17
View File
@@ -151,14 +151,14 @@ union socket_address {
// IO buffers interface // IO buffers interface
struct iobuf { struct iobuf {
char *buf; char *buf;
int len; size_t len;
int size; size_t size;
}; };
void iobuf_init(struct iobuf *, int initial_size); void iobuf_init(struct iobuf *, size_t initial_size);
void iobuf_free(struct iobuf *); void iobuf_free(struct iobuf *);
int iobuf_append(struct iobuf *, const void *data, int data_size); size_t iobuf_append(struct iobuf *, const void *data, size_t data_size);
void iobuf_remove(struct iobuf *, int data_size); void iobuf_remove(struct iobuf *, size_t data_size);
// Net skeleton interface // Net skeleton interface
// Events. Meaning of event parameter (evp) is given in the comment. // Events. Meaning of event parameter (evp) is given in the comment.
@@ -270,7 +270,7 @@ int ns_hexdump(const void *buf, int len, char *dst, int dst_len);
#define IOBUF_RESIZE_MULTIPLIER 2.0 #define IOBUF_RESIZE_MULTIPLIER 2.0
#endif #endif
void iobuf_init(struct iobuf *iobuf, int size) { void iobuf_init(struct iobuf *iobuf, size_t size) {
iobuf->len = iobuf->size = 0; iobuf->len = iobuf->size = 0;
iobuf->buf = NULL; iobuf->buf = NULL;
@@ -286,24 +286,21 @@ void iobuf_free(struct iobuf *iobuf) {
} }
} }
int iobuf_append(struct iobuf *io, const void *buf, int len) { size_t iobuf_append(struct iobuf *io, const void *buf, size_t len) {
static const double mult = IOBUF_RESIZE_MULTIPLIER;
char *p = NULL; char *p = NULL;
int new_len = 0; size_t new_len = io->len + len, new_size = new_len * IOBUF_RESIZE_MULTIPLIER;
assert(io->len >= 0);
assert(io->len <= io->size); assert(io->len <= io->size);
if (len <= 0) { if (len <= 0) {
} else if ((new_len = io->len + len) < io->size) { } else if (new_len < io->size) {
memcpy(io->buf + io->len, buf, len); memcpy(io->buf + io->len, buf, len);
io->len = new_len; io->len = new_len;
} else if ((p = (char *) } else if ((p = (char *) NS_REALLOC(io->buf, new_size)) != NULL) {
NS_REALLOC(io->buf, (int) (new_len * mult))) != NULL) {
io->buf = p; io->buf = p;
memcpy(io->buf + io->len, buf, len); memcpy(io->buf + io->len, buf, len);
io->len = new_len; io->len = new_len;
io->size = (int) (new_len * mult); io->size = new_size;
} else { } else {
len = 0; len = 0;
} }
@@ -311,8 +308,8 @@ int iobuf_append(struct iobuf *io, const void *buf, int len) {
return len; return len;
} }
void iobuf_remove(struct iobuf *io, int n) { void iobuf_remove(struct iobuf *io, size_t n) {
if (n >= 0 && n <= io->len) { if (n > 0 && n <= io->len) {
memmove(io->buf, io->buf + n, io->len - n); memmove(io->buf, io->buf + n, io->len - n);
io->len -= n; io->len -= n;
} }
@@ -597,7 +594,7 @@ static struct ns_connection *accept_conn(struct ns_server *server) {
ns_add_conn(server, c); ns_add_conn(server, c);
ns_call(c, NS_ACCEPT, &sa); ns_call(c, NS_ACCEPT, &sa);
DBG(("%p %d %p %p %d", c, c->sock, c->ssl, server->ssl_ctx, c->flags)); DBG(("%p %d %p %p", c, c->sock, c->ssl, server->ssl_ctx));
} }
return c; return c;