Example #1
0
        private void HandleApproval(NetIncomingMessage message)
        {
            var sender     = message.SenderConnection;
            var ip         = sender.RemoteEndPoint;
            var name       = message.ReadString();
            var origName   = name;
            var iterations = 1;

            if (!UsernameHelpers.IsNameValid(name))
            {
                sender.Deny("Username is invalid (contains illegal characters/too long).");
            }

            while (_assignedSessions.Values.Any(u => u.Username == name))
            {
                // This is shit but I don't care.
                name = $"{origName}_{++iterations}";
            }

            var session = new NetSessionId(name);

            if (OnConnecting(ip, session))
            {
                _assignedSessions.Add(sender, session);
                var msg = _netPeer.CreateMessage();
                msg.Write(name);
                sender.Approve(msg);
            }
            else
            {
                sender.Deny("Server is full.");
            }
        }
        public MainWindowLoginViewModel(ConfigurationManager cfg)
        {
            _cfg = cfg;

            this.WhenAnyValue(x => x.EditingUsername)
            .Subscribe(s =>
            {
                IsUsernameValid = UsernameHelpers.IsNameValid(s, out var reason);

                InvalidReason = reason ?? " ";
            });
        }
Example #3
0
        private async void HandleHandshake(NetConnection connection)
        {
            var userNamePacket = await AwaitData(connection);

            var requestedUsername = userNamePacket.ReadString();

            if (!UsernameHelpers.IsNameValid(requestedUsername))
            {
                connection.Disconnect("Username is invalid (contains illegal characters/too long).");
                return;
            }

            var endPoint   = connection.RemoteEndPoint;
            var name       = requestedUsername;
            var origName   = name;
            var iterations = 1;

            while (_assignedSessions.Values.Any(u => u.Username == name))
            {
                // This is shit but I don't care.
                name = $"{origName}_{++iterations}";
            }

            var session = new NetSessionId(name);

            if (OnConnecting(endPoint, session))
            {
                _assignedSessions.Add(connection, session);
                var msg = connection.Peer.CreateMessage();
                msg.Write(name);
                connection.Peer.SendMessage(msg, connection, NetDeliveryMethod.ReliableOrdered);
            }
            else
            {
                connection.Disconnect("Sorry, denied. Why? Couldn't tell you, I didn't implement a deny reason.");
                return;
            }

            var okMsg = await AwaitData(connection);

            if (okMsg.ReadString() != "ok")
            {
                connection.Disconnect("You should say ok.");
                return;
            }

            // Handshake complete!
            HandleInitialHandshakeComplete(connection);
        }
Example #4
0
        private void TryConnect(string address)
        {
            var inputName = _mainMenuControl.UserNameBox.Text.Trim();

            var(nameValid, invalidReason) = UsernameHelpers.IsNameValid(inputName);
            if (!nameValid)
            {
                invalidReason = _loc.GetString(invalidReason);
                userInterfaceManager.Popup(
                    _loc.GetString("Invalid username:\n{0}", invalidReason),
                    _loc.GetString("Invalid Username"));
                return;
            }

            var configName = _configurationManager.GetCVar <string>("player.name");

            if (_mainMenuControl.UserNameBox.Text != configName)
            {
                _configurationManager.SetCVar("player.name", inputName);
                _configurationManager.SaveToFile();
            }

            _setConnectingState(true);
            _netManager.ConnectFailed += _onConnectFailed;
            try
            {
                ParseAddress(address, out var ip, out var port);
                _client.ConnectToServer(ip, port);
            }
            catch (ArgumentException e)
            {
                userInterfaceManager.Popup($"Unable to connect: {e.Message}", "Connection error.");
                Logger.Warning(e.ToString());
                _netManager.ConnectFailed -= _onConnectFailed;
            }
        }