Mercurial > hg > fxanalyse
comparison xsocket.c @ 262:ebbe0f198322
Fix DDS client connection retry
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Tue, 16 Jun 2015 17:16:11 +0200 |
parents | a03df7dc98f8 |
children | 8d9a4c5eb7a4 |
comparison
equal
deleted
inserted
replaced
261:a03df7dc98f8 | 262:ebbe0f198322 |
---|---|
17 #include <math.h> | 17 #include <math.h> |
18 #include <stdarg.h> | 18 #include <stdarg.h> |
19 #endif | 19 #endif |
20 | 20 |
21 #include "xsocket.h" | 21 #include "xsocket.h" |
22 | |
23 #define XRETRY(code) \ | |
24 ({ \ | |
25 int __r, __n = 0; \ | |
26 while (__n++ < 2) { \ | |
27 __r = (code); \ | |
28 if (__r != -EAGAIN) \ | |
29 break; \ | |
30 } \ | |
31 __r; \ | |
32 }) | |
22 | 33 |
23 | 34 |
24 struct xsocket * xsocket(const char *hostname, const int port) | 35 struct xsocket * xsocket(const char *hostname, const int port) |
25 { | 36 { |
26 struct xsocket *s = malloc(sizeof(struct xsocket)); | 37 struct xsocket *s = malloc(sizeof(struct xsocket)); |
70 } | 81 } |
71 | 82 |
72 | 83 |
73 int xsend(struct xsocket *s, const char *data, size_t len) | 84 int xsend(struct xsocket *s, const char *data, size_t len) |
74 { | 85 { |
75 int r, retry = 2; | 86 int r; |
76 | 87 |
77 while (--retry) { | 88 r = xconnect(s); |
78 | 89 if (r < 0) |
79 r = xconnect(s); | 90 return r; |
80 if (r < 0) | |
81 return r; | |
82 | 91 |
83 r = send(s->fd, data, len, 0); | 92 r = send(s->fd, data, len, 0); |
84 if (r < 0) { | 93 if (r < 0) |
85 close(s->fd); | 94 return -errno; |
86 s->fd = -1; | |
87 continue; | |
88 } | |
89 | 95 |
90 break; | 96 return r; |
91 } | |
92 | |
93 return (r < 0) ? -errno : 0; | |
94 } | 97 } |
95 | 98 |
96 | 99 |
97 int xrecv(struct xsocket *s, char *buffer, size_t len) | 100 int xrecv(struct xsocket *s, char *buffer, size_t len) |
98 { | 101 { |
99 return recv(s->fd, buffer, len, 0); | 102 int r; |
103 | |
104 r = recv(s->fd, buffer, len, 0); | |
105 if (r < 0) | |
106 return -errno; | |
107 if (r == 0) { | |
108 close(s->fd); | |
109 s->fd = -1; | |
110 return -EAGAIN; | |
111 } | |
112 | |
113 return r; | |
100 } | 114 } |
101 | 115 |
102 | 116 |
103 int msend(struct xsocket *s, const char *data, size_t len) | 117 static inline int __exchange(struct xsocket *s, const char *out, size_t outlen, char *in, size_t inlen) |
104 { | 118 { |
105 char *buffer = malloc(len + 2); | 119 int r; |
106 memcpy(buffer, data, len); | 120 |
121 r = xsend(s, out, outlen); | |
122 if (r < 0) | |
123 return r; | |
124 | |
125 r = xrecv(s, in, inlen); | |
126 if (r < 0) | |
127 return r; | |
128 | |
129 return r; | |
130 } | |
131 | |
132 | |
133 int xask(struct xsocket *s, const char *cmd, size_t len, char *buffer, size_t bufferlen) | |
134 { | |
135 int r; | |
136 char *data; | |
137 | |
138 data = malloc(len + 2); | |
139 memcpy(data, cmd, len); | |
140 data[len++] = '\r'; | |
141 data[len++] = '\n'; | |
142 | |
143 r = XRETRY(__exchange(s, data, len, buffer, bufferlen)); | |
144 if (r < 0) | |
145 return r; | |
146 | |
147 if ((buffer[--r] != '\n') || (buffer[--r] != '\r')) | |
148 return -EINVAL; | |
149 buffer[r] = '\0'; | |
150 | |
151 return 0; | |
152 } | |
153 | |
154 | |
155 int xcommand(struct xsocket *s, const char *frmt, ...) | |
156 { | |
157 int r, len; | |
158 char buffer[1024]; | |
159 va_list v; | |
107 | 160 |
161 va_start(v, frmt); | |
162 len = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v); | |
163 va_end(v); | |
108 buffer[len++] = '\r'; | 164 buffer[len++] = '\r'; |
109 buffer[len++] = '\n'; | 165 buffer[len++] = '\n'; |
110 | 166 |
111 return xsend(s, buffer, len); | 167 r = XRETRY(__exchange(s, buffer, len, buffer, sizeof(buffer))); |
112 } | |
113 | |
114 | |
115 int mrecv(struct xsocket *s, char *buffer, size_t len) | |
116 { | |
117 int n; | |
118 | |
119 n = xrecv(s, buffer, len); | |
120 if (n < 0) | |
121 return n; | |
122 | |
123 if ((buffer[--n] != '\n') || (buffer[--n] != '\r')) | |
124 return -EINVAL; | |
125 | |
126 buffer[n] = '\0'; | |
127 | |
128 return n; | |
129 } | |
130 | |
131 | |
132 int command(struct xsocket *s, char *frmt, ...) | |
133 { | |
134 int r, n; | |
135 char buffer[1024]; | |
136 va_list v; | |
137 | |
138 va_start(v, frmt); | |
139 n = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v); | |
140 va_end(v); | |
141 | |
142 buffer[n++] = '\r'; | |
143 buffer[n++] = '\n'; | |
144 | |
145 r = xsend(s, buffer, n); | |
146 if (r < 0) | 168 if (r < 0) |
147 return r; | 169 return r; |
148 | 170 |
149 r = mrecv(s, buffer, sizeof(buffer)); | 171 if ((buffer[--r] != '\n') || (buffer[--r] != '\r')) |
150 if (r < 0) | 172 return -EINVAL; |
151 return r; | 173 buffer[r] = '\0'; |
152 | 174 |
153 if (strcmp(buffer, "OK") != 0) | 175 if (strcmp(buffer, "OK") != 0) |
154 return -EINVAL; | 176 return -EINVAL; |
155 | 177 |
156 return 0; | 178 return 0; |
157 } | 179 } |