예제 #1
0
void HTTPConnection::Update(void)
{
    SystemAddress sa;
    sa = tcp->HasCompletedConnectionAttempt();
    while (sa!=UNASSIGNED_SYSTEM_ADDRESS)
    {
//		printf("Connected\n");
        connectionState=CS_CONNECTED;
        server=sa;
        sa = tcp->HasCompletedConnectionAttempt();
    }

    sa = tcp->HasFailedConnectionAttempt();
    while (sa!=UNASSIGNED_SYSTEM_ADDRESS)
    {
        //printf("Failed connected\n");
        CloseConnection();
        sa = tcp->HasFailedConnectionAttempt();
    }

    sa = tcp->HasLostConnection();
    while (sa!=UNASSIGNED_SYSTEM_ADDRESS)
    {
        //printf("Lost connection\n");
        CloseConnection();
        sa = tcp->HasLostConnection();
    }


    switch (connectionState)
    {
    case CS_NONE:
    {
        if (outgoingCommand.IsEmpty())
            return;

        //printf("Connecting\n");
        server = tcp->Connect(host, port, false);
        connectionState = CS_CONNECTING;
    }
    break;
    case CS_DISCONNECTING:
    {
        if (tcp->ReceiveHasPackets()==false)
        {
            if (incomingData.IsEmpty()==false)
            {
                results.Push(incomingData, _FILE_AND_LINE_ );
            }
            incomingData.Clear();
            tcp->CloseConnection(server);
            connectionState=CS_NONE;
        }
    }
    break;
    case CS_CONNECTING:
    {
    }
    break;
    case CS_CONNECTED:
    {
        //printf("Connected\n");
        if (outgoingCommand.IsEmpty())
        {
            //printf("Closed connection (nothing to do)\n");
            CloseConnection();
            return;
        }

#if OPEN_SSL_CLIENT_SUPPORT==1
        tcp->StartSSLClient(server);
#endif

        //printf("Sending request\n");
        currentProcessingCommand = outgoingCommand.Pop();
        RakString request;
        if (currentProcessingCommand.isPost)
        {
            request.Set("POST %s HTTP/1.0\r\n"
                        "Host: %s:%i\r\n"
                        "Content-Type: %s\r\n"
                        "Content-Length: %u\r\n"
                        "\r\n"
                        "%s",
                        currentProcessingCommand.remotePath.C_String(),
                        host.C_String(),
                        port,
                        currentProcessingCommand.contentType.C_String(),
                        (unsigned) currentProcessingCommand.data.GetLength(),
                        currentProcessingCommand.data.C_String());
        }
        else
        {
            // request.Set("GET %s\r\n", host.C_String());
            // http://www.jenkinssoftware.com/forum/index.php?topic=4601.0;topicseen
            request.Set("GET %s HTTP/1.0\r\n"
                        "Host: %s:%i\r\n"
                        "\r\n",
                        currentProcessingCommand.remotePath.C_String(),
                        host.C_String(),
                        port);
        }

        //	printf(request.C_String());
        //		request.URLEncode();
        tcp->Send(request.C_String(), (unsigned int) request.GetLength(), server,false);
        connectionState=CS_PROCESSING;
    }
    break;
    case CS_PROCESSING:
    {
    }
    }

//	if (connectionState==CS_PROCESSING && currentProcessingCommand.data.IsEmpty()==false)
//		outgoingCommand.PushAtHead(currentProcessingCommand);
}
예제 #2
0
파일: main.cpp 프로젝트: jochao/HoloToolkit
void main_RakNet(void)
{
	const char *serverURL = "localhost";
	//const char *serverURL = "lobby3.raknet.com";
	//const unsigned int serverPort=80;
	//const unsigned int serverPort=8888;
	//const bool useSSL=true;
	const bool useSSL=false;
	//const unsigned int serverPort=443;
	const unsigned int serverPort=8080;



	json_t *jsonObject = json_object();
	json_object_set(jsonObject, "__devId", json_string("defaultDevId1"));
	json_object_set(jsonObject, "__userId", json_string("defaultUserId1"));
	json_object_set(jsonObject, "__userPw", json_string("defaultPw"));
	json_object_set(jsonObject, "__appId", json_string("defaultAppId1"));
	json_object_set(jsonObject, "__customTableId", json_string("defaultCustomTableId"));
	json_object_set(jsonObject, "__timeToLiveSec", json_integer(0));
	json_object_set(jsonObject, "__timeToIdleSec", json_integer(6000));
	json_object_set(jsonObject, "__key", json_integer(0));
	json_object_set(jsonObject, "__mergeMode", json_string("OVERWRITE_EXISTING"));
	//json_object_set(jsonObject, "__autoFields", json_string("svrTimestamp,svrIP,svrSerial,svrGeoIP"));
	json_object_set(jsonObject, "__fieldMetadata", json_string("sampleField1Key(_ownerRW,_putMin),sampleField2Key(_userRW,_putSum)"));
	json_object_set(jsonObject, "__protocol", json_integer(0));
	json_object_set(jsonObject, "sampleField1Key", json_integer(1));
	json_object_set(jsonObject, "sampleField2Key", json_integer(2));

	// JSON_COMPACT is required or it won't match json-lib
	char *jsonStr = json_dumps(jsonObject, JSON_COMPACT | JSON_PRESERVE_ORDER);
	printf(jsonStr);


	// For testing, see http://hash.online-convert.com/sha1-generator

	const char *__sharedKey="defaultSharedKey";
	unsigned char output[SHA1_LENGTH];
	CSHA1::HMAC((unsigned char*) __sharedKey, strlen(__sharedKey), (unsigned char*) jsonStr, strlen(jsonStr), output);
	char outputBase64[SHA1_LENGTH*2+6];
	int bytesWritten = Base64Encoding(output, sizeof(output), outputBase64);
	//outputBase64[bytesWritten]=0;
	json_object_set(jsonObject, "__hash", json_string(outputBase64));
	jsonStr = json_dumps(jsonObject, JSON_COMPACT | JSON_PRESERVE_ORDER);

	// GAE SSL https://developers.google.com/appengine/docs/ssl
	char URI[128];
	sprintf(URI, "%s/customTable/update", serverURL);
	TCPInterface *tcp = RakNet::OP_NEW<TCPInterface>(__FILE__,__LINE__); // Requires build with OPEN_SSL_CLIENT_SUPPORT
	tcp->Start(0, 64);
	tcp->Connect(serverURL, serverPort, true);
	RakString rspost = RakString::FormatForPOST(
		URI,
		RakString("text/plain; charset=UTF-8"),
		jsonStr
		);

	RakSleep(100);
	SystemAddress serverAddr = tcp->HasCompletedConnectionAttempt();
	RakAssert(serverAddr!=UNASSIGNED_SYSTEM_ADDRESS);
	if (useSSL)
		tcp->StartSSLClient(serverAddr);
	tcp->Send(rspost.C_String(), rspost.GetLength(), serverAddr, false);
	RakSleep(1000);
	Packet *p;
	
	while (1)
	{
		p = tcp->Receive();
		if (p)
		{
			printf((const char*) p->data);
			break;
		}
	}
}