Exemplo n.º 1
0
        /// <summary>
        /// P2P 마스터를 자동으로 변경함.
        /// MasterPriority 와 Ping 을 평가하여 가장 적합한 마스터를 선택하여 변경함
        /// </summary>
        /// <param name="containCurrentMaster">현재 마스터도 포함하는지 여부</param>
        /// <param name="sendInfo">변경 정보를 멤버들에게 전송할지 여부</param>
        public void ChangeMaster(bool containCurrentMaster, bool sendInfo)
        {
            // 마스터를 변경한다
            P2pMember master = null;
            ushort    prevMasterSessionKey = _masterSessionId;
            sbyte     priority             = sbyte.MinValue;
            int       bestPing             = int.MaxValue;

            lock (_memberList)
            {
                foreach (P2pMember member in _memberList)
                {
                    if (containCurrentMaster == false &&
                        prevMasterSessionKey == member.Session.SessionId)
                    {
                        continue;
                    }

                    if (priority == sbyte.MinValue)
                    {
                        master   = member;
                        priority = member.MasterPriority;
                        bestPing = member.Ping;
                        continue;
                    }

                    if (member.MasterPriority > priority ||
                        (member.MasterPriority == priority && (ushort)member.Ping < bestPing))
                    {
                        master   = member;
                        priority = member.MasterPriority;
                        bestPing = member.Ping;
                    }
                }
            }

            if (master != null)
            {
                if (sendInfo == true)
                {
                    SetMaster(master.SessionId);
                }
                else
                {
                    _masterSessionId = master.SessionId;
                }

                //Console.WriteLine("SetMaster : Session[{0}] Ping[{1}] PickType[{2}]", master.m_session.GetSessionKey(), bestPing, pickType);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 세션을 P2P에 가입시킴
        /// </summary>
        /// <param name="session">가입시킬 세션</param>
        /// <returns>성공여부</returns>
        public bool Join(ServerSession session)
        {
            if (Contains(session.SessionId) == true)
            {
                return(false);
            }

            var p2pMember = new P2pMember();

            try
            {
                lock (_memberList)
                {
                    if (_memberList.Count == 0)
                    {
                        _masterSessionId = session.SessionId;
                    }

                    p2pMember.SetSession(session);
                    _memberList.AddLast(p2pMember);

                    p2pMember.SendJoinP2pGroup(
                        Id,
                        session.SessionId,
                        MasterSessionId,
                        session.UdpChannel.RemoteEndPoint,
                        session.UdpChannel.LocalEndPoint);

                    foreach (var member in _memberList)
                    {
                        if (member.Session == session)
                        {
                            continue;
                        }

                        // 나한테 다른 유저 정보를 보내자
                        p2pMember.SendJoinP2pGroup(
                            Id,
                            member.SessionId,
                            _masterSessionId,
                            member.Session.UdpChannel.RemoteEndPoint,
                            member.Session.UdpChannel.LocalEndPoint);

                        // 다른사람한테 내정보를 보내자
                        member.SendJoinP2pGroup(
                            Id,
                            session.SessionId,
                            _masterSessionId,
                            session.UdpChannel.RemoteEndPoint,
                            session.UdpChannel.LocalEndPoint);
                    }
                }
            }
            catch
            {
                return(false);
            }

            //Console.WriteLine("Join P2P Group : {0} {1} {2}", m_groupSessionKey, session.GetSessionKey(), m_masterSessionKey);

            return(true);
        }