public JHSNetworkReader Accumulate(byte bt)
        {
            btbuf.Add(bt);
            if (btbuf.Count < PACKAGE_HEADER_LEN)
            {
                return(null);
            }

            byte[] buff = btbuf.ToArray();
            uint   head = ReadUInt32(buff, 0);
            uint   len  = ReadUInt16(buff, 4);
            int    ptr  = btbuf.Count - PACKAGE_HEADER_LEN;

            if (head == PACKAGE_HEADER_ID)
            {
                if (btbuf.Count >= 65536)
                {
                    btbuf.Clear();
                    return(null);
                }

                if (len == ptr)
                {
                    if (NetConfig.logFilter >= JHSLogFilter.Developer)
                    {
                        JHSDebug.Log("JHSPacketFarmer Read:" + BitConverter.ToString(buff));
                    }

                    btbuf.Clear();
                    var x = new JHSNetworkReader(buff);
                    x.ReadBytes(PACKAGE_HEADER_LEN);
                    return(x);
                }
            }

            return(null);
        }
예제 #2
0
 protected bool HandleReader(JHSNetworkReader reader)
 {
     try
     {
         ushort sz        = reader.ReadUInt16();
         short  msgType   = reader.ReadInt16();
         byte[] msgBuffer = reader.ReadBytes(sz);
         if (isClient)
         {
             JHSNetworkReader msgReader = new JHSNetworkReader(msgBuffer);
             if (m_MessageHandlersDict.ContainsKey(msgType))
             {
                 JHSNetworkClient.PushMessage(new JHSNetworkMessage()
                 {
                     msgType = msgType,
                     reader  = msgReader,
                     conn    = this
                 });
             }
             else
             {
                 if (NetConfig.logFilter >= JHSLogFilter.Error)
                 {
                     JHSDebug.LogError("JHSConnection :: Unknown message ID " + msgType + " connId:" + connectionId);
                 }
                 if (NetConfig.UseStatistics)
                 {
                     ReadError += 1;
                 }
             }
         }
         else
         {
             JHSNetworkReader          msgReader   = new JHSNetworkReader(msgBuffer);
             JHSNetworkMessageDelegate msgDelegate = null;
             if (m_MessageHandlersDict.ContainsKey(msgType))
             {
                 msgDelegate = m_MessageHandlersDict[msgType];
             }
             if (msgDelegate != null)
             {
                 msgDelegate(new JHSNetworkMessage()
                 {
                     msgType = msgType,
                     reader  = msgReader,
                     conn    = this
                 });
             }
             else
             {
                 if (NetConfig.logFilter >= JHSLogFilter.Error)
                 {
                     JHSDebug.LogError("JHSConnection :: Unknown message ID " + msgType + " connId:" + connectionId);
                 }
                 if (NetConfig.UseStatistics)
                 {
                     ReadError += 1;
                 }
             }
         }
     }
     catch { }
     return(true);
 }