예제 #1
0
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;
}
예제 #2
0
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);
}
예제 #3
0
void UDPSocket::send(const Address& address, const ByteQueue& data) {
#ifdef _WIN32
  SOCKADDR_IN servaddr;
  memset((void*)&servaddr, 0, sizeof(SOCKADDR_IN));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = address.ip;
  servaddr.sin_port = address.port;
  sendto(sd, (const char*)data.ptr(), data.size(), 0, (SOCKADDR*)&servaddr, sizeof(SOCKADDR_IN));
#else
  sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(sockaddr_in));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = address.ip;
  servaddr.sin_port = address.port;
  sendto(sd, data.ptr(), data.size(), 0, (const sockaddr*)&servaddr, sizeof(sockaddr_in));
#endif
}