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

📄 connectionmanager.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        TRACEMSG(ZONE_ERROR, (L"SMB_SRVR: Error converting wildcard in FindFirstFile! on %s", pSearchString));
        goto Done;
    }
    
    //
    // See if . or .. match the search pattern
    // We do this b/c the CE FS doesnt have this concept
    pSubSearchString = WildSearch.GetUnsafeString();
    pSubSearchString = pSubSearchString + WildSearch.Length() - 1;
    ASSERT(NULL == *pSubSearchString);
    while('\\' != *pSubSearchString && pSubSearchString > WildSearch.GetUnsafeString()) {
        pSubSearchString --;
    }
    if('\\' == *pSubSearchString) {
        pSubSearchString ++;
    }
    if(TRUE == MatchesWildcard(wcslen(pSubSearchString), pSubSearchString, 1, L".") ||
        TRUE == MatchesWildcard(wcslen(pSubSearchString), pSubSearchString, 2, L"..")) {
        fDotsMatch = TRUE;
    }
    
    //
    // Open up the search handle
    if (INVALID_HANDLE_VALUE == (hSearch = FindFirstFile (WildSearch.GetString(), &w32FindData)) && FALSE == fDotsMatch) {
        TRACEMSG(ZONE_FILES, (L"SMB_SRVR: Error calling FindFirstFile! on %s", WildSearch.GetString()));
        hr = E_FAIL;
        goto Done;        
    }
        
    //
    // Success
    hr = S_OK;
    Done:    
        if(FAILED(hr)) {
            if(INVALID_HANDLE_VALUE != hSearch) {
                FindClose(hSearch);
            }
            if(0xFFFF != usHandle) {
                m_UIDFindFirstHandle.RemoveID(usHandle);
            }
            *pusHandle = 0xFFFF;
        } else {            
            ASSERT(0xFFFF != usHandle);           
            
            ce::list<FIND_FIRST_NODE,ACTIVE_CONN_FIND_FIRST_ALLOC >::iterator it;
            m_FindHandleList.insert(m_FindHandleList.end());
            it = --(m_FindHandleList.end());
                     
            //
            // Create a node in our linked list
            if(INVALID_HANDLE_VALUE != hSearch) {
                memcpy(&(*it).w32LastNode, &w32FindData, sizeof(WIN32_FIND_DATA));
                (*it).fLastNodeUsed = TRUE;
            } else {
                memset(&(*it).w32LastNode, 0, sizeof(WIN32_FIND_DATA));
                (*it).fLastNodeUsed = FALSE;
            }            
            (*it).SearchString.append(WildSearch.GetString());            
            (*it).usSID = usHandle;
            (*it).hHandle = hSearch;
            if(TRUE == fDotsMatch) {
                (*it).fGivenDot = FALSE;
                (*it).fGivenDotDot = FALSE;
            } else {
                (*it).fGivenDot = TRUE;
                (*it).fGivenDotDot = TRUE;
            }        
            *pusHandle = usHandle;           
        }
    
        return hr;    
}

HRESULT 
ActiveConnection::ResetSearch(USHORT usHandle)
{  
    CCritSection csLock(&m_csConnectionLock);
    csLock.Lock();
    ce::list<FIND_FIRST_NODE,ACTIVE_CONN_FIND_FIRST_ALLOC >::iterator it; 
    ce::list<FIND_FIRST_NODE,ACTIVE_CONN_FIND_FIRST_ALLOC >::iterator itEnd = m_FindHandleList.end();
    ASSERT(0xFFFF != usHandle );
    HRESULT hr = E_FAIL;

    //
    // Find our node
    for(it = m_FindHandleList.begin(); it != itEnd; it++) {
        if((*it).usSID == usHandle) { 
            BOOL fDotsMatch = FALSE;
            WCHAR *pSubSearchString = NULL;
        
            if(INVALID_HANDLE_VALUE != (*it).hHandle) {
               FindClose((*it).hHandle);          
            }       
            
            //
            // See if . or .. match the search pattern
            // We do this b/c the CE FS doesnt have this concept
            pSubSearchString = (*it).SearchString.GetUnsafeString();
            pSubSearchString = pSubSearchString + wcslen(pSubSearchString);
            while('\\' != *pSubSearchString && pSubSearchString >(*it).SearchString.GetUnsafeString()) {
                pSubSearchString --;
            }
            if('\\' == *pSubSearchString) {
                pSubSearchString ++;
            }
            if(TRUE == MatchesWildcard(wcslen(pSubSearchString), pSubSearchString, 1, L".") ||
                TRUE == MatchesWildcard(wcslen(pSubSearchString), pSubSearchString, 2, L"..")) {
                fDotsMatch = TRUE;
            }

            //
            // Open up the search handle
            if (INVALID_HANDLE_VALUE == ((*it).hHandle = FindFirstFile ((*it).SearchString.GetString(), &((*it).w32LastNode))) &&
                FALSE == fDotsMatch) {
                (*it).fLastNodeUsed = FALSE;
                TRACEMSG(ZONE_FILES, (L"SMB_SRVR: Error calling FindFirstFile during reset!"));
                hr = E_FAIL;
                goto Done;        
            }
            
            //
            // In the case where we have dots but no handle -- invalidate the last node
            if(INVALID_HANDLE_VALUE == (*it).hHandle) {
                (*it).fLastNodeUsed = FALSE;
            } else {
                (*it).fLastNodeUsed = TRUE;
            }
            
            //
            // Set the dots given flags
            if(TRUE == fDotsMatch) {
                (*it).fGivenDot = FALSE;
                (*it).fGivenDotDot = FALSE;
            } else {
                (*it).fGivenDot = TRUE;
                (*it).fGivenDotDot = TRUE;
            }
            
            hr = S_OK;
            break;
        }
    }
    
    Done:
        return hr;  
}


