Mercurial > hg > fxanalyse
annotate 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 |
rev | line source |
---|---|
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
1 #ifdef _CVI_ |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
2 #include <winsock2.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
3 #include <ansi_c.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
4 #define strdup(s) StrDup(s) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
5 #else |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
6 #include <stdio.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
7 #include <stdlib.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
8 #include <unistd.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
9 #include <errno.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
10 #include <string.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
11 #include <sys/types.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
12 #include <sys/socket.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
13 #include <netinet/in.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
14 #include <arpa/inet.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
15 #include <netdb.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
16 #include <ctype.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
17 #include <math.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
18 #include <stdarg.h> |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
19 #endif |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
20 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
21 #include "xsocket.h" |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
22 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
23 #define XRETRY(code) \ |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
24 ({ \ |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
25 int __r, __n = 0; \ |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
26 while (__n++ < 2) { \ |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
27 __r = (code); \ |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
28 if (__r != -EAGAIN) \ |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
29 break; \ |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
30 } \ |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
31 __r; \ |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
32 }) |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
33 |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
34 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
35 struct xsocket * xsocket(const char *hostname, const int port) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
36 { |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
37 struct xsocket *s = malloc(sizeof(struct xsocket)); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
38 if (! s) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
39 return NULL; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
40 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
41 s->hostname = strdup(hostname); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
42 s->port = port; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
43 s->fd = -1; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
44 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
45 return s; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
46 } |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
47 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
48 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
49 int xconnect(struct xsocket *s) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
50 { |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
51 int r, sock; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
52 struct sockaddr_in addr; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
53 struct hostent* host; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
54 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
55 #ifdef _CVI_ |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
56 WSADATA wsdata; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
57 WSAStartup(MAKEWORD(2,2), &wsdata); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
58 #endif |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
59 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
60 if (s->fd >= 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
61 return 0; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
62 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
63 host = gethostbyname(s->hostname); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
64 if (host == NULL) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
65 return -errno; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
66 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
67 sock = socket(AF_INET, SOCK_STREAM, 0); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
68 if (sock < 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
69 return -errno; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
70 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
71 addr.sin_family = host->h_addrtype; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
72 memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
73 addr.sin_port = htons(s->port); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
74 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
75 r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
76 if (r < 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
77 return -errno; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
78 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
79 s->fd = sock; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
80 return 0; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
81 } |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
82 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
83 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
84 int xsend(struct xsocket *s, const char *data, size_t len) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
85 { |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
86 int r; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
87 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
88 r = xconnect(s); |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
89 if (r < 0) |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
90 return r; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
91 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
92 r = send(s->fd, data, len, 0); |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
93 if (r < 0) |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
94 return -errno; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
95 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
96 return r; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
97 } |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
98 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
99 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
100 int xrecv(struct xsocket *s, char *buffer, size_t len) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
101 { |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
102 int r; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
103 |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
104 r = recv(s->fd, buffer, len, 0); |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
105 if (r < 0) |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
106 return -errno; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
107 if (r == 0) { |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
108 close(s->fd); |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
109 s->fd = -1; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
110 return -EAGAIN; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
111 } |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
112 |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
113 return r; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
114 } |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
115 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
116 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
117 static inline int __exchange(struct xsocket *s, const char *out, size_t outlen, char *in, size_t inlen) |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
118 { |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
119 int r; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
120 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
121 r = xsend(s, out, outlen); |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
122 if (r < 0) |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
123 return r; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
124 |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
125 r = xrecv(s, in, inlen); |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
126 if (r < 0) |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
127 return r; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
128 |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
129 return r; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
130 } |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
131 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
132 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
133 int xask(struct xsocket *s, const char *cmd, size_t len, char *buffer, size_t bufferlen) |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
134 { |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
135 int r; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
136 char *data; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
137 |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
138 data = malloc(len + 2); |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
139 memcpy(data, cmd, len); |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
140 data[len++] = '\r'; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
141 data[len++] = '\n'; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
142 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
143 r = XRETRY(__exchange(s, data, len, buffer, bufferlen)); |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
144 if (r < 0) |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
145 return r; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
146 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
147 if ((buffer[--r] != '\n') || (buffer[--r] != '\r')) |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
148 return -EINVAL; |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
149 buffer[r] = '\0'; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
150 |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
151 return 0; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
152 } |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
153 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
154 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
155 int xcommand(struct xsocket *s, const char *frmt, ...) |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
156 { |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
157 int r, len; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
158 char buffer[1024]; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
159 va_list v; |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
160 |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
161 va_start(v, frmt); |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
162 len = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v); |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
163 va_end(v); |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
164 buffer[len++] = '\r'; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
165 buffer[len++] = '\n'; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
166 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
167 r = XRETRY(__exchange(s, buffer, len, buffer, sizeof(buffer))); |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
168 if (r < 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
169 return r; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
170 |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
171 if ((buffer[--r] != '\n') || (buffer[--r] != '\r')) |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
172 return -EINVAL; |
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
173 buffer[r] = '\0'; |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
174 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
175 if (strcmp(buffer, "OK") != 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
176 return -EINVAL; |
262
ebbe0f198322
Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
261
diff
changeset
|
177 |
261
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
178 return 0; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
179 } |