コード例 #1
0
ファイル: PackageUtil.cs プロジェクト: xqy/game
 private static object getValue(ref ByteBuffer buff, Type type)
 {
     switch (type.FullName)
     {
         case "System.Int16":
             return buff.removeShort();
         case "System.Int32":
             return buff.removeInt();
         case "System.Int64":
             return buff.removeLong();
         case "System.UInt16":
             return buff.removeUshort();
         case "System.UInt32":
             return buff.removeUint();
         //case "System.UInt64":
         //    return buff.removeUlong();
         case "System.Byte":
             return buff.removeByte();
         case "System.Boolean":
             return buff.removeBool();
         case "System.Single":
             return buff.removeFloat();
         case "System.Double":
             return buff.removeDouble();
         case "System.String":
             return buff.removeString();
         default:
             throw new Exception("协议包含不可解析类型:" + type.FullName);
     }
 }
コード例 #2
0
ファイル: SocketManager.cs プロジェクト: xqy/game
        /// <summary>
        /// 拆包,时间戳 + 协议号 + 内容长度 + 内容
        /// </summary>
        /// <param name="buff"></param>
        /// <returns></returns>
        private Package unpack(ByteBuffer buff)
        {
            Package package = new Package();
            package.timeStamp = buff.removeInt();
            package.protocol = buff.removeInt();
            package.len = buff.removeUshort();

            if (buff.length < package.len)
            {
                throw new Exception("协议" + package.protocol +
                    "包体字节长度不对,应是" + package.len + ",但当前是" + buff.length);
            }

            if (listenDic.ContainsKey(package.protocol))
            {
                package.data = PackageUtil.byteBufferToClrObject(ref buff, listenDic[package.protocol].clrType);
                Debug.Log("[接收] " + package.toString());
            }
            else
            {
                Debug.Log("[接收,未处理] " + package.toString());
            }
            return package;
        }