Ejemplo n.º 1
0
        /// <summary>
        /// 进入CS tcp消息循环
        /// </summary>
        /// <param name="cid">tcp连接的id</param>
        private static void StartMessageLoop(object cid)
        {
            int           cid_i   = (int)cid;
            NetworkStream nStream = clientList[(int)cid].GetStream();

            byte[]        buffer   = new byte[4096];
            StringBuilder sbbuffer = new StringBuilder();
            int           read;

            while (true)
            {
                if (IsOnline(clientList[(int)cid]))
                {
                    if (nStream.CanRead)
                    {
                        while (nStream.DataAvailable)
                        {
                            read = nStream.Read(buffer, 0, buffer.Length);
                            sbbuffer.Append(Encoding.Unicode.GetString(buffer, 0, read));
                            if (!nStream.DataAvailable)
                            {
                                HandleMessage(sbbuffer.ToString(), (int)cid);
                                sbbuffer = new StringBuilder();
                            }
                        }
                    }
                }
                else
                {
                    HandleXMsg.Logout((int)cid);
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="cid"></param>
        private static void HandleMessage(string msg, int cid)
        {
            XMessage xmsg = new XMessage(msg);

            switch (xmsg.Type)
            {
            case "Login":
                HandleXMsg.Login(xmsg, cid);
                break;

            case "Msg":
                HandleXMsg.Msg(xmsg, cid);
                break;

            default:
                break;
            }
        }