public void ProcessMessage(IMessage msg, ITransportHeaders reqHead, Stream reqStm, out ITransportHeaders respHead, out Stream respStm) { DBG.Info(null, "Being asked to process the serialized message!"); //Console.WriteLine("ProcessMessage TID {0}", Thread.CurrentThread.GetHashCode()); // Send the message across the pipe. PipeConnection _pipe = SendWithRetry(msg, reqHead, reqStm); respHead = null; respStm = null; // Read response if (_pipe != null) { _pipe.BeginReadMessage(); respHead = _pipe.ReadHeaders(); respHead["__CustomErrorsEnabled"] = false; respStm = _pipe.ReadStream(); _pipe.EndReadMessage(); if (_pipeConnectionPool != null) { _pipeConnectionPool.ReturnToPool(_pipe); } _pipe = null; } }
public void AsyncProcessRequest(IClientChannelSinkStack stack, IMessage msg, ITransportHeaders headers, Stream stream) { DBG.Info(null, "Async: Send the message across the pipe"); PipeConnection _pipe = SendWithRetry(msg, headers, stream); IMethodCallMessage mcm = (IMethodCallMessage)msg; MethodBase methodBase = mcm.MethodBase; bool oneway = RemotingServices.IsOneWay(methodBase); if (oneway) { if (_pipeConnectionPool != null) { _pipeConnectionPool.ReturnToPool(_pipe); } _pipe = null; } else { PipeConnectionCookie cookie = new PipeConnectionCookie(); cookie.pipe = _pipe; cookie.sinkStack = stack; //TODO Switch to use Completion port ThreadPool.QueueUserWorkItem(callback, cookie); } }
public ITransportHeaders ReadHeaders() { TransportHeaders headers = new TransportHeaders(); // read uri (and make sure that no channel specific data is present) String uri = ReadString(); if (uri != "" && uri != null) { String chanuri, objuri; chanuri = PipeConnection.Parse(uri, out objuri); if (chanuri == null) { objuri = uri; } headers[CommonTransportKeys.RequestUri] = objuri; } // read to end of headers ushort marker = ReadUShort(); while (marker == HeaderMarker) { String hname = ReadString(); String hvalue = ReadString(); headers[hname] = hvalue; marker = ReadUShort(); } return(headers); }
public void ReturnToPool(Object obj) { lock (_list) { PipeConnection _pipe = (PipeConnection)obj; _pipe.UpdateLastTimeAccessed(); _list.Add(obj); } }
private void WriteExceptionResponse(PipeConnection pipe, string exceptionMessage) { try { MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(exceptionMessage)); pipe.BeginWriteMessage(); pipe.Write(stream); pipe.EndWriteMessage(); } catch { } }
public void Dispose() { StopListening(null); if (_pipe != null) { _pipe.Dispose(); _pipe = null; } //TODO: Cancel listeners. }
internal PipeClientTransportSink(String url) { String objuri = null; String chanuri = PipeConnection.Parse(url, out objuri); DBG.Info(null, "PipeClientTransportSink: creating pipe on uri: " + chanuri); //Console.WriteLine("PipeClientTransportSink {0}", chanuri); _pipeName = chanuri; _pipeConnectionPool = PipeConnectionPoolManager.LookupPool(_pipeName); callback = new WaitCallback(this.ReceiveCallback); }
private void ReceiveCallback(Object state) { //Console.WriteLine("ReceiveCallback TID {0}", Thread.CurrentThread.GetHashCode()); PipeConnectionCookie cookie = (PipeConnectionCookie)state; PipeConnection _pipe = cookie.pipe; IClientChannelSinkStack sinkStack = cookie.sinkStack; try { // Read response // _pipe.BeginReadMessage(); ITransportHeaders responseHeaders = _pipe.ReadHeaders(); responseHeaders["__CustomErrorsEnabled"] = false; Stream responseStream = _pipe.ReadStream(); _pipe.EndReadMessage(); if (_pipeConnectionPool != null) { _pipeConnectionPool.ReturnToPool(_pipe); } _pipe = null; sinkStack.AsyncProcessResponse(responseHeaders, responseStream); } catch (Exception e) { try { if (sinkStack != null) { sinkStack.DispatchException(e); } } catch (Exception) { // Fatal Error.. ignore } } } // ReceiveCallback
private void ListenerMain() { // Common ThreadStart delegate ThreadStart ts = new ThreadStart(this.ServerMain); Thread.CurrentThread.IsBackground = true; while (true) { try { _pipe = new PipeConnection(m_pipeName, true, m_pipeSecurityDescriptor); //TODO switch to use completion ports // Wait for a client to connect bool connected = _pipe.WaitForConnect(); if (!connected) { throw new PipeIOException("Could not connect to the pipe - os returned " + Marshal.GetLastWin32Error()); } else { //TODO Consider using ThreadPool.QueueUserWorkItem Thread server = new Thread(ts); server.IsBackground = true; server.Start(); } // Wait for the handler to spin up: _event.WaitOne(); } catch (Exception e) { e = e; } } }
//////////////////////////////////////////////////////////// public void CloseStaleConnections() { ArrayList _activeConnections = new ArrayList(); foreach (Object obj in _list) { PipeConnection _pipe = (PipeConnection)obj; if (_pipe.IsConnectionStale() == true) { DBG.Info(null, "Disposing of stale connection"); _pipe.Dispose(); _pipe = null; } else { _activeConnections.Add(_pipe); } } _list = _activeConnections; }
private void WriteClientResponse(PipeConnection pipe, ITransportHeaders headers, Stream responseStream) { String uri; Object oUri = headers[CommonTransportKeys.RequestUri]; if (oUri != null) { uri = oUri.ToString(); } else { uri = ""; } pipe.BeginWriteMessage(); pipe.WriteHeaders( uri, headers); pipe.Write(responseStream); pipe.EndWriteMessage(); }
public PipeConnection SendWithRetry(IMessage msg, ITransportHeaders reqHead, Stream reqStm) { IMethodCallMessage mcm = (IMethodCallMessage)msg; String uri = mcm.Uri; PipeConnection _pipe = null; int tryCount = _defaultRetryCount; long reqStmPosition = -1; if (reqStm.CanSeek == false) tryCount=1; else reqStmPosition = reqStm.Position; while ( tryCount>0) { try { if (_pipeConnectionPool != null) { DBG.Info(null, "Look in pipe connection in pool"); _pipe = (PipeConnection)_pipeConnectionPool.Obtain(); } // otherwise create a new connection if (_pipe == null) { _pipe = new PipeConnection(_pipeName, false, IntPtr.Zero); } //Send with Retry _pipe.BeginWriteMessage(); _pipe.WriteHeaders(uri, reqHead); _pipe.Write(reqStm); _pipe.EndWriteMessage(); tryCount=0; } catch(PipeIOException pe) { pe=pe; if (_pipe != null) { _pipe.Dispose(); _pipe = null; } tryCount--; reqStm.Position = reqStmPosition; } } return _pipe; }
public String Parse(String url, out string uri) { return(PipeConnection.Parse(url, out uri)); }
private void ServerMain() { PipeConnection pipe = _pipe; _pipe = null; // Signal the guy to start waiting again... (w/ new event and endpoint) _event.Set(); try { //TODO close the connection on a timeout //TODO if no activity for Nnnn milliseconds while (true) { pipe.BeginReadMessage(); ITransportHeaders headers = pipe.ReadHeaders(); headers["__CustomErrorsEnabled"] = false; Stream request = pipe.ReadStream(); pipe.EndReadMessage(); ServerChannelSinkStack stack = new ServerChannelSinkStack(); stack.Push(_transportSink, null); IMessage responseMsg; ITransportHeaders responseHeaders; Stream responseStream; ServerProcessing processing = _transportSink.NextChannelSink.ProcessMessage(stack, null, headers, request, out responseMsg, out responseHeaders, out responseStream); // handle response switch (processing) { case ServerProcessing.Complete: // Send the response. Call completed synchronously. stack.Pop(_transportSink); WriteClientResponse(pipe, responseHeaders, responseStream); break; case ServerProcessing.OneWay: break; case ServerProcessing.Async: stack.StoreAndDispatch(_transportSink, null); break; } } } catch (Exception e) { DBG.Info(null, "Terminating client connection: " + e.Message); WriteExceptionResponse(pipe, e.ToString()); } finally { if (pipe != null) { pipe.Dispose(); } } }
public PipeConnection SendWithRetry(IMessage msg, ITransportHeaders reqHead, Stream reqStm) { IMethodCallMessage mcm = (IMethodCallMessage)msg; String uri = mcm.Uri; PipeConnection _pipe = null; int tryCount = _defaultRetryCount; long reqStmPosition = -1; if (reqStm.CanSeek == false) { tryCount = 1; } else { reqStmPosition = reqStm.Position; } while (tryCount > 0) { try { if (_pipeConnectionPool != null) { DBG.Info(null, "Look in pipe connection in pool"); _pipe = (PipeConnection)_pipeConnectionPool.Obtain(); } // otherwise create a new connection if (_pipe == null) { _pipe = new PipeConnection(_pipeName, false, IntPtr.Zero); } //Send with Retry _pipe.BeginWriteMessage(); _pipe.WriteHeaders(uri, reqHead); _pipe.Write(reqStm); _pipe.EndWriteMessage(); tryCount = 0; } catch (PipeIOException pe) { pe = pe; if (_pipe != null) { _pipe.Dispose(); _pipe = null; } tryCount--; reqStm.Position = reqStmPosition; } } return(_pipe); }
private void ListenerMain() { // Common ThreadStart delegate ThreadStart ts = new ThreadStart(this.ServerMain); Thread.CurrentThread.IsBackground = true; while(true) { try { _pipe = new PipeConnection(m_pipeName, true, m_pipeSecurityDescriptor); //TODO switch to use completion ports // Wait for a client to connect bool connected = _pipe.WaitForConnect(); if (!connected) { throw new PipeIOException("Could not connect to the pipe - os returned " + Marshal.GetLastWin32Error()); } else { //TODO Consider using ThreadPool.QueueUserWorkItem Thread server = new Thread(ts); server.IsBackground = true; server.Start(); } // Wait for the handler to spin up: _event.WaitOne(); } catch(Exception e) { e=e; } } }