public void ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, out ITransportHeaders responseHeaders, out Stream responseStream) { InternalRemotingServices.RemotingTrace("IpcClientChannel::ProcessMessage"); IpcPort port = null; // the call to SendRequest can block a func eval, so we want to notify the debugger that we're // about to call a blocking operation. System.Diagnostics.Debugger.NotifyOfCrossThreadDependency(); // The authentication config entries are only valid if secure is true if (authSet && !_channel.IsSecured) { throw new RemotingException(CoreChannel.GetResourceString( "Remoting_Ipc_AuthenticationConfig")); } port = portCache.GetConnection(_portName, _channel.IsSecured, _tokenImpersonationLevel, _timeout); IMethodCallMessage mcm = (IMethodCallMessage)msg; int requestLength = (int)requestStream.Length; Stream ipcStream = new PipeStream(port); IpcClientHandler handler = new IpcClientHandler(port, ipcStream, this); handler.SendRequest(msg, requestHeaders, requestStream); responseHeaders = handler.ReadHeaders(); responseStream = handler.GetResponseStream(); // The client port will be returned to the cache // when the response stream is closed. } // ProcessMessage
private int _timeout = 0; // throw timout exception if a read takes longer than this many milliseconds public PipeStream(IpcPort port) { if (port == null) throw new ArgumentNullException("port"); _port = port; } // SocketStream
public PipeStream(IpcPort port) { if (port == null) { throw new ArgumentNullException("port"); } this._port = port; }
} // StopListening // // end of IChannelReceiver implementation // // Thread for listening void Listen() { InternalRemotingServices.RemotingTrace("Waiting to Accept a Connection on Port: " + _portName); // // Wait for an incoming client connection // IntPtr handle = IntPtr.Zero; bool connected = false; CommonSecurityDescriptor descriptor = _securityDescriptor; // Connect with exlusive flag the first time try{ // If the descriptor is not explicitly set through code use the _authorizedGroup config if (descriptor == null && _authorizedGroup != null) { NTAccount ntAccount = new NTAccount(_authorizedGroup); descriptor = IpcPort.CreateSecurityDescriptor((SecurityIdentifier)ntAccount.Translate(typeof(SecurityIdentifier))); } _port = IpcPort.Create(_portName, descriptor, _bExclusiveAddressUse); } catch (Exception e) { _startListeningException = e; } finally { _bListening = (_startListeningException == null); _waitForStartListening.Set(); // allow main thread to continue now that we have tried to start the socket } if (_port != null) { connected = _port.WaitForConnect(); } while (_bListening) { InternalRemotingServices.RemotingTrace("IpcChannel::Listen"); // For DevDiv#220882, we need to create new IpcPort before handling the current message // to avoid race condition in case the message is handled and finished before // the new IpcPort is created. The client will intermittently fail with Port not found. IpcPort port = IpcPort.Create(_portName, descriptor, false); if (connected) { // Initialize the server handler and perform an async read IpcServerHandler serverHandler = new IpcServerHandler(_port, CoreChannel.RequestQueue, new PipeStream(_port)); serverHandler.DataArrivedCallback = new WaitCallback(_transportSink.ServiceRequest); serverHandler.BeginReadMessage(); } _port = port; connected = _port.WaitForConnect(); } }
public void ReleaseConnection(IpcPort port) { string name = port.Name; PortConnection connection = (PortConnection) _connections[name]; if (port.Cacheable && ((connection == null) || connection.Port.IsDisposed)) { lock (_connections) { _connections[name] = new PortConnection(port); return; } } port.Dispose(); }
public void ReleaseConnection(IpcPort port) { string name = port.Name; PortConnection connection = (PortConnection)_connections[name]; if (port.Cacheable && ((connection == null) || connection.Port.IsDisposed)) { lock (_connections) { _connections[name] = new PortConnection(port); return; } } port.Dispose(); }
private void Listen() { bool flag = true; bool flag2 = false; CommonSecurityDescriptor securityDescriptor = this._securityDescriptor; if (flag) { try { if ((securityDescriptor == null) && (this._authorizedGroup != null)) { NTAccount account = new NTAccount(this._authorizedGroup); securityDescriptor = IpcPort.CreateSecurityDescriptor((SecurityIdentifier)account.Translate(typeof(SecurityIdentifier))); } this._port = IpcPort.Create(this._portName, securityDescriptor, this._bExclusiveAddressUse); } catch (Exception exception) { this._startListeningException = exception; } finally { this._waitForStartListening.Set(); } if (this._port != null) { flag2 = this._port.WaitForConnect(); flag = this._bListening; } } while (flag && (this._startListeningException == null)) { IpcPort port = IpcPort.Create(this._portName, securityDescriptor, false); if (flag2) { new IpcServerHandler(this._port, CoreChannel.RequestQueue, new PipeStream(this._port)) { DataArrivedCallback = new WaitCallback(this._transportSink.ServiceRequest) }.BeginReadMessage(); } this._port = port; flag2 = this._port.WaitForConnect(); flag = this._bListening; } }
public void ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, out ITransportHeaders responseHeaders, out Stream responseStream) { IpcPort port = null; Debugger.NotifyOfCrossThreadDependency(); if (this.authSet && !this._channel.IsSecured) { throw new RemotingException(CoreChannel.GetResourceString("Remoting_Ipc_AuthenticationConfig")); } port = this.portCache.GetConnection(this._portName, this._channel.IsSecured, this._tokenImpersonationLevel, this._timeout); IMethodCallMessage message1 = (IMethodCallMessage)msg; long length = requestStream.Length; Stream stream = new PipeStream(port); IpcClientHandler handler = new IpcClientHandler(port, stream, this); handler.SendRequest(msg, requestHeaders, requestStream); responseHeaders = handler.ReadHeaders(); responseStream = handler.GetResponseStream(); }
} // GetSocket public void ReleaseConnection(IpcPort port) { string portName = port.Name; PortConnection connection = (PortConnection)_connections[portName]; if (port.Cacheable && (connection == null || connection.Port.IsDisposed)) { lock (_connections) { _connections[portName] = new PortConnection(port); } } else { // there should have been a connection, so let's just close // this socket. port.Dispose(); } } // ReleasePort
} // TimeoutConnections // The key is expected to of the form portName public IpcPort GetConnection(String portName, bool secure, TokenImpersonationLevel level, int timeout) { PortConnection connection = null; lock (_connections) { bool cacheable = true; if (secure) { try { WindowsIdentity currentId = WindowsIdentity.GetCurrent(true /*ifImpersonating*/); if (currentId != null) { cacheable = false; currentId.Dispose(); } } catch (Exception) { cacheable = false; } } if (cacheable) { connection = (PortConnection)_connections[portName]; } if (connection == null || connection.Port.IsDisposed) { connection = new PortConnection(IpcPort.Connect(portName, secure, level, timeout)); connection.Port.Cacheable = cacheable; } else { // Remove the connection from the cache _connections.Remove(portName); } } return(connection.Port); } // GetSocket
public IpcPort GetConnection(string portName, bool secure, TokenImpersonationLevel level, int timeout) { PortConnection connection = null; lock (_connections) { bool flag = true; if (secure) { try { WindowsIdentity current = WindowsIdentity.GetCurrent(true); if (current != null) { flag = false; current.Dispose(); } } catch (Exception) { flag = false; } } if (flag) { connection = (PortConnection)_connections[portName]; } if ((connection == null) || connection.Port.IsDisposed) { connection = new PortConnection(IpcPort.Connect(portName, secure, level, timeout)) { Port = { Cacheable = flag } }; } else { _connections.Remove(portName); } } return(connection.Port); }
} // StopListening // // end of IChannelReceiver implementation // // Thread for listening void Listen() { bool bOkToListen = true; InternalRemotingServices.RemotingTrace( "Waiting to Accept a Connection on Port: " + _portName); // // Wait for an incoming client connection // IntPtr handle = IntPtr.Zero; bool connected = false; CommonSecurityDescriptor descriptor = _securityDescriptor; // Connect with exlusive flag the first time if (bOkToListen) { try{ // If the descriptor is not explicitly set through code use the _authorizedGroup config if (descriptor == null && _authorizedGroup != null) { NTAccount ntAccount = new NTAccount(_authorizedGroup); descriptor = IpcPort.CreateSecurityDescriptor((SecurityIdentifier)ntAccount.Translate(typeof(SecurityIdentifier))); } _port = IpcPort.Create(_portName, descriptor, _bExclusiveAddressUse); } catch (Exception e) { _startListeningException = e; } finally { _waitForStartListening.Set(); // allow main thread to continue now that we have tried to start the socket } if (_port != null){ connected = _port.WaitForConnect(); bOkToListen = _bListening; } } while (bOkToListen && _startListeningException == null) { InternalRemotingServices.RemotingTrace("IpcChannel::Listen"); // For DevDiv#220882, we need to create new IpcPort before handling the current message // to avoid race condition in case the message is handled and finished before // the new IpcPort is created. The client will intermittently fail with Port not found. IpcPort port = IpcPort.Create(_portName, descriptor, false); if (connected) { // Initialize the server handler and perform an async read IpcServerHandler serverHandler = new IpcServerHandler(_port, CoreChannel.RequestQueue, new PipeStream(_port)); serverHandler.DataArrivedCallback = new WaitCallback(_transportSink.ServiceRequest); serverHandler.BeginReadMessage(); } _port = port; connected = _port.WaitForConnect(); bOkToListen = _bListening; } }
internal void ServiceRequest(Object state) { IpcServerHandler ipcServerHandler = (IpcServerHandler)state; // Read the headers from the stream, using the header size in the message ITransportHeaders headers = ipcServerHandler.ReadHeaders(); // Get the request Stream Stream requestStream = ipcServerHandler.GetRequestStream(); // customErrors should be disabled, since we are on the same machine headers["__CustomErrorsEnabled"] = false; // process request ServerChannelSinkStack sinkStack = new ServerChannelSinkStack(); sinkStack.Push(this, ipcServerHandler); IMessage responseMessage = null; ITransportHeaders responseHeaders = null; Stream responseStream = null; WindowsIdentity identity = null; IPrincipal oldPrincipal = null; bool impersonated = false; bool principalChanged = false; ServerProcessing processing = ServerProcessing.Complete; try{ if (_secure) { IpcPort port = ipcServerHandler.Port; port.ImpersonateClient(); oldPrincipal = Thread.CurrentPrincipal; principalChanged = true; impersonated = true; identity = WindowsIdentity.GetCurrent(); // If the authentication mode is to identify callers only revert the impersonation immediately if (!_impersonate) { NativePipe.RevertToSelf(); Thread.CurrentPrincipal = new GenericPrincipal(identity, null); impersonated = false; } else { if (identity.ImpersonationLevel == TokenImpersonationLevel.Impersonation || identity.ImpersonationLevel == TokenImpersonationLevel.Delegation) { // Set the current principal Thread.CurrentPrincipal = new WindowsPrincipal(identity); } else { throw new RemotingException(CoreChannel.GetResourceString( "Remoting_Ipc_TokenImpersonationFailure")); } } } processing = _nextSink.ProcessMessage(sinkStack, null, headers, requestStream, out responseMessage, out responseHeaders, out responseStream); } catch (Exception e) { ipcServerHandler.CloseOnFatalError(e); } finally{ // Revert the principal if we had changed the principal if (principalChanged) { Thread.CurrentPrincipal = oldPrincipal; } // Revert the impersonation if we had impersonated if (impersonated) { NativePipe.RevertToSelf(); impersonated = false; } } // handle response switch (processing) { case ServerProcessing.Complete: { // Send the response. Call completed synchronously. sinkStack.Pop(this); // Send the response back to the client ipcServerHandler.SendResponse(responseHeaders, responseStream); break; } // case ServerProcessing.Complete case ServerProcessing.OneWay: { // No response needed, but the following method will make sure that // we send at least a skeleton reply if the incoming request was // not marked OneWayRequest (client/server metadata could be out of // [....]). ipcServerHandler.SendResponse(responseHeaders, responseStream); break; } // case ServerProcessing.OneWay case ServerProcessing.Async: { sinkStack.StoreAndDispatch(this, ipcServerHandler); break; } // case ServerProcessing.Async } // switch (processing) // Start waiting for the next request if (processing != ServerProcessing.Async) { ipcServerHandler.BeginReadMessage(); } } // ServiceRequest
} // GetSocket public void ReleaseConnection(IpcPort port) { string portName = port.Name; PortConnection connection = (PortConnection)_connections[portName]; if (port.Cacheable && (connection == null || connection.Port.IsDisposed)) { lock(_connections) { _connections[portName] = new PortConnection(port); } } else { // there should have been a connection, so let's just close // this socket. port.Dispose(); } } // ReleasePort
internal IpcClientHandler(IpcPort port, Stream stream, IpcClientTransportSink sink) : base (port, null, stream) { _sink = sink; }
internal IpcServerHandler(IpcPort port, RequestQueue requestQueue, Stream stream) : base(null, requestQueue, stream) { _requestQueue = requestQueue; _port = port; _stream = stream; }
internal IpcServerHandler(IpcPort port, RequestQueue requestQueue, Stream stream) : base (null, requestQueue, stream) { _requestQueue = requestQueue; _port = port; _stream = stream; }
internal PortConnection(IpcPort port) { this._port = port; this._socketLastUsed = DateTime.Now; }
private void Listen() { bool flag = true; bool flag2 = false; CommonSecurityDescriptor securityDescriptor = this._securityDescriptor; if (flag) { try { if ((securityDescriptor == null) && (this._authorizedGroup != null)) { NTAccount account = new NTAccount(this._authorizedGroup); securityDescriptor = IpcPort.CreateSecurityDescriptor((SecurityIdentifier) account.Translate(typeof(SecurityIdentifier))); } this._port = IpcPort.Create(this._portName, securityDescriptor, this._bExclusiveAddressUse); } catch (Exception exception) { this._startListeningException = exception; } finally { this._waitForStartListening.Set(); } if (this._port != null) { flag2 = this._port.WaitForConnect(); flag = this._bListening; } } while (flag && (this._startListeningException == null)) { IpcPort port = IpcPort.Create(this._portName, securityDescriptor, false); if (flag2) { new IpcServerHandler(this._port, CoreChannel.RequestQueue, new PipeStream(this._port)) { DataArrivedCallback = new WaitCallback(this._transportSink.ServiceRequest) }.BeginReadMessage(); } this._port = port; flag2 = this._port.WaitForConnect(); flag = this._bListening; } }
internal IpcClientHandler(IpcPort port, Stream stream, IpcClientTransportSink sink) : base(port, null, stream) { this._sink = sink; }
internal PortConnection(IpcPort port) { _port = port; _socketLastUsed = DateTime.Now; }