IDevicePacketStream

更新:2007 年 11 月

通过读写实现 IPacket 的对象,与台式计算机上已连接的 DevicePacketStream 交换数据。

IDevicePacketStream : public IUnknown

方法

方法

说明

IDevicePacketStream::Read

读取从台式计算机上的 DevicePacketStream 发送的数据包。

IDevicePacketStream::Write

向流中写入一个数据包。该数据包由台式计算机上的 DevicePacketStream 读取。

IDevicePacketStream::IsConnected

检查是否已连接流。

IDevicePacketStream::Disconnect

断开与流的连接。

IDevicePacketStream::IsPacketAvailable

检查是否存在可供读取的数据包。

IDevicePacketStream::get_Buffering

检查是否对流启用了 Nagle 的缓冲算法。

IDevicePacketStream::put_Buffering

对流启用或禁用 Nagle 的缓冲算法。

IDevicePacketStream::get_MaxPacketSize

获取由基础传输机制限制的最大数据包大小。

备注

若要获取实现此接口的对象,请使用 IDeviceAgentTransport::AcceptConnectionEx

示例

下面的示例是一个设备代理应用程序,它将向台式机写入一个数据包并从台式机读取一个数据包。

#include "stdafx.h"

// Custom implementation of IAgentTransportShutdownCallback
class MyShutdownCallback: public IAgentTransportShutdownCallback
{
private:
    long ref;
public:
    HRESULT STDMETHODCALLTYPE Shutdown(IUnknown *in_pUnknown) 
    {
        // Add your cleanup code here 
        MessageBox(NULL,_T("conmanclient2 exited"),_T("conmanclient exited"),0);
        return 0;
    }

    // Must implement members from IUnknown
    HRESULT STDMETHODCALLTYPE QueryInterface( REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
    {
        return 0;
    }
    ULONG STDMETHODCALLTYPE AddRef( void)
    {
        return InterlockedIncrement(&ref);
    }

    ULONG STDMETHODCALLTYPE Release( void)
    {
        if(InterlockedDecrement(&ref) == 0)
        {
            delete this;
            return 0;
        }
        return ref;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    // Load the Device Agent Transport Library
    HINSTANCE hmod;
    hmod = LoadLibrary(L"DeviceAgentTransport.dll");

    // Get an instance of IDeviceAgentTransport
    GetDeviceAgentTransportFunc f1 = (GetDeviceAgentTransportFunc)
        ::GetProcAddress(hmod, L"GetDeviceAgentTransport");
    IDeviceAgentTransport *pTransport = NULL;
    f1(&pTransport);

    // Register the callback with the Device Agent Transport
    MyShutdownCallback *shutdownCallback = new MyShutdownCallback();
    pTransport->RegisterShutdownCallback(shutdownCallback,shutdownCallback);

    // Let the desktop application know that this remote agent was deployed successfully 
    // and that this remote agent will handle the supplied list of services.
    LPCOLESTR szServiceIds[] = {L"F85E57BA-5AE9-4FF7-8433-6AB7D991D033"};
    pTransport->AcknowledgeLaunch(1, szServiceIds);

    // Open a communcation stream with desktop application on the service.
    IDevicePacketStream *pStream = NULL;
    pTransport->AcceptConnectionEx(szServiceIds[0], &pStream);

    // Get an instance of IPacket
    GetNewPacketFunc f2 = (GetNewPacketFunc) ::  GetProcAddress(hmod, L"GetNewPacket");
    IPacket *pPacket = NULL;
    f2(&pPacket);

    // Write a message and sent the packet.
    pPacket->WriteBool(true);
    pPacket->WriteByte(0xff);
    pPacket->WriteChar('c');
    pPacket->WriteInt32(1024);
    pPacket->WriteString(L"Hello Desktop Computer");
    pStream->Write(pPacket);

    // Check for a packet while communication stream is connected.
    f2(&pPacket);
    VARIANT_BOOL connected;
    pStream->IsConnected(&connected);
    while(connected)
    {
        pStream->IsConnected(&connected);
        VARIANT_BOOL available;

        // If a packet is found, display the string.
        pStream->IsPacketAvailable(&available);
        if(available)
        {
            pStream->Read(&pPacket);
            VARIANT_BOOL endofpacket;
            pPacket->IsEndOfPacket(&endofpacket);
            while (!endofpacket) 

            {
                pPacket->IsEndOfPacket(&endofpacket);
                DataTypeEnum datatype;
                pPacket->ReadDataType(&datatype);
                switch (datatype)
                {
                    case DT_BYTE:
                        BYTE byteValue;
                        pPacket->ReadByte(&byteValue);
                        break;
                    case DT_INT32:
                        INT32 intValue;
                        pPacket->ReadInt32(&intValue);
                        break;
                    case DT_WIDECHAR:
                        wchar_t charValue;
                        pPacket->ReadChar(&charValue);
                        break;
                    case DT_BOOL:
                        VARIANT_BOOL boolValue;
                        pPacket->ReadBool(&boolValue);
                        break;
                    case DT_BYTEARRAY:
                        BYTE * buffer[100];
                        ULONG length;
                        pPacket->ReadBytes(buffer,&length);
                        break;
                    case DT_STRING:
                        LPWSTR string;
                        pPacket->ReadString(&string);
                        MessageBox(NULL, string,string,0);
                        break;
                    default:
                        break;
                }
            };
        }
    };
    return 0;
}

托管等效项

Microsoft.SmartDevice.DeviceAgentTransport.IDevicePacketStream

要求

DeviceAgentTransport.h

请参见

其他资源

非托管设备端的智能设备连接 API