Ejemplo n.º 1
0
 protected virtual async Task HandlePacket(Packet body)
 {
     if (body.Type == PacketType.ForwardBack)
     {
         _semaphoreProvider.SetResult(body.RequestId, body.ReturnValue);
     }
     else if (body.Type == PacketType.Exception)
     {
         _semaphoreProvider.SetException(body.RequestId, new PomeliumException(body.ReturnValue.ToString()));
     }
 }
Ejemplo n.º 2
0
        protected virtual async Task HandlePacket(Packet body, LocalClient sender)
        {
            if (!await _session.ExistsAsync(body.SessionId))
            {
                if (body.Type == PacketType.Forward && CheckNode(body.SessionId))
                {
                    sender.IsNode = true;
                }
                else
                {
                    if (await _garbageCollector.IsInBufferAsync(body.SessionId))
                    {
                        await _garbageCollector.UnmarkGarbageAsync(body.SessionId);

                        OnReconnected(sender);
                    }
                    else
                    {
                        OnConnected(sender);
                    }
                    if (_clientCollection.LocalClientExist(body.SessionId))
                    {
                        _clientCollection.StoreLocalClientAsync(sender);
                    }
                    _session.InitAsync(body.SessionId);
                }
            }

            if (body.Type == PacketType.Forward)
            {
                if (sender.IsNode)
                {
                    var    packet      = JsonConvert.DeserializeObject <Packet>(body.Arguments.First().ToString());
                    object returnValue = null;
                    var    client      = await _clientCollection.GetClientAsync(packet.SessionId);

                    try { returnValue = await client.InvokeAsync(packet.Method, packet.Arguments); } catch (Exception ex) { await ResponseAsync(sender, new Packet { Code = 403, Type = PacketType.Exception, ReturnValue = ex.ToString() }); }
                    await ResponseAsync(sender, new Packet
                    {
                        Type        = PacketType.ForwardBack,
                        RequestId   = packet.RequestId,
                        ReturnValue = returnValue
                    });
                }
                else
                {
                    await ResponseAsync(sender, new Packet { Code = 403, Type = PacketType.Exception, ReturnValue = "Forbidden" });
                }
            }
            else if (body.Type == PacketType.Disconnect)
            {
                await OnDisconnected(sender);
            }
            else if (body.Type == PacketType.Exception)
            {
                _semaphoreProvider.SetException(body.RequestId, new PomeliumException(body.ReturnValue.ToString()));
            }
            else if (body.Type == PacketType.Response)
            {
                _semaphoreProvider.SetResult(body.RequestId, body.ReturnValue);
            }
            else
            {
                try
                {
                    var     hub    = _hubActivitor.CreateInstance(body.Hub, sender, body);
                    var     method = _hubActivitor.GetMethod(hub, body.Method);
                    dynamic ret    = await _hubActivitor.InvokeAsync(hub, method, body.Arguments);
                    await ResponseAsync(sender, new Packet
                    {
                        RequestId   = body.RequestId,
                        Type        = PacketType.Response,
                        ReturnValue = ret
                    });
                }
                catch (Exception ex)
                {
                    await ResponseAsync(sender, new Packet
                    {
                        Type        = PacketType.Exception,
                        ReturnValue = ex.ToString(),
                        RequestId   = body.RequestId
                    });
                }
            }
        }