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

Public Member Functions

 RSocketRecv (const char *node, u_short prt)
 
int reconnect (int ntry)
 
int put (char *data, int len)
 
int write (char *data, int len)
 
int get (char *data, int len)
 
int get_wordbuf (int *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 57 of file RSocketLib.h.

Constructor & Destructor Documentation

◆ RSocketRecv()

RSocketRecv ( const char *  node,
u_short  prt 
)

Definition at line 216 of file RSocketLib.cc.

217{
218 m_errno = 0;
219 m_sock = -1;
220 // struct hostent* hp;
221 if ((m_hp = gethostbyname(node)) == NULL) {
222 m_errno = errno;
223 fprintf(stderr,
224 "RSocketRecv::gethostbyname(%s): not found\n", node);
225 return;
226 }
227
228 // struct sockaddr_in sa;
229 bzero(&m_sa, sizeof(m_sa));
230 bcopy(m_hp->h_addr, (char*)&m_sa.sin_addr, m_hp->h_length);
231 m_sa.sin_family = m_hp->h_addrtype;
232 m_sa.sin_port = htons((u_short)port);
233
234 int s;
235 m_sock = -1;
236 if ((s = socket(m_hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
237 m_errno = errno;
238 perror("RSocketRecv:socket");
239 return;
240 }
241 int sizeval = D2_SOCKBUF_SIZE;
242 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sizeval, 4);
243 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sizeval, 4);
244 int yes = 1;
245 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, 4);
246
247 // Connect
248 int maxretry = 1000;
249 for (;;) {
250 printf("SocketRecv: connect trying %d times\n", 1000 - maxretry + 1);
251 int istat = connect(s, (struct sockaddr*)&m_sa, sizeof(m_sa));
252 if (istat >= 0) {
253 m_sock = s;
254 m_port = port;
255 strcpy(m_node, node);
256 signal(SIGPIPE, SIG_IGN);
257 printf("RSocketRecv: initialized, m_sock = %d\n", s);
258 return;
259 }
260 maxretry--;
261 if (maxretry == 0) {
262 printf("RSocketRecv: connection failed. exitting\n");
263 exit(-1);
264 }
265 sleep(5);
266 }
267
268 /*
269 int maxretry = 10;
270 tryagain:
271 if (connect(s, (struct sockaddr*)&m_sa, sizeof(m_sa)) < 0) {
272 m_errno = errno;
273 perror("RSocketRecv:connect");
274 printf("tried to connect to %s, port %d\n", node, port);
275 printf("connection error..... m_sock set to %d\n", m_sock);
276 if (m_errno == ETIMEDOUT) {
277 printf(".... try again after 5 sec. \n");
278 maxretry--;
279 sleep(5);
280 if (maxretry == 0) return;
281 goto tryagain;
282 }
283 return;
284 }
285
286 m_sock = s;
287 m_port = port;
288 strcpy(m_node, node);
289 signal(SIGPIPE, SIG_IGN);
290 printf("RSocketRecv: initialized, m_sock = %d\n", s);
291 */
292}

◆ ~RSocketRecv()

Definition at line 294 of file RSocketLib.cc.

295{
296 shutdown(m_sock, 2);
297 ::close(m_sock);
298 printf("RSocketRecv: destructed, m_sock = %d\n", m_sock);
299}

Member Function Documentation

◆ err()

int err ( void  )

Definition at line 403 of file RSocketLib.cc.

404{
405 return m_errno;
406}

◆ get() [1/2]

int get ( char *  data,
int  len 
)

Definition at line 347 of file RSocketLib.cc.

348{
349 m_errno = 0;
350 return m_io.get(m_sock, data, len);
351}

◆ 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()

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() [1/2]

int get_wordbuf ( int *  data,
int  len 
)

Definition at line 353 of file RSocketLib.cc.

354{
355 // printf("RSocketRecv::get_wordbuf()\n");
356
357 m_errno = 0;
358 if (m_sock > 0)
359 return m_io.get_wordbuf(m_sock, data, len);
360 else
361 return -1;
362}

◆ get_wordbuf() [2/2]

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 383 of file RSocketLib.cc.

384{
385 return m_node;
386}

◆ port()

int port ( void  )

Definition at line 388 of file RSocketLib.cc.

389{
390 return m_port;
391}

◆ put() [1/2]

int put ( char *  data,
int  len 
)

Definition at line 370 of file RSocketLib.cc.

371{
372 m_errno = 0;
373 // printf("RSocketRecv::put (sd = %d)\n", m_sock);
374 return m_io.put(m_sock, data, len);
375}

◆ 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()

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 364 of file RSocketLib.cc.

365{
366 m_errno = 0;
367 return m_io.read_data(m_sock, data, len);
368}

◆ 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 301 of file RSocketLib.cc.

302{
303 // Close existing socket once.
304 shutdown(m_sock, 2);
305 ::close(m_sock);
306
307 // Setup socket parameters again;
308 bzero(&m_sa, sizeof(m_sa));
309 bcopy(m_hp->h_addr, (char*)&m_sa.sin_addr, m_hp->h_length);
310 m_sa.sin_family = m_hp->h_addrtype;
311 m_sa.sin_port = htons((u_short)m_port);
312
313 // Reopen the socket
314 int s;
315 m_sock = -1;
316 if ((s = socket(m_hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
317 m_errno = errno;
318 perror("RSocketRecv:socket");
319 return -3;
320 }
321 int sizeval = D2_SOCKBUF_SIZE;
322 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sizeval, 4);
323 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sizeval, 4);
324 int yes = 1;
325 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, 4);
326
327 m_sock = s;
328
329 // Connect again
330 int maxretry = ntry;
331 // printf ("RSocketRecv: reconnecting socket %d, try %d times with 5sec. interval.\n", m_sock, ntry );
332
333 for (;;) {
334 printf("RSocketRecv: reconnecting (trial %d) \n", ntry - maxretry + 1);
335 int istat = connect(m_sock, (struct sockaddr*)&m_sa, sizeof(m_sa));
336 if (istat >= 0) {
337 printf("RSocketRecv: reconnected\n");
338 return 0;
339 }
340 maxretry--;
341 if (maxretry == 0) return -1;
342 sleep(5);
343 }
344 printf("RSocketRecv: m_sock = %d reconnected.\n", m_sock);
345}

◆ sock() [1/2]

void sock ( int  sockid)

Definition at line 398 of file RSocketLib.cc.

399{
400 m_sock = sockid;
401}

◆ sock() [2/2]

int sock ( void  )

Definition at line 393 of file RSocketLib.cc.

394{
395 return m_sock;
396}

◆ write()

int write ( char *  data,
int  len 
)

Definition at line 377 of file RSocketLib.cc.

378{
379 m_errno = 0;
380 return m_io.write_data(m_sock, data, len);
381}

◆ 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 84 of file RSocketLib.h.

◆ m_hp

struct hostent* m_hp
private

Definition at line 79 of file RSocketLib.h.

◆ m_int

int m_int
privateinherited

Definition at line 44 of file SocketLib.h.

◆ m_io

SocketIO m_io
private

Definition at line 78 of file RSocketLib.h.

◆ m_node

char m_node[128]
private

Definition at line 83 of file RSocketLib.h.

◆ m_port

int m_port
private

Definition at line 82 of file RSocketLib.h.

◆ m_sa

struct sockaddr_in m_sa
private

Definition at line 80 of file RSocketLib.h.

◆ m_sock

int m_sock
private

Definition at line 81 of file RSocketLib.h.


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