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

📄 irdatransportsocket.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    //fetch the device ID from the property bag
    VARIANT pDeviceVar;
    VARIANT pNameVar;
    VARIANT pPortVar;
    VariantInit(&pDeviceVar);
    VariantInit(&pNameVar);
    VariantInit(&pPortVar);

    //place to hold the port (if the port is NULL try the default ports
    //  of OBEX:IrXfer and then OBEX
    char cPortName[25];
    BOOL fFoundPortName = FALSE;
    char pDeviceName[25];
    
    if(FAILED(pDeviceProps->Read(c_szDevicePropAddress, &pDeviceVar, 0)))
    {
        hRet = E_FAIL;
        goto connect_done;
    }
    if(FAILED(pDeviceProps->Read(c_szDevicePropName, &pNameVar, 0)) || VT_BSTR != V_VT(&pNameVar))
    {
        hRet = E_FAIL;
        goto connect_done;
    }
    //also get the wide char version
    else
    {
        wcstombs(pDeviceName, pNameVar.bstrVal, 25);            
        DEBUGMSG(OBEX_TRANSPORTSOCKET_ZONE, (L"[OBEX] ----- using device name %s\n", pNameVar.bstrVal));
    }

    //read the port and convert from a wide char (BSTR)
    if(!FAILED(pDeviceProps->Read(c_szPort, &pPortVar, 0)))
    {
        wcstombs(cPortName, pPortVar.bstrVal, 25);   
        fFoundPortName = TRUE;
        DEBUGMSG(OBEX_TRANSPORTSOCKET_ZONE, (L"[OBEX] ----- using port name %s\n", pPortVar.bstrVal));
    }

    if(!(VT_I4 == V_VT(&pDeviceVar) &&
          VT_BSTR == V_VT(&pPortVar)))
    {
        DEBUGMSG(OBEX_TRANSPORTSOCKET_ZONE, (L"[OBEX] ----- incorrect var type!\n"));
        hRet = E_FAIL;
        goto connect_done;
    }
    
   
    //figure out if we already have this guy allocated...
    pTemp = g_pConnectionList;

    //if we DO have it allocated, this loop will return an instance
    while(pTemp)
    {
        if(0 == memcmp(pTemp->DeviceID, (char *)&pDeviceVar.lVal, 4))
        {
            //get a copy of the id for our records
            memcpy(connectionID, pTemp->DeviceID, 4);

            DEBUGMSG(OBEX_TRANSPORTSOCKET_ZONE, (L"[OBEX - irda] ----->  reusing connection!\n"));
            
            (*ppConnection) = pTemp->pItem;  
            (*ppConnection)->AddRef();            

            hRet = S_OK;
            goto connect_done;
        }
        pTemp = pTemp->pNext;   
    }
 
    //since we DONT have the instance, make one create a socket
    sMySock = socket(AF_IRDA, SOCK_STREAM, 0);
    DEBUGMSG(OBEX_TRANSPORTSOCKET_ZONE, (L"[OBEX - irda] -->Created Socket\n"));

    if(INVALID_SOCKET == sMySock) 
    {
        SetLastError (ERROR_DEVICE_NOT_CONNECTED);        
        hRet = E_FAIL;
        goto connect_done;
    }  
    
    //information on the IrDA to connect to
    SOCKADDR_IRDA    DestSockAddr; 
    memset(&DestSockAddr, 0, sizeof(SOCKADDR_IRDA));
    
    //move in the appropriate values (telling the service name and address family)
    DestSockAddr.irdaAddressFamily = AF_IRDA;
        
    // move in the device id for the selected device 
    memcpy(&DestSockAddr.irdaDeviceID[0], (char *)&pDeviceVar.lVal, 4);

    //get a copy of the id for our records
    memcpy(connectionID, (char *)&pDeviceVar.lVal, 4);


    /*Connect up to the server-----------------------------------------------*/
    if(fFoundPortName)
    {        
        //copy the name and begin the connection
        strcpy(DestSockAddr.irdaServiceName, cPortName);
        
        //connect to that device   
        if (SOCKET_ERROR == reenum_connect(NULL, sMySock, &DestSockAddr,
            sizeof(SOCKADDR_IRDA),pDeviceName)) 
        {    
            closesocket(sMySock);            
            DEBUGMSG(OBEX_TRANSPORTSOCKET_ZONE, (L"[OBEX - irda] Error connecting: %d\n", WSAGetLastError()));

            hRet = E_FAIL;
            goto connect_done;                       
        }    
    }
    else
    {
        //its okay to use strncpy because we've already 0'ed out the struct
        strncpy(DestSockAddr.irdaServiceName, "OBEX", 4);
    
        //connect to that device   
        if (SOCKET_ERROR == reenum_connect(NULL, sMySock, &DestSockAddr,
            sizeof(SOCKADDR_IRDA),pDeviceName)) 
        {        
            //its okay to use strncpy because we've already 0'ed out the struct
            strncpy(DestSockAddr.irdaServiceName, "OBEX:IrXfer", 11);
        
            //connect to that device   
            if (SOCKET_ERROR == reenum_connect(NULL, sMySock, &DestSockAddr,
                sizeof(SOCKADDR_IRDA),pDeviceName)) {
                closesocket(sMySock);                
                DEBUGMSG(OBEX_TRANSPORTSOCKET_ZONE, (L"[OBEX - irda] Error connecting: %d\n", WSAGetLastError()));
                
                hRet = E_FAIL;            
                goto connect_done;
            }       
        }
    }   
    /*------------------------------------------------------------------------*/

    
    
    //if we are here, we dont have a transport connecction, so make one
    *ppConnection = new CObexTransportConnection(this, pNameVar.bstrVal, sMySock);  
    if( !(*ppConnection) )
    {
         hRet = E_OUTOFMEMORY;
         goto connect_done;
    }
	
    //create and insert a new connection holder
    pTemp = new IRDA_CONNECTION_HOLDER();
    if( !pTemp )
    {
         delete *ppConnection;
         *ppConnection = NULL;
	  
         hRet = E_OUTOFMEMORY;
         goto connect_done;
    }
    pTemp->pItem = *ppConnection;
   
    memcpy(pTemp->DeviceID, (char *)&pDeviceVar.lVal, 4);
    pTemp->pNext = g_pConnectionList;
    g_pConnectionList = pTemp;
    
    hRet = S_OK;

    connect_done:
        VariantClear(&pDeviceVar);
        VariantClear(&pNameVar);
        VariantClear(&pPortVar);
        
    return hRet; 
}

