/// <summary>
        /// Perform a login.
        /// </summary>
        public bool Login(string username, string password, string serviceUrl, string accountType, out string operationResultMessage)
        {
            _messageLoopOperator.Start();

            if (OperationalState != OperationalStateEnum.Initialized &&
                OperationalState != OperationalStateEnum.Initializing &&
                OperationalState != OperationalStateEnum.Constructed)
            {
                operationResultMessage = "Login already started.";
                return(false);
            }

            this._serviceUrl = serviceUrl;

            string operationResultMessageCopy = string.Empty;

            ChangeOperationalState(OperationalStateEnum.Initializing);

            object result = false;

            GeneralHelper.GenericReturnDelegate <bool> del = delegate()
            {
                if (_core == null)
                {
                    _core = new FXCore.CoreAutClass();
                    _desk = (FXCore.TradeDeskAut)_core.CreateTradeDesk("trader");
                }

                try
                {
                    _desk.Login(username, password, serviceUrl, accountType);

                    Managed_Subscribe();

                    SystemMonitor.Report("FXCM Service subscribed.");
                    ChangeOperationalState(OperationalStateEnum.Operational);
                }
                catch (Exception exception)
                {
                    operationResultMessageCopy = "Failed to log in [" + exception.Message + "].";
                    SystemMonitor.OperationError(operationResultMessageCopy);
                    ChangeOperationalState(OperationalStateEnum.NotOperational);
                }

                return(_desk.IsLoggedIn());
            };

            if (_messageLoopOperator.Invoke(del, TimeSpan.FromSeconds(180), out result) == false || (bool)result == false)
            {
                ChangeOperationalState(OperationalStateEnum.NotOperational);
                operationResultMessage = operationResultMessageCopy;
                return(false);
            }

            operationResultMessage = operationResultMessageCopy;
            return((bool)result);
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool Login(string username, string password)
        {
            bool result = false;

            if (OperationalState != OperationalStateEnum.Initialized &&
                OperationalState != OperationalStateEnum.Initializing &&
                OperationalState != OperationalStateEnum.Constructed)
            {
                return(false);
            }

            _messageLoopOperator.Start();

            _messageLoopOperator.Invoke(delegate()
            {
                try
                {
                    // *Never* subscribe to COM events of a property, always make sure to hold the object alive
                    // http://www.codeproject.com/Messages/2189754/Re-Losing-COM-events-handler-in-Csharp-client.aspx

                    if (_communicationManager == null)
                    {
                        // This is a slow blocking call, make sure to execute outside of lock.
                        MbtComMgr manager     = new MbtComMgr();
                        _communicationManager = manager;
                        _communicationManager.OnLogonSucceed += new IMbtComMgrEvents_OnLogonSucceedEventHandler(_communicationManager_OnLogonSucceed);
                    }

                    lock (this)
                    {
                        _quotes.Initialize(this, _communicationManager.Quotes);
                        _history.Initialize(_communicationManager.HistMgr);
                        _orders.Initialize(_adapter, this, _communicationManager);

                        _communicationManager.OnAlertAdded += new IMbtComMgrEvents_OnAlertAddedEventHandler(_communicationManager_OnAlertAdded);
                        _communicationManager.EnableSplash(false);
                    }

                    ChangeOperationalState(OperationalStateEnum.Initialized);
                    _communicationManager.EnableSplash(false);
                    _communicationManager.SilentMode = true;

                    result = _communicationManager.DoLogin(HostId, username, password, "");
                }
                catch (Exception ex)
                {
                    SystemMonitor.OperationError(ex.Message);
                    result = false;
                    throw;
                }
            }, TimeSpan.FromSeconds(25));

            return(result);
        }
        //volatile MBTradingQuote _quotes;
        //public MBTradingQuote Quotes
        //{
        //    get { return _quotes; }
        //}

        //volatile MBTradingHistory _history;
        //public MBTradingHistory History
        //{
        //    get { return _history; }
        //}

        //volatile MBTradingOrders _orders;
        ///// <summary>
        /////
        ///// </summary>
        //public MBTradingOrders Orders
        //{
        //    get { return _orders; }
        //}

        //MBTradingAdapter _adapter;

        /// <summary>
        /// Constructor.
        /// </summary>
        public MBTradingTestConnectionManager()
        {
            ChangeOperationalState(OperationalStateEnum.Constructed);

            _messageLoopOperator = new BackgroundMessageLoopOperator(false);
            //_quotes = new MBTradingQuote(_messageLoopOperator);
            //_history = new MBTradingHistory(_messageLoopOperator);
            //_orders = new MBTradingOrders(_messageLoopOperator);

            _messageLoopOperator.Start();
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        public FXCMConnectionManager(FXCMAdapter adapter)
        {
            ChangeOperationalState(OperationalStateEnum.Constructed);

            _subscriptionResponse = -1;
            _adapter             = adapter;
            _messageLoopOperator = new BackgroundMessageLoopOperator(false);
            _orders = new FXCMOrders(_messageLoopOperator);

            _messageLoopOperator.Start();
        }