進(jìn)程間通訊方式:共享內(nèi)存,管道(linux),udp通訊(若是在同一臺(tái)電腦上通過(guò)udp通訊您宪,那么它并沒(méi)有經(jīng)過(guò)網(wǎng)絡(luò)勒葱,而是利用了網(wǎng)卡上的一塊內(nèi)存坎缭,而且非潮鸩t?煽考沽拧#?/p>
這是項(xiàng)目中用到的一處共享內(nèi)存拣宰,別人寫的轩端。記錄一下放典,讓每個(gè)看似毫無(wú)意義的項(xiàng)目都能吸收到一點(diǎn)有用的東西。
項(xiàng)目中使用到的地方:服務(wù)器要發(fā)送數(shù)據(jù)給unity基茵,但是接收數(shù)據(jù)的格式是需要以固定的MFC(c/c++)代碼接收奋构。然后就在MFC代碼中增加一處共享內(nèi)存,作為中轉(zhuǎn)站拱层,unity讀取這處共享內(nèi)存獲得數(shù)據(jù)弥臼。
0.前置準(zhǔn)備
| 句柄數(shù)據(jù)結(jié)構(gòu)
typedef struct semps
{
HANDLE hMFCWrite; //聲明信號(hào)量變量
HANDLE hU3DRead; //聲明信號(hào)量變量
HANDLE hU3DWrite; //聲明信號(hào)量變量
HANDLE hMFCRead; //聲明信號(hào)量變量
char *pBuf;
CMainFrame *mFrm;
}Semps;
Semps mSemp;
HANDLE hMapFile;
1.實(shí)例化共享內(nèi)存
ProcessMsg::ProcessMsg(CMainFrame *frm)
{
mSemp.hMFCWrite = CreateSemaphoreA(NULL,1,1,"WriteMap_1");
mSemp.hU3DRead = CreateSemaphoreA(NULL,0,1,"ReadMap_1"); //第二各參數(shù)為0,代表進(jìn)程創(chuàng)建信號(hào)量之時(shí)便聲明了對(duì)該信號(hào)量的占用
mSemp.hU3DWrite = CreateSemaphoreA(NULL,1,1,"WriteMap_2");
mSemp.hMFCRead = CreateSemaphoreA(NULL,0,1,"ReadMap_2"); //第二各參數(shù)為0根灯,代表進(jìn)程創(chuàng)建信號(hào)量之時(shí)便聲明了對(duì)該信號(hào)量的占用
//創(chuàng)建共享內(nèi)存區(qū)
char szName[] = "shareMemory"; // 共享內(nèi)存的名字
// 創(chuàng)建共享文件句柄
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // 物理文件句柄
NULL, // 默認(rèn)安全級(jí)別
PAGE_READWRITE, // 可讀可寫
0, // 高位文件大小
BUF_SIZE, // 地位文件大小
szName // 共享內(nèi)存名稱
);
mSemp.pBuf = (char *)MapViewOfFile(
hMapFile, // 共享內(nèi)存的句柄
FILE_MAP_ALL_ACCESS, // 可讀寫許可
0,
0,
BUF_SIZE
);
mSemp.mFrm = frm;
AfxBeginThread((AFX_THREADPROC)ThreadProc,(LPVOID)&mSemp,THREAD_PRIORITY_IDLE);
}
Tips :
- 由上圖可知径缅,此時(shí)已經(jīng)創(chuàng)建好了共享內(nèi)存文件(通過(guò)CreateFileMapping),并完成映射(通過(guò)MapViewOfFile)烙肺,接下來(lái)就是要弄unity里面連接映射的地方了纳猪。
2.MFC向共享內(nèi)存寫入數(shù)據(jù)
//發(fā)送消息到u3d
void ProcessMsg::SendMsgToU3D(char *str){
strncpy(mSemp.pBuf, str, BUF_SIZE);
//strncpy(mSemp.pBuf, str, BUF_SIZE - 1);
//mSemp.pBuf[BUF_SIZE - 1] = '\0';
//放u3d讀
//ReleaseSemaphore(mSemp.hU3DRead,1,0);
//放u3d開(kāi)寫
//ReleaseSemaphore(mSemp.hU3DWrite,1,0);
}
3.unity連接映射地址
Ps:固定格式代碼
const int INVALID_HANDLE_VALUE = -1;
const int PAGE_READWRITE = 0x04;
bool _loadingRedStartMap = false;
//共享內(nèi)存
[DllImport("Kernel32.dll", EntryPoint = "CreateFileMapping")]
private static extern IntPtr CreateFileMapping(IntPtr hFile, //HANDLE hFile,
UInt32 lpAttributes,//LPSECURITY_ATTRIBUTES lpAttributes, //0
UInt32 flProtect,//DWORD flProtect
UInt32 dwMaximumSizeHigh,//DWORD dwMaximumSizeHigh,
UInt32 dwMaximumSizeLow,//DWORD dwMaximumSizeLow,
string lpName//LPCTSTR lpName
);
[DllImport("Kernel32.dll", EntryPoint = "OpenFileMapping")]
private static extern IntPtr OpenFileMapping(
UInt32 dwDesiredAccess,//DWORD dwDesiredAccess,
int bInheritHandle,//BOOL bInheritHandle,
string lpName//LPCTSTR lpName
);
const int FILE_MAP_ALL_ACCESS = 0x0002;
const int FILE_MAP_WRITE = 0x0002;
[DllImport("Kernel32.dll", EntryPoint = "MapViewOfFile")]
private static extern IntPtr MapViewOfFile(
IntPtr hFileMappingObject,//HANDLE hFileMappingObject,
UInt32 dwDesiredAccess,//DWORD dwDesiredAccess
UInt32 dwFileOffsetHight,//DWORD dwFileOffsetHigh,
UInt32 dwFileOffsetLow,//DWORD dwFileOffsetLow,
UInt32 dwNumberOfBytesToMap//SIZE_T dwNumberOfBytesToMap
);
[DllImport("Kernel32.dll", EntryPoint = "UnmapViewOfFile")]
private static extern int UnmapViewOfFile(IntPtr lpBaseAddress);
[DllImport("Kernel32.dll", EntryPoint = "CloseHandle")]
private static extern int CloseHandle(IntPtr hObject);
[DllImport("Kernel32.dll", EntryPoint = "CreateSemaphoreA")]
private static extern IntPtr CreateSemaphoreA(
UInt32 lpSemaphoreAttributes, // SD
UInt32 lInitialCount, // initial count
UInt32 lMaximumCount, // maximum count
string lpName// object name
);
[DllImport("Kernel32.dll", EntryPoint = "OpenSemaphoreA")]
private static extern IntPtr OpenSemaphoreA(
UInt32 dwDesiredAccess, // access 0x1F0003
int bInheritHandle, // inheritance option
string lpName // object name
);
[DllImport("Kernel32.dll", EntryPoint = "ReleaseSemaphore")]
private static extern int ReleaseSemaphore(
IntPtr hSemaphore,
UInt32 lReleaseCount,
Int32 lpPreviousCount
);
[DllImport("Kernel32.dll", EntryPoint = "WaitForSingleObject")]
private static extern UInt32 WaitForSingleObject(
IntPtr hHandle,
UInt32 dwMilliseconds
);
private IntPtr handle; //文件句柄
private IntPtr addr; //共享內(nèi)存地址
IntPtr hMFCWrite;
IntPtr hU3DRead;
IntPtr hU3DWrite;
IntPtr hMFCRead;
uint mapLength; //共享內(nèi)存長(zhǎng)
// Process pMFC; //程序
// pMFC = Process.Start(Application.streamingAssetsPath + "\\MFC\\MFC_Client.exe");
// StartCoroutine(WaitToInit(0.5f));
4.Unity數(shù)據(jù)讀取(注意,使用共享內(nèi)存時(shí)有些地方需要上鎖):
//不安全的代碼在項(xiàng)目生成的選項(xiàng)中選中允許不安全代碼
static unsafe void byteCopy(byte[] dst, IntPtr src)
{
fixed (byte* pDst = dst)
{
byte* pdst = pDst;
byte* psrc = (byte*)src;
while ((*pdst++ = *psrc++) != '\0')
;
}
}
5.Unity數(shù)據(jù)發(fā)送
static unsafe void Copy(byte[] byteSrc, IntPtr dst)
{
fixed (byte* pSrc = byteSrc)
{
byte* pDst = (byte*)dst;
byte* psrc = pSrc;
for (int i = 0; i < byteSrc.Length; i++)
{
*pDst = *psrc;
pDst++;
psrc++;
}
}
}
1.MFC
5.結(jié)束的時(shí)候要做點(diǎn)事
2.Unity
void OnApplicationQuit()
{
if (sw != null)
{
sw.Close();
}
if (fs != null)
{
fs.Close();
}
if (isLocalTest)
{
return;
}
ReleaseSemaphore(hU3DRead, 1, 0); //不調(diào)這一句的話桃笙,關(guān)閉unity時(shí)會(huì)卡死
//清空內(nèi)存
byte[] sendStr = Encoding.Default.GetBytes("" + '\0');
//如果要是超長(zhǎng)的話氏堤,應(yīng)另外處理,最好是分配足夠的內(nèi)存
if (sendStr.Length < mapLength)
{
Copy(sendStr, addr);
}
//關(guān)閉線程
if (threadRead != null)
{
threadRead.Interrupt();
threadRead.Abort();
}
//關(guān)閉MFC進(jìn)程
if (pMFC != null)
{
pMFC.Kill();
}
//UnityEngine.Debug.Log("關(guān)閉U3D");
if (addr != null)
{
UnmapViewOfFile(addr);
}
if (handle != null)
{
CloseHandle(handle);
}
if (hU3DWrite != null)
{
CloseHandle(hU3DWrite);
}
if (hMFCRead != null)
{
CloseHandle(hMFCRead);
}
if (hMFCWrite != null)
{
CloseHandle(hMFCWrite);
}
if (hU3DRead != null)
{
CloseHandle(hU3DRead);
}
}