HRESULT STDMETHODCALLTYPE 
CIRDATransportSocket::GetProperties(LPPROPERTYBAG2 * ppListenProps)
{
    return E_NOTIMPL;
}

ULONG STDMETHODCALLTYPE 
CIRDATransportSocket::AddRef() 
{
    DEBUGMSG(OBEX_ADDREFREL_ZONE,(L"CIRDATransportSocket::AddRef()\n"));
    return InterlockedIncrement((LONG *)&_refCount);
}

ULONG STDMETHODCALLTYPE 
CIRDATransportSocket::Release() 
{
    DEBUGMSG(OBEX_ADDREFREL_ZONE,(L"CIRDATransportSocket::Release()\n"));
    SVSUTIL_ASSERT(_refCount != 0xFFFFFFFF);
    ULONG ret = InterlockedDecrement((LONG *)&_refCount);    
    if(!ret) 
        delete this; 
    return ret;
}

HRESULT STDMETHODCALLTYPE 
CIRDATransportSocket::QueryInterface(REFIID riid, void** ppv) 
{
    DEBUGMSG(OBEX_TRANSPORTSOCKET_ZONE,(L"CIRDATransportSocket::QueryInterface()\n"));
    if(!ppv) 
        return E_POINTER;
    if(riid == IID_IPropertyBag) 
        *ppv = static_cast<IObexTransportSocket*>(this);    
    else 
        return *ppv = 0, E_NOINTERFACE;
    
    return AddRef(), S_OK;    
}




























⌨️ 快捷键说明

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