public static PipeInstance Connect(PipeName pipeName) { // parameters validation if (pipeName == null) throw new ArgumentNullException("pipeName", "Pipe name must be specified"); while (true) { // try to connect to the pipe IntPtr handle = PipeNative.CreateFile(pipeName.ToString(), PipeNative.GENERIC_READ | PipeNative.GENERIC_WRITE, PipeNative.FILE_SHARE_NONE, null, PipeNative.OPEN_EXISTING, PipeNative.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (handle == Win32.InvalidHandle) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode != PipeNative.ERROR_PIPE_BUSY) { throw new PipeIOException(errorCode, "Could not open pipe: " + pipeName); } if (!PipeNative.WaitNamedPipe(pipeName.ToString(), PipeNative.NMPWAIT_USE_DEFAULT_WAIT)) { throw new PipeIOException(errorCode, "Specified pipe was over-burdened: " + pipeName); } } else { return new PipeInstance(pipeName, handle, true); } } }
public static PipeInstance Create(PipeName pipeName, bool firstInstance, SecurityDescriptor securityDescriptor) { // parameters validation if (pipeName == null) { throw new ArgumentNullException("pipeName", "Pipe name must be specified"); } if (!pipeName.IsLocal) { throw new ArgumentException("Could not bind to the remote pipe"); } PipeNative.SecurityAttributes secAttrs = new PipeNative.SecurityAttributes(); secAttrs.SecurityDescriptor = (securityDescriptor == null ? IntPtr.Zero : securityDescriptor.Handle); secAttrs.InheritHandle = true; // try to create pipe IntPtr handle = PipeNative.CreateNamedPipe(pipeName.ToString(), PipeNative.PIPE_ACCESS_DUPLEX | PipeNative.FILE_FLAG_OVERLAPPED | (firstInstance ? PipeNative.FILE_FLAG_FIRST_PIPE_INSTANCE : 0), PipeNative.PIPE_TYPE_BYTE | PipeNative.PIPE_READMODE_BYTE | PipeNative.PIPE_WAIT, PipeNative.PIPE_UNLIMITED_INSTANCES, OutBufferSize, InBufferSize, PipeNative.NMPWAIT_USE_DEFAULT_WAIT, secAttrs); if (handle == Win32.InvalidHandle) { throw new PipeIOException(Marshal.GetLastWin32Error(), "Could not create pipe: " + pipeName); } else { return(new PipeInstance(pipeName, handle, false)); } }
public PipeInstance(PipeName pipeName, IntPtr handle, bool isConnected) { // parameters validation if (pipeName == null) throw new ArgumentNullException("pipeName"); if (handle == Win32.InvalidHandle) throw new ArgumentException("Invalid pipe handle", "handle"); _pipeName = pipeName; _handle = new NativeHandle(handle); _isConnected = isConnected; }
/// <summary> /// Establishes a connection to a remote system /// </summary> public void Connect(PipeName pipeName) { // check object state if (_disposed) { throw new ObjectDisposedException(GetType().FullName); } if (_instance != null) { throw new InvalidOperationException("Pipe is already connected"); } // connect to the remote host _instance = PipeInstance.Connect(pipeName); }
public PipeInstance(PipeName pipeName, IntPtr handle, bool isConnected) { // parameters validation if (pipeName == null) { throw new ArgumentNullException("pipeName"); } if (handle == Win32.InvalidHandle) { throw new ArgumentException("Invalid pipe handle", "handle"); } _pipeName = pipeName; _handle = new NativeHandle(handle); _isConnected = isConnected; }
/// <summary> /// Creates pipe on the server side to allow client to connect to /// </summary> public void Bind(PipeName pipeName, SecurityDescriptor securityDescriptor) { // check object state if (_disposed) { throw new ObjectDisposedException(GetType().FullName); } if (_instance != null) { throw new InvalidOperationException("Pipe is already connected"); } // store security descriptor _securityDescriptor = securityDescriptor; // create pipe _instance = PipeInstance.Create(pipeName, true, _securityDescriptor); _instancePool = new PipeInstancePool(this); }
public static PipeInstance Connect(PipeName pipeName) { // parameters validation if (pipeName == null) { throw new ArgumentNullException("pipeName", "Pipe name must be specified"); } while (true) { // try to connect to the pipe IntPtr handle = PipeNative.CreateFile(pipeName.ToString(), PipeNative.GENERIC_READ | PipeNative.GENERIC_WRITE, PipeNative.FILE_SHARE_NONE, null, PipeNative.OPEN_EXISTING, PipeNative.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (handle == Win32.InvalidHandle) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode != PipeNative.ERROR_PIPE_BUSY) { throw new PipeIOException(errorCode, "Could not open pipe: " + pipeName); } if (!PipeNative.WaitNamedPipe(pipeName.ToString(), PipeNative.NMPWAIT_USE_DEFAULT_WAIT)) { throw new PipeIOException(errorCode, "Specified pipe was over-burdened: " + pipeName); } } else { return(new PipeInstance(pipeName, handle, true)); } } }
/// <summary> /// Establishes a connection to a remote system /// </summary> public void Connect(PipeName pipeName) { // check object state if (_disposed) throw new ObjectDisposedException(GetType().FullName); if (_instance != null) throw new InvalidOperationException("Pipe is already connected"); // connect to the remote host _instance = PipeInstance.Connect(pipeName); }
/// <summary> /// Creates pipe on the server side to allow client to connect to /// </summary> public void Bind(PipeName pipeName, SecurityDescriptor securityDescriptor) { // check object state if (_disposed) throw new ObjectDisposedException(GetType().FullName); if (_instance != null) throw new InvalidOperationException("Pipe is already connected"); // store security descriptor _securityDescriptor = securityDescriptor; // create pipe _instance = PipeInstance.Create(pipeName, true, _securityDescriptor); _instancePool = new PipeInstancePool(this); }
public static PipeInstance Create(PipeName pipeName, bool firstInstance, SecurityDescriptor securityDescriptor) { // parameters validation if (pipeName == null) throw new ArgumentNullException("pipeName", "Pipe name must be specified"); if (!pipeName.IsLocal) throw new ArgumentException("Could not bind to the remote pipe"); PipeNative.SecurityAttributes secAttrs = new PipeNative.SecurityAttributes(); secAttrs.SecurityDescriptor = (securityDescriptor == null ? IntPtr.Zero : securityDescriptor.Handle); secAttrs.InheritHandle = true; // try to create pipe IntPtr handle = PipeNative.CreateNamedPipe(pipeName.ToString(), PipeNative.PIPE_ACCESS_DUPLEX | PipeNative.FILE_FLAG_OVERLAPPED | (firstInstance ? PipeNative.FILE_FLAG_FIRST_PIPE_INSTANCE : 0), PipeNative.PIPE_TYPE_BYTE | PipeNative.PIPE_READMODE_BYTE | PipeNative.PIPE_WAIT, PipeNative.PIPE_UNLIMITED_INSTANCES, OutBufferSize, InBufferSize, PipeNative.NMPWAIT_USE_DEFAULT_WAIT, secAttrs); if (handle == Win32.InvalidHandle) { throw new PipeIOException(Marshal.GetLastWin32Error(), "Could not create pipe: " + pipeName); } else { return new PipeInstance(pipeName, handle, false); } }
private void Initialize(IDictionary properties, IServerChannelSinkProvider sinkProvider) { if (properties != null) { // read property values foreach (DictionaryEntry property in properties) { switch ((string)property.Key) { case "name": _channelName = Convert.ToString(property.Value); break; case "priority": _channelPriority = Convert.ToInt32(property.Value); break; case "pipe": _pipe = Convert.ToString(property.Value); break; case "securityDescriptor": _securityDescriptor = (property.Value as SecurityDescriptor); break; } } } // setup pipe name _pipeName = new PipeName(@"\\.\pipe\" + _pipe); // create the chain of the sink providers that will process all messages _sinkProvider = ChannelHelper.ServerChannelCreateSinkProviderChain(sinkProvider); _channelData = ChannelHelper.ServerChannelCreateDataStore(ChannelUri, _sinkProvider); // create transport sink IServerChannelSink nextSink = ChannelServices.CreateServerChannelSinkChain(_sinkProvider, this); _transportSink = new ServerTransportSink(nextSink); // create listener thread _transportListener = new TransportListener(ChannelUri, typeof(PipeTransport)); _listenerThread = new Thread(new ThreadStart(ListenerStart)); _listenerThread.IsBackground = true; _requestHandler = new ProcessRequestCallback(_transportSink.ProcessRequest); // start listening on the channel StartListening(null); }
private static PipeName GetPipeName(string url) { IDictionary parts = null; if (!ParseUrl(url, out parts)) throw new ArgumentException("Invalid pipe address specified"); PipeName pipeName = new PipeName((string)parts["host"], (string)parts["pipe"]); return pipeName; }