Exemplo n.º 1
0
    private byte[] serialize(TestStruct data)
    {
        ObjectPacker packer     = new MsgPack.ObjectPacker();
        var          packedData = packer.Pack(data);

        return(packedData);
    }
Exemplo n.º 2
0
    IEnumerator MsgTest()
    {
        string url = "msg_test";

        // テストデータ
        var sendData = new RecvMsg();

        sendData.Id       = 12345;
        sendData.AddScore = -9876;
        sendData.Text     = "hoge";

        Dictionary <string, string> headers = new Dictionary <string, string>();

        headers["Content-Type"] = "application/x-msgpack";

        var packer = new MsgPack.ObjectPacker();

        byte[] body = packer.Pack(sendData);


        using (WWW www = new WWW(HOST + url, body, headers)) {
            yield return(www);

            if (!string.IsNullOrEmpty(www.error))
            {
                Debug.Log("error:" + www.error);
                yield break;
            }

            var unpacker = new MsgPack.ObjectPacker();
            // unpack
            var result = unpacker.Unpack <RecvMsg>(www.bytes);
            Debug.Log("id : " + result.Id + " score : " + result.AddScore + " text : " + result.Text);
        }
    }
Exemplo n.º 3
0
 static IEnumerator StringUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     reader.Read();
     if (reader.Type == TypePrefixes.Nil)
     {
         yield return(null);
     }
     if (!reader.IsStr())
     {
         throw new FormatException();
     }
     byte[] buf = new byte[reader.Length];
     reader.ReadRawBytes(buf);
     yield return(reader.StringifyBytes(buf));
 }
Exemplo n.º 4
0
    private byte[] makeData(int no, object obj)
    {
        ObjectPacker packer = new MsgPack.ObjectPacker();
        var          data   = packer.Pack(obj);
        var          info   = BitConverter.GetBytes(no);

        byte[] result = new byte[info.Length + data.Length];

        // first : command
        Buffer.BlockCopy(info, 0, result, 0, info.Length);

        // second : data
        Buffer.BlockCopy(data, 0, result, info.Length, data.Length);

        return(result);
    }
Exemplo n.º 5
0
 private static object DateTimeUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     if (!reader.Read())
     {
         throw new FormatException();
     }
     if (reader.Type == TypePrefixes.Nil)
     {
         return(null);
     }
     if (!reader.IsUnsigned())
     {
         throw new FormatException();
     }
     return(new DateTime(1970, 1, 1).ToLocalTime().AddSeconds((double)reader.ValueUnsigned));
 }
Exemplo n.º 6
0
 static object StringUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     if (!reader.Read())
     {
         throw new FormatException();
     }
     if (reader.Type == TypePrefixes.Nil)
     {
         return(null);
     }
     if (!reader.IsRaw())
     {
         throw new FormatException();
     }
     packer.CheckBufferSize((int)reader.Length);
     reader.ReadValueRaw(packer._buf, 0, (int)reader.Length);
     return(Encoding.UTF8.GetString(packer._buf, 0, (int)reader.Length));
 }
Exemplo n.º 7
0
 static void StringPacker(ObjectPacker packer, MsgPackWriter writer, object o)
 {
     writer.Write(Encoding.UTF8.GetBytes((string)o));
 }
Exemplo n.º 8
0
 static object DateTimeOffsetUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     return(DateTimeOffset.Parse(reader.ReadRawString()));
 }
Exemplo n.º 9
0
 static object StringUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     return((object)reader.ReadRawString());
 }
Exemplo n.º 10
0
 private static object XorFloatUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     return(new XorFloat((float)packer.Unpack(reader, typeof(float))));
 }
Exemplo n.º 11
0
 private static void XorFloatPacker(ObjectPacker packer, MsgPackWriter writer, object o)
 {
     packer.Pack(writer, (float)(XorFloat)o, typeof(float));
 }
Exemplo n.º 12
0
 private static object XorUIntUnpacker(ObjectPacker packer, MsgPackReader reader)
 {
     return(new XorUInt((uint)packer.Unpack(reader, typeof(uint))));
 }
Exemplo n.º 13
0
 private static void XorUIntPacker(ObjectPacker packer, MsgPackWriter writer, object o)
 {
     packer.Pack(writer, (uint)(XorUInt)o, typeof(uint));
 }
Exemplo n.º 14
0
        private static void DateTimePacker(ObjectPacker packer, MsgPackWriter writer, object o)
        {
            DateTime d = new DateTime(1970, 1, 1).ToLocalTime();

            writer.Write((long)((DateTime)o - d).TotalSeconds);
        }