コード例 #1
0
        bool HandlePublishEvent <TEvent>(
            NetworkStream stream,
            TEvent @event,
            PapercutIPCommCommandType protocolCommandType) where TEvent : IEvent
        {
            string response = stream.ReadStringBuffered().Trim();

            if (response != AppConstants.ApplicationName.ToUpper())
            {
                return(false);
            }

            var eventJson = PapercutIPCommSerializer.ToJson(@event);

            stream.WriteLine(PapercutIPCommSerializer.ToJson(
                                 new PapercutIPCommRequest()
            {
                CommandType = protocolCommandType,
                Type        = @event.GetType(),
                ByteSize    = Encoding.UTF8.GetBytes(eventJson).Length
            }));

            response = stream.ReadStringBuffered().Trim();
            if (response == "ACK")
            {
                stream.WriteStr(eventJson);
            }

            return(true);
        }
コード例 #2
0
        protected override async Task ProcessRequest(
            string incomingRequest,
            CancellationToken token = default)
        {
            try
            {
                var request = incomingRequest.FromJson <PapercutIPCommRequest>();

                this.Logger.Verbose("Incoming Request Received {@Request}", request);

                await this.Connection.SendAsync("ACK");

                if (request.CommandType.IsAny(
                        PapercutIPCommCommandType.Publish,
                        PapercutIPCommCommandType.Exchange))
                {
                    var remoteObjectBuffer = await this.Connection.ReceiveDataAsync();

                    var remoteEvent = PapercutIPCommSerializer.FromJson(
                        request.Type,
                        Encoding.UTF8.GetString(remoteObjectBuffer));

                    this.Logger.Information(
                        "Publishing Event Received {@Event} from Remote",
                        remoteEvent);

                    await this._messageBus.PublishObjectAsync(
                        remoteEvent,
                        request.Type,
                        token : token);

                    if (request.CommandType == PapercutIPCommCommandType.Exchange)
                    {
                        // send response back...
                        this.Logger.Information(
                            "Exchanging Event {@Event} -- Pushing to Remote",
                            remoteEvent);

                        await this.Connection.SendJsonAsync(request.Type, remoteEvent);
                    }
                }
            }
            catch (IOException e)
            {
                this.Logger.Error(e, "IOException received. Closing this connection.");
                this.Connection.Close();
            }
        }