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

📄 inbox.cxx

📁 这个是微软WINCE的OBEX协议例子
💻 CXX
📖 第 1 页 / 共 4 页
字号:
        DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] deleting temp file (odds are there was an error): %s\n", pConn->szFileName));
    	DeleteFile(pConn->szFileName);     
        delete [] pConn->szFileName;
    }
    
    if(pConn->szName)
        delete [] pConn->szName;
    
    if(pConn->pFileChunk)
        delete [] pConn->pFileChunk;
        
    pConn->szFileName = NULL;
    pConn->szName = NULL;
    pConn->pFileChunk = NULL;
    pConn->hFile = INVALID_HANDLE_VALUE;
    pConn->uiBytesRecv = 0;
    pConn->uiMode = NO_MODE;
}



/*****************************************************************************/
/*   CloseConnection                                                         */
/*   Clean up after a connection has been closed                             */
/*                                                                           */
/*****************************************************************************/
static void CloseConnection (ObexTransaction *pOT, InboxState *pConn) {
    DEBUGMSG(INBOX_GENERAL_ZONE, (L"[OBEX-INBOX] Closing inbox connection 0x%08x\n", pConn->uiConnectionId));
    
    ResetConnection(pConn);
    
    //clean up the inbox state
    delete pConn;
}



static HRESULT GetUserSecurityPrefs(DWORD *pdwAlwaysPrompt, DWORD *pdwPromptForOverWrite)
{
    ASSERT(pdwAlwaysPrompt && pdwPromptForOverWrite);
    
    HKEY hKey;
    
    *pdwAlwaysPrompt = 1;
    *pdwPromptForOverWrite = 1;
    
    //open the registry key, and retrieve values
    if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, OBEX_INBOX_BASE_REG, 0, KEY_QUERY_VALUE, &hKey))
    {
       DWORD dwSize; 
       DWORD dwType;
       LONG dwRes;
       
       dwSize = sizeof(DWORD);
       dwRes = RegQueryValueEx(hKey, OBEX_PROMPT_BEFORE_UPLOAD, NULL, &dwType, (LPBYTE)pdwAlwaysPrompt,&dwSize);       
       if(ERROR_SUCCESS != dwRes || REG_DWORD != dwType)
          *pdwAlwaysPrompt = 1;
         
          
       dwSize = sizeof(DWORD);
       dwRes = RegQueryValueEx(hKey, OBEX_PROMT_OVERWRITE, NULL, &dwType, (LPBYTE)pdwPromptForOverWrite,&dwSize);       
       if(ERROR_SUCCESS != dwRes || REG_DWORD != dwType)
          *pdwPromptForOverWrite = 1;
          
      
       RegCloseKey(hKey);
    } 
    
    return S_OK;
}



