• Version 1.16.0
Show / Hide Table of Contents

Flat Message Sample Project

This sample demonstrates how to use the FlatMessage mode of the session.

© Onix Solutions

Source code


using System;
using System.Globalization;
using System.IO;
using System.Net;
using NLog.Extensions.Logging;
using OnixS.Fix;
using OnixS.Fix.Fix44;

namespace FlatMessageSample
{
    static class FlatMessageSample
    {
        private const ProtocolVersion fixVersion = ProtocolVersion.Fix44;

        static int Main()
        {
            try
            {
                var settings = new EngineSettings()
                {
                    LicenseStore = GetLicenseStoreFolder(),
                    LoggerProvider = new NLogLoggerProvider()
                };
                const int Port = 10450;
                settings.ListenPorts.Add(Port);

                Engine.Init(settings);

                Engine.Instance.Error += (object sender, EngineErrorEventArgs args) =>
                {
                    Console.WriteLine("Engine Error: " + args.ToString());
                };

                Engine.Instance.Warning += (object sender, EngineWarningEventArgs args) =>
                {
                    Console.WriteLine("Engine Warning: " + args.ToString());
                };

                var dictionary = fixVersion.ToDictionary();

                const string SenderCompID = "SellSide";
                const string TargetCompID = "BuySide";

                using var acceptor = new Session(SenderCompID, TargetCompID, dictionary)
                {
                    MessageMode = MessageMode.FlatMessage
                };

                acceptor.InboundApplicationMessage += OnInboundApplicationMessage;
                acceptor.Error += OnError;
                acceptor.Warning += OnWarning;

                acceptor.LogonAsAcceptor();

                using var initiator = new Session(TargetCompID, SenderCompID, dictionary)
                {
                    MessageMode = MessageMode.FlatMessage
                };

                initiator.LogonAsInitiator(IPAddress.Loopback, Port);

                FlatMessage order = CreateOrderMessage(dictionary);
                initiator.Send(order);

                initiator.Logout();
            }
            catch (Exception ex)
            {
                Log("Exception: " + ex);

                return 1;
            }
            finally
            {
                if (Engine.IsInitialized)
                {
                    Engine.Shutdown();
                }

                // From https://github.com/NLog/NLog/wiki/Tutorial:
                // NET Application running on Mono / Linux are required to stop threads / timers before entering application shutdown phase.
                // Failing to do this will cause unhandled exceptions and segmentation faults, and other unpredictable behavior.
                NLog.LogManager.Shutdown(); // Flush and close down internal threads and timers
            }

            return 0;
        }

        private static void OnInboundApplicationMessage(object sender, InboundMessageEventArgs e)
        {
            Console.WriteLine("Received application-level Flat Message:\n" + e.FlatMessage.ToString());
        }

        private static void OnError(object sender, SessionErrorEventArgs e)
        {
            Console.WriteLine($"{sender} reported error: {e.Description}");
        }

        private static void OnWarning(object sender, SessionWarningEventArgs e)
        {
            Console.WriteLine($"{sender} reported warning: {e.Description}");
        }

        private static FlatMessage CreateOrderMessage(IMessageInfoDictionary dictionary)
        {
            var order = new FlatMessage(dictionary.Version);
            order.Set(Tag.MsgType, MsgType.NewOrderSingle);

            order.Add(Tag.HandlInst, HandlInst.AutoExecPub)
                 .Add(Tag.ClOrdID, "Unique identifier for Order")
                 .Add(Tag.Symbol, "TSLA")
                 .Add(Tag.Side, Side.Buy)
                 .Add(Tag.OrderQty, 1000)
                 .Add(Tag.OrdType, OrdType.Market)
                 .Add(Tag.TransactTime, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss", CultureInfo.InvariantCulture));

            return order;
        }

        private static string GetLicenseStoreFolder()
        {
            string path = Path.Join(AppContext.BaseDirectory, "../../../../../license");

            if (Directory.Exists(path))
                return path;

            // expecting it run after dotnet publish using default paths
            return Path.Join(AppContext.BaseDirectory, "../../../../../../license");
        }

        private static void Log(string msg)
        {
            Console.Out.WriteLine(msg);
        }
    }
}

In this article
Back to top Copyright © Onix Solutions.
Generated by DocFX