Mercurial > hg > fxanalyse
comparison xsocket.c @ 263:8d9a4c5eb7a4
Fixes to make last changes working in CVI
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Tue, 16 Jun 2015 17:19:15 +0200 |
parents | ebbe0f198322 |
children |
comparison
equal
deleted
inserted
replaced
262:ebbe0f198322 | 263:8d9a4c5eb7a4 |
---|---|
1 #ifdef _CVI_ | 1 #ifdef _CVI_ |
2 #include <winsock2.h> | 2 #include <winsock2.h> |
3 #include <toolbox.h> | |
4 #include <lowlvlio.h> | |
3 #include <ansi_c.h> | 5 #include <ansi_c.h> |
4 #define strdup(s) StrDup(s) | 6 #define strdup(s) StrDup(s) |
7 #define ERRNO 1 | |
5 #else | 8 #else |
6 #include <stdio.h> | 9 #include <stdio.h> |
7 #include <stdlib.h> | 10 #include <stdlib.h> |
8 #include <unistd.h> | 11 #include <unistd.h> |
9 #include <errno.h> | 12 #include <errno.h> |
14 #include <arpa/inet.h> | 17 #include <arpa/inet.h> |
15 #include <netdb.h> | 18 #include <netdb.h> |
16 #include <ctype.h> | 19 #include <ctype.h> |
17 #include <math.h> | 20 #include <math.h> |
18 #include <stdarg.h> | 21 #include <stdarg.h> |
22 #define ERRNO errno | |
19 #endif | 23 #endif |
20 | 24 |
21 #include "xsocket.h" | 25 #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 }) | |
33 | |
34 | 26 |
35 struct xsocket * xsocket(const char *hostname, const int port) | 27 struct xsocket * xsocket(const char *hostname, const int port) |
36 { | 28 { |
37 struct xsocket *s = malloc(sizeof(struct xsocket)); | 29 struct xsocket *s = malloc(sizeof(struct xsocket)); |
38 if (! s) | 30 if (! s) |
60 if (s->fd >= 0) | 52 if (s->fd >= 0) |
61 return 0; | 53 return 0; |
62 | 54 |
63 host = gethostbyname(s->hostname); | 55 host = gethostbyname(s->hostname); |
64 if (host == NULL) | 56 if (host == NULL) |
65 return -errno; | 57 return -ERRNO; |
66 | 58 |
67 sock = socket(AF_INET, SOCK_STREAM, 0); | 59 sock = socket(AF_INET, SOCK_STREAM, 0); |
68 if (sock < 0) | 60 if (sock < 0) |
69 return -errno; | 61 return -ERRNO; |
70 | 62 |
71 addr.sin_family = host->h_addrtype; | 63 addr.sin_family = host->h_addrtype; |
72 memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); | 64 memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); |
73 addr.sin_port = htons(s->port); | 65 addr.sin_port = htons(s->port); |
74 | 66 |
75 r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); | 67 r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); |
76 if (r < 0) | 68 if (r < 0) |
77 return -errno; | 69 return -ERRNO; |
78 | 70 |
79 s->fd = sock; | 71 s->fd = sock; |
80 return 0; | 72 return 0; |
81 } | 73 } |
82 | 74 |
89 if (r < 0) | 81 if (r < 0) |
90 return r; | 82 return r; |
91 | 83 |
92 r = send(s->fd, data, len, 0); | 84 r = send(s->fd, data, len, 0); |
93 if (r < 0) | 85 if (r < 0) |
94 return -errno; | 86 return -ERRNO; |
95 | 87 |
96 return r; | 88 return r; |
97 } | 89 } |
98 | 90 |
99 | 91 |
101 { | 93 { |
102 int r; | 94 int r; |
103 | 95 |
104 r = recv(s->fd, buffer, len, 0); | 96 r = recv(s->fd, buffer, len, 0); |
105 if (r < 0) | 97 if (r < 0) |
106 return -errno; | 98 return -ERRNO; |
107 if (r == 0) { | 99 if (r == 0) { |
108 close(s->fd); | 100 close(s->fd); |
109 s->fd = -1; | 101 s->fd = -1; |
110 return -EAGAIN; | 102 return -EAGAIN; |
111 } | 103 } |
112 | 104 |
113 return r; | 105 return r; |
114 } | 106 } |
115 | 107 |
116 | 108 |
117 static inline int __exchange(struct xsocket *s, const char *out, size_t outlen, char *in, size_t inlen) | 109 static inline int __talk(struct xsocket *s, const char *out, size_t outlen, char *in, size_t inlen) |
118 { | 110 { |
119 int r; | 111 int r = 0, n = 0; |
120 | 112 |
121 r = xsend(s, out, outlen); | 113 while (n++ < 2) { |
122 if (r < 0) | 114 r = xsend(s, out, outlen); |
123 return r; | 115 if (r < 0) |
116 return r; | |
124 | 117 |
125 r = xrecv(s, in, inlen); | 118 r = xrecv(s, in, inlen); |
126 if (r < 0) | 119 if ((r < 0) && (r != -EAGAIN)) |
127 return r; | 120 return r; |
128 | 121 } |
122 | |
129 return r; | 123 return r; |
130 } | 124 } |
131 | 125 |
132 | 126 |
133 int xask(struct xsocket *s, const char *cmd, size_t len, char *buffer, size_t bufferlen) | 127 int xask(struct xsocket *s, const char *cmd, size_t len, char *buffer, size_t bufferlen) |
138 data = malloc(len + 2); | 132 data = malloc(len + 2); |
139 memcpy(data, cmd, len); | 133 memcpy(data, cmd, len); |
140 data[len++] = '\r'; | 134 data[len++] = '\r'; |
141 data[len++] = '\n'; | 135 data[len++] = '\n'; |
142 | 136 |
143 r = XRETRY(__exchange(s, data, len, buffer, bufferlen)); | 137 r = __talk(s, data, len, buffer, bufferlen); |
144 if (r < 0) | 138 if (r < 0) |
145 return r; | 139 return r; |
146 | 140 |
147 if ((buffer[--r] != '\n') || (buffer[--r] != '\r')) | 141 if ((buffer[--r] != '\n') || (buffer[--r] != '\r')) |
148 return -EINVAL; | 142 return -EINVAL; |
162 len = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v); | 156 len = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v); |
163 va_end(v); | 157 va_end(v); |
164 buffer[len++] = '\r'; | 158 buffer[len++] = '\r'; |
165 buffer[len++] = '\n'; | 159 buffer[len++] = '\n'; |
166 | 160 |
167 r = XRETRY(__exchange(s, buffer, len, buffer, sizeof(buffer))); | 161 r = __talk(s, buffer, len, buffer, sizeof(buffer)); |
168 if (r < 0) | 162 if (r < 0) |
169 return r; | 163 return r; |
170 | 164 |
171 if ((buffer[--r] != '\n') || (buffer[--r] != '\r')) | 165 if ((buffer[--r] != '\n') || (buffer[--r] != '\r')) |
172 return -EINVAL; | 166 return -EINVAL; |