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

Public Member Functions

 RSocketSend (u_short port)
 
int examine ()
 
int accept ()
 
int close ()
 
int sock () const
 
int sender () const
 
void sock (int sockid)
 
int port () const
 
int get (char *data, int len)
 
int read (char *data, int len)
 
int put (char *data, int len)
 
int put_wordbuf (int *data, int len)
 
int write (char *data, int len)
 
int err () const
 
void interrupt ()
 
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)
 

Private Attributes

SocketIO m_io
 
int m_sock
 
int m_port
 
int m_sender
 
int m_errno
 
int m_int
 

Detailed Description

Definition at line 25 of file RSocketLib.h.

Constructor & Destructor Documentation

◆ RSocketSend()

RSocketSend ( u_short  port)

Definition at line 29 of file RSocketLib.cc.

30{
31
32 struct sockaddr_in sa;
33 bzero(&sa, sizeof(struct sockaddr_in));
34
35 m_errno = 0;
36
37 sa.sin_family = AF_INET;
38 sa.sin_port = htons(p);
39
40 int s;
41 if ((s = ::socket(AF_INET, SOCK_STREAM, 0)) < 0) {
42 m_errno = errno;
43 perror("SocketRecv::socket");
44 return;
45 }
46
47 int optval = 1;
48 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, 4);
49
50 int sizeval = D2_SOCKBUF_SIZE;
51 setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sizeval, 4);
52 setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sizeval, 4);
53
54 if ((bind(s, (struct sockaddr*)&sa, sizeof(sa))) < 0) {
55 m_errno = errno;
56 perror("RSocketSend::bind");
57 return;
58 }
59
60 m_sock = s;
61 m_port = (int)p;
62 m_sender = 0;
63 listen(s, 3);
64 printf("RSocketSend:: initialized, sock=%d\n", m_sock);
65
66 return;
67
68}

◆ ~RSocketSend()

Definition at line 70 of file RSocketLib.cc.

71{
72 if (m_sender > 0) {
73 shutdown(m_sender, 2);
74 ::close(m_sender);
75 printf("RSocketSend:: receiving socket %d closed\n", m_sender);
76 }
77
78 shutdown(m_sock, 2);
79 ::close(m_sock);
80 printf("RSocketSend:: connection socket %d closed\n", m_sock);
81}

Member Function Documentation

◆ accept()

int accept ( )

Definition at line 83 of file RSocketLib.cc.

84{
85 m_errno = 0;
86 struct sockaddr_in isa;
87 socklen_t i = sizeof(isa);
88 getsockname(m_sock, (struct sockaddr*)&isa, &i);
89
90 int t;
91 if ((t =::accept(m_sock, (struct sockaddr*)&isa, &i)) < 0) {
92 m_errno = errno;
93 return (-1);
94 }
95
96 printf("RSocketSend:: connection request accepted, sender=%d\n", t);
97
98 m_sender = t;
99 return (t);
100}

◆ close()

int close ( )

Definition at line 102 of file RSocketLib.cc.

103{
104 // ::close ( m_sender );
105 // m_sender = 0;
106 printf("RSocketSend: destructed, m_sender = %d\n", m_sender);
107 return 0;
108}

◆ err()

int err ( ) const

Definition at line 203 of file RSocketLib.cc.

204{
205 return m_errno;
206}

◆ examine()

int examine ( )

Definition at line 110 of file RSocketLib.cc.

111{
112 // printf("RSocketSend::examine(): waiting for client connection ...\n");
113
114 m_errno = 0;
115 fd_set ready;
116 FD_ZERO(&ready);
117 FD_SET(m_sock, &ready);
118 struct timeval to;
119 to.tv_sec = 0;
120 to.tv_usec = 0;
121 if (select(FD_SETSIZE, &ready, 0, 0, &to) < 0) {
122 m_errno = errno;
123 perror("select");
124 return (-1);
125 }
126 if (FD_ISSET(m_sock, &ready)) {
127 printf("RSocketSend::connected!!!!\n");
128 return (1);
129 } else
130 return (0);
131}

◆ get() [1/2]

int get ( char *  data,
int  len 
)

Definition at line 133 of file RSocketLib.cc.

134{
135 // printf("SocketSend::get()\n");
136
137 m_errno = 0;
138 if (m_sender > 0)
139 return m_io.get(m_sender, data, len);
140 else
141 return -1;
142}

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

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  )

Definition at line 208 of file RSocketLib.cc.

209{
210 m_io.interrupt();
211}

◆ port()

int port ( ) const

Definition at line 198 of file RSocketLib.cc.

199{
200 return m_port;
201}

◆ put() [1/2]

int put ( char *  data,
int  len 
)

Definition at line 153 of file RSocketLib.cc.

154{
155 // printf("RSocketSend::put (sd = %d)\n", m_sender);
156 m_errno = 0;
157 if (m_sender > 0)
158 return m_io.put(m_sender, data, len);
159 else
160 return -1;
161}

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

164{
165 // printf("RSocketSend::put (sd = %d)\n", m_sender);
166 m_errno = 0;
167 if (m_sender > 0)
168 return m_io.put_wordbuf(m_sender, data, len);
169 else
170 return -1;
171}

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

145{
146 m_errno = 0;
147 if (m_sender > 0)
148 return m_io.read_data(m_sender, data, len);
149 else
150 return -1;
151}

◆ 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}

◆ sender()

int sender ( ) const

Definition at line 193 of file RSocketLib.cc.

194{
195 return m_sender;
196}

◆ sock() [1/2]

int sock ( ) const

Definition at line 183 of file RSocketLib.cc.

184{
185 return m_sock;
186}

◆ sock() [2/2]

void sock ( int  sockid)

Definition at line 188 of file RSocketLib.cc.

189{
190 m_sock = sockid;
191}

◆ write()

int write ( char *  data,
int  len 
)

Definition at line 173 of file RSocketLib.cc.

174{
175 m_errno = 0;
176 if (m_sender > 0)
177 return m_io.write_data(m_sender, data, len);
178 else
179 return -1;
180}

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

◆ m_port

int m_port
private

Definition at line 51 of file RSocketLib.h.

◆ m_sender

int m_sender
private

Definition at line 52 of file RSocketLib.h.

◆ m_sock

int m_sock
private

Definition at line 50 of file RSocketLib.h.


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