Ejemplo n.º 1
0
 public static ETradeResponse <TResp> Create <TResp>(ETradeRequest <TResp> req, TResp responseData, Exception exception)
 {
     return(new ETradeResponse <TResp>(req, responseData)
     {
         Exception = exception
     });
 }
Ejemplo n.º 2
0
            protected virtual void Start()
            {
                if (IsStarted)
                {
                    return;
                }

                IsStarted  = true;
                IsStopping = false;
                _doneEvt.Reset();

                _lastLimitsUpdateTime = DateTime.MinValue;
                //_nextIntervalStartsAt = DateTime.MinValue;
                _requestsPerSecond = 0;

                CurrentAutoRequest = null;

                _recentRequests.Clear();

                UpdateLimitsIfNecessary();

                ThreadingHelper
                .Thread(() =>
                {
                    try
                    { ModuleThreadFunc(); }
                    catch (Exception e)
                    { Client.RaiseError(new ETradeException(LocalizedStrings.Str3362Params.Put(_name), e)); }
                    finally
                    { _doneEvt.Set(); }
                })
                .Background(true)
                .Name("etrade_" + _name)
                .Launch();
            }
Ejemplo n.º 3
0
 protected ETradeResponse(ETradeRequest req)
 {
     Request = req;
 }
Ejemplo n.º 4
0
            private void ModuleThreadFunc()
            {
                Client.AddDebugLog("Module '{0}' has started.", _name);
                CurrentAutoRequest = GetNextAutoRequest();

                while (!IsStopping)
                {
                    _wakeupEvt.WaitOne();

                    while (!IsStopping)
                    {
                        var now          = DateTime.UtcNow;
                        var removeBefore = now - TimeSpan.FromSeconds(.999d);
                        _recentRequests.RemoveAll(dt => dt < removeBefore);
                        var canDoRequestNow = _recentRequests.Count + 1 <= _requestsPerSecond;

                        if (CurrentAutoRequest == null)
                        {
                            CurrentAutoRequest = GetNextAutoRequest();
                        }

                        var request = !_userRequests.IsEmpty() ? _userRequests.Peek() : CurrentAutoRequest;

                        if (request == null)
                        {
                            break;
                        }

                        if (request.IsRequestRateLimited)
                        {
                            if (!canDoRequestNow)
                            {
                                StartWakeupTimer(_recentRequests.Count > 0 ? 1 - (now - _recentRequests[0]).TotalSeconds : 1);
                                break;
                            }

                            _recentRequests.Add(now);
                        }

                        Client.AddDebugLog("{0}: executing request: {1}", _name, request.ToString());

                        var response = request.ExecuteNextPart(Client);

                        if (response.Exception is ETradeUnauthorizedException)
                        {
                            Client.AddWarningLog("{0}: Response returned Unauthorized exception.", _name);
                            IsStopping = true;
                            Client.Connection.ReconnectAsync();
                            break;
                        }

                        ProcessResponse(response);

                        if (request.IsDone)
                        {
                            if (request == CurrentAutoRequest)
                            {
                                CurrentAutoRequest = GetNextAutoRequest();
                            }
                            else
                            {
                                _userRequests.Dequeue();
                            }

                            if (response.Exception == null)
                            {
                                UpdateLimitsIfNecessary();
                            }
                        }
                    }
                }
            }
Ejemplo n.º 5
0
 public void ExecuteUserRequest <TResp>(ETradeRequest <TResp> request, Action <ETradeResponse <TResp> > responseHandler)
 {
     request.ResponseHandler = responseHandler;
     _userRequests.Enqueue(request);
     _wakeupEvt.Set();
 }