public void OnRead(int msgType, IntPtr _packId , int userMsgType, int userValue, IntPtr _userShortStr , IntPtr data, int dataLen) { string packId = FastIPCNative.ptr2string(_packId); string userShortStr = FastIPCNative.ptr2string(_userShortStr); if (msgType == FastIPCNative.MSG_TYPE_NORMAL) { OnRead(userMsgType, userValue, userShortStr, FastIPCNative.ptr2string(data, dataLen)); } else { Stream bos = null; if (caches.ContainsKey(packId)) { bos = caches[packId]; } else { bos = new MemoryStream(); caches.Add(packId, bos); } FastIPCNative.readPtr(bos, data, dataLen); if (msgType == FastIPCNative.MSG_TYPE_END) { caches.Remove(packId); byte[] bytes = new byte[bos.Length]; bos.Position = 0; // 设置当前流的位置为流的开始 bos.Read(bytes, 0, bytes.Length); string rtn = Encoding.UTF8.GetString(bytes); OnRead(userMsgType, userValue, userShortStr, rtn); } } }
void startServer() { String dir = "..\\..\\..\\Release\\"; String clientExe = (dir + "TestFastIPCClient.exe"); //System.out.println("客户端可执行文件 = " + clientExe.getAbsolutePath()); if (!File.Exists(clientExe)) { throw new Exception("客户端可执行文件[" + clientExe + "]不存在"); } //initDLL(dir); this.server = new FastIPCServer(); this.serverName = FastIPCNative.genServerName(); //System.out.println("serverName = " + serverName); this.blockSize = 5; server.create(serverName, blockSize); server.setListener(new MYReadListener(this)); Thread th = new Thread(new ThreadStart(__startIPCServer)); //也可简写为new Thread(ThreadMethod); th.Start(); //启动线程 ProcessStartInfo process = new ProcessStartInfo(); process.FileName = Path.GetFullPath(clientExe); process.Arguments = " " + serverName + " " + blockSize; process.UseShellExecute = false; process.CreateNoWindow = true; process.RedirectStandardOutput = true; Process.Start(process); // string Result = p.StandardOutput.ReadToEnd();// 这里会造成线程等待 }
public void close() { if (nativeServer != 0) { FastIPCNative.closeServer(nativeServer); nativeServer = 0; } }
protected void wrtieMsgTo() { if (client == null) { client = new FastIPCClient(); client.create(FastIPCNative.genServerName(serverName), blockSize); } client.write(txtMsg.Text); txtSended.Text += "\r\n发送 " + txtMsg.Text; }
// 创建客户端 public static int createClient(String serverName, int blockSize) { FastIPCNative.checkServerName(serverName); int result = -1; int objectPtr = FastIPCNative.nCreateClient(serverName, blockSize, ref result); if (result != FastIPCNative.ERR_NONE) { throw new FastIPCException("创建客户端失败(" + result + ")!"); } return(objectPtr); }
public void startRead() { if (nativeServer == 0) { throw new FastIPCException("服务器尚未创建!"); } if (listener == null) { throw new FastIPCException("必须指定listener才能侦听!"); } FastIPCNative.startRead(nativeServer, listener); }
public void close() { FastIPCNative.closeClient(nativeClient); }
public void write(int userMsgType, int userValue, String userShortStr, String data) { // System.out.println("Writed msgId=" + userMsgType + " userValue=" + userValue + " taskId=" + userShortStr + " msg=" + data); FastIPCNative.write(nativeClient, userMsgType, userValue, userShortStr, data); }
private int nativeClient = 0;// 指向fastipc::Client实例的指针 public void create(String serverName, int blockSize) { nativeClient = FastIPCNative.createClient(serverName, blockSize); }
// 关闭服务器 public static void closeServer(int nativeServer) { FastIPCNative.nCloseServer(nativeServer); }
public void create(String serverName, int blockSize) { nativeServer = FastIPCNative.createServer(serverName, blockSize); }