HRESULT 
ActiveConnection::NextFile(USHORT usHandle, WIN32_FIND_DATA **ppData) 
{
    CCritSection csLock(&m_csConnectionLock);
    csLock.Lock();
    ce::list<FIND_FIRST_NODE,ACTIVE_CONN_FIND_FIRST_ALLOC >::iterator it; 
    ce::list<FIND_FIRST_NODE,ACTIVE_CONN_FIND_FIRST_ALLOC >::iterator itEnd = m_FindHandleList.end();
    PREFAST_ASSERT(0xFFFF != usHandle && NULL != ppData);
    SYSTEMTIME STCurrentTime;
    FILETIME   FTCurrentTime;
    GetSystemTime(&STCurrentTime);
    
    if(0 == SystemTimeToFileTime(&STCurrentTime, &FTCurrentTime)) {
        memset(&FTCurrentTime, 0, sizeof(FILETIME));
        ASSERT(FALSE);
    }
    
    *ppData = NULL;
    //
    // Find our node
    for(it = m_FindHandleList.begin(); it != itEnd; it++) {
      
      if((*it).usSID == usHandle) {
          if(FALSE == (*it).fGivenDot) {
                memset(&((*it).w32TempNode), 0, sizeof(WIN32_FIND_DATA));
                (*it).w32TempNode.dwFileAttributes = 16;
                (*it).w32TempNode.ftCreationTime = FTCurrentTime;
                (*it).w32TempNode.ftLastAccessTime = FTCurrentTime;
                (*it).w32TempNode.ftLastWriteTime = FTCurrentTime;
                swprintf((*it).w32TempNode.cFileName, L".");                
                *ppData = &((*it).w32TempNode);                
          } else if (FALSE == (*it).fGivenDotDot) {
                memset(&((*it).w32TempNode), 0, sizeof(WIN32_FIND_DATA));
                (*it).w32TempNode.dwFileAttributes = 16;
                (*it).w32TempNode.ftCreationTime = FTCurrentTime;
                (*it).w32TempNode.ftLastAccessTime = FTCurrentTime;
                (*it).w32TempNode.ftLastWriteTime = FTCurrentTime;
                swprintf((*it).w32TempNode.cFileName, L"..");
                *ppData = &((*it).w32TempNode);                
          } else if(TRUE == (*it).fLastNodeUsed) {            
              *ppData = &((*it).w32LastNode); 
          }    
          break;
      }
    }

    if(NULL == *ppData) {
        return E_FAIL;
    } else {
        return S_OK;
    }
}
   
   
HRESULT 
ActiveConnection::AdvanceToNextFile(USHORT usHandle)
{
    CCritSection csLock(&m_csConnectionLock);
    csLock.Lock();
    ce::list<FIND_FIRST_NODE,ACTIVE_CONN_FIND_FIRST_ALLOC >::iterator it; 
    ce::list<FIND_FIRST_NODE,ACTIVE_CONN_FIND_FIRST_ALLOC >::iterator itEnd = m_FindHandleList.end();
    ASSERT(0xFFFF != usHandle );
    HRESULT hr = E_FAIL;
    //
    // Find our node
    for(it = m_FindHandleList.begin(); it != itEnd; it++) {
     
      if((*it).usSID == usHandle) { 
          if(FALSE == (*it).fGivenDot) {
            (*it).fGivenDot = TRUE;
            hr = S_OK;
          } else if(FALSE == (*it).fGivenDotDot) {
            (*it).fGivenDotDot = TRUE;
            hr = S_OK;
          } else if(0 != FindNextFile ((*it).hHandle, &((*it).w32LastNode))) {
               (*it).fLastNodeUsed = TRUE;
               hr = S_OK;
          } else {
               (*it).fLastNodeUsed = FALSE;
               hr = E_FAIL;
          }
          break;
      }
    }
    return hr;
}
   
   
HRESULT 
ActiveConnection::CloseFindHandle(USHORT usHandle)
{
   CCritSection csLock(&m_csConnectionLock);
   csLock.Lock();
   ce::list<FIND_FIRST_NODE,ACTIVE_CONN_FIND_FIRST_ALLOC >::iterator it; 
   ce::list<FIND_FIRST_NODE,ACTIVE_CONN_FIND_FIRST_ALLOC >::iterator itEnd = m_FindHandleList.end();
   ASSERT(0xFFFF != usHandle );
   HRESULT hr = E_FAIL;
   
   //
   // Find our node
   for(it = m_FindHandleList.begin(); it != itEnd; it++) {
       if((*it).usSID == usHandle) { 
          if(INVALID_HANDLE_VALUE != (*it).hHandle) {
             FindClose((*it).hHandle);             
          }          
          if(0xFFFF != (*it).usSID) {
            m_UIDFindFirstHandle.RemoveID((*it).usSID);
          }
          m_FindHandleList.erase(it); 
          hr = S_OK;
          break;
       }
   }
   return hr;  
}

