示例#1
0
            /// <summary>
            /// 探测并解包数据,属于外部接口
            /// </summary>
            /// <param name="data"></param>
            /// <param name="apLayer"></param>
            /// <param name="npLayer"></param>
            /// <param name="mapLayer"></param>
            /// <returns></returns>
            public static CmccUnpacketResult Unpacket(byte[] data, ref APLayer apLayer, ref NPLayer npLayer, ref MapLayer mapLayer)
            {
                if (data == null)
                {
                    return CmccUnpacketResult.OtherError;
                }

                int begin = -1;
                int end = -1;

                for (int n = 0; n < data.Length; n++)
                {
                    if (data[n] == CmccConst.ApFlag.A)
                    {

                        if (data[n + 1] == CmccConst.ApId.A)
                        {
                            apLayer.apId = CmccConst.ApId.A;
                            begin = n;
                            break;
                        }
                        else if (data[n + 1] == CmccConst.ApId.C)
                        {
                            apLayer.apId = CmccConst.ApId.C;
                            begin = n;
                            break;
                        }
                    }

                    if (data[n] == CmccConst.ApFlag.B)
                    {
                        apLayer.apId = CmccConst.ApId.B;
                        begin = n;
                        break;
                    }
                }

                if (begin < 0)
                {
                    return CmccUnpacketResult.OtherError;
                }

                for (int n = begin + 1; n < data.Length; n++)
                {
                    if (data[n] == data[begin])
                    {
                        end = n;
                        break;
                    }
                }

                if (end < 0)
                {
                    return CmccUnpacketResult.OtherError;
                }

                byte[] apData = new byte[end - begin + 1];

                for (int n = 0; n < apData.Length; n++)
                {
                    apData[n] = data[n + begin];
                }

                switch (apLayer.apId)
                {
                    case CmccConst.ApId.A:
                        return UnpacketAPA(apData, ref npLayer, ref mapLayer);

                    case CmccConst.ApId.B:
                        return UnpacketAPB(apData, ref npLayer, ref mapLayer);

                    case CmccConst.ApId.C:
                        return UnpacketAPC(apData, ref npLayer, ref mapLayer);

                    default:
                        return CmccUnpacketResult.OtherError;
                }
            }
示例#2
0
            /// <summary>
            /// 提供给用户的打包接口
            /// </summary>
            /// <param name="apLayer"></param>
            /// <param name="npLayer"></param>
            /// <param name="mapLayer"></param>
            /// <returns></returns>
            public static byte[] Packet(APLayer apLayer, NPLayer npLayer, MapLayer mapLayer)
            {
                List<byte> apData = new List<byte>();
                List<byte> npData = new List<byte>();
                List<byte> mapData = new List<byte>();

                // 打包MAP层数据包
                mapData = PacketMapLayer(mapLayer);

                switch (npLayer.npId)
                {
                    case CmccConst.NpId.A:
                        npData = PacketNPLayer(npLayer, mapLayer.mapId, mapData);
                        break;

                    default:
                        npData = mapData;
                        break;
                }

                switch (apLayer.apId)
                {
                    case CmccConst.ApId.A:
                        apData = PacketAPA(npLayer.npId, npData);
                        break;

                    case CmccConst.ApId.B:
                        apData = PacketAPB(npLayer.npId, npData);
                        break;
                    case CmccConst.ApId.C:
                        apData = PacketAPC(npLayer.npId, npData);
                        break;
                    default:
                        apData = npData;
                        break;
                }

                return apData.ToArray();
            }