int UDP_Socket::receive(IPaddress &ip, const void * const &data, const Uint16 &num_bytes) { IPaddress ipaddress = {0, 0}; UDPpacket packet = { -1, reinterpret_cast<Uint8 *>(const_cast<void *>(data)), 0, num_bytes, 0, ipaddress }; int retval = SDLNet_UDP_Recv(sock, &packet); if(retval == -1) throw Socket_Closed(); else if(!retval) { packet.address.host = 0; packet.address.port = 0; } ip = packet.address; return packet.len; }
int TCP_Socket::receive(void * const &data, const Uint16 &num_bytes) { int retval = check_socket(); if(retval) { retval = SDLNet_TCP_Recv(sock, data, num_bytes); if(retval <= 0) throw Socket_Closed(); } return retval; }
void UDP_Socket::send(const IPaddress &ip, const void * const &data, const Uint16 &num_bytes) { if(num_bytes < 8167u) { UDPpacket packet = { -1, reinterpret_cast<Uint8 *>(const_cast<void *>(data)), num_bytes, num_bytes, 0, // Will == -1 on error after UDP_Send, otherwise == # of bytes sent ip }; if(!SDLNet_UDP_Send(sock, -1, &packet)) throw Socket_Closed(); } throw UDP_Packet_Overflow(); }
void TCP_Socket::send(const void * const &data, const Uint16 &num_bytes) { if(SDLNet_TCP_Send(sock, const_cast<void *>(data), num_bytes) < num_bytes) throw Socket_Closed(); }
int TCP_Socket::check_socket() { int retval = SDLNet_CheckSockets(sockset, 0); if(retval == -1) throw Socket_Closed(); return retval; }