예제 #1
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);
            }
        }