示例#1
0
 internal void RemoveCareMePeer(RoomPeer peer)
 {
     lock (m_LockObj)
     {
         m_CareList.Remove(peer);
     }
 }
示例#2
0
 internal void AddSameRoomPeer(RoomPeer peer)
 {
     lock (m_LockObj)
     {
         m_SameRoomPeerList.Add(peer);
     }
 }
示例#3
0
 internal void AddCareMePeer(RoomPeer peer)
 {
     lock (m_LockObj)
     {
         m_CareList.Add(peer);
     }
 }
示例#4
0
 internal void RemoveSameRoomPeer(RoomPeer peer)
 {
     lock (m_LockObj)
     {
         m_SameRoomPeerList.Remove(peer);
     }
 }
示例#5
0
        internal RoomPeer GetPeerByConnection(NetConnection conn)
        {
            RoomPeer peer = null;

            m_DicPeer.TryGetValue(conn, out peer);
            return(peer);
        }
        internal void Dispatch(int id, object msg, NetConnection conn)
        {
            try {
                // 特殊处理认证消息
                if (id == (int)RoomMessageDefine.Msg_CR_ShakeHands)
                {
                    Msg_CR_ShakeHands shakehandsMsg = msg as Msg_CR_ShakeHands;
                    if (shakehandsMsg == null)
                    {
                        return;
                    }
                    bool ret = RoomPeerMgr.Instance.OnPeerShakeHands(shakehandsMsg.auth_key, conn);
                    Msg_RC_ShakeHands_Ret builder = new Msg_RC_ShakeHands_Ret();
                    if (ret)
                    {
                        builder.auth_result = Msg_RC_ShakeHands_Ret.RetType.SUCCESS;
                        IOManager.Instance.SendMessage(conn, RoomMessageDefine.Msg_RC_ShakeHands_Ret, builder);
                    }
                    else
                    {
                        builder.auth_result = Msg_RC_ShakeHands_Ret.RetType.ERROR;
                        IOManager.Instance.SendUnconnectedMessage(conn, RoomMessageDefine.Msg_RC_ShakeHands_Ret, builder);
                        conn.Disconnect("disconnect");
                    }
                    return;
                }

                RoomPeer peer = RoomPeerMgr.Instance.GetPeerByConnection(conn);
                // 没有认证连接的消息不进行处理
                if (peer == null)
                {
                    Msg_RC_ShakeHands_Ret builder = new Msg_RC_ShakeHands_Ret();
                    builder.auth_result = Msg_RC_ShakeHands_Ret.RetType.ERROR;
                    IOManager.Instance.SendUnconnectedMessage(conn, RoomMessageDefine.Msg_RC_ShakeHands_Ret, builder);

                    conn.Disconnect("unauthed");
                    LogSys.Log(LOG_TYPE.DEBUG, "unauthed peer {0} got message {1}, can't deal it!", conn.RemoteEndPoint.ToString(), msg.ToString());
                    return;
                }

                // 直接转发消息(或进行其它处理)
                MsgHandler msghandler;
                if (m_DicHandler.TryGetValue(id, out msghandler))
                {
                    msghandler(msg, peer);
                }
                if (msg is Msg_Ping)
                {
                    return;
                }

                // 消息分发到peer
                RoomPeerMgr.Instance.DispatchPeerMsg(peer, id, msg);
            } catch (Exception ex) {
                LogSys.Log(LOG_TYPE.ERROR, "Exception {0}\n{1}", ex.Message, ex.StackTrace);
            }
        }
示例#7
0
 private void AddToNeedAdd(NetConnection conn, RoomPeer peer)
 {
     lock (m_LockObj) {
         if (!m_NeedAddConnection2Peers.ContainsKey(conn))
         {
             m_NeedAddConnection2Peers.Add(conn, peer);
         }
     }
 }
示例#8
0
 internal bool DispatchPeerMsg(RoomPeer peer, int id, object msg)
 {
     if (peer == null)
     {
         return(false);
     }
     peer.InsertLogicMsg(id, msg);
     return(true);
 }
示例#9
0
 private void AddToNeedAdd(uint key, RoomPeer peer)
 {
     lock (m_LockObj) {
         if (!m_NeedAddKey2Peers.ContainsKey(key))
         {
             m_NeedAddKey2Peers.Add(key, peer);
         }
     }
 }
