示例#1
0
        public async Task HandleAtomMessage(NetsyChannel channel, AtomMessage atomMessage)
        {
            //channel can be null
            Guard.NotNull(atomMessage, nameof(atomMessage));

            var netsyMessage = NetsyMessage.FromAtomMessage(atomMessage.Data);
            var package      = this._packageSerializer.Deserialize(netsyMessage.Data);

            switch (netsyMessage.Kind)
            {
            case NetsyMessageKind.SendPackage:
                this._handlerRegistry.HandlePackage(channel, package);
                break;

            case NetsyMessageKind.Request:
                var(success, response) = await this._handlerRegistry.HandleIncomingRequest(channel, package);

                if (success)
                {
                    var responseData = this._packageSerializer.Serialize(response);

                    var responseMessage = NetsyMessage.ResponseFor(responseData, netsyMessage.RequestId);
                    await this.SendMessageAsync(responseMessage);
                }
                break;

            case NetsyMessageKind.Response:
                this._handlerRegistry.HandleResponse(netsyMessage.RequestId, package);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
示例#2
0
        public async Task SendPackageAsync(object package)
        {
            Guard.NotNull(package, nameof(package));

            var data = this._packageSerializer.Serialize(package);

            await this.SendMessageAsync(NetsyMessage.Package(data));
        }
示例#3
0
        public async Task <TResponse> SendRequestAsync <TResponse>(object request)
        {
            Guard.NotNull(request, nameof(request));

            var data    = this._packageSerializer.Serialize(request);
            var message = NetsyMessage.Request(data);

            var             result          = new TaskCompletionSource <TResponse>();
            Action <object> responseHandler = (response) => result.SetResult((TResponse)response);

            this._handlerRegistry.AddOutgoingRequest(message.RequestId, responseHandler);

            await this.SendMessageAsync(message);

            return(await result.Task);
        }
示例#4
0
        private async Task SendMessageAsync(NetsyMessage message)
        {
            Guard.NotNull(message, nameof(message));

            await this._sendAtomMessage(message.ToAtomMessage());
        }