Esempio n. 1
0
        public static void FromStream <TItemType>(Stream stream, DataHandler <TItemType> dataHandler,
                                                  ExceptionHandler exceptionHandler)
        {
            if (dataHandler == null)
            {
                throw new ArgumentNullException(nameof(dataHandler));
            }
            if (exceptionHandler == null)
            {
                throw new ArgumentNullException(nameof(exceptionHandler));
            }

            //BufferedStream bufferedStream = new BufferedStream(stream);
            var reader = new BinaryReader(stream);


            var items = reader.ReadInt32();

            for (var i = 0; i < items; i++)
            {
                var useProtocolBuffers = reader.ReadBoolean();
                var useCompression     = reader.ReadBoolean();
                reader.ReadDouble(); // the rank is not used in this case
                var dataSize = reader.ReadInt32();

                var data = reader.ReadBytes(dataSize);

                using (var memStream = new MemoryStream(data))
                {
                    try
                    {
                        var mode = SerializationMode.Json;
                        if (useProtocolBuffers)
                        {
                            mode = SerializationMode.ProtocolBuffers;
                        }

                        var    deserializationFailure = false;
                        object result;
                        try
                        {
                            result = SerializationHelper.ObjectFromStream <TItemType>(memStream, mode, useCompression);
                            dataHandler((TItemType)result, i + 1, items);
                        }
                        catch (Exception)
                        {
                            deserializationFailure = true;
                        }

                        if (deserializationFailure)
                        {
                            memStream.Seek(0, SeekOrigin.Begin);
                            result = SerializationHelper.ObjectFromStream <ExceptionResponse>(memStream, mode,
                                                                                              useCompression);

                            if (result != null)
                            {
                                exceptionHandler((ExceptionResponse)result);
                            }
                            else
                            {
                                var message = $"Received an unknown item type while expecting {typeof(TItemType)}";
                                throw new StreamingException(message);
                            }
                        }
                    }
                    catch (IOException ex)
                    {
                        var exResponse = new ExceptionResponse(ex);
                        exceptionHandler(exResponse);
                    }
                    catch (SerializationException ex)
                    {
                        var exResponse = new ExceptionResponse(ex);
                        exceptionHandler(exResponse);
                    }
                }
            }
        }