Belle II Software development
SocketSend Class Reference
Inheritance diagram for SocketSend:
SocketIO

Public Member Functions

 SocketSend (const char *node, u_short prt)
 
int reconnect (int ntry)
 
int put (char *data, int len)
 
int put_wordbuf (int *data, int len)
 
int write (char *data, int len)
 
int get (char *data, int len)
 
int get_pxd (char *data, int len)
 
int read (char *data, int len)
 
char * node (void)
 
int port (void)
 
int sock (void)
 
void sock (int sockid)
 
int err (void)
 
int put (int sock, char *data, int len)
 
int put_wordbuf (int sock, int *data, int len)
 
int get (int sock, char *data, int len)
 
int get_wordbuf (int sock, int *data, int len)
 
int get_pxd (int sock, char *data, int len)
 
int read_data (int sock, char *data, int len)
 
int write_data (int sock, char *data, int len)
 
void interrupt ()
 

Private Attributes

SocketIO m_io
 
struct hostent * m_hp
 
struct sockaddr_in m_sa
 
int m_sock
 
int m_port
 
char m_node [128]
 
int m_errno
 
int m_int
 

Detailed Description

Definition at line 78 of file SocketLib.h.

Constructor & Destructor Documentation

◆ SocketSend()

SocketSend ( const char *  node,
u_short  prt 
)

Definition at line 390 of file SocketLib.cc.

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}

◆ ~SocketSend()

~SocketSend ( )

Definition at line 445 of file SocketLib.cc.

446{
447 shutdown(m_sock, 2);
448 ::close(m_sock);
449 printf("SocketSend: destructed, m_sock = %d\n", m_sock);
450}

Member Function Documentation

◆ err()

int err ( void  )

Definition at line 556 of file SocketLib.cc.

557{
558 return m_errno;
559}

◆ get() [1/2]

int get ( char *  data,
int  len 
)

Definition at line 498 of file SocketLib.cc.

499{
500 m_errno = 0;
501 return m_io.get(m_sock, data, len);
502}

◆ get() [2/2]

int get ( int  sock,
char *  data,
int  len 
)
inherited

Definition at line 95 of file SocketLib.cc.

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}

◆ get_pxd() [1/2]

int get_pxd ( char *  data,
int  len 
)

Definition at line 504 of file SocketLib.cc.

505{
506 m_errno = 0;
507 return m_io.get_pxd(m_sock, data, len);
508}
int get_pxd(int sock, char *data, int len)
Definition: SocketLib.cc:126

◆ get_pxd() [2/2]

int get_pxd ( int  sock,
char *  data,
int  len 
)
inherited

as this is fixed .... might not be needed if taken care of in unpacker

Definition at line 126 of file SocketLib.cc.

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}

◆ get_wordbuf()

int get_wordbuf ( int  sock,
int *  data,
int  len 
)
inherited

Definition at line 109 of file SocketLib.cc.

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}

◆ interrupt()

void interrupt ( void  )
inherited

Definition at line 197 of file SocketLib.cc.

198{
199 m_int = 1;
200}

◆ node()

char * node ( void  )

Definition at line 536 of file SocketLib.cc.

537{
538 return m_node;
539}

◆ port()

int port ( void  )

Definition at line 541 of file SocketLib.cc.

542{
543 return m_port;
544}

◆ put() [1/2]

int put ( char *  data,
int  len 
)

Definition at line 516 of file SocketLib.cc.

517{
518 m_errno = 0;
519 // printf("SocketSend::put (sd = %d)\n", m_sock);
520 return m_io.put(m_sock, data, len);
521}

◆ put() [2/2]

int put ( int  sock,
char *  data,
int  len 
)
inherited

Definition at line 36 of file SocketLib.cc.

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}

◆ put_wordbuf() [1/2]

int put_wordbuf ( int *  data,
int  len 
)

Definition at line 523 of file SocketLib.cc.

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}

◆ put_wordbuf() [2/2]

int put_wordbuf ( int  sock,
int *  data,
int  len 
)
inherited

Definition at line 51 of file SocketLib.cc.

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}

◆ read()

int read ( char *  data,
int  len 
)

Definition at line 510 of file SocketLib.cc.

511{
512 m_errno = 0;
513 return m_io.read_data(m_sock, data, len);
514}

◆ read_data()

int read_data ( int  sock,
char *  data,
int  len 
)
inherited

Definition at line 161 of file SocketLib.cc.

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}

◆ reconnect()

int reconnect ( int  ntry)

Definition at line 452 of file SocketLib.cc.

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}

◆ sock() [1/2]

void sock ( int  sockid)

Definition at line 551 of file SocketLib.cc.

552{
553 m_sock = sockid;
554}

◆ sock() [2/2]

int sock ( void  )

Definition at line 546 of file SocketLib.cc.

547{
548 return m_sock;
549}

◆ write()

int write ( char *  data,
int  len 
)

Definition at line 530 of file SocketLib.cc.

531{
532 m_errno = 0;
533 return m_io.write_data(m_sock, data, len);
534}

◆ write_data()

int write_data ( int  sock,
char *  data,
int  len 
)
inherited

Definition at line 62 of file SocketLib.cc.

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}

Member Data Documentation

◆ m_errno

int m_errno
private

Definition at line 106 of file SocketLib.h.

◆ m_hp

struct hostent* m_hp
private

Definition at line 101 of file SocketLib.h.

◆ m_int

int m_int
privateinherited

Definition at line 44 of file SocketLib.h.

◆ m_io

SocketIO m_io
private

Definition at line 100 of file SocketLib.h.

◆ m_node

char m_node[128]
private

Definition at line 105 of file SocketLib.h.

◆ m_port

int m_port
private

Definition at line 104 of file SocketLib.h.

◆ m_sa

struct sockaddr_in m_sa
private

Definition at line 102 of file SocketLib.h.

◆ m_sock

int m_sock
private

Definition at line 103 of file SocketLib.h.


The documentation for this class was generated from the following files: