赞
踩
在下文中,管道已被证明是一种非常复杂的工具,可以轻松安全地实现数据交换(大量数据交换)。在可能的情况下,您应该使用管道而不是共享内存,并节省昂贵的同步费用!
管道允许在应用程序级和内核级之间快速交换数据,也可以跨多个程序实例进行快速数据交换。管道机制基于特定大小的共享内存空间。在内部,管道机构被组织为环形缓冲器。有两种不同类型的管道:数据管道和消息管道。
数据管道是一种内存,它在内部组织为环形缓冲区。它总是采用相同大小的元素。大小最小为一个字节,将在创建管道期间确定。特点是:
保存操作(“KS_putPipe”)一次可以包含多个元素。可以在实际调用之前查询自由元素的数量。
读取操作(“KS_getPipe”)可以通过一次调用从管道中提取多个元素。可以在实际调用之前查询可用元素的数量。
消息管道在内部也组织为环形缓冲区。但是您可以保存不同长度的按摩。单个消息的最短大小为 1 个字节,最长大小为消息的整个可用内存的最大值。特点是:
保存操作(“KS_putPipe”)一次只能接收一条消息。可以确定消息管道的可用内存。
读取操作(“KS_getPipe”)一次从管道中读取一条消息。可以在实际通话之前查询下一条消息的长度。
注意:
始终选择数据管道以保存相同大小的数据元素,因为它工作效率更高。
选择消息管道以保存不同大小的大型数据块。
将使用函数 KS_createPipe 创建一个管道。
将创建一个名为 name 的新管道。管道句柄将写回 phPipe。参数 itemSize 和 itemCount 的确定方式如下:
数据管道: 管道为总共 itemCount 元素分配共享内存,这些元素的固定大小为 itemSize(以字节为单位)。
消息管道: 管道为总计 itemCount 元素分配共享内存,其固定大小为 itemSize(以字节为单位)。您可能需要选择预期消息的平均 itemSize。对于每条消息,需要 4 个额外的字节来存储消息长度信息。
数据或消息始终可以从应用程序级别或内核级别写入或读取。但是,同时进行多个读取或写入访问将不会同步,必须从用户中排除或同步。管道的大小和数量仅受可用内存的限制。
管道将使用 KS_removePipe 移除。为了释放已使用的共享内存,每次调用“KS_createPipe”都必须调用“KS_removePipe”。
管道只不过是共享内存。这就是为什么可以确定一个名字的原因。该名称用于共享内存,通过这种方式,多个程序实例可以使用一个管道,例如用于进程间通信(有关详细信息,请参见 您需要了解的共享内存 )。程序的所有实例都需要创建具有已知名称的管道。实际上,只有第一个调用“KS_createPipe”的实例正在创建管道,所有具有相同名称的调用都只提供管道的句柄。此外,所有实例都必须通过调用“KS_removePipe”来释放管道,最后一次调用将删除管道并实际释放已使用的共享内存。
一旦知道管道手柄,就可以调用 KS_putPipe 和 KS_getPipe。
使用 KS_putPipe,可以写入使用 hPipe 确定的管道。参数 pBuffer 指向应存储到管道的数据块或数据消息。
数据管道: 在这里,参数 length 确定当前调用“KS_putPipe”时应存储在管道中的元素数。管道将存储可用内存中尽可能多的元素。如果所有元素的内存都不够,则函数将返回“KSERROR_FUNCTION_FAILED”,并且已写入的元素数将通过参数 pLength 传递。
消息管道: 在这里,参数 length 确定消息的长度,该消息应存储在当前调用“KS_putPipe”的管道中。如果没有足够的可用内存,则不会存储该消息。在这种情况下,将返回“KSERROR_NOT_ENOUGH_MEMORY”。这些函数还会将实际可用内存写回参数 pLength。
注意: 对’KS_putPipe’的并行调用必须同步!
函数 ‘KS_putPipe’ 可用于查询管道中的可用空间。因此,参数 length 必须为 0。该函数将元素的可用空间(数据管道)或可用字节的大小(消息管道)返回给参数 pLength。
使用 KS_getPipe 可以从使用 hPipe 确定的管道中读取。参数 pBuffer 指向应存储管道数据或消息的数据块。
数据管道: 在这里,参数 length 确定应通过当前调用从管道中读取的元素数。管道将读取当前可用的数据。如果请求的数据多于存储的数据,则返回“KSERROR_FUNCTION_FAILED”,并将已读取的元素数返回到 pLength。
消息管道: 此处,参数 length 确定 pBuffer 指示的缓冲区的长度。如果没有足够的可用内存,则不会读取管道中的下一条消息。在这种情况下,将返回“KSERROR_NOT_ENOUGH_MEMORY”。这些函数还会将下一条消息的实际长度写回参数 pLength。
注意: 对“KS_getPipe”的并行调用必须同步!
函数“KS_readPipe”可用于查询存储的元素数(数据管道)或下一条消息的长度(消息管道)。因此,参数 length 必须为 0。该函数将向参数 pLength 返回存储的数据元素数(数据管道)或下一条消息(消息管道)的大小。
代码结构:
SharedData.h
#pragma once
#include <KrtsDemo.h>
// 内核层数据需要1字节对齐
#pragma pack(1)
#define MESSAGE_SIZE 8
typedef struct SharedData {
KSHandle pipe_handle; // 管道句柄
}SharedData;
#pragma pack()
KitharaDemo.cpp
#include <iostream> #include <mutex> #include "../KitharaDll/SharedData.h" #define WIN32_LEAN_AND_MEAN #include <Windows.h> const char customerNumber[256] = "DEMO"; SharedData* app_data_ptr_ = nullptr; // 应用层共享内存指针 SharedData* sys_data_ptr_ = nullptr; // 内核层共享内存指针 // 应用层不能使用内核层指针!!! bool is_running_{false}; // 正在运行 std::mutex mutex_; // 线程锁 void OutputErr(KSError error, const char* pFuncName, const char* pComment) { if (error == KS_OK) return; const char* pError; KS_getErrorString(error, &pError, KSLNG_DEFAULT); printf("ERROR (%08X = \'%s\') - %s: %s\n", error, pError, pFuncName, pComment); } KSError _stdcall PipeThread(void* pArgs) { if (app_data_ptr_ != nullptr) { int length = 0; KSError error; while (is_running_) { Sleep(100); error = KS_getPipe(app_data_ptr_->pipe_handle, nullptr, 0, &length, KSF_NO_FLAGS); if (error != KS_OK) { OutputErr(error, "KS_getPipe", "get length failed!"); } for (int i = 0; i < length; i++) { char chr[MESSAGE_SIZE]; error = KS_getPipe(app_data_ptr_->pipe_handle, &chr, length, nullptr, KSF_NO_FLAGS); if (error != KS_OK) { OutputErr(error, "KS_getPipe", "get data failed!"); } else { printf("Pipe data: %s", chr); mutex_.lock(); is_running_ = false; mutex_.unlock(); return KS_OK; } } } } return KS_OK; } int main() { KSError error; error = KS_openDriver(customerNumber); // 所有Kithar项目的第一步 if (error != KS_OK) { error = 1; OutputErr(error, "KS_openDriver", "Unable to open the driver!"); } else { printf("Hello Kithara! \n"); } // 创建共享内存 error = KS_createSharedMem(reinterpret_cast<void**>(&app_data_ptr_), reinterpret_cast<void**>(&sys_data_ptr_), "KitharaDemo", sizeof(SharedData), 0); if (error != KS_OK) { OutputErr(error, "KS_createSharedMem", "failed to allocate shared memory"); KS_closeDriver(); return 0; } // 加载内核DLL KSHandle kermel_handle; error = KS_loadKernel(&kermel_handle, "KitharaDll.dll",nullptr, nullptr, KSF_KERNEL_EXEC | KSF_SAVE_FPU); if (error != KS_OK) { OutputErr(error, "KS_loadKernel", "load dll failed!"); KS_closeDriver(); return 0; } // 内核初始化 error = KS_execKernelFunction(kermel_handle, "_initFunction", sys_data_ptr_, nullptr, KSF_REALTIME_EXEC | KSF_SAVE_FPU); if (error != KS_OK) { OutputErr(error, "KS_execKernelFunction", "init failed"); KS_closeDriver(); return 0; } // 创建线程 is_running_ = true; error = KS_createThread(PipeThread, nullptr, nullptr); if (error != KS_OK) { OutputErr(error, "KS_createThread", "create thread failed"); KS_closeDriver(); return 0; } // 发送管道数据 error = KS_execKernelFunction(kermel_handle, "_sendMessage", sys_data_ptr_, nullptr, KSF_REALTIME_EXEC|KSF_SAVE_FPU); if (error != KS_OK) { OutputErr(error, "KS_execKernelFunction", "get message failed"); KS_closeDriver(); return 0; } // 等待管道读取 int time_count = 0; while (is_running_) { if (++time_count > 10) { printf("time out!"); mutex_.lock(); is_running_ = false; mutex_.unlock(); } Sleep(200); } // 资源释放 KS_execKernelFunction(kermel_handle, "_exitFunction", sys_data_ptr_, nullptr, KSF_REALTIME_EXEC | KSF_SAVE_FPU); // 资源内核 error = KS_freeKernel(kermel_handle); if (error != KS_OK) { OutputErr(error, "KS_freeKernel", ""); KS_closeDriver(); return 0; } // 资源共享内存 error = KS_freeSharedMem(app_data_ptr_); if (error != KS_OK) { OutputErr(error, "KS_freeSharedMem", ""); KS_closeDriver(); return 0; } // 关闭驱动 error = KS_closeDriver(); if (error != KS_OK) { OutputErr(error, "KS_closeDriver", ""); KS_closeDriver(); return 0; } Sleep(1000); return 0; }
KitharaDll.cpp
#include "SharedData.h" #include <cstdint> SharedData* sys_data_ptr_ = nullptr; uint global_counter_ = 0; // 初始化函数 extern "C" __declspec(dllexport) KSError __stdcall _initFunction(void* pArgs, void* pConttext) { sys_data_ptr_ = (SharedData*)pArgs; // 创建数据管道 KSError error = KS_createPipe(&sys_data_ptr_->pipe_handle, "DemoPipe", sizeof(char), MESSAGE_SIZE, KS_INVALID_HANDLE, KSF_NO_FLAGS); if (error != KS_OK) { // Todo return error; } return KS_OK; } // 通过管道发送数据 extern "C" __declspec(dllexport) KSError __stdcall _sendMessage(void* pArgs, void* pConttext) { char chr[MESSAGE_SIZE] = "Pipe"; KSError error = KS_putPipe(sys_data_ptr_->pipe_handle, chr, sizeof(chr), nullptr, KSF_NO_FLAGS); if (error != KS_OK) { // Todo return error; } return KS_OK; } extern "C" __declspec(dllexport) KSError __stdcall _exitFunction(void* pArgs,void *pConttext) { if (sys_data_ptr_ != nullptr) { KS_removePipe(sys_data_ptr_->pipe_handle); } return KS_OK; } #define WIN32_LEAN_AND_MEAN // Windows 头文件 #include <windows.h> BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD reason, LPVOID pReserved) { return TRUE; }
更多示例:
smp\TaskBasics
smp\UsbHidKeyboard
smp\UsbHidKeyboard
smp\NetworkEthernetMonitor
smp\CanFDRecv
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。