| 565 | |
| 566 | |
| 567 | int ServerLink::read(uint16_t& code, uint16_t& len, void* msg, int blockTime) { |
| 568 | code = MsgNull; |
| 569 | len = 0; |
| 570 | |
| 571 | if (state != Okay) { return -1; } |
| 572 | |
| 573 | if ((urecvfd >= 0) /* && ulinkup */) { |
| 574 | int n; |
| 575 | |
| 576 | if (!udpLength) { |
| 577 | size_t recvlen = sizeof(urecvaddr); |
| 578 | n = recvfrom(urecvfd, ubuf, MaxPacketLen, 0, &urecvaddr, |
| 579 | (socklen_t*) &recvlen); |
| 580 | if (n > 0) { |
| 581 | udpLength = n; |
| 582 | udpBufferPtr = ubuf; |
| 583 | } |
| 584 | } |
| 585 | if (udpLength) { |
| 586 | // unpack header and get message |
| 587 | udpLength -= 4; |
| 588 | if (udpLength < 0) { |
| 589 | udpLength = 0; |
| 590 | return -1; |
| 591 | } |
| 592 | udpBufferPtr = (char*)nboUnpackUInt16(udpBufferPtr, len); |
| 593 | udpBufferPtr = (char*)nboUnpackUInt16(udpBufferPtr, code); |
| 594 | UDEBUG("<** UDP Packet Code %x Len %x\n", code, len); |
| 595 | if (len > udpLength) { |
| 596 | udpLength = 0; |
| 597 | return -1; |
| 598 | } |
| 599 | memcpy((char*)msg, udpBufferPtr, len); |
| 600 | udpBufferPtr += len; |
| 601 | udpLength -= len; |
| 602 | return 1; |
| 603 | } |
| 604 | if (UDEBUGMSG) { printError("Fallback to normal TCP receive"); } |
| 605 | len = 0; |
| 606 | code = MsgNull; |
| 607 | |
| 608 | blockTime = 0; |
| 609 | } |
| 610 | |
| 611 | // block for specified period. default is no blocking (polling) |
| 612 | struct timeval timeout; |
| 613 | timeout.tv_sec = blockTime / 1000; |
| 614 | timeout.tv_usec = blockTime - 1000 * timeout.tv_sec; |
| 615 | |
| 616 | // only check server |
| 617 | fd_set read_set; |
| 618 | FD_ZERO(&read_set); |
| 619 | FD_SET((unsigned int)fd, &read_set); |
| 620 | int nfound = select(fd + 1, (fd_set*)&read_set, NULL, NULL, |
| 621 | (struct timeval*)(blockTime >= 0 ? &timeout : NULL)); |
| 622 | if (nfound == 0) { return 0; } |
| 623 | if (nfound < 0) { return -1; } |
| 624 |
no test coverage detected