Example #1
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();
            }
        }
Example #2
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();
            }
        }
Example #3
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();
            }
        }