43 for (
size_t iWord = 0; iWord < size; iWord++) {
44 ss <<
"0x" << std::hex << std::setfill(
'0') << std::setw(4) << iWord;
45 ss <<
": 0x" << std::hex << std::setfill(
'0') << std::setw(2) << int(data[iWord]);
48 ss << std::hex << std::setfill(
'0') << std::setw(2) << int(data[iWord]);
113 if (port_offset > 128) {
114 BUException::BNL_UDP_PORT_OUT_OF_RANGE e;
125 struct addrinfo* res;
126 if (getaddrinfo(address.c_str(), NULL, NULL, &res)) {
128 if (address.size() <= 5 && 1 == std::count(address.begin(), address.end(),
'.')) {
129 std::string strCrate = address.substr(0, address.find(
'.'));
130 std::string strSlot = address.substr(address.find(
'.') + 1);
131 if (strCrate.size() != 0 && strSlot.size() != 0) {
132 uint8_t crate = strtoul(strCrate.c_str(), NULL, 0);
133 uint8_t slot = strtoul(strSlot.c_str(), NULL, 0);
134 if ((((crate > 0) && (crate < 7)) || 0xF == crate) && (((slot > 0) && (slot < 7)) || 0xF == slot)) {
155 if (getaddrinfo(address.c_str(), NULL, NULL, &res)) {
156 BUException::BAD_REMOTE_IP e;
158 e.Append(address.c_str());
159 e.Append(
" could not be resolved.\n");
167 if ((
readSocketFD = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
168 BUException::BAD_SOCKET e;
169 e.Append(
"read socket\n");
173 BUException::BAD_SOCKET e;
174 e.Append(
"write socket\n");
181 setsockopt(
readSocketFD, SOL_SOCKET, SO_RCVTIMEO, (
const char*)&tv,
sizeof(
struct timeval));
182 setsockopt(
writeSocketFD, SOL_SOCKET, SO_RCVTIMEO, (
const char*)&tv,
sizeof(
struct timeval));
185 readAddr = *((
struct sockaddr_in*)res->ai_addr);
188 BUException::CONNECTION_FAILED e;
189 e.Append(
"read socket connect\n");
190 e.Append(strerror(errno));
194 writeAddr = *((
struct sockaddr_in*)res->ai_addr);
197 BUException::CONNECTION_FAILED e;
198 e.Append(
"write socket connect\n");
199 e.Append(strerror(errno));
241 packet.
data_MSW = htons(uint16_t((value >> 16) & 0xFFFF));
242 packet.
data_LSW = htons(uint16_t((value >> 0) & 0xFFFF));
246 ssize_t send_size =
sizeof(packet);
247 ssize_t sent_size = 0;
248 if (send_size != (sent_size = send(
writeSocketFD, &packet, send_size, 0))) {
250 BUException::SEND_FAILED e;
251 if (sent_size == -1) {
252 e.Append(
"BNL_UDP::Write(uint16_t,uint32_t)\n");
253 e.Append(
"Errnum: ");
254 e.Append(strerror(errno));
263 if (-1 == reply_size) {
264 BUException::BAD_REPLY e;
265 std::stringstream ss;
266 e.Append(
"BNL_UDP::Write(uint16_t,uint32_t)\n");
267 ss <<
"Errnum(" << errno <<
"): " << strerror(errno) <<
"\n";
268 e.Append(ss.str().c_str());
269 e.Append(
dump_packet((uint8_t*)&packet, send_size).c_str());
272 BUException::BAD_REPLY e;
273 std::stringstream ss;
274 ss <<
"Bad Size: " << reply_size <<
"\n";
275 e.Append(
"BNL_UDP::Write(uint16_t,uint32_t)\n");
276 e.Append(ss.str().c_str());
280 uint16_t reply_address = uint16_t(
buffer[0] << 8 |
buffer[1]);
281 if (reply_address != address) {
282 BUException::BAD_REPLY e;
283 std::stringstream ss;
284 ss <<
"Bad address: " << uint32_t(address) <<
" != " << uint32_t(reply_address) <<
"\n";
285 e.Append(
"BNL_UDP::Write(uint16_t,uint32_t)\n");
286 e.Append(ss.str().c_str());
424 ssize_t send_size =
sizeof(packet);
425 ssize_t sent_size = 0;
426 if (send_size != (sent_size = send(
readSocketFD, &packet, send_size, 0))) {
428 BUException::SEND_FAILED e;
429 if (sent_size == -1) {
430 e.Append(
"BNL_UDP::Read(uint16_t)\n");
431 e.Append(
"Errnum: ");
432 e.Append(strerror(errno));
440 if (ssize_t(-1) == reply_size) {
441 BUException::BAD_REPLY e;
442 std::stringstream ss;
443 e.Append(
"BNL_UDP::Read(uint16_t)\n");
444 ss <<
"Errnum(" << errno <<
"): " << strerror(errno) <<
"\n";
445 e.Append(ss.str().c_str());
446 e.Append(
dump_packet((uint8_t*)&packet, send_size).c_str());
449 BUException::BAD_REPLY e;
450 std::stringstream ss;
451 ss <<
"Bad Size: " << reply_size <<
"\n";
452 e.Append(
"BNL_UDP::Read(uint16_t)\n");
453 e.Append(ss.str().c_str());
457 uint16_t reply_address = uint16_t(
buffer[0] << 8 |
buffer[1]);
458 if (reply_address != address) {
459 BUException::BAD_REPLY e;
460 std::stringstream ss;
461 ss <<
"Bad address: " << uint32_t(address) <<
" != " << uint32_t(reply_address) <<
"\n";
462 e.Append(
"BNL_UDP::Read(uint16_t)\n");
463 e.Append(ss.str().c_str());
469 uint32_t ret = ((uint32_t(
buffer[2]) << 24) | (uint32_t(
buffer[3]) << 16) | (uint32_t(
buffer[4]) << 8) |
470 (uint32_t(
buffer[5]) << 0));