Example #1
0
        public static MessageBufferPackage ParseFromMessage(Message message)
        {
            if (message.Class != (byte)MessageClass.Package)
            {
                throw new Exception("Not A Package Message!");
            }

            var buffer = new MessageBufferPackage();

            using (var mem = new MemoryStream(message.Content))
            {
                using (var br = new BinaryReader(mem))
                {
                    var count = br.ReadInt32();
                    for (var i = 0; i < count; i++)
                    {
                        byte   type    = br.ReadByte();
                        int    flag    = br.ReadInt32();
                        int    size    = br.ReadInt32();
                        byte[] content = br.ReadBytes(size);
                        buffer.AddMessage(new Message((MessageClass)type, flag, content));
                    }
                }
            }

            return(buffer);
        }
Example #2
0
 /// <summary>
 /// 接收到消息
 /// </summary>
 /// <param name="client"></param>
 /// <param name="msg"></param>
 public virtual void OnReceivedMessag(Client client, Message msg)
 {
     client.LastMessageTime = DateTime.UtcNow;
     if (msg.Class == MessageClass.Ping)
     {
         SendMessage(client, msg);
     }
     else if (msg.Class == MessageClass.Package)
     {
         var package = MessageBufferPackage.ParseFromMessage(msg);
         foreach (var i in package.Messages)
         {
             OnReceivedMessag(client, i);
         }
     }
     else if (msg.Class == MessageClass.Action)
     {
         client.SetActionMessage(msg);
     }
     else if (this.HandlerManager != null)
     {
         this.HandlerManager.Handle(msg, client);
     }
     else
     {
         Debuger.DebugLog("Server No Handler!");
     }
 }
Example #3
0
 /// <summary>
 /// 接收到消息
 /// </summary>
 /// <param name="message"></param>
 public virtual void OnReceived(Message message)
 {
     if (message.Class == MessageClass.Ping)
     {
         #region Ping
         var  tickNow  = DateTime.Now.Ticks;
         long tickSend = 0;
         using (var mem = new MemoryStream(message.Content))
         {
             using (var br = new BinaryReader(mem))
             {
                 tickSend = br.ReadInt64();
             }
         }
         Delay = (tickNow - tickSend);
         SyncCall.Add(() =>
         {
             if (OnPingCompleted != null)
             {
                 OnPingCompleted(this, new PingCompletedArgs {
                     DelayTicks = Delay
                 });
             }
         });
         #endregion
     }
     else if (message.Class == MessageClass.Package)
     {
         var bufferPackage = MessageBufferPackage.ParseFromMessage(message);
         foreach (var m in bufferPackage.Messages)
         {
             OnReceived(m);
         }
     }
     else if (message.Class == MessageClass.Close)
     {
         this.Disconnect();
     }
     else
     {
         ReceivedBufferMessage(message);
     }
     if (UsedThread)
     {
         SendEvent.Set();
     }
 }