HRESULT 
ActiveConnection::SetUserName(const WCHAR *pName)
{
    CCritSection csLock(&m_csConnectionLock);
    csLock.Lock();
    m_UserName.Clear();
    TRACEMSG(ZONE_SECURITY, (L"SMB_SRV:  Setting username to %s", pName));
    return m_UserName.append(pName);
}

const WCHAR *
ActiveConnection::UserName()
{
    CCritSection csLock(&m_csConnectionLock);
    csLock.Lock();
    return m_UserName.GetString();
}

ConnectionManager::ConnectionManager() : m_fZeroUIDTaken(FALSE)
{
    #ifdef DEBUG
        //
        // Just used to make sure (in debug) that multiple instances of the connection manager
        //   dont get created
        if(m_fFirstInst) {
            m_fFirstInst = FALSE;
        }
        else {
            ASSERT(FALSE);
            TRACEMSG(ZONE_ERROR, (L"Only one instance of ConnectionManager allowed!!!"));
        }
    #endif  

    InitializeCriticalSection(&m_MyCS);
}

ConnectionManager::~ConnectionManager()
{
    if(TRUE == m_fZeroUIDTaken) {
        //
        // Give back the zero UID 
        if(FAILED(m_UIDGenerator.RemoveID(0))) {
            ASSERT(FALSE);
        } 
    }    
#ifdef DEBUG
    ASSERT(0 == m_MyConnections.size());    
    m_fFirstInst = TRUE;
#endif
    DeleteCriticalSection(&m_MyCS);
}

ce::smart_ptr<ActiveConnection> 
ConnectionManager::FindConnection(SMB_PACKET *pSMB)
{
    CCritSection csLock(&m_MyCS);
    csLock.Lock();
   
    ce::list<ce::smart_ptr<ActiveConnection> , CONNECTION_MANAGER_CONNECTION_ALLOC >::iterator it;
    ce::list<ce::smart_ptr<ActiveConnection> , CONNECTION_MANAGER_CONNECTION_ALLOC >::iterator itEnd = m_MyConnections.end();   
    
    for(it=m_MyConnections.begin(); it!=itEnd; it++) {
        ActiveConnection *pUse = *it;
        
        if(pUse->ConnectionID() == pSMB->ulConnectionID &&
            pUse->Uid() == pSMB->pInSMB->Uid) {

            //
            // Mark the connection as being used -- we use this number
            //   to terminate connections that have become stale
            pUse->m_dwLastUsed = GetTickCount();     
            return *it;
       }
    }   
    return NULL;     
}


HRESULT 
ConnectionManager::FindAllConnections(ULONG ulConnectionID, ce::list<ce::smart_ptr<ActiveConnection>, ACTIVE_CONN_PTR_LIST > &ConnList)
{
    
    ASSERT(0 == ConnList.size());
    CCritSection csLock(&m_MyCS);
    csLock.Lock();

    ce::list<ce::smart_ptr<ActiveConnection> , CONNECTION_MANAGER_CONNECTION_ALLOC >::iterator it;
    ce::list<ce::smart_ptr<ActiveConnection> , CONNECTION_MANAGER_CONNECTION_ALLOC >::iterator itEnd = m_MyConnections.end();   
    
    for(it=m_MyConnections.begin(); it!=itEnd; it++) {
        if((*it)->ConnectionID() == ulConnectionID) {
            if(!ConnList.push_back(*it)) {
                return E_OUTOFMEMORY;
            }
        }
    }    
    return S_OK;   
}

⌨️ 快捷键说明

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