/// <summary>
 /// 重组数据
 /// </summary>
 public void RebuildData()
 {
     while (true)
     {
         ReciveData data = null;
         try
         {
             if (MQ.TryDequeue(out data))
             {
                 if (!ProcessByte(data))
                 {
                     data.UserToken.Reset("CollectReceiveData 复原数据出错");
                 }
             }
             else
             {
                 Thread.Sleep(1);
                 if (IsClient)
                 {
                     lock (this)
                     {
                         Monitor.Wait(this);
                     }
                 }
             }
         }
         catch (Exception e)
         {
             if (data != null)
             {
                 data.UserToken.Reset("RebuildData:" + e.Message + e.StackTrace);
             }
         }
     }
 }
 /// <summary>
 /// 整理缓冲区
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="totalpackage"></param>
 /// <param name="dataLength"></param>
 /// <param name="list"></param>
 /// <param name="userToken"></param>
 private void CleanWare(byte[] buffer, int totalpackage, int dataLength, List <ReciveData> list, UserToken userToken, int dataType)
 {
     try
     {
         //解包后剩下未处理的数据存放
         byte[] remain = new byte[buffer.Length - totalpackage];
         //数据实体
         byte[] data = new byte[dataLength];
         Array.Copy(buffer, NetWorkBase.TcpHeadLength, data, 0, data.Length);
         Array.Copy(buffer, totalpackage, remain, 0, remain.Length);
         //将剩下的字节放回接收缓存
         userToken.DataList = remain.ToList <byte>();
         ReciveData reciveData = new ReciveData()
         {
             UserToken = userToken,
             Command   = dataType == 1 ? (Command)BitConverter.ToInt32(data) : Command.Nothing,
             Actual    = data.Length,
             Buffer    = data,
         };
         //添加到数据包list
         list.Add(reciveData);
         //剩余数据继续递归调用
         if (userToken.DataList.Count > NetWorkBase.TcpHeadLength)
         {
             DecodeData(list, userToken);
         }
     }
     catch (Exception e)
     {
         userToken.Reset("CleanWare " + e.Message);
     }
 }
        /// <summary>
        /// 处理接收到的数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns>是否成功</returns>
        private bool ProcessByte(ReciveData data)
        {
            try
            {
                //将收到的数据存在缓冲区
                byte[] temp = new byte[data.Actual];
                Array.Copy(data.Buffer, 0, temp, 0, temp.Length);
                data.UserToken.DataList.AddRange(temp);

                //数据超过Tcp自定义头部长度
                if (data.UserToken.DataList.Count > NetWorkBase.TcpHeadLength && data.UserToken.DataList.Count >= data.UserToken.CurrentPackageLength)
                {
                    //创建信息容器
                    List <ReciveData> list = new List <ReciveData>();
                    //处理完成后 不管连接是否失效 那信息都是有效的
                    DecodeData(list, data.UserToken);
                    if (list.Count > 0)
                    {
                        foreach (var item in list)
                        {
                            //加入队列
                            MCQ.Enqueue(item);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                data.UserToken.Reset("处理接收到的数据:" + e.Message);
                return(false);
            }
            return(true);
        }
Exemple #4
0
 private void OnMessages(ReciveData data)
 {
     if (data.Command == Command.Login)
     {
         OnConnected?.Invoke(data);
     }
     else
     {
         OnMessage?.Invoke(data);
     }
 }
Exemple #5
0
 public void InvokeMessage()
 {
     while (true)
     {
         Thread.Sleep(1);
         ReciveData data = null;
         try
         {
             if (MQ.TryDequeue(out data))
             {
                 if (!data.UserToken.WsShakeHand)
                 {
                     var shakingKey = WebSocketConverter.GetSecKeyAccetp(data.Buffer, data.Actual);
                     if (!string.IsNullOrEmpty(shakingKey))
                     {
                         var buffer = WebSocketConverter.PackHandShakeData(shakingKey);
                         if (data.UserToken.Send(buffer))
                         {
                             data.UserToken.WsShakeHand = true;
                             Console.WriteLine("已经发送握手协议了....");
                         }
                     }
                     else
                     {
                         data.UserToken.Reset("无效的协议");
                     }
                 }
                 else
                 {
                     if (string.IsNullOrEmpty(data.UserToken.SessionId))
                     {
                         data.UserToken.SessionId = Guid.NewGuid().ToString("N");
                         ISessionAdd.AddSession(data.UserToken);
                         ISessionAdd.OnConnected(data.UserToken);
                     }
                     var(message, buffer) = WebSocketConverter.AnalyticData(data.Buffer, data.Actual);
                     data.Message         = message;
                     data.Buffer          = buffer;
                     data.Actual          = buffer.Length;
                     Process.Invoke(data);
                 }
             }
         }
         catch (Exception e)
         {
             if (data != null)
             {
                 data.UserToken.Reset("WsProcessor " + e.Message);
             }
         }
     }
 }
Exemple #6
0
 /// <summary>
 /// 客户端发送心跳
 /// </summary>
 /// <param name="data"></param>
 public void Token(ReciveData data)
 {
     if (Processor.IsClient && !HasToken)
     {
         if (data.Command == Command.Login)
         {
             HasToken = true;
             Task.Factory.StartNew(() =>
             {
                 while (true)
                 {
                     Thread.Sleep(TokenTime);
                     var buffer = BitConverter.GetBytes(Command.Token.CInt());
                     PushSystemData(buffer);
                 }
             }, TaskCreationOptions.LongRunning);
         }
     }
 }
 /// <summary>
 /// 信息处理
 /// </summary>
 /// <param name="server"></param>
 public void InvokeMessage()
 {
     while (true)
     {
         ReciveData data = null;
         try
         {
             if (MCQ.TryDequeue(out data))
             {
                 //登陆保存会话 此处已经分配了SESSIONID
                 if (data.Command == Command.Login && ISessionAdd != null)
                 {
                     ISessionAdd.AddSession(data.UserToken);
                     data.UserToken.LoginFinish();
                     ISessionAdd.OnConnected(data.UserToken);
                 }
                 else if (data.Command == Command.Token)
                 {
                     data.UserToken.LastTokenDateTime = DateTime.Now;
                 }
                 else
                 {
                     //客户端定时发送心跳
                     data.UserToken.Token(data);
                     Process.Invoke(data);
                 }
             }
             else
             {
                 Thread.Sleep(1);
             }
         }
         catch (Exception e)
         {
             if (data != null)
             {
                 data.UserToken.Reset("InvokeMessage:" + e.Message + e.StackTrace);
             }
         }
     }
 }
Exemple #8
0
 public void ReceiveEnQuene(ReciveData reciveData)
 {
     MQ.Enqueue(reciveData);
 }
Exemple #9
0
 public abstract void OnMessage(ReciveData data);