Ejemplo n.º 1
0
        public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
        {
            if (e.Stream.Length < minMsgLength)
            {
                return;
            }

            string json = string.Empty;

            lock (lockObj)
            {
                MsgFullInfo msgFullInfo = msgFullInfos.GetOrAdd(e.Session.ID, key =>
                {
                    return(new MsgFullInfo(key));
                });
                if (!msgFullInfo.NewMsg)
                {
                    byte   firstChar = (byte)e.Stream.ToPipeStream().ReadByte();
                    string beginMsg  = Encoding.UTF8.GetString(new byte[] { firstChar });
                    if (beginFlag.Equals(beginMsg))
                    {
                        msgFullInfo.MsgAllLength = e.Stream.ToPipeStream().ReadInt32();
                        msgFullInfo.NewMsg       = true;
                    }
                }

                //Console.WriteLine($"the msg currentMsgLength is: {e.Stream.Length}");
                //Console.WriteLine($"the msgAllLength value is: {msgFullInfo.MsgAllLength}");

                if (msgFullInfo.MsgAllLength > e.Stream.Length)
                {
                    return;
                }

                msgFullInfo.Reset();
                json = e.Stream.ToPipeStream().ReadToEnd();
            }
            if (json.IsNullOrEmpty())
            {
                return;
            }

            ISession session = e.Session;

            //Console.WriteLine($"the json length is:{json.Length}, value is: {json}");
            CmdInfo info = null;

            try
            {
                info = JsonSerializer.Deserialize <CmdInfo>(json);

                if (info == null)
                {
                    SendError(session, "参数错误-JSON");
                }

                if (!info.IsValid(serverConfig.ValidString))
                {
                    SendError(session, "参数错误-Token");
                    return;
                }

                switch (info.Type)
                {
                case CmdType.Login:
                    Login(session, info);
                    break;

                case CmdType.SendMsg:
                    SendMsg(server, session, info);
                    break;

                case CmdType.SearchUser:
                    SearchUser(server, session, info);
                    break;

                case CmdType.AddUser:
                    AddUser(session, info);
                    break;

                case CmdType.LoginById:
                    LoginById(session, info);
                    break;

                case CmdType.Check:
                    Check(server, session, info);
                    break;

                default:
                    SendError(session, "参数错误-CmdType");
                    break;
                }
            }
            catch (Exception ex)
            {
                server.Error(ex, e.Session, ex.Message);
            }
        }