⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transportagent.cpp

📁 This SDK allows to integrate a syncml stack in a C++ application on a variety of platforms. Current
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    
    compare = WaitForSingleObject (hThread, getResponseTimeout() * 1000);
    
    if (compare == WAIT_TIMEOUT ) {
        
        LOG.debug(TEXT("HttpSendRequest failed: timeout error!"));  
        lastErrorCode = ERR_HTTP_TIME_OUT;
        wsprintf (lastErrorMsg, TEXT("%s: %d"), TEXT("HttpSendRequest Error Timeout "), ERR_HTTP_TIME_OUT);
        goto exit ;

    } else if (compare == WAIT_OBJECT_0){
        LOG.debug(TEXT("HttpSendRequest success!!"));
    }

    // The state of the specified object (thread) is signaled
    dwExitCode = 0;
    
    if ( !GetExitCodeThread( hThread, &dwExitCode ) ) {
        
        lastErrorCode = ERR_CONNECT;        
        wsprintf (lastErrorMsg, TEXT("%s: %d"), TEXT("HttpSendRequest Error"),GetLastError());
        LOG.debug(TEXT("HttpSendRequest failed: closing thread error!"));
        goto exit ;
    }

    CloseHandle (hThread);  
    
    LOG.debug(MESSAGE_SENT);
    
    size = sizeof(status);
    HttpQueryInfo (request,
                       HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
                       (LPDWORD)&status,
                       (LPDWORD)&size,
                       NULL);    
    //
    // If status code is not OK, returns immediately, otherwise reads the response
    //
    if (status != STATUS_OK) {
        lastErrorCode = ERR_HTTP;
        wsprintf(lastErrorMsg, TEXT("HTTP request error: %d"), status);
        goto exit;
    }

    HttpQueryInfo (request,
                   HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
                   (LPDWORD)&contentLength,
                   (LPDWORD)&size,
                   NULL);
    
    LOG.debug(READING_RESPONSE);

    wsprintf(logmsg, TEXT("Content-length: %d"), contentLength);
    LOG.debug(logmsg);
    
    if (contentLength <= 0) {
        lastErrorCode = ERR_READING_CONTENT;
        wsprintf(lastErrorMsg, TEXT("Invalid content-length: %d"), contentLength);
        goto exit;
    }   
    
    // Allocate a block of memory for lpHeadersW.
    response = new WCHAR[contentLength+1];   
    if (response == NULL) {
        lastErrorCode = ERR_NOT_ENOUGH_MEMORY;
        wsprintf(lastErrorMsg, TEXT("Not enough memory to allocate a buffer for the server response: %d required"), contentLength);
        goto exit;        

    }

    hThread = CreateThread(
                 NULL,                      // Pointer to thread security attributes
                 0,                         // Initial thread stack size, in bytes
                 WorkerFunctionInternetReadFile,    // Pointer to thread function
                 NULL,                      // The argument for the new thread
                 0,                         // Creation flags
                 &dwThreadID                // Pointer to returned thread identifier
             );
   
    
    previousNumRead = -1; 
    sumRead = 0;
    t = 0;
    do {
       
        Sleep(T);
        if (previousNumRead == sumRead) {
            t += T;
            if (t > TO) {
                lastErrorCode = ERR_HTTP_TIME_OUT;
                wsprintf(lastErrorMsg, TEXT("InternetReadFile Time out: %d"), contentLength);
                goto exit;
            }
        } else {
            previousNumRead = sumRead;
            t = 0;
        }


    } while(cont);
     
    LOG.debug(TEXT("Response read")); 

    exit:
    CloseHandle (hThread);
    
    // Close the Internet handles.
    if (inet) {
        InternetCloseHandle (inet);
    }

    if (connection) {
        InternetCloseHandle (connection);
    }

    if (request) {
        InternetCloseHandle (request);
    }

    if ((status != STATUS_OK) && (response !=NULL)) {
        delete [] response; response = NULL;
    }
    
    if (lastErrorCode != 0 && lastErrorCode == ERR_HTTP_TIME_OUT) {
        delete [] response; response = NULL;
    }

    return response;
}

/*
 * Returns the url.
 */

URL TransportAgent::getURL() {
    return url;

}


/*
 * Returns the last response read from the server.
 * Use releaseResponseBuffer() to release the memory buffer.
 */

