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

📄 transportagent.cpp

📁 This SDK allows to integrate a syncml stack in a C++ application on a variety of platforms. Current
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2003-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "common/Log.h"

#include "http/Constants.h"
#include "http/TransportAgent.h"

#define T  1000
#define TO 10000

DWORD WINAPI WorkerFunctionInternetConnect( IN LPVOID vThreadParm);
DWORD WINAPI WorkerFunctionInternetReadFile(IN LPVOID vThreadParm);
DWORD WINAPI WorkerFunctionHttpOpenRequest( IN LPVOID vThreadParm);
DWORD WINAPI WorkerFunctionHttpSendRequest( IN LPVOID vThreadParm);

typedef struct
{
    WCHAR* pHost;
    INTERNET_PORT nServerPort;  

} PARM_INTERNET_CONNECT;

typedef struct
{
    WCHAR* pResource;
    
} PARM_HTTP_OPEN_REQUEST;

typedef struct
{
    WCHAR* pHeaders;
    int      headersLength;
    char*    pMsg;
    int      msgLength;

    
} PARM_HTTP_SEND_REQUEST;


int sumRead, previousNumRead;
WCHAR* response;
BOOL cont;

HINTERNET inet       = NULL,
          connection = NULL,
          request    = NULL;
/*
 * This is the Pocket PC 2002 implementation of the TransportAgent object
 */


/*
 * This function translate a UNICODE string into a UTF string without
 * allocating additional memory. The translation is performed removing
 * the second byte of the UNICODE coding.
 *
 * @param s the string to translate
 */
void toUTF(WCHAR* s) {
    int l = wcslen(s);
    WCHAR* w = s;
    char*    c = (char*)s;

    while (l--) {
        *c = (char)*w;
        ++c; ++w;
    }

    *c = 0;
}



/*
 * Constructor.
 * In this implementation newProxy is ignored, since proxy configuration
 * is taken from the WinInet subsystem.
 *
 * @param url the url where messages will be sent with sendMessage()
 * @param proxy proxy information or NULL if no proxy should be used
 */
TransportAgent::TransportAgent(URL& newURL, Proxy& newProxy, BOOL useCheckConnection, unsigned int maxResponseTimeout) {
    url   = newURL  ;
    
    if (maxResponseTimeout == 0) {
        setResponseTimeout(DEFAULT_MAX_TIMEOUT);
    } else {
        setResponseTimeout(maxResponseTimeout);
    }

    // the flag useCheckConnection is not used by this transportAgent for PPC 2002
        
}

TransportAgent::~TransportAgent(){}


/*
 * Change the URL the subsequent calls to setMessage() should
 * use as target url.
 *
 * @param url the new target url
 */
void TransportAgent::setURL(URL& newURL) {
    url = newURL;

}

void TransportAgent::setResponseTimeout(unsigned int resTimeout) {
    responseTimeout = resTimeout;
}

unsigned int TransportAgent::getResponseTimeout() {
    return responseTimeout;
}

/*
 * Sends the given SyncML message to the server specified
 * by the instal property 'url'. Returns the response status code or -1
 * if it was not possible initialize the connection.
 *
 * Use getResponse() to get the server response.
 */
