扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
public static PipeHandle Create(string name,uintoutBuffer, uintinBuffer){ name = @"\.\pipe\" + name; PipeHandle handle = new PipeHandle(); for(inti=1;i<=ATTEMPTS;i++){ handle.State=InterProcessConnectionState.Creating; handle.Handle = NamedPipeNative.CreateNamedPipe( name, NamedPipeNative.PIPE_ACCESS_DUPLEX, NamedPipeNative.PIPE_TYPE_MESSAGE | NamedPipeNative.PIPE_READMODE_MESSAGE | NamedPipeNative.PIPE_WAIT, NamedPipeNative.PIPE_UNLIMITED_INSTANCES, outBuffer, inBuffer, NamedPipeNative.NMPWAIT_WAIT_FOREVER, IntPtr.Zero); if(handle.Handle.ToInt32()!=NamedPipeNative.INVALID_HANDLE_VALUE){ handle.State=InterProcessConnectionState.Created; break; } if (i >= ATTEMPTS) { handle.State = InterProcessConnectionState.Error; throw new NamedPipeIOException("Error creating named pipe"+name+".Internalerror:"+NamedPipeNative.GetLastError().ToString(),NamedPipeNative.GetLastError()); } } returnhandle; } |
public static PipeHandle ConnectToPipe(string pipeName, string serverName) { PipeHandle handle = new PipeHandle(); //Buildthename ofthe pipe. string name = @"\" + serverName + @"\pipe\" + pipeName; for(inti=1;i<=ATTEMPTS;i++){ handle.State = InterProcessConnectionState.ConnectingToServer; // Try to connect to the server handle.Handle = NamedPipeNative.CreateFile(name, NamedPipeNative.GENERIC_READ | NamedPipeNative. GENERIC_WRITE, 0,null,NamedPipeNative.OPEN_EXISTING,0,0); |
if (handle.Handle.ToInt32() != NamedPipeNative.INVALID_HANDLE_VALUE){ // The client managed to connect to the server pipe handle.State = InterProcessConnectionState. ConnectedToServer; // Set the read mode of the pipe channel uint mode = NamedPipeNative.PIPE_READMODE_MESSAGE; if(NamedPipeNative.SetNamedPipeHandleState(handle.Handle,refmode,IntPtr.Zero,IntPtr.Zero)){ break; } |
public static void WriteBytes(PipeHandle handle, byte[]bytes) { byte[] numReadWritten = new byte[4]; uint len; if(bytes==null){ bytes=newbyte[0]; } if (bytes.Length == 0) { bytes = new byte[1]; bytes = System.Text.Encoding.UTF8.GetBytes(" "); } // 获取消息的长度: len= (uint)bytes.Length; handle.State = InterProcessConnectionState.Writing; // 获取消息长度的字节表示,先写这四字节 if(NamedPipeNative.WriteFile(handle.Handle,BitConverter.GetBytes(len),4,numReadWritten,0)){ // 写余下的消息 if(!NamedPipeNative.WriteFile(handle.Handle,bytes,len,numReadWritten,0)){ handle.State=InterProcessConnectionState.Error; thrownewNamedPipeIOException("Errorwritingtopipe. Internalerror:"+NamedPipeNative.GetLastError().ToString(), NamedPipeNative.GetLastError()); } } else{ handle.State=InterProcessConnectionState.Error; thrownewNamedPipeIOException("Errorwritingtopipe.Internalerror:"+NamedPipeNative.GetLastError().ToString(), NamedPipeNative.GetLastError()); } handle.State =InterProcessConnectionState.Flushing; // 激活管道,保证任何缓存数据都被写入管道,不会丢失: Flush(handle); handle.State = InterProcessConnectionState.FlushedData; } |
public static byte[] ReadBytes(PipeHandle handle, int maxBytes) { byte[]numReadWritten=newbyte[4]; byte[]intBytes=newbyte[4]; byte[]msgBytes=null; intlen; handle.State=InterProcessConnectionState.Reading; handle.State=InterProcessConnectionState.Flushing; // 读前四个字节并转化为整数: if(NamedPipeNative.ReadFile(handle.Handle, intBytes,4, numReadWritten, 0)) { len=BitConverter.ToInt32(intBytes,0); msgBytes=newbyte[len]; handle.State=InterProcessConnectionState.Flushing; // 读余下的数据或抛出异常: if(!NamedPipeNative.ReadFile(handle.Handle,msgBytes,(uint) len,numReadWritten,0)){ handle.State=InterProcessConnectionState.Error; thrownewNamedPipeIOException("Error readingfrompipe. Internalerror:"+NamedPipeNative.GetLastError().ToString(), NamedPipeNative.GetLastError()); } } else { handle.State=InterProcessConnectionState.Error; thrownewNamedPipeIOException("Errorreadingfrompipe. Internalerror:"+NamedPipeNative.GetLastError().ToString(), NamedPipeNative.GetLastError()); } handle.State=InterProcessConnectionState.ReadData; if(len>maxBytes){ returnnull; } returnmsgBytes; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。