///////////////////// // Wait until the socket contains some data or is writeable // TODO: remove this function, don't use it! void NetworkSocket::WaitForSocketReadOrWrite(int timeout) { if(!isOpen()) { errors << "WaitForSocketReadOrWrite: socket closed" << endl; return; } NLint group = nlGroupCreate(); nlGroupAddSocket(group, m_socket->sock); NLsocket s; if (timeout < 0) { // Infinite timeout while (true) { if (nlPollGroup(group, NL_READ_STATUS, &s, 1, 0)) break; if (nlPollGroup(group, NL_WRITE_STATUS, &s, 1, 0)) break; SDL_Delay(2); } } else if (timeout > 0) { // Blocking, with a timeout Uint32 start = SDL_GetTicks(); while (SDL_GetTicks() - start <= (Uint32)timeout) { if (nlPollGroup(group, NL_READ_STATUS, &s, 1, 0)) break; if (nlPollGroup(group, NL_WRITE_STATUS, &s, 1, 0)) break; SDL_Delay(2); } } nlGroupDestroy(group); }
bool NetworkSocket::isDataAvailable() { NLint group = nlGroupCreate(); nlGroupAddSocket( group, m_socket->sock ); NLsocket sock_out[2]; int ret = nlPollGroup( group, NL_READ_STATUS, sock_out, 1, 0 ); nlGroupDestroy(group); return ret > 0; }
////////////////////// // Wait until the socket contains some data to read // TODO: remove this function, don't use it! void NetworkSocket::WaitForSocketRead(int timeout) { if(!isOpen()) { errors << "WaitForSocketRead: socket closed" << endl; return; } NLint group = nlGroupCreate(); nlGroupAddSocket(group, m_socket->sock); NLsocket s; nlPollGroup(group, NL_READ_STATUS, &s, 1, (NLint)timeout); nlGroupDestroy(group); }
NLsocket loopback_Open(NLushort port, NLenum type) { nl_socket_t *newsock; NLsocket newsocket; NLint i; NLushort lport; switch (type) { case NL_RELIABLE: case NL_UNRELIABLE: case NL_RELIABLE_PACKETS: case NL_BROADCAST: break; default: nlSetError(NL_INVALID_ENUM); return NL_INVALID; } lport = loopback_TryPort(port, type); if(lport == 0) { nlSetError(NL_INVALID_PORT); return NL_INVALID; } newsocket = nlGetNewSocket(); if(newsocket == NL_INVALID) { return NL_INVALID; } newsock = nlSockets[newsocket]; newsock->type = type; newsock->localport = lport; if(type == NL_BROADCAST) { newsock->remoteport = lport; } for(i=0;i<NL_NUM_PACKETS;i++) { NLboolean err = NL_FALSE; /* malloc the max packet length plus two bytes for the port number */ if((newsock->inpacket[i] = (NLbyte *)malloc((size_t)(NL_MAX_PACKET_LENGTH + 2))) == NULL) { nlSetError(NL_OUT_OF_MEMORY); err = NL_TRUE; } if(err == NL_TRUE) { while(i-- > 0) { free(newsock->inpacket[i]); } sock_Close(newsocket); return NL_INVALID; } } (void)nlGroupAddSocket(loopgroup, newsocket); return newsocket; }
void NetworkSocket::EventHandler::SharedData::addSocketToGroup() { nlGroupAddSocket(nlGroup, sock->m_socket->sock); }