annotate xsocket.c @ 271:4a2fa10b8421 default tip

Publish DDS and measured frequencies through ZMQ
author Daniele Nicolodi <daniele.nicolodi@nist.gov>
date Sat, 15 Aug 2020 18:22:42 -0600
parents 8d9a4c5eb7a4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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>
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
3 #include <toolbox.h>
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
4 #include <lowlvlio.h>
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
5 #include <ansi_c.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
6 #define strdup(s) StrDup(s)
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
7 #define ERRNO 1
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
8 #else
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
9 #include <stdio.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
10 #include <stdlib.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
11 #include <unistd.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
12 #include <errno.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
13 #include <string.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
14 #include <sys/types.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
15 #include <sys/socket.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
16 #include <netinet/in.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
17 #include <arpa/inet.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
18 #include <netdb.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
19 #include <ctype.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
20 #include <math.h>
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
21 #include <stdarg.h>
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
22 #define ERRNO errno
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
23 #endif
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
24
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25 #include "xsocket.h"
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27 struct xsocket * xsocket(const char *hostname, const int port)
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
28 {
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
29 struct xsocket *s = malloc(sizeof(struct xsocket));
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
30 if (! s)
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
31 return NULL;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
32
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
33 s->hostname = strdup(hostname);
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
34 s->port = port;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
35 s->fd = -1;
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 return s;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
38 }
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
39
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 int xconnect(struct xsocket *s)
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
42 {
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
43 int r, sock;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
44 struct sockaddr_in addr;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
45 struct hostent* host;
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 #ifdef _CVI_
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
48 WSADATA wsdata;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
49 WSAStartup(MAKEWORD(2,2), &wsdata);
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
50 #endif
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
51
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
52 if (s->fd >= 0)
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
53 return 0;
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 host = gethostbyname(s->hostname);
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
56 if (host == NULL)
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
57 return -ERRNO;
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59 sock = socket(AF_INET, SOCK_STREAM, 0);
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60 if (sock < 0)
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
61 return -ERRNO;
261
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 addr.sin_family = host->h_addrtype;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 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
65 addr.sin_port = htons(s->port);
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 r = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
68 if (r < 0)
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
69 return -ERRNO;
261
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 s->fd = sock;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
72 return 0;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73 }
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
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
76 int xsend(struct xsocket *s, const char *data, size_t len)
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
77 {
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
78 int r;
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
79
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
80 r = xconnect(s);
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
81 if (r < 0)
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
82 return r;
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
83
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
84 r = send(s->fd, data, len, 0);
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
85 if (r < 0)
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
86 return -ERRNO;
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 return r;
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
89 }
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
90
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
91
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
92 int xrecv(struct xsocket *s, char *buffer, size_t len)
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
93 {
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
94 int r;
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
95
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
96 r = recv(s->fd, buffer, len, 0);
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
97 if (r < 0)
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
98 return -ERRNO;
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
99 if (r == 0) {
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
100 close(s->fd);
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
101 s->fd = -1;
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
102 return -EAGAIN;
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
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
105 return r;
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
106 }
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
107
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
108
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
109 static inline int __talk(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
110 {
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
111 int r = 0, n = 0;
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
112
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
113 while (n++ < 2) {
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
114 r = xsend(s, out, outlen);
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
115 if (r < 0)
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
116 return r;
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
117
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
118 r = xrecv(s, in, inlen);
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
119 if ((r < 0) && (r != -EAGAIN))
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
120 return r;
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
121 }
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
122
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
123 return r;
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
124 }
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
125
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
126
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
127 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
128 {
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
129 int r;
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
130 char *data;
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
131
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
132 data = malloc(len + 2);
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
133 memcpy(data, cmd, len);
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
134 data[len++] = '\r';
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
135 data[len++] = '\n';
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
136
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
137 r = __talk(s, data, len, buffer, bufferlen);
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
138 if (r < 0)
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
139 return r;
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
140
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
141 if ((buffer[--r] != '\n') || (buffer[--r] != '\r'))
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
142 return -EINVAL;
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
143 buffer[r] = '\0';
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
144
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
145 return 0;
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
146 }
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
147
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
148
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
149 int xcommand(struct xsocket *s, const char *frmt, ...)
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
150 {
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
151 int r, len;
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
152 char buffer[1024];
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
153 va_list v;
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
154
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
155 va_start(v, frmt);
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
156 len = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v);
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
157 va_end(v);
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
158 buffer[len++] = '\r';
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
159 buffer[len++] = '\n';
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
160
263
8d9a4c5eb7a4 Fixes to make last changes working in CVI
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 262
diff changeset
161 r = __talk(s, buffer, len, buffer, sizeof(buffer));
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
162 if (r < 0)
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
163 return r;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
164
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
165 if ((buffer[--r] != '\n') || (buffer[--r] != '\r'))
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
166 return -EINVAL;
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
167 buffer[r] = '\0';
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
168
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
169 if (strcmp(buffer, "OK") != 0)
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
170 return -EINVAL;
262
ebbe0f198322 Fix DDS client connection retry
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 261
diff changeset
171
261
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
172 return 0;
a03df7dc98f8 Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
173 }