public Task SendResponseAsync(PipeMessenger messenger, ServiceMessage message) { message.MessageId = Interlocked.Increment(ref _counter); var jsonStr = JsonConvert.SerializeObject(message); var bytes = Encoding.UTF8.GetBytes(jsonStr); return(messenger.SendMessageAsync(bytes, _cts.Token)); }
private void Initialize() { var pipe = new NamedPipeClientStream(".", _name, PipeDirection.InOut, PipeOptions.Asynchronous); pipe.Connect(ConnectTimeout); _cts = new CancellationTokenSource(); _messenger = new PipeMessenger(pipe, new JsonMessageProcessor(_typeResolver, Callback)); _messenger.StartReading(_cts.Token); }
public async Task StartServerAsync() { var ps = new PipeSecurity(); var sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); var par = new PipeAccessRule(sid, PipeAccessRights.ReadWrite, AccessControlType.Allow); ps.AddAccessRule(par); sid = new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null); par = new PipeAccessRule(sid, PipeAccessRights.FullControl, AccessControlType.Allow); ps.AddAccessRule(par); while (!_cts.IsCancellationRequested) { var pipe = new NamedPipeServerStream(_name, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 0, 0, ps); await pipe.WaitForConnectionAsync(); PipeMessenger messenger = null; // ReSharper disable once AccessToModifiedClosure messenger = new PipeMessenger(pipe, new JsonMessageProcessor(_typeResolver, msg => Callback(messenger, msg))); messenger.StartReading(_cts.Token); _messengers.Add(messenger); } }
private async void Callback(PipeMessenger messenger, ServiceMessage serviceMessage) { var response = _processor(serviceMessage); await SendResponseAsync(messenger, new ServiceMessage(await response)); }