Belle II Software development
SocketLib.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <unistd.h>
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13#include <netdb.h>
14#include <sys/socket.h>
15#include <netinet/in.h>
16#include <errno.h>
17#include <sys/time.h>
18#include <signal.h>
19
20#include "daq/dataflow/SocketLib.h"
21
22#define MAXHOSTNAME 100
23
24using namespace Belle2;
25
26// SocketIO class
27SocketIO::SocketIO()
28{
29}
30
31SocketIO::~SocketIO()
32{
33}
34
35
36int SocketIO::put(int sock, char* data, int len)
37{
38 int to_size = htonl(len);
39 // printf("SocketIO::put(%d): sending size...\n",sock);
40 int br = write_data(sock, (char*)&to_size, 4);
41 if (br < 0) {
42 perror("put: sending size");
43 return br;
44 }
45 // printf("SocketIO::put(%d): sending data...\n",sock);
46 int bcount = write_data(sock, data, len);
47 if (bcount < 0) perror("put: sending data");
48 return (bcount);
49}
50
51int SocketIO::put_wordbuf(int sock, int* data, int len)
52{
53 // printf("SocketIO::put_wordbuf(%d): sending data...\n",sock);
54 int bcount = write_data(sock, (char*)data, len * sizeof(int));
55 if (bcount < 0) perror("put: sending data");
56 if (bcount <= 0)
57 return bcount;
58 else
59 return ((bcount - 1) / 4 + 1);
60}
61
62int SocketIO::write_data(int sock, char* data, int len)
63{
64 errno = 0;
65 char* ptr = data;
66 int bcount = 0;
67
68 // printf("write_data( sock=%d. data=%p. len=%d )\n", sock, data, len);
69
70 while (bcount < len) {
71 int br = 0;
72 if ((br =::write(sock, ptr, len - bcount)) > 0) {
73 bcount += br;
74 ptr += br;
75 }
76 if (br < 0) {
77 switch (errno) {
78 case EINTR:
79 return -1;
80 // continue;
81 case EPIPE:
82 return -1; // connection closed, sigpipe
83 case ENETUNREACH:
84 case EHOSTUNREACH:
85 case ETIMEDOUT:
86 usleep(500);
87 continue;
88 default: return -1;
89 }
90 }
91 }
92 return (bcount);
93}
94
95int SocketIO::get(int sock, char* data, int len)
96{
97 int gcount;
98 int br = read_data(sock, (char*)&gcount, 4);
99 if (br <= 0) return br;
100 gcount = ntohl(gcount);
101 if (gcount > len) {
102 printf("buffer too small : %d(%d)", gcount, len);
103 exit(0);
104 }
105 int bcount = read_data(sock, data, gcount);
106 return (bcount);
107}
108
109int SocketIO::get_wordbuf(int sock, int* wrdbuf, int len)
110{
111 int gcount;
112 int br = read_data(sock, (char*)wrdbuf, sizeof(int));
113 if (br <= 0) return br;
114 // gcount = ntohl(wrdbuf[0]);
115 gcount = wrdbuf[0];
116 // printf ( "gcount = %8.8x (%d)\n", gcount, gcount );
117 if (gcount > len) {
118 printf("buffer too small : %d(%d)", gcount, len);
119 exit(0);
120 }
121 read_data(sock, (char*)&wrdbuf[1], (gcount - 1) * sizeof(int));
122 // printf ( "term = %8.8x\n", wrdbuf[gcount-1] );
123 return (wrdbuf[0]);
124}
125
126int SocketIO::get_pxd(int sock, char* data, int len)
127{
128#define MAX_PXD_FRAMES 256
129 const int headerlen = 8;
130 int* pxdheader = (int*) data; // TODO should it be unsigned int?
131 int* pxdheadertable = (int*) &data[headerlen]; // TODO should it be unsigned int?
132 int framenr = 0, tablelen = 0, datalen = 0;
133 int br = read_data(sock, data, headerlen);
134 if (br <= 0) return br;
135 if (static_cast<uint>(pxdheader[0]) != htonl(0xCAFEBABE)) {
136 printf("pxdheader wrong : Magic %X , Frames %X \n", pxdheader[0], ntohl(pxdheader[1]));
137 exit(0);
138 }
139 pxdheader[0] = ntohl(pxdheader[0]);
140 framenr = pxdheader[1] = ntohl(pxdheader[1]);
141 if (framenr > MAX_PXD_FRAMES) {
142 printf("MAX_PXD_FRAMES too small : %d(%d) \n", framenr, MAX_PXD_FRAMES);
143 exit(0);
144 }
145 tablelen = 4 * framenr;
146 br = read_data(sock, (char*)&data[headerlen], tablelen);
147 if (br <= 0) return br;
148 for (int i = 0; i < framenr; i++) {
149 pxdheadertable[i] = ntohl(pxdheadertable[i]);
150 datalen += (pxdheadertable[i] + 3) & 0xFFFFFFFC;
151 }
152
153 if (datalen + headerlen + tablelen > len) {
154 printf("buffer too small : %d %d %d(%d) \n", headerlen, tablelen, datalen, len);
155 exit(0);
156 }
157 int bcount = read_data(sock, data + headerlen + tablelen, datalen);
158 return (headerlen + tablelen + bcount);
159}
160
161int SocketIO::read_data(int sock, char* data, int len)
162{
163 char* buf = data;
164 int bcount = 0;
165
166 while (bcount < len) {
167 int br = 0;
168 if ((br =::read(sock, buf, len - bcount)) > 0) {
169 bcount += br;
170 buf += br;
171 }
172 fflush(stdout);
173 if (br == 0) return 0;
174 if (br < 0) {
175 switch (errno) {
176 case EINTR:
177 if (m_int == 1) {
178 printf("read: interrupted!\n");
179 m_int = 0;
180 return -2;
181 } else
182 // continue;
183 return -1;
184 case EAGAIN:
185 continue;
186 default:
187 perror("SocketIO:read");
188 fprintf(stderr, "sock = %d, buf=%p, len = %d\n", sock, buf, len - bcount);
189 return -1;
190 }
191 }
192 }
193 // printf ( "SocketIO::read_data ended : bcount = %d!!!\n", bcount );
194 return (bcount);
195}
196
197void SocketIO::interrupt(void)
198{
199 m_int = 1;
200}
201
202// SocketRecv class
203
204SocketRecv::SocketRecv(u_short p)
205{
206
207 struct sockaddr_in sa;
208 bzero(&sa, sizeof(struct sockaddr_in));
209
210 m_errno = 0;
211
212 sa.sin_family = AF_INET;
213 sa.sin_port = htons(p);
214
215 int s;
216 if ((s = ::socket(AF_INET, SOCK_STREAM, 0)) < 0) {
217 m_errno = errno;
218 perror("SocketRecv::socket");
219 return;
220 }
221
222 int optval = 1;
223 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, 4);
224
225 int sizeval = D2_SOCKBUF_SIZE;
226 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sizeval, 4);
227 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sizeval, 4);
228
229 signal(SIGPIPE, SIG_IGN); // ignore SIGPIPE
230 // printf ( "SocketRecv: SIGPIPE will be ignored\n" );
231 // fflush ( stdout );
232
233 if ((bind(s, (struct sockaddr*)&sa, sizeof(sa))) < 0) {
234 m_errno = errno;
235 perror("SocketRecv::bind");
236 return;
237 }
238
239 m_sock = s;
240 m_sender = 0;
241 listen(s, 3);
242 printf("SocketRecv:: initialized, sock=%d\n", m_sock);
243
244 return;
245
246}
247
248SocketRecv::~SocketRecv()
249{
250 if (m_sender > 0) {
251 shutdown(m_sender, 2);
252 ::close(m_sender);
253 printf("SocketRecv:: receiving socket %d closed\n", m_sender);
254 }
255
256 shutdown(m_sock, 2);
257 ::close(m_sock);
258 printf("SocketRecv:: connection socket %d closed\n", m_sock);
259}
260
261int SocketRecv::accept()
262{
263 m_errno = 0;
264 struct sockaddr_in isa;
265 socklen_t i = sizeof(isa);
266 getsockname(m_sock, (struct sockaddr*)&isa, &i);
267
268 int t;
269 if ((t =::accept(m_sock, (struct sockaddr*)&isa, &i)) < 0) {
270 m_errno = errno;
271 return (-1);
272 }
273
274 printf("SocketRecv:: connection request accepted, sender=%d\n", t);
275
276 m_sender = t;
277 return (t);
278}
279
280int SocketRecv::close()
281{
282 // ::close ( m_sender );
283 // m_sender = 0;
284 printf("SocketRecv: destructed, m_sender = %d\n", m_sender);
285 return 0;
286}
287
288int SocketRecv::examine()
289{
290 // printf("SocketRecv::examine(): waiting for client connection ...\n");
291
292 m_errno = 0;
293 fd_set ready;
294 FD_ZERO(&ready);
295 FD_SET(m_sock, &ready);
296 struct timeval to;
297 to.tv_sec = 0;
298 to.tv_usec = 0;
299 if (select(FD_SETSIZE, &ready, 0, 0, &to) < 0) {
300 m_errno = errno;
301 perror("select");
302 return (-1);
303 }
304 if (FD_ISSET(m_sock, &ready)) {
305 printf("SocketRecv::connected!!!!\n");
306 return (1);
307 } else
308 return (0);
309}
310
311int SocketRecv::get(char* data, int len)
312{
313 // printf("SocketSend::get()\n");
314
315 m_errno = 0;
316 if (m_sender > 0)
317 return m_io.get(m_sender, data, len);
318 else
319 return -1;
320}
321
322int SocketRecv::get_wordbuf(int* data, int len)
323{
324 // printf("SocketSend::get()\n");
325
326 m_errno = 0;
327 if (m_sender > 0)
328 return m_io.get_wordbuf(m_sender, data, len);
329 else
330 return -1;
331}
332
333int SocketRecv::read(char* data, int len)
334{
335 m_errno = 0;
336 if (m_sender > 0)
337 return m_io.read_data(m_sender, data, len);
338 else
339 return -1;
340}
341
342int SocketRecv::put(char* data, int len)
343{
344 // printf("SocketRecv::put (sd = %d)\n", m_sender);
345 m_errno = 0;
346 if (m_sender > 0)
347 return m_io.put(m_sender, data, len);
348 else
349 return -1;
350}
351
352int SocketRecv::write(char* data, int len)
353{
354 m_errno = 0;
355 if (m_sender > 0)
356 return m_io.write_data(m_sender, data, len);
357 else
358 return -1;
359}
360
361
362int SocketRecv::sock() const
363{
364 return m_sock;
365}
366
367void SocketRecv::sock(int sockid)
368{
369 m_sock = sockid;
370}
371
372int SocketRecv::sender() const
373{
374 return m_sender;
375}
376
377int SocketRecv::err() const
378{
379 return m_errno;
380}
381
382void SocketRecv::interrupt()
383{
384 m_io.interrupt();
385}
386
387
388// SocketSend class
389
390SocketSend::SocketSend(const char* node, u_short port)
391{
392 m_errno = 0;
393 m_sock = -1;
394 if ((m_hp = gethostbyname(node)) == NULL) {
395 m_errno = errno;
396 fprintf(stderr,
397 "SocketSend::gethostbyname(%s): not found\n", node);
398 return;
399 }
400
401 struct sockaddr_in sa;
402 bzero(&sa, sizeof(sa));
403 bcopy(m_hp->h_addr, (char*)&sa.sin_addr, m_hp->h_length);
404 sa.sin_family = m_hp->h_addrtype;
405 sa.sin_port = htons((u_short)port);
406
407 int s;
408 m_sock = -1;
409 if ((s = socket(m_hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
410 m_errno = errno;
411 perror("SocketSend:socket");
412 return;
413 }
414 int sizeval = D2_SOCKBUF_SIZE;
415 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sizeval, 4);
416 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sizeval, 4);
417
418 signal(SIGPIPE, SIG_IGN); // ignore SIGPIPE
419 // printf ( "SocketSend: SIGPIPE will be ignored\n" );
420 // fflush ( stdout );
421
422 int maxretry = 10;
423tryagain:
424 if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) < 0) {
425 m_errno = errno;
426 perror("SocketSend:connect");
427 printf("connection error..... m_sock set to %d\n", m_sock);
428 if (m_errno == ETIMEDOUT) {
429 printf(".... try again after 5 sec. \n");
430 maxretry--;
431 sleep(5);
432 if (maxretry == 0) exit(1);
433 goto tryagain;
434 }
435 return;
436 }
437
438 m_sock = s;
439 m_port = port;
440 strcpy(m_node, node);
441 signal(SIGPIPE, SIG_IGN);
442 printf("SocketSend: initialized, m_sock = %d\n", s);
443}
444
445SocketSend::~SocketSend()
446{
447 shutdown(m_sock, 2);
448 ::close(m_sock);
449 printf("SocketSend: destructed, m_sock = %d\n", m_sock);
450}
451
452int SocketSend::reconnect(int ntry)
453{
454 // Close existing socket once.
455 shutdown(m_sock, 2);
456 ::close(m_sock);
457
458 // Setup socket parameters again;
459 bzero(&m_sa, sizeof(m_sa));
460 bcopy(m_hp->h_addr, (char*)&m_sa.sin_addr, m_hp->h_length);
461 m_sa.sin_family = m_hp->h_addrtype;
462 m_sa.sin_port = htons((u_short)m_port);
463
464 // Reopen the socket
465 int s;
466 m_sock = -1;
467 if ((s = socket(m_hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
468 m_errno = errno;
469 perror("RSocketRecv:socket");
470 return -3;
471 }
472 int sizeval = D2_SOCKBUF_SIZE;
473 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sizeval, 4);
474 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sizeval, 4);
475 int yes = 1;
476 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, 4);
477
478 m_sock = s;
479
480 // Connect again
481 int maxretry = ntry;
482 // printf ("RSocketRecv: reconnecting socket %d, try %d times with 5sec. interval.\n", m_sock, ntry );
483
484 for (;;) {
485 printf("SocketSend: reconnecting (trial %d) \n", ntry - maxretry + 1);
486 int istat = connect(m_sock, (struct sockaddr*)&m_sa, sizeof(m_sa));
487 if (istat >= 0) {
488 printf("SocketSend: reconnected\n");
489 return 0;
490 }
491 maxretry--;
492 if (maxretry == 0) return -1;
493 sleep(5);
494 }
495 printf("SocketSend: m_sock = %d reconnected.\n", m_sock);
496}
497
498int SocketSend::get(char* data, int len)
499{
500 m_errno = 0;
501 return m_io.get(m_sock, data, len);
502}
503
504int SocketSend::get_pxd(char* data, int len)
505{
506 m_errno = 0;
507 return m_io.get_pxd(m_sock, data, len);
508}
509
510int SocketSend::read(char* data, int len)
511{
512 m_errno = 0;
513 return m_io.read_data(m_sock, data, len);
514}
515
516int SocketSend::put(char* data, int len)
517{
518 m_errno = 0;
519 // printf("SocketSend::put (sd = %d)\n", m_sock);
520 return m_io.put(m_sock, data, len);
521}
522
523int SocketSend::put_wordbuf(int* data, int len)
524{
525 m_errno = 0;
526 // printf("SocketSend::put (sd = %d)\n", m_sock);
527 return m_io.put_wordbuf(m_sock, data, len);
528}
529
530int SocketSend::write(char* data, int len)
531{
532 m_errno = 0;
533 return m_io.write_data(m_sock, data, len);
534}
535
536char* SocketSend::node()
537{
538 return m_node;
539}
540
541int SocketSend::port()
542{
543 return m_port;
544}
545
546int SocketSend::sock()
547{
548 return m_sock;
549}
550
551void SocketSend::sock(int sockid)
552{
553 m_sock = sockid;
554}
555
556int SocketSend::err()
557{
558 return m_errno;
559}
560
561
562
563
564
565
566
567
int get_pxd(int sock, char *data, int len)
Definition: SocketLib.cc:126
Abstract base class for different kinds of events.