1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
/*
* 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;
}
|