Mercurial > hg > fxanalyse
annotate xsocket.c @ 261:a03df7dc98f8
Add xsocket wrappers
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Tue, 16 Jun 2015 15:09:23 +0200 |
parents | |
children | ebbe0f198322 |
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 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
23 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
24 struct xsocket * xsocket(const char *hostname, const int port) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
25 { |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
26 struct xsocket *s = malloc(sizeof(struct xsocket)); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
27 if (! s) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
28 return NULL; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
29 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
30 s->hostname = strdup(hostname); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
31 s->port = port; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
32 s->fd = -1; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
33 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
34 return s; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
35 } |
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 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
38 int xconnect(struct xsocket *s) |
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 int r, sock; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
41 struct sockaddr_in addr; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
42 struct hostent* host; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
43 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
44 #ifdef _CVI_ |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
45 WSADATA wsdata; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
46 WSAStartup(MAKEWORD(2,2), &wsdata); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
47 #endif |
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 if (s->fd >= 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
50 return 0; |
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 host = gethostbyname(s->hostname); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
53 if (host == NULL) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
54 return -errno; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
55 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
56 sock = socket(AF_INET, SOCK_STREAM, 0); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
57 if (sock < 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
58 return -errno; |
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 addr.sin_family = host->h_addrtype; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
61 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
|
62 addr.sin_port = htons(s->port); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
63 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
64 r = connect(sock, (struct sockaddr*)&addr, sizeof(addr)); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
65 if (r < 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
66 return -errno; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
67 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
68 s->fd = sock; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
69 return 0; |
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 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
72 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
73 int xsend(struct xsocket *s, const char *data, size_t len) |
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 int r, retry = 2; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
76 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
77 while (--retry) { |
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 r = xconnect(s); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
80 if (r < 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
81 return r; |
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 r = send(s->fd, data, len, 0); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
84 if (r < 0) { |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
85 close(s->fd); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
86 s->fd = -1; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
87 continue; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
88 } |
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 break; |
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 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
93 return (r < 0) ? -errno : 0; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
94 } |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
95 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
96 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
97 int xrecv(struct xsocket *s, char *buffer, size_t len) |
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 return recv(s->fd, buffer, len, 0); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
100 } |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
101 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
102 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
103 int msend(struct xsocket *s, const char *data, size_t len) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
104 { |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
105 char *buffer = malloc(len + 2); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
106 memcpy(buffer, data, len); |
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 buffer[len++] = '\r'; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
109 buffer[len++] = '\n'; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
110 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
111 return xsend(s, buffer, len); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
112 } |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
113 |
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 int mrecv(struct xsocket *s, char *buffer, size_t len) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
116 { |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
117 int n; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
118 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
119 n = xrecv(s, buffer, len); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
120 if (n < 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
121 return n; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
122 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
123 if ((buffer[--n] != '\n') || (buffer[--n] != '\r')) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
124 return -EINVAL; |
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 buffer[n] = '\0'; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
127 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
128 return n; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
129 } |
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 int command(struct xsocket *s, char *frmt, ...) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
133 { |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
134 int r, n; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
135 char buffer[1024]; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
136 va_list v; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
137 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
138 va_start(v, frmt); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
139 n = vsnprintf(buffer, sizeof(buffer) - 2, frmt, v); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
140 va_end(v); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
141 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
142 buffer[n++] = '\r'; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
143 buffer[n++] = '\n'; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
144 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
145 r = xsend(s, buffer, n); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
146 if (r < 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
147 return r; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
148 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
149 r = mrecv(s, buffer, sizeof(buffer)); |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
150 if (r < 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
151 return r; |
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 if (strcmp(buffer, "OK") != 0) |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
154 return -EINVAL; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
155 |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
156 return 0; |
a03df7dc98f8
Add xsocket wrappers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff
changeset
|
157 } |