diff options
author | Michael Steinert <mike.steinert@gmail.com> | 2012-12-03 12:53:53 -0700 |
---|---|---|
committer | Alan Antonuk <alan.antonuk@gmail.com> | 2013-04-09 15:54:49 -0700 |
commit | 173e563d3fcd87a03a65a9444d284d71a121e5b4 (patch) | |
tree | c0fddf724598fd29ae52ab704b9dec1985daf221 /librabbitmq/amqp_tcp_socket.c | |
parent | 12e068b65110f1780379459b96c28a4f5662109e (diff) | |
download | rabbitmq-c-173e563d3fcd87a03a65a9444d284d71a121e5b4.tar.gz |
Start addressing review comments
Signed-off-by: Michael Steinert <mike.steinert@gmail.com>
Diffstat (limited to 'librabbitmq/amqp_tcp_socket.c')
-rw-r--r-- | librabbitmq/amqp_tcp_socket.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/librabbitmq/amqp_tcp_socket.c b/librabbitmq/amqp_tcp_socket.c new file mode 100644 index 0000000..84d8554 --- /dev/null +++ b/librabbitmq/amqp_tcp_socket.c @@ -0,0 +1,125 @@ +/* + * Copyright 2012 Michael Steinert + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "amqp_private.h" +#include "amqp_tcp_socket.h" +#include <stdio.h> +#include <stdlib.h> + +struct amqp_tcp_socket_t { + const struct amqp_socket_class_t *klass; + int sockfd; +}; + +static ssize_t +amqp_tcp_socket_writev(void *base, const struct iovec *iov, int iovcnt) +{ + struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base; + return amqp_os_socket_writev(self->sockfd, iov, iovcnt); +} + +static ssize_t +amqp_tcp_socket_send(void *base, const void *buf, size_t len, int flags) +{ + struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base; + return send(self->sockfd, buf, len, flags); +} + +static ssize_t +amqp_tcp_socket_recv(void *base, void *buf, size_t len, int flags) +{ + struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base; + return recv(self->sockfd, buf, len, flags); +} + +static int +amqp_tcp_socket_open(void *base, const char *host, int port) +{ + struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base; + self->sockfd = amqp_open_socket(host, port); + if (0 > self->sockfd) { + return -1; + } + return 0; +} + +static int +amqp_tcp_socket_close(void *base) +{ + struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base; + int status = -1; + if (self) { + status = amqp_os_socket_close(self->sockfd); + free(self); + } + return status; +} + +static int +amqp_tcp_socket_error(AMQP_UNUSED void *base) +{ + return amqp_os_socket_error(); +} + +static int +amqp_tcp_socket_get_sockfd(void *base) +{ + struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base; + return self->sockfd; +} + +static const struct amqp_socket_class_t amqp_tcp_socket_class = { + amqp_tcp_socket_writev, /* writev */ + amqp_tcp_socket_send, /* send */ + amqp_tcp_socket_recv, /* recv */ + amqp_tcp_socket_open, /* open */ + amqp_tcp_socket_close, /* close */ + amqp_tcp_socket_error, /* error */ + amqp_tcp_socket_get_sockfd /* get_sockfd */ +}; + +amqp_socket_t * +amqp_tcp_socket_new(void) +{ + struct amqp_tcp_socket_t *self = calloc(1, sizeof(*self)); + if (!self) { + return NULL; + } + self->klass = &amqp_tcp_socket_class; + self->sockfd = -1; + return (amqp_socket_t *)self; +} + +void +amqp_tcp_socket_set_sockfd(amqp_socket_t *base, int sockfd) +{ + struct amqp_tcp_socket_t *self; + if (base->klass != &amqp_tcp_socket_class) { + amqp_abort("<%p> is not of type amqp_tcp_socket_t", base); + } + self = (struct amqp_tcp_socket_t *)base; + self->sockfd = sockfd; +} |