Example #1
0
int ThreadData::recv(char* data, size_t datalen)
{
	NETLIBBUFFER nlb = { data, (int)datalen, 0 };

	if (!proto->usingGateway) {
		resetTimeout();
		NETLIBSELECT nls = { 0 };
		nls.cbSize = sizeof(nls);
		nls.dwTimeout = 1000;
		nls.hReadConns[0] = s;

		for (;;) {
			int ret = CallService(MS_NETLIB_SELECT, 0, (LPARAM)&nls);
			if (ret < 0) {
				proto->debugLogA("Connection abortively closed, error %d", WSAGetLastError());
				return ret;
			}
			else if (ret == 0) {
				if (isTimeout()) return 0;
			}
			else
				break;
		}
	}

LBL_RecvAgain:
	int ret = CallService(MS_NETLIB_RECV, (WPARAM)s, (LPARAM)&nlb);
	if (ret == 0) {
		proto->debugLogA("Connection closed gracefully");
		return 0;
	}

	if (ret < 0) {
		proto->debugLogA("Connection abortively closed, error %d", WSAGetLastError());
		return ret;
	}

	if (proto->usingGateway) {
		if (ret == 1 && *data == 0) {
			if (sessionClosed || isTimeout()) return 0;
			if ((mGatewayTimeout += 2) > 20) mGatewayTimeout = 20;

			CallService(MS_NETLIB_SETPOLLINGTIMEOUT, WPARAM(s), mGatewayTimeout);
			goto LBL_RecvAgain;
		}
		else {
			resetTimeout();
			mGatewayTimeout = 1;
			CallService(MS_NETLIB_SETPOLLINGTIMEOUT, WPARAM(s), mGatewayTimeout);
		}
	}

	return ret;
}
State RtttlPlayer::PLAYING_NOTE() {
    if (isTimeout(_noteDuration)) {
        noTone(_buzzerPin);
        _noteDuration *= 0.2; // pause between notes
        setState((PState) &RtttlPlayer::PAUSE_BETWEEN_NOTES);
    }
}
State RtttlPlayer::PAUSE_BETWEEN_NOTES() {
    if (isTimeout(_noteDuration))
        setState((PState) &RtttlPlayer::PLAYING);
}
C_RESULT video_navdata_handler_process( const navdata_unpacked_t* const navdata )
{
 /* Compute time between two prevous navdata to detect a timeout
  * (i.e. more than 1 sec between two navdatas)
  */
  static int firstTime = 1;
#if _WIN32
  static DWORD prev, now;
#else
  static struct timeval prev, now;
#endif
  getTime (now);
  if (1 == firstTime)
  {
    firstTime = 0;
  }
  else
  {
  	int flag = isTimeout (now, prev);
    if (flag)
    {
  	  video_com_stage_notify_timeout ();
    }
  }

  /* Video storage navdatas */
  hdvideo_remaining_frames = navdata->navdata_hdvideo_stream.storage_fifo_nb_packets;
  hdvideo_remaining_kilobytes = navdata->navdata_hdvideo_stream.storage_fifo_size;
  hdvideo_maximum_kilobytes = ardrone_control_config.video_storage_space;
  if (NAVDATA_HDVIDEO_STORAGE_FIFO_IS_FULL & navdata->navdata_hdvideo_stream.hdvideo_state)
    {
      // Socket is flagged as full 
      hdvideo_fifo_fill_percentage = 100.0;
    }
  else if ((0 == hdvideo_maximum_kilobytes) ||
           (hdvideo_maximum_kilobytes < hdvideo_remaining_kilobytes))
    {
      // Unexpected result (we don't have the total size
      // or the current size is greater than the total size)
      hdvideo_fifo_fill_percentage = -1.0;
    }
  else
    {
      // Normal case
      hdvideo_fifo_fill_percentage = (hdvideo_remaining_kilobytes * 100.0) / (hdvideo_maximum_kilobytes * 1.0);
    }

  // Process check
  if (0 != hdvideo_frames_to_retreive &&
      hdvideo_frames_to_retreive >= hdvideo_remaining_frames)
    {
      uint32_t _retreived_frames = hdvideo_frames_to_retreive - hdvideo_remaining_frames;
      hdvideo_retrieving_progress = (float)(_retreived_frames) / (float)(hdvideo_frames_to_retreive);
    }
  else
    {
      hdvideo_retrieving_progress = -1.0;
    }

  getTime (prev);
  return C_OK;
}
Example #5
0
        virtual void run() {
            do {
                if (!setSendTimeout(_sockfd, 1)) {
                    LogError("setSendTimeout failed: %d", getLastErrorNo());
                    break;
                }

                if (!setRecvTimeout(_sockfd, 1)) {
                    LogError("setRecvTimeout failed: %d", getLastErrorNo());
                    break;
                }
                HttpRequest httpRequest;
                int recvLen, parsedLength, unparsed = 0;

                while ((_startTime == 0 || time(nullptr) - _startTime <= _timeout) && _requestCount > 0) {
                    recvLen = recv(_sockfd, _recvBuff + unparsed, RECV_BUFFER_SIZE - unparsed, 0);
                    if (recvLen <= 0 && isTimeout()) {
                        LogError("recv returns %d", recvLen);
                        continue;
                    }
                    if (recvLen <= 0) {
                        LogError("recv failed: %d", getLastErrorNo());
                        break;
                    }

                    unparsed += recvLen;
                    parsedLength = httpRequest.parse(_recvBuff);

                    if (parsedLength < 0) {
                        LogError("parsed wrong http protocol format");
                        break;
                    }

                    if (parsedLength == 0) continue;

                    if (parsedLength <= unparsed) {
                        unparsed -= parsedLength;
                        memmove(_recvBuff, _recvBuff + parsedLength, unparsed);
                    }

                    if (httpRequest.readBodyFinished()) {
                        if (_startTime == 0) _startTime = time(nullptr);
                        
                        string connection;
                        if (httpRequest.getHeaderByKey("CONNECTION", connection) && connection == "close") _requestCount = 1;

                        _requestCount--;

                        string response = _httpRequestHandler.handle(httpRequest);
                        if (!_send(response)) {
                            LogError("send http response failed");
                            break;
                        }

                        httpRequest.reset();
                    }
                }
            } while (false);

            if (closeSocket(_sockfd) < 0) {
                LogError("close socket failed: %d", getLastErrorNo());
            }
        }
Example #6
0
int main(void){
    char time[15] = "20160329112319";
    int n = 3;
   printf("%d\n", isTimeout(time, n));    
    return 0;
}