Esempio n. 1
0
void TcpClient::ExampleRun()
{
	const std::string host = "127.0.0.1";
	int port = 666;

	ConnectToHost(host, port);
	ShowEndpointInfos();
	_isRunning = true;

	while (_isRunning)
	{
		std::string request;
		
		std::cout << "Enter request: ";
		getline(std::cin, request);
		
		if (request == "END")
			_isRunning = false;
		else if (request == "")
			request = "HELLO-HOW-SHOULD-I-WORK";

		SendHttpRequest(host, request);

		WaitForResponse();
		
		ReceiveResponse(true);

		//DisplayResults();
		//_isRunning = false;
	}
	
	CloseConnection();
}
Esempio n. 2
0
/**************************************************************
功能:执行一次 HTTP 请求
***************************************************************/
void DoOnce() { /* send and receive */
ConnectWeb(); /* connect to the web server */

/* send a request */
SendRequest();

/* receive a response message from web server */
ReceiveResponse();

close(sockfd); /* because HTTP protocol do something one connection, so I can close it after receiving */
}
bool FHTTPTransport::SendPayloadAndReceiveResponse(TArray<uint8>& In, TArray<uint8>& Out)
{
	RecieveBuffer.Empty();
	ReadPtr = 0; 

#if !PLATFORM_HTML5
	
	if (GIsRequestingExit) // We have already lost HTTP Module. 
		return false; 

	class HTTPRequestHandler
	{

	public:
		HTTPRequestHandler(TArray<uint8>& InOut)
			:Out(InOut)
		{} 
		void HttpRequestComplete(	FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded)
		{
			if (HttpResponse.IsValid())
				Out.Append(HttpResponse->GetContent());
		}
	private: 
		TArray<uint8>& Out; 
	};

	HTTPRequestHandler Handler(RecieveBuffer);

	HttpRequest->OnProcessRequestComplete().BindRaw(&Handler,&HTTPRequestHandler::HttpRequestComplete );
	if ( In.Num() )
	{
		HttpRequest->SetVerb("POST");

		FBufferArchive Ar; 
		Ar << Guid; 
		Ar.Append(In); 

		HttpRequest->SetContent(Ar);
	}
	else
	{
		HttpRequest->SetVerb("GET");
	}

	HttpRequest->ProcessRequest();

	FDateTime StartTime; 
	FTimespan Span = FDateTime::UtcNow() - StartTime; 

	while( HttpRequest->GetStatus() != EHttpRequestStatus::Failed && HttpRequest->GetStatus() != EHttpRequestStatus::Succeeded &&  Span.GetSeconds() < 10 )
	{
		HttpRequest->Tick(0);
		Span = FDateTime::UtcNow() - StartTime; 
	}


	if (HttpRequest->GetStatus() == EHttpRequestStatus::Succeeded)
		return true; 

	HttpRequest->CancelRequest();

	return false; 
#else  // PLATFORM_HTML5

	FBufferArchive Ar; 
	if ( In.Num() )
	{
		Ar << Guid; 
	}

	Ar.Append(In); 
	unsigned char *OutData = NULL; 
	unsigned int OutSize= 0; 

	bool RetVal = true; 

#if PLATFORM_HTML5_WIN32
	RetVal = HTML5Win32::NFSHttp::SendPayLoadAndRecieve(Ar.GetData(), Ar.Num(), &OutData, OutSize);
#endif 
#if PLATFORM_HTML5_BROWSER
	UE_SendAndRecievePayLoad(TCHAR_TO_ANSI(Url),(char*)Ar.GetData(),Ar.Num(),(char**)&OutData,(int*)&OutSize);
#endif 

	if (!Ar.Num())
	{
		uint32 Size = OutSize;
		uint32 Marker = 0xDeadBeef; 
		RecieveBuffer.Append((uint8*)&Marker,sizeof(uint32));
		RecieveBuffer.Append((uint8*)&Size,sizeof(uint32));
	}


	if (OutSize)
	{
		RecieveBuffer.Append(OutData,OutSize); 

#if PLATFORM_HTML5_WIN32
		free (OutData); 
#endif 
#if PLATFORM_HTML5_BROWSER
		// don't go through the Unreal Memory system. 
		::free(OutData);
#endif 

	}

	return RetVal & ReceiveResponse(Out); 
#endif 
}