示例#10
0
 internal bool OnSetKey(uint key, RoomPeer peer)
 {
     if (m_KeyPeer.ContainsKey(key))
     {
         return(false);
     }
     if (!m_KeyPeer.TryAdd(key, peer))
     {
         AddToNeedAdd(key, peer);
     }
     return(true);
 }
示例#11
0
 internal void SendPeerMessage(RoomPeer peer, RoomMessageDefine id, object msg)
 {
     try {
         NetOutgoingMessage om = m_NetServer.CreateMessage();
         om.Write(Serialize.Encode(msg, (int)id));
         if (null != peer.GetConnection())
         {
             NetSendResult res = m_NetServer.SendMessage(om, peer.GetConnection(), NetDeliveryMethod.ReliableOrdered, 0);
             if (res == NetSendResult.Dropped)
             {
                 LogSys.Log(LOG_TYPE.ERROR, "SendPeerMessage {0} failed:dropped, User:{1}({2})", msg.ToString(), peer.Guid, peer.GetKey());
             }
         }
     } catch (Exception ex) {
         LogSys.Log(LOG_TYPE.ERROR, "Exception {0}\n{1}", ex.Message, ex.StackTrace);
     }
 }
示例#12
0
        internal bool OnUpdateKey(uint newKey, RoomPeer peer)
        {
            bool     ret    = false;
            uint     oldKey = peer.GetKey();
            RoomPeer dummy;

            if (m_KeyPeer.TryGetValue(oldKey, out dummy))
            {
                if (peer == dummy && m_KeyPeer.TryRemove(oldKey, out dummy))
                {
                    if (m_KeyPeer.TryAdd(newKey, peer))
                    {
                        ret = true;
                    }
                }
            }
            return(ret);
        }
示例#13
0
        internal void OnPeerDestroy(RoomPeer peer)
        {
            if (peer == null)
            {
                return;
            }
            RoomPeer dummy = null;

            if (m_KeyPeer.TryRemove(peer.GetKey(), out dummy))
            {
                if (peer.GetConnection() != null)
                {
                    if (!m_DicPeer.TryRemove(peer.GetConnection(), out dummy))
                    {
                        AddToNeedDelete(peer.GetConnection());
                    }
                }
            }
            else
            {
                AddToNeedDelete(peer.GetKey());
            }
        }
示例#14
0
        internal void Dispatch(object msg, NetConnection conn)
        {
            try
            {
                // 特殊处理机器人系统消息

                /*if (msg.GetType() == typeof(Lobby_RoomServer.Msg_LR_CreateBattleRoom)) {
                 * LobbyMsgHandler lobby_msghandler;
                 * if (m_SpecialHandlers.TryGetValue(msg.GetType(), out lobby_msghandler)) {
                 *  lobby_msghandler.Invoke(msg, conn);
                 * }
                 * return;
                 * }*/

                // 特殊处理认证消息
                if (msg.GetType() == typeof(Msg_CR_ShakeHands))
                {
                    Msg_CR_ShakeHands shakehandsMsg = msg as Msg_CR_ShakeHands;
                    if (shakehandsMsg == null)
                    {
                        return;
                    }
                    bool ret = RoomPeerMgr.Instance.OnPeerShakeHands(shakehandsMsg.auth_key,
                                                                     conn);
                    Msg_RC_ShakeHands_Ret builder = new Msg_RC_ShakeHands_Ret();
                    if (ret)
                    {
                        builder.auth_result = Msg_RC_ShakeHands_Ret.RetType.SUCCESS;
                        IOManager.Instance.SendMessage(conn, builder);
                    }
                    else
                    {
                        builder.auth_result = Msg_RC_ShakeHands_Ret.RetType.ERROR;
                        IOManager.Instance.SendUnconnectedMessage(conn, builder);
                    }
                    return;
                }

                RoomPeer peer = RoomPeerMgr.Instance.GetPeerByConnection(conn);
                // 没有认证连接的消息不进行处理
                if (peer == null)
                {
                    Msg_RC_ShakeHands_Ret builder = new Msg_RC_ShakeHands_Ret();
                    builder.auth_result = Msg_RC_ShakeHands_Ret.RetType.ERROR;
                    IOManager.Instance.SendUnconnectedMessage(conn, builder);

                    conn.Disconnect("unauthed");
                    LogSys.Log(LOG_TYPE.DEBUG, "unauthed peer {0} got message {1}, can't deal it!", conn.RemoteEndPoint.ToString(), msg.ToString());
                    return;
                }

                // 直接转发消息(或进行其它处理)
                MsgHandler msghandler;
                if (m_DicHandler.TryGetValue(msg.GetType(), out msghandler))
                {
                    //object[] param = new object[] { msg, peer };
                    msghandler.Invoke(msg, peer);
                }
                if (msg is Msg_Ping)
                {
                    return;
                }

                // 消息分发到peer
                RoomPeerMgr.Instance.DispatchPeerMsg(peer, msg);
            }
            catch (Exception ex)
            {
                LogSys.Log(LOG_TYPE.ERROR, "Exception {0}\n{1}", ex.Message, ex.StackTrace);
            }
        }
