IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> LPC通信 -> 正文阅读

[开发测试]LPC通信

服务端

DWORD WINAPI ServerThread1(LPVOID)
{
    SECURITY_DESCRIPTOR sd;
    OBJECT_ATTRIBUTES ObjAttr;              // Object attributes for the name
    UNICODE_STRING PortName;
    NTSTATUS Status;
    HANDLE LpcPortHandle = NULL;
    BYTE RequestBuffer[sizeof(PORT_MESSAGE) + MAX_LPC_DATA];
    BOOL WeHaveToStop = FALSE;
    int nError;

    __try     // try-finally
    {
        //
        // Initialize security descriptor that will be set to
        // "Everyone has the full access"
        //

        if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
        {
            nError = GetLastError();
            __leave;
        }

        //
        // Set the empty DACL to the security descriptor
        //

        if(!SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE))
        {
            nError = GetLastError();
            __leave;
        }

        //
        // Initialize attributes for the port and create it
        //

        RtlInitUnicodeString(&PortName, LpcPortName);
        InitializeObjectAttributes(&ObjAttr, &PortName, 0, NULL, &sd);

        _tprintf(_T("Server: Creating LPC port \"%s\" (NtCreatePort) ...\n"), LpcPortName);
        Status = NtCreatePort(&LpcPortHandle,
                              &ObjAttr,
                               NULL,
                               sizeof(PORT_MESSAGE) + MAX_LPC_DATA,
                               0);
        _tprintf(_T("Server: NtCreatePort result 0x%08lX\n"), Status);

        if(!NT_SUCCESS(Status))
            __leave;

        //
        // Process all incoming LPC messages
        //

        while(WeHaveToStop == FALSE)
        {
            PTRANSFERRED_MESSAGE LpcMessage = NULL;
            HANDLE ServerHandle = NULL;

            //
            // Create the data buffer for the request
            //

            LpcMessage = (PTRANSFERRED_MESSAGE)RequestBuffer;
            _tprintf(_T("Server: ------------- Begin loop ----------------------0x%x\n"), Status);

            //
            // Listen to the port. This call is blocking, and cannot be interrupted,
            // even if the handle is closed. The only way how to stop the block is to send
            // an LPC request which will be recognized by server thread as "Stop" command
            //

            if(NT_SUCCESS(Status))
            {
                _tprintf(_T("Server: Listening to LPC port (NtListenPort) ...%s\n"), LpcPortName);
				//监听客户端连接  在此阻塞
                Status = NtListenPort(LpcPortHandle,
                                     &LpcMessage->Header);
                _tprintf(_T("Server: NtListenPort result 0x%08lX\n"), Status);
            }

            //
            // Accept the port connection
            //

            if(NT_SUCCESS(Status))
            {
                _tprintf(_T("Server: Accepting LPC connection (NtAcceptConnectPort)%s ...\n"), LpcPortName);
				//该函数执行完毕客户端NtConnectPort 结束阻塞
                Status = NtAcceptConnectPort(&ServerHandle,
                                              NULL,
                                             &LpcMessage->Header,
                                              TRUE,
                                              NULL,
                                              NULL);
                _tprintf(_T("Server: NtAcceptConnectPort result 0x%08lX\n"), Status);
            }

            //
            // Complete the connection
            //

            if(NT_SUCCESS(Status))
            {
                _tprintf(_T("Server: Completing LPC connection (NtCompleteConnectPort) %s...\n"), LpcPortName);
                Status = NtCompleteConnectPort(ServerHandle);
                _tprintf(_T("Server: NtCompleteConnectPort result 0x%08lX\n"), Status);
            }

            //
            // Now accept the data request coming from the port.
            //

            if(NT_SUCCESS(Status))
            {
                _tprintf(_T("Server: Receiving LPC data (NtReplyWaitReceivePort)%s ...\n"), LpcPortName);
				//等待 客户端调用NtRequestPort 或者 NtRequestWaitReplyPort 发送消息过来
                Status = NtReplyWaitReceivePort(ServerHandle,
                                                NULL,
                                                NULL,
                                               &LpcMessage->Header);
                _tprintf(_T("Server: NtReplyWaitReceivePort result 0x%08lX\n"), Status);
            }

            //
            // Get the data sent by the client 
            //

            if(NT_SUCCESS(Status))
            {
                // If a request has been received, answer to it.
                switch(LpcMessage->Command)
                {
                    case LPC_COMMAND_REQUEST_NOREPLY:
						//第一次通信,客户端发送了一段内容,服务器接收.服务器没有向客户端回复内容
                        _tprintf(_T("Server: Received request \"%s\"\n"), LpcMessage->MessageText);
                        break;      // Nothing more to do

                    case LPC_COMMAND_REQUEST_REPLY:
                        _tprintf(_T("Server: Received request \"%s\"\n"), LpcMessage->MessageText);
                        _tprintf(_T("Server: Sending reply (NtReplyPort) %s...\n"), LpcPortName);
                        Status = NtReplyPort(LpcPortHandle, &LpcMessage->Header);
                        _tprintf(_T("Server: NtReplyPort result 0x%08lX\n"), Status);
                        break;

                    case LPC_COMMAND_STOP:      // Stop the work
                        _tprintf(_T("Server: Stopping ...\n"));
                        WeHaveToStop = TRUE;
                        break;
                }
            }

            //
            // Close the server connection handle
            //

            if(ServerHandle != NULL)
            {
                _tprintf(_T("Server: Closing the request handle (NtClose) %s...\n"), LpcPortName);
                Status = NtClose(ServerHandle);
                _tprintf(_T("Server: NtClose result 0x%08lX\n"), Status);
            }

            _tprintf(_T("Server: ------------- End loop ---------------------- %x\n"), Status);
        }
    }

    __finally
    {
        if(LpcPortHandle != NULL)
            NtClose(LpcPortHandle);
    }
    
    return 0;
}

