Exemple #1
0
        private void WriteMemStreamToOutput()
        {
            if (memStream.Length > int.MaxValue)
            {
                throw new InvalidOperationException("Message size exceeded 2GB, this is not supported by Fudge.");
            }
            int memStreamLength = (int)memStream.Length;
            var reader          = new FudgeBinaryReader(memStream);
            int currentPos      = 0;

            for (int i = 0; i < sizePositions.Count; i++)
            {
                int sizePos = sizePositions[i];
                Debug.Assert(sizePos >= currentPos);

                // Copy up to the size
                currentPos += CopyStream(reader, outputWriter, sizePos - currentPos, copyBuffer);

                // Now read the size, which was written as four bytes
                int size = reader.ReadInt32();
                currentPos += 4;

                // And write it out at the correct length
                WriteVariableSize(outputWriter, size);
            }

            // Copy anything left
            CopyStream(reader, outputWriter, memStreamLength - currentPos, copyBuffer);
        }
Exemple #2
0
        protected static FudgeMsg CycleMessage(FudgeMsg msg, string filename) //throws IOException
        {
            Assembly     assembly        = Assembly.GetExecutingAssembly();
            Stream       stream          = assembly.GetManifestResourceStream("Fudge.Tests.Resources." + filename);
            BinaryReader referenceReader = new FudgeBinaryReader(stream);
            Stream       memoryStream    = new MemoryStream();
            // set the last parameter of the following line to true to see the full diff report between streams and not fail at the first difference.
            BinaryWriter bw = new StreamComparingBinaryNBOWriter(referenceReader, memoryStream, false);

            fudgeContext.Serialize(msg, bw);
            bw.Close();

            // Reload as closed above
            stream = assembly.GetManifestResourceStream("Fudge.Tests.Resources." + filename);
            FudgeMsg outputMsg = fudgeContext.Deserialize(stream).Message;      // Load the message from the resource rather than our output

            return(outputMsg);
        }
        private static int FudgeCycle(bool useNames, bool useOrdinals)
        {
            MemoryStream       outputStream = new MemoryStream();
            var                bw           = new FudgeBinaryWriter(outputStream);
            SmallFinancialTick tick         = new SmallFinancialTick();
            FudgeMsg           msg          = new FudgeMsg(fudgeContext);

            if (useNames && useOrdinals)
            {
                msg.Add("ask", (short)1, tick.Ask);
                msg.Add("askVolume", (short)2, tick.AskVolume);
                msg.Add("bid", (short)3, tick.Bid);
                msg.Add("bidVolume", (short)4, tick.BidVolume);
                msg.Add("ts", (short)5, tick.Timestamp);
            }
            else if (useNames)
            {
                msg.Add("ask", tick.Ask);
                msg.Add("askVolume", tick.AskVolume);
                msg.Add("bid", tick.Bid);
                msg.Add("bidVolume", tick.BidVolume);
                msg.Add("ts", tick.Timestamp);
            }
            else if (useOrdinals)
            {
                msg.Add(1, tick.Ask);
                msg.Add(2, tick.AskVolume);
                msg.Add(3, tick.Bid);
                msg.Add(4, tick.BidVolume);
                msg.Add(5, tick.Timestamp);
            }
            fudgeContext.Serialize(msg, bw);

            byte[] data = outputStream.ToArray();

            MemoryStream inputstream = new MemoryStream(data);
            var          br          = new FudgeBinaryReader(inputstream);

            msg = fudgeContext.Deserialize(inputstream).Message;

            tick = new SmallFinancialTick();
            if (useOrdinals)
            {
                tick.Ask       = msg.GetDouble(1).Value;
                tick.AskVolume = msg.GetDouble(2).Value;
                tick.Bid       = msg.GetDouble(3).Value;
                tick.BidVolume = msg.GetDouble(4).Value;
                tick.Timestamp = msg.GetLong(5).Value;
            }
            else if (useNames)
            {
                tick.Ask       = msg.GetDouble("ask").Value;
                tick.AskVolume = msg.GetDouble("askVolume").Value;
                tick.Bid       = msg.GetDouble("bid").Value;
                tick.BidVolume = msg.GetDouble("bidVolume").Value;
                tick.Timestamp = msg.GetLong("ts").Value;
            }
            else
            {
                throw new InvalidOperationException("Names or ordinals, pick at least one.");
            }
            return(data.Length);
        }