예제 #1
0
 public PipeReader(IPipeStream pipeStream, TransportMessageHandler messageHandler, Action onDisconnected)
 {
     _pipeStream     = pipeStream;
     _messageHandler = messageHandler;
     _onDisconnected = onDisconnected;
     _transport      = new NamedPipeTransport(_pipeStream);
 }
예제 #2
0
        public async Task Read(TransportMessageHandler messageHandler)
        {
            var packet = await ReadPacketFromPipe().ConfigureAwait(false);

            while (packet.Position < packet.Length)
            {
                var message = new TransportMessage();
                message.MergeDelimitedFrom(packet);
                switch (message.DataCase)
                {
                case TransportMessage.DataOneofCase.RequestInit:
                    messageHandler.HandleRequestInit(message.RequestInit.MethodFullName,
                                                     message.RequestInit.Deadline?.ToDateTime());
                    break;

                case TransportMessage.DataOneofCase.Headers:
                    var headerMetadata = ConstructMetadata(message.Headers.Metadata);
                    messageHandler.HandleHeaders(headerMetadata);
                    break;

                case TransportMessage.DataOneofCase.PayloadInfo:
                    var payload = new byte[message.PayloadInfo.Size];
                    if (message.PayloadInfo.InSamePacket)
                    {
                        packet.Read(payload, 0, payload.Length);
                    }
                    else
                    {
                        _pipeStream.Read(payload, 0, payload.Length);
                    }

                    messageHandler.HandlePayload(payload);
                    break;

                case TransportMessage.DataOneofCase.RequestControl:
                    switch (message.RequestControl)
                    {
                    case RequestControl.Cancel:
                        messageHandler.HandleCancel();
                        break;

                    case RequestControl.StreamEnd:
                        messageHandler.HandleStreamEnd();
                        break;
                    }

                    break;

                case TransportMessage.DataOneofCase.Trailers:
                    var trailerMetadata = ConstructMetadata(message.Trailers.Metadata);
                    var status          = new Status((StatusCode)message.Trailers.StatusCode,
                                                     message.Trailers.StatusDetail);
                    messageHandler.HandleTrailers(trailerMetadata, status);
                    break;
                }
            }
        }