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

📄 filesvr.cxx

📁 这个是微软WINCE的OBEX协议例子
💻 CXX
📖 第 1 页 / 共 5 页
字号:
    }


    //
    //  Process ABORT packets
    //
    if (pOT->pObex->uiOp == (OBEX_OP_ABORT & OBEX_OP_OPMASK)) {
        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_ABORT : 0x%08x\n", pConn->uiConnectionId));
        if (pConn->op == SEND) {
            CloseHandle (pConn->hFile);
            pConn->hFile = INVALID_HANDLE_VALUE;
            pConn->szCurFile[0] = '\0';
        } else if (pConn->op == RECEIVE) {
            CloseHandle (pConn->hFile);
            pConn->hFile = INVALID_HANDLE_VALUE;
            if(TRUE == CanWrite())
                DeleteFile (pConn->szCurFile);
            pConn->szCurFile[0] = '\0';
        } else if (pConn->op == FILELIST) {
            pOT->ObexFree (pConn->ucListing);
            pConn->ucListing = NULL;
            pConn->cCompleted = pConn->cListingSize = 0;
        }

        pConn->op = NONE;

        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (OBEX_RESP_OK, pOT->uiTransactionId, NULL);
        EnterCriticalSection (&g_cs);
        return TRUE;
    }


 //
 //  SERVICE PUT OPs
 //
 if (pOT->pObex->uiOp == (OBEX_OP_PUT & OBEX_OP_OPMASK)) {
        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x\n", pConn->uiConnectionId));

        if ((pConn->op != NONE) && (pConn->op != RECEIVE)) {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x : wrong state, aborting\n", pConn->uiConnectionId));
            LeaveCriticalSection (&g_cs);
            pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
            EnterCriticalSection (&g_cs);
            return TRUE;
        }
        
        //
        // Make sure we are ALLOWED to write (if not, delete, and put will fail)
        if(FALSE == CanWrite()) {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x : we are not allowed to write -- aborting\n", pConn->uiConnectionId));
            LeaveCriticalSection (&g_cs);
            pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
            EnterCriticalSection (&g_cs);
            return TRUE;
        }
        
        //
        //  If HandlePacketAuthentication handled the packet for us, just return TRUE
        //
        if(HandlePacketAuthentication(pOT, pConn))
        {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : packet handled by authentication\n"));
            return TRUE;
        }


        int iResp = pOT->pObex->fFinal ? OBEX_RESP_OK : OBEX_RESP_CONTINUE;
        int fStartedHere = FALSE;
        int fNoBody = FALSE;

        //
        // Start new receive sequence (hunt out a HID_NAME)
        //
        if (pConn->op == NONE) 
        {    
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x : starting new receive sequence...\n", pConn->uiConnectionId));

            WCHAR *szName = NULL;
            for (int i = 0 ; i < (int)pOT->pObex->cProp ; ++i) {
                if (pOT->pObex->aPropID[i] == OBEX_HID_NAME) {
                    szName = pOT->pObex->aPropVar[i].pwsz;  
                    
                    if(!CheckNameForValidity(szName))
                    {
                        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : invalid name! (used invalid characters) -- rejecting\n"));         
                        szName = NULL;
                        pConn->fNameHeaderFound = FALSE;
                        iResp = OBEX_RESP_REJECT;                        
                    }

                    break;
                }
            }
            //
            //  If we got a filename, build up directory/file information
            //
            if (szName)
            {
                //
                //  Set a flag saying that we have found the NAME header and that
                //        its been handled
                //
                int iFileSize = wcslen (szName);
                int iDirSize  = wcslen (pConn->szCurDir);
                int iBaseSize = wcslen (pConn->szBaseDir);
                if (iFileSize + iDirSize + iBaseSize + 3 >= _MAX_PATH) {
                    IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x : name too long\n", pConn->uiConnectionId));
                    iResp = OBEX_RESP_REJECT;
                    pConn->fNameHeaderFound = FALSE;
                } else {
                    memcpy (pConn->szCurFile, pConn->szBaseDir, sizeof(WCHAR) * iBaseSize);
                    pConn->szCurFile[iBaseSize] = L'\\';
                    ++iBaseSize;

                    if (iDirSize) {
                        memcpy (pConn->szCurFile + iBaseSize, pConn->szCurDir, sizeof(WCHAR) * iDirSize);
                        pConn->szCurFile[iBaseSize + iDirSize] = '\\';
                        ++iDirSize;
                    }

                    memcpy (pConn->szCurFile + iBaseSize + iDirSize, szName, sizeof (WCHAR) * (iFileSize + 1));  
                    pConn->fNameHeaderFound = TRUE;
                }
            }
        } 
        else
        {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x : continuing to %s\n", pConn->uiConnectionId, pConn->szCurFile));
        }

        //
        //  If the final bit has been set on the PUT, OPEN up the file, set the Op to 
        //        RECEIVE and start the show
        //
        if ((INVALID_HANDLE_VALUE == pConn->hFile) && pConn->fNameHeaderFound)
        {        
            DWORD dwAttr = GetFileAttributes (pConn->szCurFile);
            if ((dwAttr != 0xffffffff) && (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
                pConn->fDir = TRUE;
            else {
                ASSERT(TRUE == CanWrite());
                pConn->hFile = CreateFile (pConn->szCurFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
            }
            if ((INVALID_HANDLE_VALUE == pConn->hFile) && (! pConn->fDir))
            {
                IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x\n : could not create %s, error %d", pConn->uiConnectionId, pConn->szCurFile, GetLastError ()));
                iResp = OBEX_RESP_REJECT;
            } 
            else 
            {
                IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x, file %s\n", pConn->uiConnectionId, pConn->szCurFile));
                fStartedHere = TRUE;
                fNoBody = pOT->pObex->fFinal;
                pConn->op = RECEIVE;
            }
        }

        
        //
        //  Hunt through the headers for BODY and BODY_END's
        //     when found, put the contents into the file
        //
        int j = 0;        
        while (iResp != OBEX_RESP_REJECT) 
        {
            int cBody = 0;
            unsigned char *pBody = NULL;
            for (; j < (int)pOT->pObex->cProp ; ++j) {
                if ((pOT->pObex->aPropID[j] == OBEX_HID_BODY) ||
                    (pOT->pObex->aPropID[j] == OBEX_HID_BODY_END)) {
                    cBody = pOT->pObex->aPropVar[j].caub.cuc;
                    pBody = pOT->pObex->aPropVar[j].caub.puc;
                    j = (pOT->pObex->aPropID[j] == OBEX_HID_BODY) ? j + 1 : (int)pOT->pObex->cProp;
                    break;
                }
            }

            if (! pBody)
                break;

            fNoBody = FALSE;

            if (cBody) 
            {
                DWORD dwWritten = 0;

                //
                //  If there is no name header, we have no choice but to bail out..... they
                //        should have told us where to put the file
                //
                if ((!pConn->fNameHeaderFound) || (pConn->fDir) || (! WriteFile (pConn->hFile, pBody, cBody, &dwWritten, NULL)) ||
                    ((int)dwWritten != cBody)) 
                {
                    if(pConn->fNameHeaderFound && (! pConn->fDir))
                    {
                        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x : writefile error %d\n", pConn->uiConnectionId, GetLastError ()));
                    }
                    else
                    {
                        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : NO FILE NAME GIVEN/DIRECTORY... must quit!\n"));
                    }
                    iResp = OBEX_RESP_REJECT;
                } 
                else
                {
                    IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x : wrote another %d bytes into %s\n", pConn->uiConnectionId, dwWritten, pConn->szCurFile));
                }
            }
        }


        //
        //  If the FINAL bit has been set, close the file and if required
        //        delete it
        //
        if (pOT->pObex->fFinal || (iResp == OBEX_RESP_REJECT)) 
        {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : 0x%08x : put sequence completed %s\n", pConn->uiConnectionId, iResp == OBEX_RESP_REJECT ? L"(with error)" : L""));

            if (pConn->hFile != INVALID_HANDLE_VALUE)
                CloseHandle (pConn->hFile);

            pConn->hFile = INVALID_HANDLE_VALUE;
            pConn->op = NONE;
            if (TRUE == pConn->fNameHeaderFound  && ((fStartedHere && fNoBody) || (iResp == OBEX_RESP_REJECT)))
            {
                if (pConn->fDir) {
                    ASSERT(TRUE == CanWrite());
                    if(FAILED(RecursiveDeleteDirectory(pConn->szCurFile, pConn->szBaseDir))) {                  
                        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : RemoveDirectory for %s failed", pConn->szCurFile));
                        iResp = OBEX_RESP_REJECT;
                    }
                }     
                else {
                    ASSERT(TRUE == CanWrite());
                    DeleteFile (pConn->szCurFile);
                }    
            }

            pConn->szCurFile[0] = '\0';

            // Reset the directory bit
            pConn->fDir = FALSE;

            //reset the name header flag
            pConn->fNameHeaderFound = FALSE;
        }

        LeaveCriticalSection (&g_cs);
        pOT->ObexExecute (iResp, pOT->uiTransactionId, NULL);
        EnterCriticalSection (&g_cs);

        return TRUE;
    }

    //
    // SERVICE GET ops
    //
    if (pOT->pObex->uiOp == (OBEX_OP_GET & OBEX_OP_OPMASK)) 
    {
        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] GET on 0x%08x\n", pConn->uiConnectionId));

        if ((pConn->op != NONE) && (pConn->op != SEND) && (pConn->op != FILELIST)) 
        {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] GET on 0x%08x in the middle of current op %d -- rejecting\n", pConn->uiConnectionId, pConn->op));
            LeaveCriticalSection (&g_cs);
            pOT->ObexExecute (OBEX_RESP_REJECT, pOT->uiTransactionId, NULL);
            EnterCriticalSection (&g_cs);
            return TRUE;
        }

        int iResp = OBEX_RESP_RESPOND;

        //
        //  If HandlePacketAuthentication handled the packet for us, just return TRUE
        //
        if(HandlePacketAuthentication(pOT, pConn))
        {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : packet handled by authentication\n"));
            return TRUE;
        }
        
        //
        // Start new send sequence by fetching headers (in particular HID_NAME) from the given 
        //      packet  
        //
        if (pConn->op == NONE) 
        {    
            WCHAR *szName = NULL;
            for (int i = 0 ; i < (int)pOT->pObex->cProp ; ++i) {
                if (pOT->pObex->aPropID[i] == OBEX_HID_NAME) {
                    szName = pOT->pObex->aPropVar[i].pwsz;
                    pConn->fNameHeaderFound = TRUE;
                    
                    
                    if(!CheckNameForValidity(szName))
                    {
                        IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] ServiceRequest : OBEX_OP_PUT : invalid name! (used invalid characters) -- rejecting\n"));         
                        szName = NULL;
                        pConn->fNameHeaderFound = FALSE;
                        iResp = OBEX_RESP_REJECT;                        
                    }
                    break;
                }
            }

            int iFileSize = szName ? wcslen (szName) : 0;
            int iDirSize  = wcslen (pConn->szCurDir);
            int iBaseSize = wcslen (pConn->szBaseDir);

            if (iFileSize + iDirSize + iBaseSize + 3 + 3 >= _MAX_PATH) 
            {
                IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] GET object's name is too long\n"));
                iResp = OBEX_RESP_REJECT;
            } 
            else 
            {
                memcpy (pConn->szCurFile, pConn->szBaseDir, iBaseSize * sizeof(WCHAR));
                pConn->szCurFile[iBaseSize] = L'\\';
                ++iBaseSize;

                if (iDirSize) 
                {
                    memcpy (pConn->szCurFile + iBaseSize, pConn->szCurDir, iDirSize * sizeof(WCHAR));
                    pConn->szCurFile[iBaseSize + iDirSize] = L'\\';
                    ++iDirSize;
                }

                //
                //  If we have recieved a name, put it in (NOTE: if they have sent us
                //        2 HID_NAMES...why would they do it?  who knows... but take the newest of
                //        them.  if there was no name, and if we have NOT found a header with 
                //        a NAME at this point, remove the filename (this will force
                //        GetDirListing to get us a directory later)
                //
                if (szName)
                    memcpy (pConn->szCurFile + iBaseSize + iDirSize, szName, sizeof (WCHAR) * (iFileSize + 1));
                else if(!pConn->fNameHeaderFound)
                    pConn->szCurFile[iBaseSize + iDirSize] = L'\0';                
            }
        }


        int fCloseOp = FALSE;
        unsigned char *pFileChunk = NULL;

        ObexCommand o;
        unsigned int aPropId[1];
        ObexVariant aPropVar[1];


        //
        //  If this packet contains a FINAL bit, then we know all headers
        //        have been processed.  Therefore we can see if the request
        //        was a GET or SETPATH 
        if (pConn->op == NONE && pOT->pObex->uiOp == OBEX_OP_GET && pOT->pObex->fFinal == OBEX_OP_ISFINAL) 
        {
            IFDBG(svslog_DebugOut (DEBUG_FTP_TRACE, L"[OBEX-FTP] GET final bit set, we can now open the file/directory\n"));
            //
            // 

⌨️ 快捷键说明

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