ByteQueue UDPSocket::recv(Address& address) { ByteQueue data; #ifdef _WIN32 fd_set fds; timeval timeout; FD_ZERO(&fds); FD_SET(sd, &fds); timeout.tv_sec = 0; timeout.tv_usec = 0; if (select(0, &fds, nullptr, nullptr, &timeout) > 0) { SOCKADDR_IN addr; int addrsize = sizeof(SOCKADDR_IN); data.resize(maxlen); data.resize(uint32_t(recvfrom(SOCKET(sd), (char*)data.ptr(), maxlen, 0, (SOCKADDR*)&addr, &addrsize))); address = Address(addr.sin_addr.S_un.S_addr, addr.sin_port); } #else fd_set fds; timeval timeout; FD_ZERO(&fds); FD_SET(sd, &fds); timeout.tv_sec = 0; timeout.tv_usec = 0; if (select(sd + 1, &fds, nullptr, nullptr, &timeout) > 0) { sockaddr_in addr; socklen_t addrsize = sizeof(sockaddr_in); data.resize(maxlen); data.resize(uint32_t(recvfrom(sd, data.ptr(), maxlen, 0, (sockaddr*)&addr, &addrsize))); address = Address(addr.sin_addr.s_addr, addr.sin_port); } #endif return data; }
void ByteQueue::pop(ByteQueue& data) { uint32_t total = data.size() <= buf.size() ? data.size() : buf.size(); if (!total) return; memcpy(data.ptr(), (const void*)&buf[0], total); buf.erase(buf.begin(), buf.begin() + total); data.resize(total); }