amqp_tcp_socket.c

filter: Statementstmt 40.2% 33 / 82 Branchbran 23.0% 6 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 36.1% 39 / 108 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« amqp_time.c ↑ Summary
1 /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
2 /*
3 * Copyright 2012-2013 Michael Steinert
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23  
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27  
28 #include "amqp_private.h"
29 #include "amqp_tcp_socket.h"
30  
31 #include <errno.h>
32 #ifdef _WIN32
33 # ifndef WIN32_LEAN_AND_MEAN
34 # define WIN32_LEAN_AND_MEAN
35 # endif
36 # include <WinSock2.h>
37 #else
38 # include <sys/socket.h>
39 # include <netinet/in.h>
40 # include <netinet/tcp.h>
41 #endif
42 #include <stdio.h>
43 #include <stdlib.h>
44  
45 struct amqp_tcp_socket_t {
46 const struct amqp_socket_class_t *klass;
47 int sockfd;
48 int internal_error;
49 int state;
50 };
51  
52  
53 static ssize_t
54 45 amqp_tcp_socket_send(void *base, const void *buf, size_t len, int flags)
55 {
56 45 struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base;
57 ssize_t res;
58 45 int flagz = 0;
59  
60 45 ▶ if (-1 == self->sockfd) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == self->sockfd) { 45 0 45
61 45 return AMQP_STATUS_SOCKET_CLOSED;
62 }
63  
64 #ifdef MSG_NOSIGNAL
65 0 flagz |= MSG_NOSIGNAL;
66 #endif
67  
68 #if defined(MSG_MORE)
69 0 ▶ if (flags & AMQP_SF_MORE) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & AMQP_SF_MORE) { 0 0 0
70 0 flagz |= MSG_MORE;
71 }
72 #elif defined(TCP_NOPUSH)
73 if (flags & AMQP_SF_MORE && !(self->state & AMQP_SF_MORE)) {
74 int one = 1;
75 res = setsockopt(self->sockfd, IPPROTO_TCP, TCP_NOPUSH, &one, sizeof(one));
76 if (0 != res) {
77 self->internal_error = res;
78 return AMQP_STATUS_SOCKET_ERROR;
79 }
80 self->state |= AMQP_SF_MORE;
81 } else if (!(flags & AMQP_SF_MORE) && self->state & AMQP_SF_MORE) {
82 int zero = 0;
83 res = setsockopt(self->sockfd, IPPROTO_TCP, TCP_NOPUSH, &zero, sizeof(&zero));
84 if (0 != res) {
85 self->internal_error = res;
86 res = AMQP_STATUS_SOCKET_ERROR;
87 } else {
88 self->state &= ~AMQP_SF_MORE;
89 }
90 }
91 #endif
92  
93 0 start:
94 #ifdef _WIN32
95 res = send(self->sockfd, buf, (int)len, flagz);
96 #else
97 0 res = send(self->sockfd, buf, len, flagz);
98 #endif
99  
100 0 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 0 0
101 0 self->internal_error = amqp_os_socket_error();
102 0 switch (self->internal_error) {
103 0 case EINTR:
104 0 goto start;
105 #ifdef _WIN32
106 case WSAEWOULDBLOCK:
107 #else
108 0 case EWOULDBLOCK:
109 #endif
110 #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
111 case EAGAIN:
112 #endif
113 0 res = AMQP_PRIVATE_STATUS_SOCKET_NEEDWRITE;
114 0 break;
115 0 default:
116 0 res = AMQP_STATUS_SOCKET_ERROR;
117 }
118 } else {
119 0 self->internal_error = 0;
120 }
121  
122 0 return res;
123 }
124  
125 static ssize_t
126 0 amqp_tcp_socket_recv(void *base, void *buf, size_t len, int flags)
127 {
128 0 struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base;
129 ssize_t ret;
130 0 ▶ if (-1 == self->sockfd) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == self->sockfd) { 0 0 0
131 0 return AMQP_STATUS_SOCKET_CLOSED;
132 }
133  
134 0 start:
135 #ifdef _WIN32
136 ret = recv(self->sockfd, buf, (int)len, flags);
137 #else
138 0 ret = recv(self->sockfd, buf, len, flags);
139 #endif
140  
141 0 ▶ if (0 > ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 > ret) { 0 0 0
142 0 self->internal_error = amqp_os_socket_error();
143 0 switch (self->internal_error) {
144 0 case EINTR:
145 0 goto start;
146 #ifdef _WIN32
147 case WSAEWOULDBLOCK:
148 #else
149 0 case EWOULDBLOCK:
150 #endif
151 #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
152 case EAGAIN:
153 #endif
154 0 ret = AMQP_PRIVATE_STATUS_SOCKET_NEEDREAD;
155 0 break;
156 0 default:
157 0 ret = AMQP_STATUS_SOCKET_ERROR;
158 }
159 0 ▶ } else if (0 == ret) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (0 == ret) { 0 0 0
160 0 ret = AMQP_STATUS_CONNECTION_CLOSED;
161 }
162  
163 0 return ret;
164 }
165  
166 static int
167 29 amqp_tcp_socket_open(void *base, const char *host, int port, struct timeval *timeout)
168 {
169 29 struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base;
170 29 ▶ if (-1 != self->sockfd) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 != self->sockfd) { 0 29 29
171 0 return AMQP_STATUS_SOCKET_INUSE;
172 }
173 29 self->sockfd = amqp_open_socket_noblock(host, port, timeout);
174 29 ▶ if (0 > self->sockfd) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 > self->sockfd) { 29 0 29
175 29 int err = self->sockfd;
176 29 self->sockfd = -1;
177 29 return err;
178 }
179 0 return AMQP_STATUS_OK;
180 }
181  
182 static int
183 36 amqp_tcp_socket_close(void *base)
184 {
185 36 struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base;
186 36 ▶ if (-1 == self->sockfd) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == self->sockfd) { 36 0 36
187 36 return AMQP_STATUS_SOCKET_CLOSED;
188 }
189  
190 0 ▶ if (amqp_os_socket_close(self->sockfd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (amqp_os_socket_close(self->sockfd)) { 0 0 0
191 0 return AMQP_STATUS_SOCKET_ERROR;
192 }
193 0 self->sockfd = -1;
194  
195 0 return AMQP_STATUS_OK;
196 }
197  
198 static int
199 272 amqp_tcp_socket_get_sockfd(void *base)
200 {
201 272 struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base;
202 272 return self->sockfd;
203 }
204  
205 static void
206 29 amqp_tcp_socket_delete(void *base)
207 {
208 29 struct amqp_tcp_socket_t *self = (struct amqp_tcp_socket_t *)base;
209  
210 29 ▶ if (self) {
Branch: 1 branch
BranchTrueFalseTotal
if (self) { 29 0 29
211 29 amqp_tcp_socket_close(self);
212 29 free(self);
213 }
214 29 }
215  
216 static const struct amqp_socket_class_t amqp_tcp_socket_class = {
217 amqp_tcp_socket_send, /* send */
218 amqp_tcp_socket_recv, /* recv */
219 amqp_tcp_socket_open, /* open */
220 amqp_tcp_socket_close, /* close */
221 amqp_tcp_socket_get_sockfd, /* get_sockfd */
222 amqp_tcp_socket_delete /* delete */
223 };
224  
225 amqp_socket_t *
226 29 amqp_tcp_socket_new(amqp_connection_state_t state)
227 {
228 29 struct amqp_tcp_socket_t *self = calloc(1, sizeof(*self));
229 29 ▶ if (!self) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self) { 0 29 29
230 0 return NULL;
231 }
232 29 self->klass = &amqp_tcp_socket_class;
233 29 self->sockfd = -1;
234  
235 29 amqp_set_socket(state, (amqp_socket_t *)self);
236  
237 29 return (amqp_socket_t *)self;
238 }
239  
240 void
241 0 amqp_tcp_socket_set_sockfd(amqp_socket_t *base, int sockfd)
242 {
243 struct amqp_tcp_socket_t *self;
244 0 ▶ if (base->klass != &amqp_tcp_socket_class) {
Branch: 1 branch
BranchTrueFalseTotal
if (base->klass != &amqp_tcp_socket_class) { 0 0 0
245 0 amqp_abort("<%p> is not of type amqp_tcp_socket_t", base);
246 }
247 0 self = (struct amqp_tcp_socket_t *)base;
248 0 self->sockfd = sockfd;
249 0 }
« amqp_time.c ↑ Summary