示例#15
0
        private void IOHandler()
        {
            while (m_Status == RoomSrvStatus.STATUS_RUNNING)
            {
                try {
                    m_NetServer.MessageReceivedEvent.WaitOne(1000);
                    long startTime = TimeUtility.GetElapsedTimeUs();
                    NetIncomingMessage im;
                    for (int ct = 0; ct < 1024; ++ct)
                    {
                        try {
                            if ((im = m_NetServer.ReadMessage()) != null)
                            {
                                switch (im.MessageType)
                                {
                                case NetIncomingMessageType.DebugMessage:
                                case NetIncomingMessageType.VerboseDebugMessage:
                                    LogSys.Log(LOG_TYPE.DEBUG, "Debug Message: {0}", im.ReadString());
                                    break;

                                case NetIncomingMessageType.ErrorMessage:
                                    LogSys.Log(LOG_TYPE.DEBUG, "Error Message: {0}", im.ReadString());
                                    break;

                                case NetIncomingMessageType.WarningMessage:
                                    LogSys.Log(LOG_TYPE.DEBUG, "Warning Message: {0}", im.ReadString());
                                    break;

                                case NetIncomingMessageType.StatusChanged:
                                    NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();
                                    string reason = im.ReadString();
                                    if (null != im.SenderConnection)
                                    {
                                        RoomPeer peer = RoomPeerMgr.Instance.GetPeerByConnection(im.SenderConnection);
                                        if (null != peer)
                                        {
                                            LogSys.Log(LOG_TYPE.DEBUG, "Network Status Changed: {0} reason:{1} EndPoint:{2} Key:{3} User:{4}\nStatistic:{5}", status, reason, im.SenderEndPoint.ToString(), peer.GetKey(), peer.Guid, im.SenderConnection.Statistics.ToString());
                                        }
                                        else
                                        {
                                            LogSys.Log(LOG_TYPE.DEBUG, "Network Status Changed: {0} reason:{1} EndPoint:{2}\nStatistic:{3}", status, reason, im.SenderEndPoint.ToString(), im.SenderConnection.Statistics.ToString());
                                        }
                                    }
                                    else
                                    {
                                        LogSys.Log(LOG_TYPE.DEBUG, "Network Status Changed:{0} reason:{1}", status, reason);
                                    }
                                    break;

                                case NetIncomingMessageType.Data:
                                    int    id   = 0;
                                    object msg  = null;
                                    byte[] data = null;
                                    try {
                                        data = im.ReadBytes(im.LengthBytes);
                                        msg  = Serialize.Decode(data, out id);
                                    } catch {
                                        if (null != im.SenderConnection)
                                        {
                                            RoomPeer peer = RoomPeerMgr.Instance.GetPeerByConnection(im.SenderConnection);
                                            if (null != peer)
                                            {
                                                LogSys.Log(LOG_TYPE.WARN, "room server decode message error !!! from User:{0}({1})", peer.Guid, peer.GetKey());
                                            }
                                        }
                                    }
                                    if (msg != null)
                                    {
                                        m_Dispatch.Dispatch(id, msg, im.SenderConnection);
                                    }
                                    else
                                    {
                                        if (null != im.SenderConnection)
                                        {
                                            RoomPeer peer = RoomPeerMgr.Instance.GetPeerByConnection(im.SenderConnection);
                                            if (null != peer)
                                            {
                                                LogSys.Log(LOG_TYPE.DEBUG, "got unknow message !!! from User:{0}({1})", peer.Guid, peer.GetKey());
                                            }
                                            else
                                            {
                                                LogSys.Log(LOG_TYPE.DEBUG, "got unknow message !!!");
                                            }
                                        }
                                        else
                                        {
                                            LogSys.Log(LOG_TYPE.DEBUG, "got unknow message !!!");
                                        }
                                    }
                                    break;

                                default:
                                    break;
                                }
                                m_NetServer.Recycle(im);
                            }
                            else
                            {
                                break;
                            }
                        } catch (Exception ex) {
                            LogSys.Log(LOG_TYPE.ERROR, "Exception {0}\n{1}", ex.Message, ex.StackTrace);
                        }
                    }
                    RoomPeerMgr.Instance.Tick();
                    long endTime = TimeUtility.GetElapsedTimeUs();
                    if (endTime - startTime >= 10000)
                    {
                        LogSys.Log(LOG_TYPE.DEBUG, "Warning, IOHandler() cost {0} us !\nNetPeer Statistic:{1}", endTime - startTime, m_NetServer.Statistics.ToString());
                    }
                } catch (Exception ex) {
                    LogSys.Log(LOG_TYPE.ERROR, "Exception {0}\n{1}", ex.Message, ex.StackTrace);
                }

                Thread.Sleep(10);
            }
        }
