SOCKET UDPSocket::create(Uint16 port) { if (sid != SOCKET_ERROR) { WSACloseEvent(event); closesocket(sid); } sid = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sid == SOCKET_ERROR) { printf("socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) error : %s [%i]\n", WSAGetErrorString(WSAGetLastError()), WSAGetLastError()); return (SOCKET)SOCKET_ERROR; } INADDR sa(0, port); if (bind(sid, sa.getAddress(), sizeof(INADDR)) == SOCKET_ERROR) { printf("bind(%i) error\n", port); return (SOCKET)SOCKET_ERROR; } event = WSACreateEvent(); WSAEventSelect(sid, event, FD_READ); return sid; }
void beginWinsock() { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 0), &wsaData) == SOCKET_ERROR) { printf("WSAStartup(0x202, &wsaData) error : %s [%i]\n", WSAGetErrorString(WSAGetLastError()), WSAGetLastError()); } }
void EventManager::Run() { printf("RUN START\n"); SOCKET local_pipe[2]; m_pipelock.Lock(); memcpy(local_pipe, m_pipe, sizeof(SOCKET) * 2); m_pipelock.Unlock(); LARGE_INTEGER run_start; QueryPerformanceCounter(&run_start); RetVal status = ReturnCode::SUCCESS; //while( running() && (status == ReturnCode::SUCCESS) ) while( (status == ReturnCode::SUCCESS) ) { //m_lock.Lock(); // set fds bits and determine highest file descriptor fd_set fds; FD_ZERO(&fds); int fd_max = 0; for (EventList_t::iterator i = m_events.begin(); i != m_events.end(); i++) { const FDEventBase* ev = *i; if (ev == NULL) // TODO: How can ev be NULL? continue; const SOCKET fd = ev->fd(); if (fd == INVALID_SOCKET) continue; fd_max = SOCKETMAX(fd_max, fd); // HACK: // HACK: FD_SET(fd, &fds); } struct timeval tv; tv.tv_sec = 2; tv.tv_usec = 500 * 1000; //m_lock.Unlock(); FD_SET(local_pipe[0], &fds); fd_max = SOCKETMAX(fd_max, local_pipe[0]); // Windows' select MUST have a valid fd, but the first (fd_max) parameter is ignored const int ret = select(fd_max + 1, &fds, NULL, &fds, &tv); //m_lock.Lock(); switch(ret) { case 0: // timedout //BVT_LOG(LOG_TRACE, "EventManager - select timeout"); printf("EventManager - select timeout\n"); { std::wstring errorstring = WSAGetErrorString(WSAGetLastError()); wprintf(L"Error: %s\n", errorstring.c_str()); } status = ReturnCode::SUCCESS; break; case -1: // error //BVT_LOG(LOG_DEBUG, "EventManager - select returned -1 %d %s", errno, strerror(errno)); printf("EventManager - select failed\n"); if (errno != EINTR) status = ReturnCode::SOCKET_READ; break; default: // data // printf("EventManager - select returned at least one FD ready\n"); for (EventList_t::iterator i = m_events.begin(); i != m_events.end(); i++) { SOCKET fd = (*i)->fd(); if (fd != INVALID_SOCKET && FD_ISSET(fd, &fds)) // NB: fd could be zero and valid if stdin is closed before a socket is created! { //BVT_LOG(LOG_DEBUG, "EventManager - got data on fd %d", (*i)->fd() ); (*i)->ReadableEvent(this); } } if (FD_ISSET(local_pipe[0], &fds)) { char discard; LARGE_INTEGER start, stop; QueryPerformanceCounter(&start); if (kCancelLength != recv(local_pipe[0], &discard, kCancelLength, 0)) { BVT_LOG("EventManager cancel socket error\n"); } else { Sleep(1); } // Sleep(1); QueryPerformanceCounter(&stop); goto LABEL_DONE; // HACK: skips unlock } } // Sleep(1); //m_lock.Unlock(); } LABEL_DONE: LARGE_INTEGER run_stop; QueryPerformanceCounter(&run_stop); LONGLONG total = run_stop.QuadPart - run_start.QuadPart; printf("run total: %ld\n", total); closesocket(local_pipe[0]); closesocket(local_pipe[1]); // if( status != BVT::ReturnCode::SUCCESS ) BVT_LOG("EventManager::Run() exiting: retval:%d errno:%d\n", status, errno); }