Exemple #1
0
        private async void HandleHandshake(NetConnection connection)
        {
            string requestedUsername;

            try
            {
                var userNamePacket = await AwaitData(connection);

                requestedUsername = userNamePacket.ReadString();
            }
            catch (ClientDisconnectedException)
            {
                return;
            }

            var(nameValid, invalidReason) = UsernameHelpers.IsNameValid(requestedUsername);
            if (!nameValid)
            {
                connection.Disconnect($"Username is invalid ({invalidReason}).");
                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;
            }

            NetIncomingMessage okMsg;

            try
            {
                okMsg = await AwaitData(connection);
            }
            catch (ClientDisconnectedException)
            {
                return;
            }

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

            // Handshake complete!
            HandleInitialHandshakeComplete(connection);
        }
 /// <summary>
 /// Constructs a new instance.
 /// </summary>
 /// <param name="sessionId">The session ID of the incoming connection.</param>
 public NetConnectingArgs(NetSessionId sessionId, IPEndPoint ip)
 {
     SessionId = sessionId;
     IP        = ip;
 }
Exemple #3
0
 /// <summary>
 ///     Creates a new instance of a NetChannel.
 /// </summary>
 /// <param name="manager">The server this channel belongs to.</param>
 /// <param name="connection">The raw NetConnection to the remote peer.</param>
 internal NetChannel(NetManager manager, NetConnection connection, NetSessionId sessionId)
 {
     _manager    = manager;
     _connection = connection;
     SessionId   = sessionId;
 }