客户端

DWORD WINAPI ServerThread1(LPVOID)
{
    SECURITY_DESCRIPTOR sd;
    OBJECT_ATTRIBUTES ObjAttr;              // Object attributes for the name
    UNICODE_STRING PortName;
    NTSTATUS Status;
    HANDLE LpcPortHandle = NULL;
    BYTE RequestBuffer[sizeof(PORT_MESSAGE) + MAX_LPC_DATA];
    BOOL WeHaveToStop = FALSE;
    int nError;

    __try     // try-finally
    {
        //
        // Initialize security descriptor that will be set to
        // "Everyone has the full access"
        //

        if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
        {
            nError = GetLastError();
            __leave;
        }

        //
        // Set the empty DACL to the security descriptor
        //

        if(!SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE))
        {
            nError = GetLastError();
            __leave;
        }

        //
        // Initialize attributes for the port and create it
        //

        RtlInitUnicodeString(&PortName, LpcPortName);
        InitializeObjectAttributes(&ObjAttr, &PortName, 0, NULL, &sd);

        _tprintf(_T("Server: Creating LPC port \"%s\" (NtCreatePort) ...\n"), LpcPortName);
        Status = NtCreatePort(&LpcPortHandle,
                              &ObjAttr,
                               NULL,
                               sizeof(PORT_MESSAGE) + MAX_LPC_DATA,
                               0);
        _tprintf(_T("Server: NtCreatePort result 0x%08lX\n"), Status);

        if(!NT_SUCCESS(Status))
            __leave;

        //
        // Process all incoming LPC messages
        //

        while(WeHaveToStop == FALSE)
        {
            PTRANSFERRED_MESSAGE LpcMessage = NULL;
            HANDLE ServerHandle = NULL;

            //
            // Create the data buffer for the request
            //

            LpcMessage = (PTRANSFERRED_MESSAGE)RequestBuffer;
            _tprintf(_T("Server: ------------- Begin loop ----------------------0x%x\n"), Status);

            //
            // Listen to the port. This call is blocking, and cannot be interrupted,
            // even if the handle is closed. The only way how to stop the block is to send
            // an LPC request which will be recognized by server thread as "Stop" command
            //

            if(NT_SUCCESS(Status))
            {
                _tprintf(_T("Server: Listening to LPC port (NtListenPort) ...%s\n"), LpcPortName);
                Status = NtListenPort(LpcPortHandle,
                                     &LpcMessage->Header);
                _tprintf(_T("Server: NtListenPort result 0x%08lX\n"), Status);
            }

            //
            // Accept the port connection
            //

            if(NT_SUCCESS(Status))
            {
                _tprintf(_T("Server: Accepting LPC connection (NtAcceptConnectPort)%s ...\n"), LpcPortName);
                Status = NtAcceptConnectPort(&ServerHandle,
                                              NULL,
                                             &LpcMessage->Header,
                                              TRUE,
                                              NULL,
                                              NULL);
                _tprintf(_T("Server: NtAcceptConnectPort result 0x%08lX\n"), Status);
            }

            //
            // Complete the connection
            //

            if(NT_SUCCESS(Status))
            {
                _tprintf(_T("Server: Completing LPC connection (NtCompleteConnectPort) %s...\n"), LpcPortName);
                Status = NtCompleteConnectPort(ServerHandle);
                _tprintf(_T("Server: NtCompleteConnectPort result 0x%08lX\n"), Status);
            }

            //
            // Now accept the data request coming from the port.
            //

            if(NT_SUCCESS(Status))
            {
                _tprintf(_T("Server: Receiving LPC data (NtReplyWaitReceivePort)%s ...\n"), LpcPortName);
                Status = NtReplyWaitReceivePort(ServerHandle,
                                                NULL,
                                                NULL,
                                               &LpcMessage->Header);
                _tprintf(_T("Server: NtReplyWaitReceivePort result 0x%08lX\n"), Status);
            }

            //
            // Get the data sent by the client 
            //

            if(NT_SUCCESS(Status))
            {
                // If a request has been received, answer to it.
                switch(LpcMessage->Command)
                {
                    case LPC_COMMAND_REQUEST_NOREPLY:
                        _tprintf(_T("Server: Received request \"%s\"\n"), LpcMessage->MessageText);
                        break;      // Nothing more to do

                    case LPC_COMMAND_REQUEST_REPLY:
                        _tprintf(_T("Server: Received request \"%s\"\n"), LpcMessage->MessageText);
                        _tprintf(_T("Server: Sending reply (NtReplyPort) %s...\n"), LpcPortName);
                        Status = NtReplyPort(LpcPortHandle, &LpcMessage->Header);
                        _tprintf(_T("Server: NtReplyPort result 0x%08lX\n"), Status);
                        break;

                    case LPC_COMMAND_STOP:      // Stop the work
                        _tprintf(_T("Server: Stopping ...\n"));
                        WeHaveToStop = TRUE;
                        break;
                }
            }

            //
            // Close the server connection handle
            //

            if(ServerHandle != NULL)
            {
                _tprintf(_T("Server: Closing the request handle (NtClose) %s...\n"), LpcPortName);
                Status = NtClose(ServerHandle);
                _tprintf(_T("Server: NtClose result 0x%08lX\n"), Status);
            }

            _tprintf(_T("Server: ------------- End loop ---------------------- %x\n"), Status);
        }
    }

    __finally
    {
        if(LpcPortHandle != NULL)
            NtClose(LpcPortHandle);
    }
    
    return 0;
}

流程图

在这里插入图片描述

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2022-03-24 00:53:03  更:2022-03-24 00:53:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/18 0:43:07-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码