const WCHAR* TransportAgent::getResponse();

/*
 * Releases the response buffer allocated by sendMessage().
 */
 void TransportAgent::releaseResponseBuffer();


 /*
* The function try to read the content of a file with InternetReadFile . It was called by a thread in the main
* procedure. 
*/

DWORD WINAPI WorkerFunctionInternetReadFile(IN LPVOID vThreadParm) {
    WCHAR* p        = NULL;
    p = response;
    (*p) = 0;
    char bufferA[5000+1];
    DWORD size = 0, read = 0;
          
    do {
        sumRead += read;
        if (!InternetReadFile (request, (LPVOID)bufferA, 5000, &read)) {
            lastErrorCode = ERR_READING_CONTENT;
            wsprintf(lastErrorMsg, TEXT("%s: %d"), TEXT("InternetReadFile Error"), GetLastError());
            cont = FALSE;
        }        
                
        if (read != 0) {
            bufferA[read] = 0;

            // Get the required size of the buffer which receives the Unicode
            // string.
            size = MultiByteToWideChar (CP_ACP, 0, bufferA, -1, NULL, 0);

            // Convert the buffer from ASCII to Unicode.
            MultiByteToWideChar (CP_ACP, 0, bufferA, read, p, read);
            p[read] = 0;
            p += size -1;
   
        }
        
    } while (read);
    cont = FALSE;

    return 0;
}

/*
* The function to open a request with HttpOpenRequest . It was called by a thread in the main
* procedure. 
*/

DWORD WINAPI WorkerFunctionHttpOpenRequest(IN LPVOID vThreadParm) {
    
    DWORD flags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
    LPTSTR acceptTypes[2] = {TEXT("*/*"), NULL};

    PARM_HTTP_OPEN_REQUEST* pThreadParm;
    // Initialize local pointer to void pointer passed to thread
    pThreadParm = (PARM_HTTP_OPEN_REQUEST*)vThreadParm;
        
    if (!(request = HttpOpenRequest (connection,
                                     METHOD_POST,
                                     pThreadParm->pResource,
                                     HTTP_VERSION,
                                     NULL,
                                     (LPCTSTR*)acceptTypes,
                                     flags, 0))) {
      lastErrorCode = ERR_CONNECT;
      wsprintf (lastErrorMsg, TEXT("%s: %d"), TEXT("HttpOpenRequest Error"), GetLastError());
      return 1; // failure
    }
    
    
    else {
        return 0;  // success
    }
}

/*
* The function try to send a message with HttpSendRequest . It was called by a thread in the main
* procedure. 
*/
DWORD WINAPI WorkerFunctionHttpSendRequest(IN LPVOID vThreadParm) {
    
    PARM_HTTP_SEND_REQUEST* pThreadParm;
    // Initialize local pointer to void pointer passed to thread
    pThreadParm = (PARM_HTTP_SEND_REQUEST*)vThreadParm;
        
    if (!HttpSendRequest (request, 
                          pThreadParm->pHeaders, 
                          pThreadParm->headersLength, 
                          pThreadParm->pMsg, 
                          pThreadParm->msgLength)) {
        lastErrorCode = ERR_CONNECT;
        wsprintf (lastErrorMsg, TEXT("%s: %d"), TEXT("HttpSendRequest Error"),GetLastError());
        return 1; // failure
    }
        
    else {
        return 0;  // success
    }

}


/*
* The function try to connect to internet. It was called by a thread in the main
* procedure. 
*/
DWORD WINAPI WorkerFunctionInternetConnect( IN LPVOID vThreadParm) {
    
    PARM_INTERNET_CONNECT* pThreadParm;
    // Initialize local pointer to void pointer passed to thread
    pThreadParm = (PARM_INTERNET_CONNECT*)vThreadParm;
        
    if ( !( connection  = InternetConnect (inet,
                                pThreadParm->pHost,
                                pThreadParm->nServerPort,
                                NULL, // username
                                NULL, // password
                                INTERNET_SERVICE_HTTP,
                                0,
                                0))) 
        {      
        lastErrorCode = ERR_CONNECT;
        wsprintf (lastErrorMsg, TEXT("%s: %d"), TEXT("InternetConnect Error"), GetLastError());        
        return 1; // failure
    }
    else {
        return 0;  // success
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -