Exemplo n.º 1
0
        protected override object DeserializeObject(ArraySegment <byte> value)
        {
            using (var ms = new MemoryStream(value.Array, value.Offset, value.Count, writable: false))
            {
                var unpacker = MsgPack.Unpacker.Create(ms);

                // unpack object
                unpacker.Read();
                if (unpacker.IsArrayHeader)
                {
                    // read type
                    unpacker.Read();
                    var typeName = (string)unpacker.Data;
                    var type     = readCache.GetOrAdd(typeName, x => Type.GetType(x, throwOnError: true)); // Get type or Register type

                    // unpack object
                    unpacker.Read();
                    var unpackedValue = MessagePackSerializer.Create(type, defaultContext).UnpackFrom(unpacker);

                    return(unpackedValue);
                }
                else
                {
                    throw new InvalidDataException("MessagePackMapTranscoder only supports [\"TypeName\", object]");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// </summary>
        /// <param name="asyncResult">
        /// </param>
        public void ReadCallBack(IAsyncResult asyncResult)
        {
            lock (this.streamLockRead)
            {
                try
                {
                    NetworkStream network = this.clientSocket.GetStream();
                    int           read    = network.EndRead(asyncResult);
                    if (read == 0)
                    {
                        network.Close();
                        this.clientSocket.Close();
                        this.OnDisconnect();
                        return;
                    }

                    MemoryStream memoryStream = new MemoryStream(asyncResult.AsyncState as byte[]);

                    MessagePackSerializer <OnMessageArgs> messagePackSerializer =
                        MessagePackSerializer.Create <OnMessageArgs>();

                    OnMessageArgs args = messagePackSerializer.Unpack(memoryStream);

                    this.MessageReceived(this, args);
                }
                catch (Exception e)
                {
                    LogUtil.ErrorException(e);
                    return;
                }
            }

            this.WaitForRequest();
        }
Exemplo n.º 3
0
        /// <summary>
        /// </summary>
        /// <param name="fname">
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// </returns>
        public static List <T> UncompressData <T>(string fname)
        {
            // Need to build the serializer/deserializer prior to Task invocations
            MessagePackSerializer <List <T> > constructor = MessagePackSerializer.Create <List <T> >();
            List <T> resultList = new List <T>();

            using (Stream fileStream = new FileStream(fname, FileMode.Open))
            {
                BinaryReader binaryReader = new BinaryReader(fileStream);

                byte   versionlength = binaryReader.ReadByte();
                char[] version       = new char[versionlength];
                version = binaryReader.ReadChars(versionlength);
                string versionString = "";
                foreach (char c in version)
                {
                    versionString += c;
                }

                Console.WriteLine("Loading data for client version " + versionString);
                // TODO: Check version and print a warning if not same as config.xml's

                // packaged is unused here
                int packaged = binaryReader.ReadInt32();

                int capacity = binaryReader.ReadInt32();

                int slices = binaryReader.ReadInt32();

                TaskedSerializer <T>[] tasked = new TaskedSerializer <T> [slices];

                Task[] tasks = new Task[slices];

                for (int i = 0; i < slices; i++)
                {
                    int    size       = binaryReader.ReadInt32();
                    byte[] tempBuffer = binaryReader.ReadBytes(size);
                    using (MemoryStream tempStream = new MemoryStream(tempBuffer))
                    {
                        tasked[i] = new TaskedSerializer <T>(tempStream);
                    }
                    int i1 = i;
                    tasks[i] = new Task(() => tasked[i1].Deserialize());
                    tasks[i].Start();
                }

                Task.WaitAll(tasks);

                resultList = new List <T>(capacity);
                for (int i = 0; i < slices; i++)
                {
                    resultList.AddRange(tasked[i].DataSlice);
                    tasked[i].DataSlice.Clear();
                    tasks[i].Dispose();
                    tasked[i].Dispose();
                }
            }

            return(resultList);
        }
Exemplo n.º 4
0
        /// <summary>
        /// </summary>
        /// <param name="fname">
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <typeparam name="TU">
        /// </typeparam>
        /// <returns>
        /// </returns>
        public static Dictionary <T, TU> UncompressData <T, TU>(string fname)
        {
            var    tempList   = new Dictionary <T, TU>();
            Stream fileStream = new FileStream(fname, FileMode.Open);

            ZlibStream inputStream = new ZlibStream(fileStream, CompressionMode.Decompress);

            inputStream.Seek(0, SeekOrigin.Begin);
            BinaryReader binaryReader  = new BinaryReader(inputStream);
            byte         versionlength = binaryReader.ReadByte();

            char[] version = new char[versionlength];
            version = binaryReader.ReadChars(versionlength);

            // TODO: Check version and print a warning if not same as config.xml's
            MessagePackSerializer <Dictionary <T, TU> > messagePackSerializer =
                MessagePackSerializer.Create <Dictionary <T, TU> >();

            var buffer = new byte[4];

            inputStream.Read(buffer, 0, 4);

            inputStream.Read(buffer, 0, 4);

            return(messagePackSerializer.Unpack(inputStream));
        }
Exemplo n.º 5
0
        protected bool LoadFromCache(string path)
        {
            try
            {
                using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    var serializer = MessagePackSerializer.Create <TList>();
                    this.Entries = serializer.Unpack(stream);
                }
            }
            catch (IOException)
            {
                // One server trying to read while the other one is still
                // creating the cache.
                return(false);
            }
            catch (TypeInitializationException)
            {
                // Hotfix for issue #20 in Aura

                this.Warnings.Add(new DatabaseWarningException("MsgPack failed to deserialize cache. " +
                                                               "This is usually caused by an incorrect version of the MsgPack library. " +
                                                               "Please download and compile the latest version of MsgPack (https://github.com/msgpack/msgpack-cli), " +
                                                               "then place the generated dll in Aura's Lib folder. Lastly, recompile Aura."));

                return(false);
            }

            return(true);
        }
Exemplo n.º 6
0
        public override Task <object> ReadFromStreamAsync(Type type, Stream stream, HttpContent content, IFormatterLogger formatterLogger)
        {
            var tcs = new TaskCompletionSource <object>();

            try
            {
                var    serializer = MessagePackSerializer.Create(type);
                object result;
                using (var unpacker = Unpacker.Create(stream))
                {
                    unpacker.Read();
                    result = serializer.UnpackFrom(unpacker);
                }

                tcs.SetResult(result);
            }
            catch (Exception e)
            {
                if (formatterLogger == null)
                {
                    throw;
                }
                formatterLogger.LogError(String.Empty, e.Message);
                tcs.SetResult(GetDefaultValueForType(type));
            }

            return(tcs.Task);
        }
        /// <summary>
        /// </summary>
        /// <param name="fname">
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <typeparam name="TU">
        /// </typeparam>
        /// <returns>
        /// </returns>
        public static Dictionary <T, TU> UncompressData <T, TU>(string fname)
        {
            var          tempList     = new Dictionary <T, TU>();
            Stream       fileStream   = new FileStream(fname, FileMode.Open);
            MemoryStream memoryStream = new MemoryStream(20 * 1024 * 1024);

            ZOutputStream zOutputStream = new ZOutputStream(memoryStream);

            CopyStream(fileStream, zOutputStream, "deflated");

            memoryStream.Seek(0, SeekOrigin.Begin);
            BinaryReader binaryReader  = new BinaryReader(memoryStream);
            byte         versionlength = (byte)memoryStream.ReadByte();

            char[] version = new char[versionlength];
            version = binaryReader.ReadChars(versionlength);

            // TODO: Check version and print a warning if not same as config.xml's
            MessagePackSerializer <Dictionary <T, TU> > messagePackSerializer =
                MessagePackSerializer.Create <Dictionary <T, TU> >();

            var buffer = new byte[4];

            memoryStream.Read(buffer, 0, 4);

            memoryStream.Read(buffer, 0, 4);

            return(messagePackSerializer.Unpack(memoryStream));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Cache all item templates
        /// </summary>
        /// <param name="fname">
        /// </param>
        /// <returns>
        /// number of cached items
        /// </returns>
        public static int CacheAllNanos(string fname)
        {
            Contract.Requires(!string.IsNullOrEmpty(fname));
            DateTime _now = DateTime.Now;

            NanoList = new Dictionary <int, NanoFormula>();
            Stream       sf = new FileStream(fname, FileMode.Open);
            MemoryStream ms = new MemoryStream();

            ZOutputStream sm = new ZOutputStream(ms);

            CopyStream(sf, sm);

            ms.Seek(0, SeekOrigin.Begin);
            BinaryReader br            = new BinaryReader(ms);
            byte         versionlength = (byte)ms.ReadByte();

            char[] version = new char[versionlength];
            version = br.ReadChars(versionlength);

            // TODO: Check version and print a warning if not same as config.xml's
            Console.WriteLine("Reading Nanos (" + new string(version) + "):");

            MessagePackSerializer <List <NanoFormula> > bf = MessagePackSerializer.Create <List <NanoFormula> >();

            byte[] buffer = new byte[4];
            ms.Read(buffer, 0, 4);
            int packaged = BitConverter.ToInt32(buffer, 0);

            while (true)
            {
                List <NanoFormula> templist;
                try
                {
                    templist = bf.Unpack(ms);
                    foreach (NanoFormula nf in templist)
                    {
                        NanoList.Add(nf.ID, nf);
                    }

                    if (templist.Count != packaged)
                    {
                        break;
                    }
                }
                catch (Exception)
                {
                    throw;
                }

                Console.Write(
                    "Loaded {0} nanos in {1}\r",
                    new object[] { NanoList.Count, new DateTime((DateTime.Now - _now).Ticks).ToString("mm:ss.ff") });
            }

            GC.Collect();
            return(NanoList.Count);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Cache all item templates
        /// </summary>
        /// <param name="fname">
        /// File to load from
        /// </param>
        /// <returns>
        /// Number of cached items
        /// </returns>
        public static int CacheAllItems(string fname)
        {
            Contract.Requires(!string.IsNullOrEmpty(fname));
            DateTime _now = DateTime.UtcNow;

            ItemList = new Dictionary <int, ItemTemplate>();
            Stream       fileStream   = new FileStream(fname, FileMode.Open);
            MemoryStream memoryStream = new MemoryStream();

            ZOutputStream zOutputStream = new ZOutputStream(memoryStream);

            CopyStream(fileStream, zOutputStream);

            memoryStream.Seek(0, SeekOrigin.Begin);
            BinaryReader binaryReader  = new BinaryReader(memoryStream);
            byte         versionlength = (byte)memoryStream.ReadByte();

            char[] version = new char[versionlength];
            version = binaryReader.ReadChars(versionlength);

            // TODO: Check version and print a warning if not same as config.xml's
            MessagePackSerializer <List <ItemTemplate> > messagePackSerializer =
                MessagePackSerializer.Create <List <ItemTemplate> >();

            var buffer = new byte[4];

            memoryStream.Read(buffer, 0, 4);
            int packaged = BitConverter.ToInt32(buffer, 0);

            Console.WriteLine("Reading Items (" + new string(version) + "):");
            while (true)
            {
                try
                {
                    List <ItemTemplate> templates = messagePackSerializer.Unpack(memoryStream);
                    foreach (ItemTemplate template in templates)
                    {
                        ItemList.Add(template.ID, template);
                    }

                    if (templates.Count != packaged)
                    {
                        break;
                    }
                }
                catch (Exception)
                {
                    throw;
                }

                Console.Write(
                    "Loaded {0} items in {1}\r",
                    new object[] { ItemList.Count, new DateTime((DateTime.UtcNow - _now).Ticks).ToString("mm:ss.ff") });
            }

            GC.Collect();
            return(ItemList.Count);
        }
Exemplo n.º 10
0
        /// <summary>
        /// </summary>
        /// <param name="filename">
        /// </param>
        /// <param name="version">
        /// </param>
        /// <param name="dataList">
        /// </param>
        /// <param name="packCount">
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <exception cref="Exception">
        /// </exception>
        public static void CompressData <T>(string filename, string version, List <T> dataList, int packCount = 500)
        {
            Console.WriteLine("Compressing " + typeof(T).Name + "s");

            if (packCount == 0)
            {
                throw new Exception("Dont use 0 as packCount!!");
            }

            Stream fileStream   = new FileStream(filename, FileMode.Create);
            var    zipStream    = new ZOutputStream(fileStream, zlibConst.Z_BEST_COMPRESSION);
            var    bufferStream = new MemoryStream(20 * 1024 * 1024);

            byte[] versionbuffer = Encoding.ASCII.GetBytes(version);
            bufferStream.WriteByte((byte)versionbuffer.Length);
            bufferStream.Write(versionbuffer, 0, versionbuffer.Length);

            byte[] buffer = BitConverter.GetBytes(packCount);
            bufferStream.Write(buffer, 0, buffer.Length);
            int tempCapacity = dataList.Count;

            buffer = BitConverter.GetBytes(tempCapacity);
            bufferStream.Write(buffer, 0, buffer.Length);
            MessagePackSerializer <List <T> > bf = MessagePackSerializer.Create <List <T> >();

            int      counter  = 0;
            int      maxCount = dataList.Count;
            List <T> tempList = new List <T>(tempCapacity);

            while (counter < maxCount)
            {
                tempList.Add(dataList[counter]);
                counter++;

                if (counter % packCount == 0)
                {
                    bf.Pack(bufferStream, tempList);
                    bufferStream.Flush();
                    tempList.Clear();
                }
            }

            if (tempList.Count > 0)
            {
                bf.Pack(bufferStream, tempList);
                bufferStream.Flush();
            }

            // bf.Pack(bufferStream, dataList);

            Console.WriteLine("100% serialized");
            bufferStream.Seek(0, SeekOrigin.Begin);
            CopyStream(bufferStream, zipStream);
            bufferStream.Close();
            zipStream.Close();
            fileStream.Close();
        }
Exemplo n.º 11
0
        public override T Deserialize(dynamic bytes)
        {
            var serializer = MessagePackSerializer.Create <T>();

            using (var byteStream = new MemoryStream(bytes))
            {
                return(serializer.Unpack(byteStream));
            }
        }
Exemplo n.º 12
0
        protected sealed override Func <ServerRequestContext, ServerResponseContext, Task> Dispatch(string methodName)
        {
            // Ignore methodName
            return
                ((requestContext, responseContext) =>
            {
                var argumentsUnpacker = requestContext.ArgumentsUnpacker;
                argumentsUnpacker.Read();
                MessagePackObject[] args = MessagePackSerializer.Create <MessagePackObject[]>(_serializationContext).UnpackFrom(argumentsUnpacker);
                var messageId = requestContext.MessageId;

                return
                Task.Factory.StartNew(
                    () =>
                {
                    MessagePackObject returnValue;

                    try
                    {
                        this.BeginOperation();
                        try
                        {
                            returnValue = this._dispatch(methodName, messageId, args);
                        }
                        catch (ThreadAbortException ex)
                        {
                            this.HandleThreadAbortException(ex);
                            returnValue = MessagePackObject.Nil;
                        }
                        finally
                        {
                            this.EndOperation();
                        }
                    }
                    catch (Exception exception)
                    {
                        if (responseContext != null)
                        {
                            base.SetException(responseContext, methodName, exception);
                        }
                        else
                        {
                            // notification
                            InvocationHelper.HandleInvocationException(requestContext.SessionId, MessageType.Notification, requestContext.MessageId, requestContext.MethodName, exception, this.IsDebugMode);
                        }
                        return;
                    }

                    if (responseContext != null)
                    {
                        base.SetReturnValue(responseContext, returnValue);
                    }
                }
                    );
            });
        }
Exemplo n.º 13
0
        /// <summary>
        /// </summary>
        /// <param name="client">
        /// </param>
        /// <param name="dataBytes">
        /// </param>
        private void ISComV2ServerDataReceived(object sender, OnDataReceivedArgs e)
        {
            if (this.DataReceived != null)
            {
                MessagePackSerializer <DynamicMessage> serializer = MessagePackSerializer.Create <DynamicMessage>();
                DynamicMessage result = serializer.UnpackSingleObject(e.dataBytes);

                this.DataReceived(sender, result);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// </summary>
        /// <param name="client">
        /// </param>
        /// <param name="dataBytes">
        /// </param>
        private void ISComV2ServerDataReceived(ISComV2ClientHandler client, byte[] dataBytes)
        {
            if (this.DataReceived != null)
            {
                MessagePackSerializer <DynamicMessage> serializer = MessagePackSerializer.Create <DynamicMessage>();
                DynamicMessage result = serializer.UnpackSingleObject(dataBytes);

                this.DataReceived(client, result);
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// </summary>
        /// <returns>
        /// </returns>
        public string Serialize()
        {
            MessagePackSerializer <Functions> toByte = MessagePackSerializer.Create <Functions>();
            var ms = new MemoryStream();

            toByte.Pack(ms, this);

            ms.Position = 0;
            return(BitConverter.ToString(ms.ToArray()).Replace("-", string.Empty));
        }
Exemplo n.º 16
0
        /// <summary>
        /// </summary>
        /// <param name="dataBytes">
        /// </param>
        private void clientBase_ReceivedData(byte[] dataBytes)
        {
            MessagePackSerializer <DynamicMessage> serializer = MessagePackSerializer.Create <DynamicMessage>();
            DynamicMessage tmp = serializer.UnpackSingleObject(dataBytes);

            // Is the handler set?
            if (this.OnReceiveData != null)
            {
                this.OnReceiveData(tmp);
            }
        }
Exemplo n.º 17
0
        public override dynamic Serialize(object thisObj)
        {
            var serializer = MessagePackSerializer.Create <T>();

            using (var ms = new MemoryStream())
            {
                serializer.Pack(ms, (T)thisObj);
                SerBytes = ms.ToArray();
                return(SerBytes);
            }
        }
Exemplo n.º 18
0
        private T makeHTTPGETMsgPackRequest <T>(String url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "GET";
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                var serializer = MessagePackSerializer.Create <T>();
                return(serializer.Unpack(new BufferedStream(response.GetResponseStream())));
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// </summary>
        /// <param name="dataObject">
        /// </param>
        public void Send(DynamicMessage dataObject)
        {
            MessagePackSerializer <object> serializer = MessagePackSerializer.Create <object>();

            byte[] data   = serializer.PackSingleObject(dataObject);
            byte[] header = new byte[8];
            BitConverter.GetBytes(0x00ff55aa).CopyTo(header, 0);
            BitConverter.GetBytes(data.Length).CopyTo(header, 4);
            this.Send(header);
            this.Send(data);
        }
Exemplo n.º 20
0
        /// <summary>
        /// </summary>
        /// <param name="dataBytes">
        /// </param>
        private void ClientBaseReceivedData(object sender, OnDataReceivedArgs e)
        {
            MessagePackSerializer <DynamicMessage> serializer = MessagePackSerializer.Create <DynamicMessage>();
            DynamicMessage tmp = serializer.UnpackSingleObject(e.dataBytes);

            // Is the handler set?
            if (this.OnReceiveData != null)
            {
                this.OnReceiveData(this, tmp);
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// </summary>
        /// <param name="dataObject">
        /// </param>
        public void Send(DynamicMessage dataObject)
        {
            MessagePackSerializer <object> serializer = MessagePackSerializer.Create <object>();

            byte[] data      = serializer.PackSingleObject(dataObject);
            byte[] finalData = new byte[8 + data.Length];
            BitConverter.GetBytes(0x00ff55aa).CopyTo(finalData, 0);
            BitConverter.GetBytes(data.Length).CopyTo(finalData, 4);
            Array.Copy(data, 0, finalData, 8, data.Length);
            this.clientBase.Send(finalData);
        }
Exemplo n.º 22
0
        public static byte[] SerializeData <T>(List <T> dataList)
        {
            byte[] temp = null;
            MessagePackSerializer <List <T> > bf = MessagePackSerializer.Create <List <T> >();

            using (MemoryStream ms = new MemoryStream())
            {
                bf.Pack(ms, dataList);
                temp = ms.GetBuffer();
            }
            return(temp);
        }
        public void TestSerialize_OnNotNullResult_ReturnValueDataIsSet_ErrorDataIsNil()
        {
            TestCore((target, transport) =>
            {
                target.MessageId = 123;
                target.Serialize("Test", RpcErrorMessage.Success, MessagePackSerializer.Create <string>());

                Assert.That(Unpacking.UnpackObject(target.GetReturnValueData()).Value.Equals("Test"));
                Assert.That(Unpacking.UnpackObject(target.GetErrorData()).Value.Equals(MessagePackObject.Nil));
            }
                     );
        }
Exemplo n.º 24
0
        /// <summary>
        /// </summary>
        /// <param name="arg">
        /// </param>
        public void SendData(OnMessageArgs arg)
        {
            lock (this.streamLockWrite)
            {
                MessagePackSerializer <OnMessageArgs> messagePackSerializer =
                    MessagePackSerializer.Create <OnMessageArgs>();

                byte[]        buffer    = messagePackSerializer.PackSingleObject(arg);
                NetworkStream serStream = this.clientSocket.GetStream();
                serStream.Write(buffer, 0, buffer.Length);
                serStream.Flush();
            }
        }
Exemplo n.º 25
0
 public static void Serialize(IRequestContext requestContext, object dto, Stream outputStream)
 {
     if (dto == null) return;
     try
     {
         var serializer = MessagePackSerializer.Create(dto.GetType());
         serializer.PackTo(Packer.Create(outputStream), dto);
     }
     catch (Exception ex)
     {
         HandleException(ex, dto.GetType());
     }
 }
Exemplo n.º 26
0
 /// <summary>Deserialize from stream.</summary>
 ///
 /// <typeparam name="T">Generic type parameter.</typeparam>
 /// <param name="stream">The stream.</param>
 ///
 /// <returns>A T.</returns>
 public override T DeserializeFromStream <T>(Stream stream)
 {
     try
     {
         var serializer = MessagePackSerializer.Create <T>();
         var obj        = serializer.Unpack(stream);
         return(obj);
     }
     catch (Exception ex)
     {
         return((T)MsgPackFormat.HandleException(ex, typeof(T)));
     }
 }
Exemplo n.º 27
0
        internal void Deserialize()
        {
            MessagePackSerializer <List <T> > messagePackSerializer = MessagePackSerializer.Create <List <T> >();
            ZlibStream   zs = new ZlibStream(this.Stream, CompressionMode.Decompress);
            MemoryStream ms = new MemoryStream();

            zs.CopyTo(ms);
            zs.Flush();
            ms.Flush();
            ms.Position    = 0;
            this.DataSlice = messagePackSerializer.Unpack(ms);
            ms.Close();
        }
Exemplo n.º 28
0
 public static List <T> DeserializeData <T>(byte[] data)
 {
     if (data.Length > 0)
     {
         using (MemoryStream ms = new MemoryStream(data))
         {
             ms.Position = 0;
             MessagePackSerializer <List <T> > bf = MessagePackSerializer.Create <List <T> >();
             return(bf.Unpack(ms));
         }
     }
     return(new List <T>());
 }
Exemplo n.º 29
0
        public static object BytesToObj(Type type, byte[] data)
        {
            if (data == null || data.Length == 0)
            {
                return(null);
            }

            IMessagePackSerializer ser    = serDic.GetOrAdd(type, MessagePackSerializer.Create(type));
            MemoryStream           stream = new MemoryStream(data);
            Unpacker up = Unpacker.Create(stream);

            up.Read();
            return(ser.UnpackFrom(up));
        }
Exemplo n.º 30
0
        public void Can_serialize_email_dto_generic()
        {
            using (var ms = new MemoryStream())
            {
                var serializer = MessagePackSerializer.Create<MsgPackEmail>();
                serializer.Pack(ms, request);

                ms.Position = 0;

                var response = serializer.Unpack(ms);

                Assert.That(response.Equals(request));
            }
        }