pack() public method

public pack ( byte data, int len ) : byte[]
data byte
len int
return byte[]
Esempio n. 1
0
            public byte[] Invoke <T>(SprotoTypeBase request = null, long?session = null)
            {
                int tag = protocol[typeof(T)];

                ProtocolFunctionDictionary.MetaInfo info = protocol[tag];
#if (!INCLUDE_IL2CPP)
                if (request != null && request.GetType() != info.Request.Key)
                {
                    throw new Exception("request type: " + request.GetType().ToString() + "not is expected. [" + info.Request.Key.GetType().ToString() + "]");
                }
#endif
                package.clear();
                package.type = tag;

                if (session != null)
                {
                    rpc.sessionDictionary.Add((long)session, info.Response.Value);
                    package.session = (long)session;
                }

                stream.Seek(0, System.IO.SeekOrigin.Begin);
                int len = package.encode(stream);

                if (request != null)
                {
                    len += request.encode(stream);
                }

                return(spack.pack(stream.Buffer, len));
            }
Esempio n. 2
0
    /// <summary>
    /// 发一个消息
    /// </summary>
    /// <typeparam name="T">消息类型</typeparam>
    /// <param name="msg">消息内容</param>

    public void SendMessage <T>(T msg) where T : Sproto.SprotoTypeBase
    {
        if (_socket == null)
        {
            return;
        }

        uint msgID = _metaSet.GetByType <T>().id;

        if (msgID == 0)
        {
            throw new InvalidCastException("Error when getting msgID:" + typeof(T).FullName);
        }

        if (DebugMessage)
        {
            ReflectMessage(msgID, msg);
        }


        try
        {
            var data = spack.pack(msg.encode());

            _socket.SendPacket(msgID, data);
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
            return;
        }
    }
Esempio n. 3
0
    private byte[] GetDataBuffer()
    {
        if (_headBuffer == null || _headBuffer.Length == 0 || _bodyBuffer == null)
        {
            return(new byte[0]);
        }
        UInt16 headLength = (UInt16)_headBuffer.Length;
        UInt16 bodyLength = (UInt16)_bodyBuffer.Length;

        //加头长度,合并body
        //byte[] lenBytes = BitConverter.GetBytes(Converter.GetBigEndian(headLength));
        byte[] buffer = new byte[headLength + bodyLength];
        //Buffer.BlockCopy(lenBytes, 0, buffer, 0, lenBytes.Length);
        Buffer.BlockCopy(_headBuffer, 0, buffer, 0, _headBuffer.Length);
        Buffer.BlockCopy(_bodyBuffer, 0, buffer, _headBuffer.Length, _bodyBuffer.Length);
        Sproto.SprotoPack pack = new Sproto.SprotoPack();
        buffer = pack.pack(buffer);
        return(buffer);
    }
