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

Public Member Functions

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

int m_int
 

Detailed Description

Definition at line 25 of file SocketLib.h.

Constructor & Destructor Documentation

◆ SocketIO()

SocketIO ( )

Definition at line 27 of file SocketLib.cc.

28{
29}

◆ ~SocketIO()

~SocketIO ( )

Definition at line 31 of file SocketLib.cc.

32{
33}

Member Function Documentation

◆ get()

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

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 
)

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 
)

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  )

Definition at line 197 of file SocketLib.cc.

198{
199 m_int = 1;
200}

◆ put()

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

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 
)

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

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

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}

◆ write_data()

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

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_int

int m_int
private

Definition at line 44 of file SocketLib.h.


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