Example #1
0
        private void CloseWithError(FIXContext context, MessageFIX4_4 packetIn, string textMessage)
        {
            Message   message    = context.LocalSocket.MessageFactory.Create();
            var       fixFactory = new FIXFactory4_4(1, fixSender, packetIn.Sender);
            var       fixMsg     = (FIXMessage4_4)fixFactory.Create();
            TimeStamp timeStamp  = TimeStamp.UtcNow;

            fixMsg.SetAccount(packetIn.Account);
            fixMsg.SetText(textMessage);
            fixMsg.AddHeader("j");
            string errorMessage = fixMsg.ToString();

            message.DataOut.Write(errorMessage.ToCharArray());
            long end = Factory.Parallel.TickCount + 2000;

            if (debug)
            {
                log.Debug("Writing Error Message: " + textMessage);
            }
            while (!context.LocalSocket.TrySendMessage(message))
            {
                if (Factory.Parallel.TickCount > end)
                {
                    throw new ApplicationException("Timeout while sending an order.");
                }
                Factory.Parallel.Yield();
            }
            throw new FilterException();
        }
Example #2
0
        private void FIXLogin(PacketFIX4_4 packet)
        {
            if (fixState != ServerState.Startup)
            {
                CloseWithFixError(packet, "Invalid login request. Already logged in.");
            }
            fixState = ServerState.LoggedIn;
            var writePacket = fixSocket.CreatePacket();

            target     = packet.Target;
            sender     = packet.Sender;
            FixFactory = new FIXFactory4_4(1, packet.Target, packet.Sender);
            var mbtMsg = (FIXMessage4_4)FixFactory.Create();

            mbtMsg.SetEncryption(0);
            mbtMsg.SetHeartBeatInterval(30);
            mbtMsg.AddHeader("A");
            string login = mbtMsg.ToString();

            writePacket.DataOut.Write(login.ToCharArray());
            fixPacketQueue.Enqueue(writePacket);

            if (debug)
            {
                log.Debug("Sending login response: " + login);
            }
        }
Example #3
0
        public override Yield OnLogin()
        {
            if (debug)
            {
                log.Debug("Login()");
            }

            if (lastLoginTry + loginRetryTime > Factory.Parallel.TickCount)
            {
                return(Yield.NoWork.Repeat);
            }

            lastLoginTry = Factory.Parallel.TickCount;

            FixFactory = new FIXFactory4_4(1, UserName, fixDestination);
            var mbtMsg = FixFactory.Create();

            mbtMsg.SetEncryption(0);
            mbtMsg.SetHeartBeatInterval(30);
            mbtMsg.ResetSequence();
            mbtMsg.SetEncoding("554_H1");
            mbtMsg.SetPassword(Password);
            mbtMsg.AddHeader("A");
            if (debug)
            {
                log.Debug("Login message: \n" + mbtMsg);
            }
            SendMessage(mbtMsg);

            var    end = Factory.Parallel.TickCount + 15 * 1000;
            Packet packet;

            while (!Socket.TryGetPacket(out packet))
            {
                if (IsInterrupted)
                {
                    return(Yield.NoWork.Repeat);
                }
                Factory.Parallel.Yield();
                if (Factory.Parallel.TickCount > end)
                {
                    FailLogin(mbtMsg.ToString());
                }
            }

            if (debug)
            {
                log.Debug("Received FIX message: " + packet);
            }
            if (!VerifyLogin(packet))
            {
                RegenerateSocket();
                return(Yield.DidWork.Repeat);
            }

            StartRecovery();

            return(Yield.DidWork.Repeat);
        }
