Action <string> dataAction;//数据委托 ///<summary> /// 初始化共享内存数据 创建一个共享内存 ///</summary> public void Init(Action <string> _dataAction, IPCType type, IPCType addrtype) { dataAction = _dataAction; m_Write = new Semaphore(1, 1, IPCName.GetWriteMapName(type)); //开始的时候有一个可以写 m_Read = new Semaphore(0, 1, IPCName.GetReadMapName(type)); //没有数据可读 //mapLength = 10240000; IntPtr hFile = new IntPtr(INVALID_HANDLE_VALUE); handle = CreateFileMapping(hFile, 0, PAGE_READWRITE, 0, IPCName.mapLength, IPCName.GetshareMemoryName(type)); addr = MapViewOfFile(handle, IPCName.GetshareAddress(addrtype), 0, 0, 0); //handle = OpenFileMapping(0x0002, 0, "shareMemory"); //addr = MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 0); threadRed = new Thread(new ThreadStart(ReceiveData)); threadRed.Start(); }
private IntPtr addr; //共享内存地址 //uint mapLength=10240000; //共享内存长 //Thread threadRed; public void WriteData(string data, IPCType type, IPCType addrtype) { try { m_Write = Semaphore.OpenExisting(IPCName.GetWriteMapName(type)); m_Read = Semaphore.OpenExisting(IPCName.GetReadMapName(type)); handle = OpenFileMapping(FILE_MAP_WRITE, 0, IPCName.GetshareMemoryName(type)); addr = MapViewOfFile(handle, IPCName.GetshareAddress(addrtype), 0, 0, 0); m_Write.WaitOne(); byte[] sendStr = Encoding.Default.GetBytes(data + '\0'); //如果要是超长的话,应另外处理,最好是分配足够的内存 if (sendStr.Length < IPCName.mapLength) { Copy(sendStr, addr); } m_Read.Release(); } catch { //throw new Exception("不存在系统信号量!"); } }
public string CallCmd(string eprocess, string method, string arg) { if (IPCName.GetProcessName(IPCType) == eprocess) { string cmdtext = CmdObject.BuildCmdText(IPCName.GetProcessName(IPCType), eprocess, Guid.NewGuid().ToString(), method, arg); string retval = funcExecCmd(CmdObject.AnalysisCmdText(cmdtext).methodstr, CmdObject.AnalysisCmdText(cmdtext).argdic);//执行命令 arg1方法名 arg2参数 //string datatext = CmdObject.BuildDataText(CmdObject.AnalysisCmdText(cmdtext).pathstr_end, CmdObject.AnalysisCmdText(cmdtext).pathstr_begin, CmdObject.AnalysisCmdText(cmdtext).uniqueid, retval); return(retval); } lock (syncObj) { string cmdtext = ""; bool IsCompleted = false;//是否完成 string retData = ""; actionReturnData = ((CmdObject cobj) => { if (cobj.uniqueid == CmdObject.AnalysisCmdText(cmdtext).uniqueid) { retData = cobj.retdata; IsCompleted = true; } }); if (IPCType == IPCType.efwplusBase) { cmdtext = CmdObject.BuildCmdText("efwplusbase", eprocess, Guid.NewGuid().ToString(), method, arg); ipcw.WriteData(cmdtext, IPCType.efwplusServer, IPCType); } else if (IPCType == IPCType.efwplusRoute) { cmdtext = CmdObject.BuildCmdText("efwplusroute", eprocess, Guid.NewGuid().ToString(), method, arg); ipcw.WriteData(cmdtext, IPCType.efwplusServer, IPCType); } else if (IPCType == IPCType.efwplusWebAPI) { cmdtext = CmdObject.BuildCmdText("efwpluswebapi", eprocess, Guid.NewGuid().ToString(), method, arg); ipcw.WriteData(cmdtext, IPCType.efwplusServer, IPCType); } //超时计时器 Stopwatch sw = new Stopwatch(); sw.Start(); //是否超时 bool isouttime = false; while (!IsCompleted) { if (IsCompleted) { break; } //如果还未获取连接判断是否超时5秒,如果超时抛异常 if (sw.Elapsed >= new TimeSpan(5 * 1000 * 10000)) { isouttime = true; break; } else { Thread.Sleep(100); } } if (isouttime) { throw new Exception("命令执行超时"); } return(retData); } }