Esempio n. 4
0
		public override void run() {
			SprotoPack extract = new SprotoPack ();

			byte[] data = new byte[] {
				0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
				0X00, 0X00, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X68, 0X65, 0X6c, 0X6c,
				0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00, 0X1b, 0X00, 0X00, 0X00,
				0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00,
				0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00, 0X00, 0Xff, 0Xff, 0Xff,
				0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X41, 0X42, 0X43, 0X03,
				0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00, 0X00, 0X04, 0Xfd, 0Xff,
				0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X00, 0X00, 0X00,
				0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00,
				0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X02, 0X00,
				0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X10, 0X00, 0X00,
				0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X04, 0X00, 0X00, 0X00,
				0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X02,
				0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00,
				0X74, 0X65, 0X73, 0X74,
			};

			byte[] result_pack_data = new byte[] {
				0X41, 0X08, 0X04, 0X00, 0Xc4, 0X05, 0X68, 0X65, 0X8f, 0X6c, 0X6c, 0X6f, 0X04,
				0X40, 0X8b, 0X42, 0X0f, 0X1b, 0X04, 0X22, 0X01, 0X05, 0Xe2, 0X05, 0X77, 0X6f,
				0X72, 0Xc7, 0X6c, 0X64, 0X04, 0Xff, 0Xff, 0X47, 0Xff, 0Xff, 0X0e, 0X03, 0X3c,
				0X41, 0X42, 0X43, 0X03, 0X1e, 0X64, 0X65, 0X66, 0X19, 0Xff, 0X00, 0X04, 0Xfd,
				0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0X1f, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X22,
				0X01, 0X02, 0Xa2, 0X03, 0X01, 0X01, 0X11, 0X3e, 0X06, 0X55, 0X02, 0X01, 0Xca,
				0X02, 0X10, 0X10, 0X45, 0X03, 0X01, 0X02, 0Xf1, 0X04, 0X9c, 0Xff, 0Xff, 0Xff,
				0X51, 0X16, 0X04, 0X01, 0X45, 0X02, 0X03, 0X08, 0Xc4, 0X04, 0X74, 0X65, 0X03,
				0X73, 0X74,

			};

			byte[] result_unpack_data = new byte[] {
				0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
				0X00, 0X00, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X68, 0X65, 0X6c, 0X6c,
				0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00, 0X1b, 0X00, 0X00, 0X00,
				0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00,
				0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00, 0X00, 0Xff, 0Xff, 0Xff,
				0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X41, 0X42, 0X43, 0X03,
				0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00, 0X00, 0X04, 0Xfd, 0Xff,
				0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X00, 0X00, 0X00,
				0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00,
				0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X02, 0X00,
				0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X10, 0X00, 0X00,
				0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X04, 0X00, 0X00, 0X00,
				0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X02,
				0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00,
				0X74, 0X65, 0X73, 0X74, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
			};

			byte[] pack_data = extract.pack (data);
			dump_bytes (pack_data);
			assert (pack_data, result_pack_data);

			byte[] unpack_data = extract.unpack (pack_data);
			assert (unpack_data, result_unpack_data);

			byte[] data2 = new byte[] {
				1, 2, 3, 4, 5, 6, 7, 8,
				1, 2, 3, 4, 5, 0, 7, 8,
				1, 2, 3, 4, 5, 0,
			};

			Console.WriteLine ("======pack=======");

			byte[] pack_data1 = extract.pack (data2);
			dump_bytes (pack_data1);

			Console.WriteLine ("=======unpack======");
			byte[] unpack_data1 = extract.unpack (pack_data1);
			dump_bytes (unpack_data1);
		}
Esempio n. 5
0
        /// <summary>
        /// sproto消息
        /// </summary>
        void OnSprotoMessage(ClientSession session, ByteBuffer buffer) {
            byte[] data = buffer.ReadBytes();
            //-------------------------------------------------------------
            SprotoPack spack = new SprotoPack();
            byte[] pack_data = spack.pack(data);             // pack
            byte[] unpack_data = spack.unpack(pack_data);     // unpack

            AddressBook addr = new AddressBook(unpack_data);   // decode
            Console.WriteLine("OnSprotoMessage id=>>" + addr.person.Count);
            buffer.Close(); buffer = null;

            //-------------------------------------------------------------
            ByteBuffer newBuffer = new ByteBuffer();
            newBuffer.WriteByte((byte)ProtocalType.SPROTO);
            newBuffer.WriteBytes(data); //添加数据
            SocketUtil.SendMessage(session, Protocal.Login, newBuffer);
        }
