15#include <netinet/in.h>
18#include "daq/roisend/util.h"
19#include "daq/roisend/b2_socket.h"
23b2_timed_blocking_io(
const int sd,
const int timeout )
33 ret = setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO, &tv,
sizeof(
struct timeval));
41 ret = setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(
struct timeval));
46 }
else if (timeout == 0) {
47 ret = fcntl(sd, F_SETFL, O_NDELAY);
52 }
else if (timeout < 0) {
53 ret = fcntl(sd, F_GETFL, O_NDELAY);
59 ret = fcntl(sd, F_SETFL, ret);
72b2_build_sockaddr_in(
const char* hostname,
const unsigned short port,
struct sockaddr_in* in)
74 memset(in, 0,
sizeof(
struct sockaddr_in));
76 in->sin_family = AF_INET;
78 struct hostent* hoste;
79 hoste = gethostbyname(hostname);
84 in->sin_addr = *(
struct in_addr*)(hoste->h_addr);
86 in->sin_port = htons(port);
94b2_create_tcp_socket(
void)
99 sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
106 ret = b2_timed_blocking_io(sd, 0);
108 ERROR(b2_timed_blocking_io);
113 ret = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (
char*)&one,
sizeof(
int));
125b2_create_accept_socket(
const unsigned short port)
128 struct sockaddr_in in;
131 sd = b2_create_tcp_socket();
133 ERROR(b2_create_tcp_socket);
137 ret = b2_build_sockaddr_in(
"0.0.0.0", port, &in);
139 ERROR(b2_build_sockaddr_in);
143 ret = bind(sd, (
const struct sockaddr*)&in,
sizeof(
struct sockaddr_in));
161b2_create_connect_socket(
const char* hostname,
const unsigned short port)
164 struct sockaddr_in in;
167 sd = b2_create_tcp_socket();
169 ERROR(b2_create_tcp_socket);
173 ret = b2_build_sockaddr_in(hostname, port, &in);
175 ERROR(b2_build_sockaddr_in);
179 ret = connect(sd, (
const struct sockaddr*)&in,
sizeof(
struct sockaddr_in));
180 if (ret == -1 && errno != EINPROGRESS) {
191b2_send(
const int sd,
const void* buf,
const size_t size)
193 unsigned char* ptr = (
unsigned char*)buf;
194 size_t n_bytes_remained = size;
198 int ret, n_bytes_send;
200 ret = send(sd, ptr, n_bytes_remained, 0);
201 if (ret == -1 && errno != EINTR) {
205 if (ret == -1 && errno == EINTR) {
206 fprintf(stderr,
"%s:%d: recv(): Packet send timed out\n", __FILE__, __LINE__);
210 fprintf(stderr,
"%s:%d: send(): Connection closed\n", __FILE__, __LINE__);
217 if (n_bytes_remained <
size_t(n_bytes_send))
220 fprintf(stderr,
"%s:%d: send(): Internal error\n", __FILE__, __LINE__);
223 n_bytes_remained -= n_bytes_send;
225 if (n_bytes_remained == 0)
238b2_recv(
const int sd,
void* buf,
const size_t size)
240 unsigned char* ptr = (
unsigned char*)buf;
241 size_t n_bytes_remained = size;
245 int ret, n_bytes_recv;
247 ret = recv(sd, ptr, n_bytes_remained, 0);
248 if (ret == -1 && (errno != EINTR && errno != EWOULDBLOCK)) {
252 if (ret == -1 && (errno == EINTR || errno == EWOULDBLOCK)) {
253 fprintf(stderr,
"%s:%d: recv(): Packet receive timed out\n", __FILE__, __LINE__);
257 fprintf(stderr,
"%s:%d: recv(): Connection closed\n", __FILE__, __LINE__);
263 if (n_bytes_remained <
size_t(n_bytes_recv))
266 fprintf(stderr,
"%s:%d: recv(): Internal error\n", __FILE__, __LINE__);
269 n_bytes_remained -= n_bytes_recv;
271 if (n_bytes_remained == 0)