示例#16
0
 internal void Tick()
 {
     try {
         if (m_NeedAddKey2Peers.Count > 0)
         {
             lock (m_LockObj) {
                 foreach (KeyValuePair <uint, RoomPeer> pair in m_NeedAddKey2Peers)
                 {
                     if (m_KeyPeer.ContainsKey(pair.Key) || m_KeyPeer.TryAdd(pair.Key, pair.Value))
                     {
                         m_DeletedAddKeys.Add(pair.Key);
                     }
                 }
             }
         }
         if (m_NeedAddConnection2Peers.Count > 0)
         {
             lock (m_LockObj) {
                 foreach (KeyValuePair <NetConnection, RoomPeer> pair in m_NeedAddConnection2Peers)
                 {
                     if (m_DicPeer.ContainsKey(pair.Key) || m_DicPeer.TryAdd(pair.Key, pair.Value))
                     {
                         m_DeletedAddConnections.Add(pair.Key);
                     }
                 }
             }
         }
         if (m_DeletedAddKeys.Count > 0)
         {
             lock (m_LockObj) {
                 foreach (uint key in m_DeletedAddKeys)
                 {
                     m_NeedAddKey2Peers.Remove(key);
                 }
                 m_DeletedAddKeys.Clear();
             }
         }
         if (m_DeletedAddConnections.Count > 0)
         {
             lock (m_LockObj) {
                 foreach (NetConnection conn in m_DeletedAddConnections)
                 {
                     m_NeedAddConnection2Peers.Remove(conn);
                 }
                 m_DeletedAddConnections.Clear();
             }
         }
         if (m_NeedDeleteKeys.Count > 0)
         {
             lock (m_LockObj) {
                 foreach (uint key in m_NeedDeleteKeys)
                 {
                     RoomPeer peer = null;
                     if (!m_KeyPeer.ContainsKey(key))
                     {
                         m_DeletedKeys.Add(key);
                     }
                     else if (m_KeyPeer.TryRemove(key, out peer))
                     {
                         m_DeletedKeys.Add(key);
                         if (null != peer)
                         {
                             m_NeedDeleteConnections.Add(peer.GetConnection());
                         }
                     }
                 }
             }
         }
         if (m_NeedDeleteConnections.Count > 0)
         {
             lock (m_LockObj) {
                 foreach (NetConnection conn in m_NeedDeleteConnections)
                 {
                     RoomPeer peer = null;
                     if (!m_DicPeer.ContainsKey(conn))
                     {
                         m_DeletedConnections.Add(conn);
                     }
                     else if (m_DicPeer.TryRemove(conn, out peer))
                     {
                         m_DeletedConnections.Add(conn);
                     }
                 }
             }
         }
         if (m_DeletedKeys.Count > 0)
         {
             lock (m_LockObj) {
                 foreach (uint key in m_DeletedKeys)
                 {
                     m_NeedDeleteKeys.Remove(key);
                 }
                 m_DeletedKeys.Clear();
             }
         }
         if (m_DeletedConnections.Count > 0)
         {
             lock (m_LockObj) {
                 foreach (NetConnection conn in m_DeletedConnections)
                 {
                     m_NeedDeleteConnections.Remove(conn);
                 }
                 m_DeletedConnections.Clear();
             }
         }
     } catch (Exception ex) {
         LogSys.Log(LOG_TYPE.ERROR, "Exception {0}\n{1}", ex.Message, ex.StackTrace);
     }
 }