Esempio n. 6
0
		public override void run() {
			Foobar obj = new Foobar ();
			obj.a = "hello";
			obj.b = 1000000;
			obj.c = true;

			obj.d = new Foobar.Nest ();
			obj.d.a = "world";
			obj.d.c = -1;

			obj.e = new System.Collections.Generic.List<string> {"ABC", "def"};
			obj.f = new System.Collections.Generic.List<long> { -3, -2, -1, 0 , 1, 2};
			obj.g = new System.Collections.Generic.List<bool> { true, false, true};

			obj.h = new System.Collections.Generic.List<Foobar> ();

			Foobar tmp = new Foobar ();
			tmp.b = 100;
			obj.h.Add (tmp);

			obj.h.Add (new Foobar ());

			tmp = new Foobar ();
			tmp.b = -100;
			tmp.c = false;
			obj.h.Add (tmp);

			tmp = new Foobar ();
			tmp.b = 0;
			tmp.e = new System.Collections.Generic.List<string> { "test" };
			obj.h.Add (tmp);

			byte[] data = obj.encode ();
			byte[] result_data = new byte[] {
				0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
				0X00, 0X00, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X68, 0X65, 0X6c, 0X6c,
				0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00, 0X1b, 0X00, 0X00, 0X00,
				0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00,
				0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00, 0X00, 0Xff, 0Xff, 0Xff,
				0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X41, 0X42, 0X43, 0X03,
				0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00, 0X00, 0X04, 0Xfd, 0Xff,
				0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X00, 0X00, 0X00,
				0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00,
				0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X02, 0X00,
				0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X10, 0X00, 0X00,
				0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X04, 0X00, 0X00, 0X00,
				0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X02,
				0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00,
				0X74, 0X65, 0X73, 0X74,
			};
			assert (data, result_data);

			Sproto.SprotoPack spack = new SprotoPack ();

			byte[] pack_data = spack.pack (data);
			byte[] result_pack_data = new byte[] {
				0X41, 0X08, 0X04, 0X00, 0Xc4, 0X05, 0X68, 0X65, 0X8f, 0X6c, 0X6c, 0X6f, 0X04,
				0X40, 0X8b, 0X42, 0X0f, 0X1b, 0X04, 0X22, 0X01, 0X05, 0Xe2, 0X05, 0X77, 0X6f,
				0X72, 0Xc7, 0X6c, 0X64, 0X04, 0Xff, 0Xff, 0X47, 0Xff, 0Xff, 0X0e, 0X03, 0X3c,
				0X41, 0X42, 0X43, 0X03, 0X1e, 0X64, 0X65, 0X66, 0X19, 0Xff, 0X00, 0X04, 0Xfd,
				0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0X1f, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X22,
				0X01, 0X02, 0Xa2, 0X03, 0X01, 0X01, 0X11, 0X3e, 0X06, 0X55, 0X02, 0X01, 0Xca,
				0X02, 0X10, 0X10, 0X45, 0X03, 0X01, 0X02, 0Xf1, 0X04, 0X9c, 0Xff, 0Xff, 0Xff,
				0X51, 0X16, 0X04, 0X01, 0X45, 0X02, 0X03, 0X08, 0Xc4, 0X04, 0X74, 0X65, 0X03,
				0X73, 0X74,
			};
			assert (pack_data, result_pack_data);

			byte[] unpack_data = spack.unpack (pack_data);
			byte[] result_unpack_data = new byte[] {
				0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
				0X00, 0X00, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X68, 0X65, 0X6c, 0X6c,
				0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00, 0X1b, 0X00, 0X00, 0X00,
				0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00,
				0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00, 0X00, 0Xff, 0Xff, 0Xff,
				0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X41, 0X42, 0X43, 0X03,
				0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00, 0X00, 0X04, 0Xfd, 0Xff,
				0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X00, 0X00, 0X00,
				0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00,
				0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X02, 0X00,
				0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X10, 0X00, 0X00,
				0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X04, 0X00, 0X00, 0X00,
				0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X02,
				0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00,
				0X74, 0X65, 0X73, 0X74, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
			};
			assert (unpack_data, result_unpack_data);

			Foobar foo = new Foobar (unpack_data);
			assert (foo.a == "hello");
			assert (foo.b == 1000000);
			assert (foo.c == true);
			assert (foo.d.a == "world");
			assert (foo.d.c == -1);
			assert (foo.e [0] == "ABC");
			assert (foo.e [1] == "def");
			assert (foo.f, new Int64[] { -3, -2, -1, 0 , 1, 2});
			assert (foo.g, new bool[] {true, false, true });
			assert (foo.h [0].b == 100);
			assert (foo.h [2].b == -100);
			assert (foo.h [2].c == false);
			assert (foo.h [3].b == 0);
			assert (foo.h [3].e [0] == "test");

			// test extend proto

			byte[] extend_data = new byte[] {
				0X0a, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00,
				0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X70, 0X4a, 0X05, 0X00, 0X00, 0X00,
				0X68, 0X65, 0X6c, 0X6c, 0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00,
				0X1b, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00,
				0X00, 0X05, 0X00, 0X00, 0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00,
				0X00, 0Xff, 0Xff, 0Xff, 0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00,
				0X41, 0X42, 0X43, 0X03, 0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00,
				0X00, 0X04, 0Xfd, 0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff,
				0Xff, 0X00, 0X00, 0X00, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00,
				0X03, 0X00, 0X00, 0X00, 0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00,
				0X00, 0X00, 0X02, 0X00, 0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00,
				0X00, 0X10, 0X00, 0X00, 0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00,
				0X04, 0X00, 0X00, 0X00, 0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04,
				0X00, 0X01, 0X00, 0X02, 0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00,
				0X04, 0X00, 0X00, 0X00, 0X74, 0X65, 0X73, 0X74, 0X05, 0X00, 0X00, 0X00, 0X7a,
				0X69, 0X78, 0X75, 0X6e, 0x95, 0x27
			};

			long size =  foo.init (extend_data);
			Console.WriteLine ("szie: " + size);
		}