Esempio n. 1
0
 /// <summary>
 /// Remove user from match and re-assign the match host if needed. Uses a monitor lock to ensure multiple users are not writing to the list at the same time.
 /// </summary>
 /// <param name="user">The user to remove from the match.</param>
 public void RemoveUser(User user)
 {
     lock (_usersLock)
     {
         Users.Remove(user);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Adds a user to the match. Uses a monitor lock to ensure multiple users are not writing to the list at the same time.
        /// </summary>
        /// <param name="user">The user to be added.</param>
        /// <returns>Returns true if added, and false if not added.</returns>
        public bool AddUser(User user)
        {
            lock (_usersLock)
            {
                if (Users.Count >= 4)
                    return false;

                Users.Add(user);
                return true;
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Creates a new user in the _users dictionary.
 /// </summary>
 private void CreateUser()
 {
     // Create
     User user = new User(this.Context)
     {
         ConnectionId = Context.ConnectionId,
         Name = Context.User.Identity.Name
     };
     // Add to collection
     _users.TryAdd(Context.ConnectionId, user);
 }
Esempio n. 4
0
        /// <summary>
        /// Adds a user to his/her Match.
        /// If it doesn't exist yet then it will be created.
        /// </summary>
        /// <param name="user">The user (that was just created), to be added to the match.</param>
        private void AddToMatch(User user)
        {
            Match match;

            // Try and find match
            if (_matches.TryGetValue(user.MatchId, out match))
            {
                match.AddUser(user);
                if (DEBUG)
                    Console.WriteLine("DEBUG: Added " + user.Username + " to match");
            }
            // Match not found, create it
            else
            {
                match = new Match()
                {
                    ID = user.MatchId
                };
                match.AddUser(user);
                _matches.TryAdd(user.MatchId, match);
                if (DEBUG)
                    Console.WriteLine("DEBUG: Created new match and added " + user.Username + " to it.");
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Sends data to all users in a match. The data is from one client and it needs
        /// to find the match that that one client belongs to, but will broadcast to all client in the match. 
        /// </summary>
        /// <param name="user">The user that sent the data.</param>
        /// <param name="instruction">The data that is sent from one client.</param>
        private void SendToMatch(User user, byte[] data)
        {
            // Find match
            Match match;
            if (!_matches.TryGetValue(user.MatchId, out match))
                return;

            // Send to every user in the match
            foreach (User u in match.Users)
                newsock.Send(data, data.Length, u.EndPoint);
        }
Esempio n. 6
0
        /// <summary>
        /// When the JOIN command is received by a user.
        /// </summary>
        /// <param name="remoteEndPoint">The information such as IP, port and etc. of the client that just sent the 'Join' Instruction.</param>
        /// <param name="ipAddress">The ip address of the client that requested to join.</param>
        /// <param name="instruction">The Instruction that the client sent.</param>
        public void OnJoin(IPEndPoint remoteEndPoint, string ipAddress, Instruction instruction)
        {
            User user = new User()
            {
                EndPoint = remoteEndPoint
            };

            // Set username
            user.Username = instruction.Arg1;

            // Set match id
            user.MatchId = instruction.Arg2;

            // Add user
            _users.TryAdd(ipAddress, user);

            // Find the user's match and add him/her to it
            AddToMatch(user);

            // Send JOINED command to client as connection confirmation
            Instruction i = new Instruction() { Command = Instruction.Type.JOINED };
            byte[] data = Serializer.Serialize(i);
            newsock.Send(data, data.Length, user.EndPoint);
        }
 private void FindMatch(User user, string matchId)
 {
     // Assign user to match based on matchId
 }
 /// <summary>
 /// Sends to all users in a match except the current user
 /// </summary>
 /// <param name="user"></param>
 /// <param name="instruction"></param>
 private void SendToMatchExcept(User user, Instruction instruction)
 {
     // TODO
     // Find match
     // Send instruction to everyone in match except current user
 }
        /// <summary>
        /// Sends to all users in a match
        /// </summary>
        /// <param name="user"></param>
        /// <param name="instruction"></param>
        private void SendToMatch(User user, Instruction instruction)
        {
            byte[] returningByte = Encoding.ASCII.GetBytes("HELLO");
            socket.SendTo(returningByte, user.EndPoint);

            // TODO
            // Find match
            // Send instruction to everyone in match

            foreach (KeyValuePair<string, User> u in _users) // This needs to be users in a match instead
            {
                //byte[] returningByte = Encoding.ASCII.GetBytes(instruction.ToString().ToCharArray()); // Is ToCharArray necessary here?
                //socket.SendTo(returningByte, u.Value.EndPoint);
            }
        }
 private void OnJoin(EndPoint remoteEndPoint, string ipAddress, Instruction instruction)
 {
     User user;
     user = new User(remoteEndPoint);
     _users.TryAdd(ipAddress, user);
     FindMatch(user, instruction.Arg1);
 }