/// <summary> /// Writes a request. /// </summary> /// <param name="header"></param> /// <param name="requestStream"></param> public void Write(ITransportHeaders header, Stream requestStream) { Stream bs = socket.GetStream(); MemoryStream m = new MemoryStream(); formatter.Serialize(m, header); m.Position = 0; byte[] bytes = BitConverter.GetBytes((int)m.Length); bs.Write(bytes, 0, bytes.Length); m.WriteTo(bs); try { bytes = BitConverter.GetBytes((int)requestStream.Length); bs.Write(bytes, 0, bytes.Length); IpcChannelHelper.Copy(requestStream, bs); } catch { bs.Write(bytes, 0, bytes.Length); } bs.Flush(); }
/// <summary> /// Creates a server channel. /// </summary> /// <param name="properties">The channel properties.</param> /// <param name="provider">The sink provider.</param> public IpcServerChannel(IDictionary properties, IServerChannelSinkProvider provider) { bool impersonate = false; if (properties != null) { foreach (DictionaryEntry e in properties) { switch ((string)e.Key) { case "name": channelName = (string)e.Value; break; case "priority": channelPriority = Convert.ToInt32(e.Value); break; case "portName": portName = (string)e.Value; if (!IpcChannelHelper.IsValidPipeName(portName)) { throw new ArgumentException("Invalid pipe name.", "portName"); } break; case "impersonate": impersonate = Boolean.Parse((string)e.Value); break; } } } if (portName == null) { portName = Guid.NewGuid().ToString("N"); } serverProvider = provider; if (serverProvider == null) { serverProvider = new BinaryServerFormatterSinkProvider(); } dataStore = new ChannelDataStore( new string[] { IpcChannelHelper.SchemeStart + portName } ); PopulateChannelData(dataStore, serverProvider); sink = new IpcServerChannelSink( ChannelServices.CreateServerChannelSinkChain(serverProvider, this), portName, impersonate ); StartListening(null); }
public IpcClientChannelSink(string url) { string unused; IpcChannelHelper.Parse(url, out pipeName, out unused); }
public string Parse(string url, out string objectURI) { return(IpcChannelHelper.Parse(url, out objectURI)); }
void ProcessClient(object state) { try { NamedPipeSocket socket = (NamedPipeSocket)state; ITransportHeaders requestHeaders; Stream requestStream; IpcTransport t = new IpcTransport(socket); t.Read(out requestHeaders, out requestStream); // parse the RequestUri string objectUri; string uri = (string)requestHeaders[CommonTransportKeys.RequestUri]; IpcChannelHelper.Parse(uri, out objectUri); if (objectUri == null) { objectUri = uri; } requestHeaders[CommonTransportKeys.RequestUri] = objectUri; ServerChannelSinkStack stack = new ServerChannelSinkStack(); stack.Push(this, null); IMessage responseMsg; ITransportHeaders responseHeaders; Stream responseStream; requestHeaders["__CustomErrorsEnabled"] = false; if (impersonate) { // TODO: Impersonate might throw exceptions. What to do with them? socket.Impersonate(); } ServerProcessing op = nextSink.ProcessMessage( stack, null, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream ); if (impersonate) { NamedPipeSocket.RevertToSelf(); } switch (op) { case ServerProcessing.Complete: stack.Pop(this); // send the response headers and the response data to the client t.Write(responseHeaders, responseStream); break; case ServerProcessing.Async: stack.StoreAndDispatch(nextSink, null); break; case ServerProcessing.OneWay: break; } } catch (Exception) { // Console.WriteLine(ex); } }