// if timeout occurs, nbytes=-1, nresult=1 // if socket error, nbyte=-1, nresult=-1 // if the other side has disconnected in either block mode or nonblock mode, nbytes=0, nresult=-1 void SocketTryRecv(HSocket hs, char* ptr, int nbytes, int milliseconds, transresult_t& rt) { rt.nbytes = 0; rt.nresult = 0; if(!ptr|| nbytes<1) { return; } if(milliseconds>2) { CMyTimeSpan start; while(1) { rt.nbytes = recv(hs, ptr, nbytes, NONBLOCKREADWRITE); if(rt.nbytes>0) { break; } else if(rt.nbytes==0) { rt.nresult = -1; break; } else { rt.nresult = GetLastSocketError(); if( ETRYAGAIN(rt.nresult)) { if(start.GetSpaninMilliseconds() > (unsigned int)milliseconds) { rt.nresult= 1; break; } //CLightThread::DiscardTimeSlice(); int dwSleep=20; std::this_thread::sleep_for(std::chrono::milliseconds(dwSleep*1000)); } else { rt.nresult = -1; break; } } } } else { SocketRecv(hs, ptr, nbytes, rt); } }
/*************************************************** ReceiveLine Receives a line of data from the currently connected client, or receives until the maxDataLen is reached, or if the time-out has been reached. Params data - buffer for incomming data maxDataLen - length of the buffer timeOut - timeout in seconds ( 0- for no time-out) Return number of bytes received 0 if the connection was closed or timed-out any other error returns SOCKET_ERROR ****************************************************/ int CUT_WSThread::ReceiveLine(LPSTR data,int maxDataLen,int timeOut){ int x, rt, count = 0; LPSTR dataPtr = data; maxDataLen --; while(maxDataLen > 0) { // Wait for a data up til the timeout value if(timeOut > 0) if(WaitForReceive(timeOut,0) != UTE_SUCCESS) return count; // Look at the data coming in rt = SocketRecv(m_clientSocket,(char *)dataPtr,maxDataLen, MSG_PEEK); // Error checking if(rt < 1) return count; // Find the fist CR/LF for(x = 0; x < rt; x++) { if(dataPtr[x] == '\r' && dataPtr[x+1] == '\n') { rt = SocketRecv(m_clientSocket,(char *)dataPtr,x+2,0); dataPtr[x+2]=0; count += x+2; if(count == 0) count = 0; return count; } } // If the CR/LF is not found then copy what is available rt = SocketRecv(m_clientSocket,(char *)dataPtr,rt,0); count += rt; dataPtr[rt]=0; dataPtr = &dataPtr[rt]; maxDataLen -= rt; } return count; }
void GDBStubUpdate(struct GDBStub* stub) { if (stub->socket == INVALID_SOCKET) { if (stub->d.state == DEBUGGER_PAUSED) { stub->d.state = DEBUGGER_RUNNING; } return; } if (stub->connection == INVALID_SOCKET) { if (stub->shouldBlock) { Socket reads = stub->socket; SocketPoll(1, &reads, 0, 0, SOCKET_TIMEOUT); } stub->connection = SocketAccept(stub->socket, 0); if (!SOCKET_FAILED(stub->connection)) { if (!SocketSetBlocking(stub->connection, false)) { goto connectionLost; } mDebuggerEnter(&stub->d, DEBUGGER_ENTER_ATTACHED, 0); } else if (SocketWouldBlock()) { return; } else { goto connectionLost; } } while (true) { if (stub->shouldBlock) { Socket reads = stub->connection; SocketPoll(1, &reads, 0, 0, SOCKET_TIMEOUT); } ssize_t messageLen = SocketRecv(stub->connection, stub->line, GDB_STUB_MAX_LINE - 1); if (messageLen == 0) { goto connectionLost; } if (messageLen == -1) { if (SocketWouldBlock()) { return; } goto connectionLost; } stub->line[messageLen] = '\0'; mLOG(DEBUGGER, DEBUG, "< %s", stub->line); ssize_t position = 0; while (position < messageLen) { position += _parseGDBMessage(stub, &stub->line[position]); } } connectionLost: mLOG(DEBUGGER, WARN, "Connection lost"); GDBStubHangup(stub); }
// if timeout occurs, nbytes=-1, nresult=1 // if socket error, nbyte=-1, nresult=-1 // if the other side has disconnected in either block mode or nonblock mode, nbytes=0, nresult=-1 void SocketTryRecv(HSocket hs, char *ptr, int nbytes, int milliseconds, transresult_t &rt) { rt.nbytes = 0; rt.nresult = 0; if(!ptr|| nbytes<1) return; if(milliseconds>2) { CMyTimeSpan start; while(1) { rt.nbytes = recv(hs, ptr, nbytes, NONBLOCKREADWRITE); if(rt.nbytes>0) { break; } else if(rt.nbytes==0) { rt.nresult = -1; break; } else { rt.nresult = GetLastSocketError(); if( ETRYAGAIN(rt.nresult)) { if(start.GetSpaninMilliseconds()>milliseconds) { rt.nresult= 1; break;} CLightThread::DiscardTimeSlice(); } else { rt.nresult = -1; break; } } } } else { SocketRecv(hs, ptr, nbytes, rt); } }
static bool _mPerfRunServer(const char* listen, const struct mArguments* args, const struct PerfOpts* perfOpts) { SocketSubsystemInit(); Socket server = SocketOpenTCP(7216, NULL); if (SOCKET_FAILED(server)) { SocketSubsystemDeinit(); return false; } if (SOCKET_FAILED(SocketListen(server, 0))) { SocketClose(server); SocketSubsystemDeinit(); return false; } _socket = SocketAccept(server, NULL); if (perfOpts->csv) { const char* header = "game_code,frames,duration,renderer\n"; SocketSend(_socket, header, strlen(header)); } char path[PATH_MAX]; memset(path, 0, sizeof(path)); ssize_t i; while ((i = SocketRecv(_socket, path, sizeof(path) - 1)) > 0) { path[i] = '\0'; char* nl = strchr(path, '\n'); if (nl == path) { break; } if (nl) { nl[0] = '\0'; } if (!_mPerfRunCore(path, args, perfOpts)) { break; } memset(path, 0, sizeof(path)); } SocketClose(_socket); SocketClose(server); SocketSubsystemDeinit(); return true; }
static PRInt32 PR_CALLBACK SocketRead(PRFileDesc *fd, void *buf, PRInt32 amount) { return SocketRecv(fd, buf, amount, 0, PR_INTERVAL_NO_TIMEOUT); }
transresult_t CSockWrap::Recv(void* ptr, int nbytes ) { transresult_t rt; SocketRecv(m_hSocket, (char*)ptr, nbytes,rt); return rt; }