Exemplo n.º 1
0
        public Task <bool> OnHandle(MessageContext context, UnreliableRelay1Message message)
        {
            var session = context.Session;

            foreach (var destination in message.Destination.Where(id => id != session.HostId))
            {
                if (destination == Constants.HostIdServerHack)
                {
                    var decodedMessage = CoreMessageDecoder.Decode(_serializer, Unpooled.WrappedBuffer(message.Data));
                    context.ChannelHandlerContext.Handler.ChannelRead(context.ChannelHandlerContext, new MessageContext
                    {
                        ChannelHandlerContext = context.ChannelHandlerContext,
                        Session     = context.Session,
                        Message     = decodedMessage,
                        UdpEndPoint = context.UdpEndPoint
                    });
                    continue;
                }

                var target = session.P2PGroup?.GetMemberInternal(destination);
                target?.Send(new UnreliableRelay2Message(session.HostId, message.Data), true);
            }

            return(Task.FromResult(true));
        }
Exemplo n.º 2
0
        public Task <bool> OnHandle(MessageContext context, ReliableUdp_FrameMessage message)
        {
            var decodedMessage = CoreMessageDecoder.Decode(_serializer, Unpooled.WrappedBuffer(message.Data));

            context.ChannelHandlerContext.Handler.ChannelRead(context.ChannelHandlerContext, new MessageContext
            {
                ChannelHandlerContext = context.ChannelHandlerContext,
                Session     = context.Session,
                Message     = decodedMessage,
                UdpEndPoint = context.UdpEndPoint
            });
            return(Task.FromResult(true));
        }
Exemplo n.º 3
0
        public override void ChannelRead(IChannelHandlerContext context, object obj)
        {
            var message = obj as UdpMessage;

            Debug.Assert(message != null);

            try
            {
                var session = _server.SessionsByUdpId.GetValueOrDefault(message.SessionId);
                if (session == null)
                {
                    if (message.Content.GetByte(0) != (byte)ProudCoreOpCode.ServerHolepunch)
                    {
                        throw new ProudException(
                                  $"Expected {ProudCoreOpCode.ServerHolepunch} as first udp message but got {(ProudCoreOpCode)message.Content.GetByte(0)}");
                    }

                    var holepunch = (ServerHolepunchMessage)CoreMessageDecoder.Decode(message.Content);

                    session = _server.Sessions.Values.FirstOrDefault(x =>
                                                                     x.HolepunchMagicNumber.Equals(holepunch.MagicNumber));
                    if (session == null)
                    {
                        return;
                    }

                    if (session.UdpSocket != _socket)
                    {
                        return;
                    }

                    session.UdpSessionId = message.SessionId;
                    session.UdpEndPoint  = message.EndPoint;
                    _server.SessionsByUdpId[session.UdpSessionId] = session;

                    session.SendUdpAsync(new ServerHolepunchAckMessage(session.HolepunchMagicNumber, session.UdpEndPoint));
                    return;
                }

                if (session.UdpSocket != _socket)
                {
                    return;
                }

                session.Channel.Pipeline.Context <ProudFrameDecoder>().FireChannelRead(message.Content.Retain());
            }
            finally
            {
                message.Content.Release();
            }
        }
Exemplo n.º 4
0
        public override void ChannelRead(IChannelHandlerContext context, object obj)
        {
            var message = obj as UdpMessage;

            Debug.Assert(message != null);

            var log = _server.Configuration.Logger?
                      .ForContext("EndPoint", message.EndPoint.ToString());

            try
            {
                var session = _server.SessionsByUdpId.GetValueOrDefault(message.SessionId);
                if (session == null)
                {
                    if (message.Content.GetByte(0) != (byte)ProudCoreOpCode.ServerHolepunch)
                    {
                        log?.Warning("Expected ServerHolepunch as first udp message but got {MessageType}", (ProudCoreOpCode)message.Content.GetByte(0));
                        return;
                    }

                    var holepunch = (ServerHolepunchMessage)CoreMessageDecoder.Decode(message.Content);

                    // TODO add a lookup by holepunch magic
                    session = _server.Sessions.Values.FirstOrDefault(x =>
                                                                     x.HolepunchMagicNumber.Equals(holepunch.MagicNumber));

                    if (session == null)
                    {
                        log?.Warning("Invalid holepunch magic number");
                        return;
                    }

                    if (session.UdpSocket != _socket)
                    {
                        log?.Warning("Client is sending to the wrong udp socket");
                        return;
                    }

                    session.UdpSessionId = message.SessionId;
                    session.UdpEndPoint  = message.EndPoint;
                    _server.SessionsByUdpId[session.UdpSessionId] = session;

                    session.SendUdpAsync(new ServerHolepunchAckMessage(session.HolepunchMagicNumber, session.UdpEndPoint));
                    return;
                }

                if (session.UdpSocket != _socket)
                {
                    log?.Warning("Client is sending to the wrong udp socket");
                    return;
                }

                var recvContext = new RecvContext
                {
                    Message     = message.Content.Retain(),
                    UdpEndPoint = message.EndPoint
                };
                session.Channel.Pipeline.Context <RecvContextDecoder>().FireChannelRead(recvContext);
            }
            finally
            {
                message.Content.Release();
            }
        }
Exemplo n.º 5
0
        public override void ChannelRead(IChannelHandlerContext context, object obj)
        {
            var message = obj as UdpMessage;

            Debug.Assert(message != null);

            try
            {
                var session = _udpSessionManager.GetSession(message.SessionId);
                if (session == null)
                {
                    if (message.Content.GetByte(0) != (byte)ProudCoreOpCode.ServerHolepunch)
                    {
                        _logger.Warning("<{EndPoint}> Expected ServerHolepunch as first udp message but got {MessageType}",
                                        message.EndPoint.ToString(), (ProudCoreOpCode)message.Content.GetByte(0));
                        return;
                    }

                    var holepunch = (ServerHolepunchMessage)CoreMessageDecoder.Decode(_serializer, message.Content);
                    session = _magicNumberSessionManager.GetSession(holepunch.MagicNumber);
                    if (session == null)
                    {
                        _logger.Warning("<{EndPoint}> Invalid holepunch magic number={MagicNumber}",
                                        message.EndPoint.ToString(), holepunch.MagicNumber);
                        return;
                    }

                    if (session.UdpSocket != Socket)
                    {
                        _logger.Warning("<{EndPoint}> Client is sending to the wrong udp socket",
                                        message.EndPoint.ToString());
                        return;
                    }

                    session.UdpSessionId = message.SessionId;
                    session.UdpEndPoint  = message.EndPoint;
                    _udpSessionManager.AddSession(session.UdpSessionId, session);
                    session.UdpSocket.Send(
                        new ServerHolepunchAckMessage(session.HolepunchMagicNumber, session.UdpEndPoint),
                        session.UdpEndPoint
                        );
                    return;
                }

                if (session.UdpSocket != Socket)
                {
                    _logger.Warning("<{EndPoint}> Client is sending to the wrong udp socket",
                                    message.EndPoint.ToString());
                    return;
                }

                session.UdpEndPoint = message.EndPoint;
                var recvContext = new MessageContext
                {
                    Session     = session,
                    Message     = message.Content.Retain(),
                    UdpEndPoint = message.EndPoint
                };

                session?.Channel?.Pipeline?.Context <MessageContextDecoder>()?.FireChannelRead(recvContext);
            }
            finally
            {
                message.Content.Release();
            }
        }