WCHAR* TransportAgent::sendMessage(WCHAR* msg) {
    
    
    int status        = -1;
    int contentLength = 0;
    DWORD compare     = 0;    
    DWORD size = 0, read = 0;          
    DWORD    dwTimeout = 10000; 
    HANDLE   hThread;
    DWORD    dwThreadID;    
    DWORD    dwExitCode;
    cont    = TRUE;
    int t   = 0;            
    unsigned int m = 0;
    BOOL queryInfo = TRUE;


    inet = InternetOpen (TEXT(USER_AGENT), INTERNET_OPEN_TYPE_PRECONFIG, NULL, 0, 0);    

    if (!inet) {
        lastErrorCode = ERR_TPCIP_INIT;
        wsprintf (lastErrorMsg, TEXT("%s: %d"), TEXT("InternetOpen Error"), GetLastError());
        goto exit;
    }
    
    
    wsprintf(logmsg, TEXT("Connecting to %s:%d"), url.host, url.port);
    LOG.debug(logmsg);
    

    // Open an HTTP session for a specified site by using lpszServer.
    
    PARM_INTERNET_CONNECT    threadParm;
    threadParm.pHost       = url.host;
    threadParm.nServerPort = url.port;   

    hThread = CreateThread(
                 NULL,            // Pointer to thread security attributes
                 0,               // Initial thread stack size, in bytes
                 WorkerFunctionInternetConnect,  // Pointer to thread function
                 &threadParm,     // The argument for the new thread
                 0,               // Creation flags
                 &dwThreadID      // Pointer to returned thread identifier
             );
    
    
    
    compare = WaitForSingleObject ( hThread, dwTimeout );

    if (compare == WAIT_TIMEOUT ) {
        
        LOG.debug(TEXT("InternetConnect failed: timeout error."));     
        lastErrorCode = ERR_CONNECT;
        wsprintf (lastErrorMsg, TEXT("%s: %d"), TEXT("InternetConnect timeout error"), GetLastError());
        goto exit ;

    } else if (compare == WAIT_OBJECT_0){
        LOG.debug(TEXT("InternetConnect 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("InternetConnect Error"), GetLastError());
        LOG.debug(TEXT("InternetConnect failed: closing thread error."));    
        goto exit ;
    }
    
    CloseHandle (hThread);    

    wsprintf(logmsg, TEXT("Requesting resource %s"), url.resource);
    LOG.debug(logmsg);
    
    PARM_HTTP_OPEN_REQUEST     threadParmHttpOpenRequest;    
    threadParmHttpOpenRequest.pResource       = url.resource;        

    hThread = CreateThread(
                 NULL,            // Pointer to thread security attributes
                 0,               // Initial thread stack size, in bytes
                 WorkerFunctionHttpOpenRequest,  // Pointer to thread function
                 &threadParmHttpOpenRequest,     // The argument for the new thread
                 0,               // Creation flags
                 &dwThreadID      // Pointer to returned thread identifier
             );
    
    compare = WaitForSingleObject ( hThread, dwTimeout );
    if (compare == WAIT_TIMEOUT ) {
        
        LOG.debug(TEXT("HttpOpenRequest failed: timeout error!"));      
        lastErrorCode = ERR_CONNECT;
        wsprintf (lastErrorMsg, TEXT("%s: %d"), TEXT("HttpOpenRequest timeout error"), GetLastError());
        goto exit;

    } else if (compare == WAIT_OBJECT_0){
        
        LOG.debug(TEXT("HttpOpenRequest 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("HttpOpenRequest Error"),GetLastError());
        LOG.debug(TEXT("HttpOpenRequest failed: closing thread  error!"));            
        goto exit ;
    }

    CloseHandle (hThread);    
    
    //
    // Prepares headers
    //
    WCHAR headers[512];

    contentLength = wcslen(msg);
    wsprintf(headers, TEXT("Content-Type: %s\r\nContent-Length: %d"), TEXT(SYNCML_CONTENT_TYPE), contentLength);
    
    // Send a request to the HTTP server.
    toUTF(msg);
    
    PARM_HTTP_SEND_REQUEST     threadParmHttpSendRequest;    

    threadParmHttpSendRequest.pHeaders       = headers;        
    threadParmHttpSendRequest.headersLength  = wcslen(headers);
    threadParmHttpSendRequest.pMsg           = (char*) msg;
    threadParmHttpSendRequest.msgLength      = contentLength;
      
    
    hThread = CreateThread(
                 NULL,                      // Pointer to thread security attributes
                 0,                         // Initial thread stack size, in bytes
                 WorkerFunctionHttpSendRequest,    // Pointer to thread function
                 &threadParmHttpSendRequest,       // The argument for the new thread
                 0,                         // Creation flags
                 &dwThreadID                // Pointer to returned thread identifier
             );

⌨️ 快捷键说明

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