public WebSocketConnectionState()
 {
     floodControl       = new Dictionary <DateTime, long>();
     lastPacketReceived = null;
     header             = new Header();
     lastPacketSent     = null;
     lastActivity       = null;
     webSocket          = null;
     ipAddress          = null;
 }
Exemple #2
0
 public ChatController(WebSocketHub socketHub, WSUserInfo wsUserInfo, ILogger logger)
 {
     _socketHub  = socketHub;
     _wsUserInfo = wsUserInfo;
     _logger     = logger;
 }
 public NotificationService(WebSocketHub hub)
 {
     _hub = hub;
 }
        public static WebSocketConnectionState Accept(WebSocketHub hub)
        {
            var ipAddress = hub.Context.UserEndPoint.Address.MapToIPv4().ToString();

            using (var dbContext = Database.For <ThePalaceEntities>())
            {
                var now  = DateTime.UtcNow;
                var bans = dbContext.Bans.AsNoTracking()
                           .AsEnumerable()
                           .Where(b =>
                                  b.Ipaddress == ipAddress &&
                                  (!b.UntilDate.HasValue || b.UntilDate.Value < now))
                           .Count();

                if (bans > 0)
                {
                    ThePalace.Core.Utility.Logger.Log(MessageTypes.Info, $"Banned connection from: {ipAddress}");

                    hub.Context.WebSocket.Send(new Header
                    {
                        eventType = EventTypes.MSG_SERVERDOWN.ToString(),
                        refNum    = (int)ServerDownFlags.SD_Banished,
                        message   = new MSG_SERVERDOWN
                        {
                            whyMessage = "You have been banned!",
                        }.SerializeJSON(),
                    }.SerializeJSON());

                    hub.Context.WebSocket.Close();

                    return(null);
                }
            }

            var sessionState = SessionManager.GetNewSession(SessionTypes.WebSocket);

            if (sessionState == null)
            {
                ThePalace.Core.Utility.Logger.Log(MessageTypes.Info, $"Server is full, turned away: {ipAddress}");

                hub.Context.WebSocket.Send(new Header
                {
                    eventType = EventTypes.MSG_SERVERDOWN.ToString(),
                    refNum    = (int)ServerDownFlags.SD_ServerFull,
                    message   = new MSG_SERVERDOWN
                    {
                        whyMessage = "The Server is full!",
                    }.SerializeJSON(),
                }.SerializeJSON());

                hub.Context.WebSocket.Close();

                return(null);
            }

            lock (connectionStates)
            {
                connectionStates.TryAdd(
                    sessionState.UserID,
                    new WebSocketConnectionState
                {
                    sessionState = sessionState,
                    ipAddress    = ipAddress,
                    webSocket    = hub,
                });

                ((WebSocketDriver)sessionState.driver).connectionState = connectionStates[sessionState.UserID];
            }

            ThePalace.Core.Utility.Logger.Log(MessageTypes.Info, $"Connection from: {ipAddress}[{sessionState.UserID}]");

            new Business.MSG_TIYID().Send(null, new Message
            {
                sessionState = sessionState,
            });

            return(connectionStates[sessionState.UserID]);
        }