Example #4
0
        public unsafe void ConnectToFIXFails()
        {
            string addrStr  = "216.52.236.112";
            ushort port     = 5679;
            string password = "******";
            string userName = "******";

            using (Selector selector = Factory.Provider.Selector(OnException))
                using (Socket socket = Factory.Provider.Socket("TestSocket")) {
                    socket.PacketFactory = new PacketFactoryFIX4_4();
                    selector.Start();
                    selector.OnDisconnect = OnDisconnect;
                    socket.SetBlocking(true);
                    socket.Connect(addrStr, port);
                    socket.SetBlocking(false);
                    selector.AddReader(socket);
                    selector.AddWriter(socket);

                    Packet packet       = socket.CreatePacket();
                    string hashPassword = MBTQuotesProvider.Hash(password);

                    var fixFactory = new FIXFactory4_4(1, userName, destination);
                    var mbtMsg     = (FIXMessage4_4)fixFactory.Create();
                    mbtMsg.SetEncryption(0);
                    mbtMsg.SetHeartBeatInterval(30);
                    mbtMsg.ResetSequence();
                    mbtMsg.SetEncoding("554_H1");
                    mbtMsg.SetPassword(password);
                    mbtMsg.AddHeader("A");

                    string login = mbtMsg.ToString();
                    packet.DataOut.Write(login.ToCharArray());
                    log.Info("Login message: \n" + packet);
                    while (!socket.TrySendPacket(packet))
                    {
                        Factory.Parallel.Yield();
                    }

                    long end = Factory.Parallel.TickCount + 5000;
                    while (!socket.TryGetPacket(out packet))
                    {
                        if (Factory.Parallel.TickCount > end)
                        {
                            // If it times out here. Then return with success
                            // for the test.
                            return;
                        }
                        Factory.Parallel.Yield();
                    }
                    Assert.Fail("Should have received login timed out response.");
                }
        }
Example #5
0
        public unsafe void ConnectToFIX()
        {
            string addrStr = "216.52.236.112";
            ushort port    = 5679;
            // Forex
//			string password = "******";
//			string userName = "******";
            // Equity
            string password = "******";
            string userName = "******";

            using (var filter = new FIXPretradeFilter(addrStr, port))
                using (Socket socket = Factory.Provider.Socket("TestSocket", "127.0.0.1", filter.LocalPort))
                {
                    socket.MessageFactory = new MessageFactoryFix44();
                    socket.SetBlocking(true);
                    socket.Connect();
                    socket.SetBlocking(false);

                    Message message      = socket.MessageFactory.Create();
                    string  hashPassword = MBTQuotesProvider.Hash(password);

                    var fixFactory = new FIXFactory4_4(1, userName, destination);
                    var mbtMsg     = (FIXMessage4_4)fixFactory.Create();
                    mbtMsg.SetEncryption(0);
                    mbtMsg.SetHeartBeatInterval(30);
                    mbtMsg.ResetSequence();
                    mbtMsg.SetEncoding("554_H1");
                    mbtMsg.SetPassword(password);
                    mbtMsg.AddHeader("A");

                    string login = mbtMsg.ToString();
                    message.DataOut.Write(login.ToCharArray());
                    log.Info("Login message: \n" + message);
                    while (!socket.TrySendMessage(message))
                    {
                        Factory.Parallel.Yield();
                    }

                    long end = Factory.Parallel.TickCount + 5000;
                    while (!socket.TryGetMessage(out message))
                    {
                        if (Factory.Parallel.TickCount > end)
                        {
                            Assert.Fail("Login Timed Out.");
                        }
                        Factory.Parallel.Yield();
                    }
                    MessageFIX4_4 packetFIX = (MessageFIX4_4)message;

//				packetFIX.ReadMessage();
                    Assert.AreEqual("FIX.4.4", packetFIX.Version);
                    Assert.AreEqual("A", packetFIX.MessageType);
                    Assert.AreEqual("MBT", packetFIX.Sender);
                    Assert.AreEqual(userName, packetFIX.Target);
                    Assert.AreEqual(1, packetFIX.Sequence);
                    Assert.NotNull(packetFIX.TimeStamp);
                    Assert.AreEqual("0", packetFIX.Encryption);
                    Assert.AreEqual(30, packetFIX.HeartBeatInterval);
                    socket.MessageFactory.Release(message);
                }
        }