async Task RunServerAsync(string coordinationName, CancellationToken cancelToken, bool restartOnDisconnect) { bool terminate = false; int loopCount = 0; do { using (var controlPipe = new NamedPipeServerStream(coordinationName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous)) { Func <AsyncCallback, object, IAsyncResult> beginConnect = controlPipe.BeginWaitForConnection; Action <IAsyncResult> endConnect = controlPipe.EndWaitForConnection; Task connection = Task.Factory.FromAsync(beginConnect, endConnect, null); if (loopCount++ == 0) { TryReleaseNamedSemaphore(coordinationName); } await connection; bool ok = controlPipe.IsConnected && LengthPrefixMessenger.ServerHandshake(controlPipe); if (!ok) { Trace.TraceInformation("Got connection, but it was lost or handshake failed."); continue; } Trace.TraceInformation("Setting up hardware for " + coordinationName + "..."); using (IFlow flow = flowBuilder(RingBufferCapacity, customSerializer)) { latestFlow = flow; using (var rpc = new RpcServer <IFlow>(controlPipe, flow, customSerializer)) { Trace.TraceInformation("Started new " + typeof(IFlow).FriendlyNameForGenerics()); bool disconnect = false; while (!cancelToken.WaitHandle.WaitOne(0) && !terminate && !disconnect && !objectCancel.WaitOne(0)) { rpc.ProcessMessage(out disconnect, out terminate); } } } latestFlow = default(IFlow); } }while (restartOnDisconnect && !cancelToken.WaitHandle.WaitOne(0) && !terminate && !objectCancel.WaitOne(0)); Trace.TraceInformation("RunServerAsync completed ok"); }
protected RpcBase(Stream stream, TypeModel customSerializer) { if (stream == null) { throw new ArgumentNullException(); } if (!stream.CanWrite) { throw new ArgumentException("stream isn't writable!"); } this.stream = stream; this.customSerializer = customSerializer; this.messenger = new LengthPrefixMessenger(stream, customSerializer); this.byteConverter = new ProtoBufByteConverter(customSerializer); NameForTrace = this.GetType().FriendlyNameForGenerics(); Trace.TraceInformation("Instantiated " + NameForTrace); }
internal void SendInitialHandshake() { LengthPrefixMessenger.ClientHandshake(stream); }