int ServiceOPGet(ObexTransaction *pOT)
{
    //if this is a GET packet, fetch the state information for the connection
    //  then read data (up to the max allowed in a packet) and ship it off
    InboxState *pParent = NULL;
    InboxState *pConn = g_pConn;
    BOOL fLastPacket = FALSE;
    BOOL fRejectPacket = FALSE;
    
    //search for the connection ID (the stored state)
    while (pConn) {
        if (pConn->uiConnectionId == pOT->uiConnectionId)
            break;            
        pParent = pConn;
        pConn = pConn->pNext;
    }
    
    //
    //  if pre-stored state couldnt be found the person is probabally 
    //    giving us data without connecting first... this is allowed in 
    //    OBEX so get them some state! :)
    if(!pConn) 
    {  
        //create a new object to hold this connections state
        InboxState *pNew = new InboxState;
        if (! pNew) {
            DEBUGMSG(INBOX_PUT_ZONE,(L"[OBEX-INBOX] ServiceRequest : GET making a CONNECT 0x%08x OUT OF MEMORY\n", pOT->uiConnectionId, pOT->pObex->sPktData.ConnectRequestResponse.maxlen));
            return FALSE;
        }
        
        //initilize all important fields
        pNew->uiConnectionId = pOT->uiConnectionId;
        pNew->uiPacketLimit = pOT->pObex->sPktData.ConnectRequestResponse.maxlen;
        if(pNew->uiPacketLimit < 255)
            pNew->uiPacketLimit = 255;          
        pNew->pNext = g_pConn;
        pNew->uiMode = NO_MODE;
        g_pConn = pNew;
        
        ObexCommand oc;
        
        //zero out the command
        memset (&oc, 0, sizeof(oc));
        
        DEBUGMSG(INBOX_PUT_ZONE, (L"[OBEX-INBOX] ServiceRequest : PUT 0x%08x : connection not found -- making a new one\n", pOT->uiConnectionId));
        pConn = pNew;        
    }
    
    
    //make sure we are in the right mode (NO_MODE or GET_MODE)
    ASSERT(pConn && pConn->uiMode == NO_MODE || pConn->uiMode == GET_MODE);    
    if(pConn->uiMode != NO_MODE && pConn->uiMode != GET_MODE)
    {
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_DENY, pOT->uiTransactionId, NULL);
        EnterCriticalSection (&g_cs); 
        return TRUE;
    }
    
    unsigned int aPropID[3];
    ObexVariant  aPropVar[3];
    ObexCommand oc;    
    
    //zero out the command
    memset (&oc, 0, sizeof(oc));
    
    //fill in the command info
    oc.uiOp = pOT->pObex->uiOp;
    oc.cProp = 0;
    oc.aPropID = aPropID;
    oc.aPropVar = aPropVar;
    
    //if we havent yet opened up the input file check to see if the 
    //  OBEX_HID_NAME field is set (thats where the filename will be 
    //  stored)
    if(pConn->hFile == INVALID_HANDLE_VALUE)
    {      
        ASSERT(!pConn->pFileChunk);        
        
        for (int i = 0 ; i < (int)pOT->pObex->cProp ; ++i) {
            if (pConn->hFile == INVALID_HANDLE_VALUE     && 
                pOT->pObex->aPropID[i] == OBEX_HID_TYPE)
            {   
                HKEY hMainMIMEKey;
                DWORD dwRes; 
                
                if(pOT->pObex->aPropVar[i].caub.puc[pOT->pObex->aPropVar[i].caub.cuc - 1] != NULL)
                {
                    DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- GET ERROR!  Type NOT null terminated!\n"));
                    LeaveCriticalSection (&g_cs);
                    pOT->ObexExecute (OBEX_RESP_DENY, pOT->uiTransactionId, NULL);
                    EnterCriticalSection (&g_cs);   
                    return FALSE;   
                }
                
                WCHAR wcMIMEType[MAX_PATH];
                MultiByteToWideChar (CP_ACP, 
                    0, 
                    (char *)pOT->pObex->aPropVar[i].caub.puc, 
                    pOT->pObex->aPropVar[i].caub.cuc, 
                    wcMIMEType, 
                    MAX_PATH);
                
                DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- found TYPE in GET (%s)\n", wcMIMEType));
                
                //open the main MIME registry key, and enumerate through it
                if ((ERROR_SUCCESS == (dwRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                    OBEX_INBOX_MIME_REG, 0, KEY_QUERY_VALUE, &hMainMIMEKey)))) 
                {                
                    HKEY hMIMEKey;
                                        
                    //if a MIME type was given, use it                
                    if ((ERROR_SUCCESS == (dwRes = RegOpenKeyEx(hMainMIMEKey,
                        wcMIMEType, 0, KEY_QUERY_VALUE, &hMIMEKey)))) 
                    {                       
                        WCHAR szDefaultFile[MAX_PATH];
                        DWORD dwType;
                        DWORD uiNameSize = MAX_PATH * sizeof(WCHAR);
                        
                        if((ERROR_SUCCESS == (dwRes = RegQueryValueEx(hMIMEKey,
                            L"DefaultFile", NULL, &dwType, (LPBYTE)szDefaultFile,
                            &uiNameSize))))
                        {
                            if(REG_SZ == dwType)
                            {
                                DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- using %s as default file\n", szDefaultFile));
                                ASSERT(INVALID_HANDLE_VALUE == pConn->hFile);
                                pConn->hFile = CreateFile (szDefaultFile, 
                                    GENERIC_READ, 0, NULL, 
                                    OPEN_EXISTING, 0, NULL);
                                pConn->uiMode = GET_MODE;

                                if(pConn->hFile == INVALID_HANDLE_VALUE)                                
                                    fRejectPacket = TRUE;            
                            }
                            else
                            {   
                                DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- default file is of wrong type...\n"));
                                fRejectPacket = TRUE;
                            }
                        }
                        else
                        {
                            DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- no default file found for MIME type...\n"));
                            fRejectPacket = TRUE;                            
                        }
                        RegCloseKey(hMIMEKey); 
                    }
                    else
                    {
                        DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- MIME type not registered\n"));
                        fRejectPacket = TRUE;
                    }
                    
                    RegCloseKey(hMainMIMEKey);
                    break; 
                }
                else
                {
                    DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- cant open OBEX inbox registry keys\n"));
                    fRejectPacket = TRUE;
                }
            }
        }            
    }
    
    //if we have a filename now, then we can send off some data
    if(!fRejectPacket && pConn->hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwRead;
        
        //if we need memory, allocate some
        //  NOTE: this memory is kept around (and reused) to cut down
        //  on time spend in new.  it is freed in CloseConnection
        if(!pConn->pFileChunk)
            pConn->pFileChunk = new unsigned char [pConn->uiPacketLimit];
        
        if(!pConn->pFileChunk)
        {
            ASSERT(FALSE);       
            DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- Error, couldnt allocate mem\n"));
            return FALSE;
        }
        
        //grab some data (up to the max allowed by the client)
        if(!ReadFile(pConn->hFile, 
            pConn->pFileChunk, 
            pConn->uiPacketLimit - MAX_GET_HEADER_SIZE, 
            &dwRead, 0))
        {    
            DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- Error, couldnt read from file\n"));
            
            ResetConnection(pConn);
            
            DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- GET ERROR!  error reading from file!\n"));
            LeaveCriticalSection (&g_cs);
            pOT->ObexExecute (OBEX_RESP_DENY, pOT->uiTransactionId, NULL);
            EnterCriticalSection (&g_cs);   
            return FALSE;  
        }
        else
        {          
            //if the number of bytes read is zero, this is the last packet
            //    otherwise, send out the data
            pConn->uiBytesSent += dwRead;
            
            aPropVar[oc.cProp].caub.puc = pConn->pFileChunk;
            aPropVar[oc.cProp].caub.cuc = dwRead;
            
            //if we cant read more, mark the end bit
            if(dwRead == 0)
            {
                aPropID[oc.cProp] = OBEX_HID_BODY_END;
                fLastPacket = TRUE;
            }
            else
            {
                aPropID[oc.cProp] = OBEX_HID_BODY;
                fLastPacket = FALSE;
            }
            
            ++oc.cProp;
        }
        
    }
    
    
    //if we dont have a file opened, and the final bit has been set, reject the packet
    //  they havent provided enough info for us to complete there request
    if(pConn->hFile == INVALID_HANDLE_VALUE && pOT->pObex->fFinal == OBEX_OP_ISFINAL)
    {
        fRejectPacket = TRUE;
    }
    
    //fill in the response type... if this is a final packet (as said
    //  by the read function, send SUCCESS, otherwise, send Continue
    if(!fRejectPacket)    
    {
        
        if(fLastPacket)     
            oc.uiResp = OBEX_STAT_OK | OBEX_OP_ISFINAL;
        else
            oc.uiResp = OBEX_STAT_CONTINUE | OBEX_OP_ISFINAL;
        
        
        //send off the command
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_RESPOND, pOT->uiTransactionId, &oc);
        EnterCriticalSection (&g_cs);
        
        if(fLastPacket)
        {
            DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- GET sending final bit set so closing up files\n"));        
            ResetConnection(pConn);
        }
    }
    else
    {
        DEBUGMSG(INBOX_GET_ZONE,(L"[OBEX-INBOX] -- Rejecting packet!\n")); 
        ASSERT(pConn->hFile == INVALID_HANDLE_VALUE);
        ASSERT(pConn->pFileChunk == NULL);
        
        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_DENY, pOT->uiTransactionId, NULL);
        EnterCriticalSection (&g_cs);  
    }
    
    return TRUE;      
    
}


HRESULT PassToInboxService(ObexTransaction *pOT, InboxState *pConn)

⌨️ 快捷键说明

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