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(); } }
private void AtomServerOnChannelConnected(object sender, AtomChannelConnectedEventArgs e) { var id = Guid.NewGuid(); e.Channel.Data[NetsyServerChannelId] = id; var netsyChannel = new NetsyChannel(e.Channel, this._packageSerializer, this._handlerRegistry); this._channels.TryAdd(id, netsyChannel); this.ChannelConnected?.Invoke(this, new NetsyChannelConnectedEventArgs(netsyChannel)); }
public void HandlePackage(NetsyChannel channel, object package) { Guard.NotNull(package, nameof(package)); if (this._packageHandlers.TryGetValue(package.GetType(), out var handlers)) { foreach (var handler in handlers) { handler.Invoke(channel, package); } } }
public async Task <(bool, object)> HandleIncomingRequest(NetsyChannel channel, object package) { Guard.NotNull(package, nameof(package)); if (this._incomingRequestHandlers.TryGetValue(package.GetType(), out var handler)) { var result = await handler(channel, package); return(true, result); } return(false, null); }
public NetsyChannelConnectedEventArgs(NetsyChannel channel) { Guard.NotNull(channel, nameof(channel)); this.Channel = channel; }