Example #1
0
        private void RetryUsingNewSocket()
        {
            // Wait until making a new request, disposing or error throwing is in progress
            lock (_syncObject)
            {
                try
                {
                    CloseSocket();

                    if (_asyncResults.Count() == 0)
                    {
                        Logger.Debug("No pending requests.");
                    }
                    else
                    {
                        _isReceivingRetry = true;
                        CreateSocketAndConnect();

                        Logger.Debug("Rerun all requests.");

                        foreach (ulong key in _asyncResults.GetKeys())
                        {
                            BeginSend(_asyncResults.GetValue(key));
                        }
                    }
                }
                catch (Exception ex)
                {
                    SetError(ex, "Retrying with a new socket failed.");
                }
            }
        }
        /// <summary>
        /// Returns asyncResults according to response payload.
        /// If aggregation response payload then return asyncResult by request id.
        /// If error payload then return all asyncResults.
        /// If config response payload the return all asyncResults created by config requests.
        /// </summary>
        /// <param name="payloadInfo">Response payload info</param>
        /// <returns></returns>
        private List <TcpKsiServiceAsyncResult> GetAsyncResults(KsiServiceResponsePayloadInfo payloadInfo)
        {
            List <TcpKsiServiceAsyncResult> list = new List <TcpKsiServiceAsyncResult>();

            ulong[] keys = _asyncResults.GetKeys();

            switch (payloadInfo.ResponsePayloadType)
            {
            case KsiServiceResponsePayloadType.Aggregation:
            case KsiServiceResponsePayloadType.Extending:
                foreach (ulong key in keys)
                {
                    if (key == payloadInfo.RequestId)
                    {
                        TcpKsiServiceAsyncResult asyncResult = _asyncResults.GetValue(key);

                        if (asyncResult == null)
                        {
                            continue;
                        }

                        list.Add(asyncResult);
                    }
                }
                break;

            case KsiServiceResponsePayloadType.AggregatorConfig:
                // return all async results of all aggregator configuration requests
                foreach (ulong key in keys)
                {
                    TcpKsiServiceAsyncResult asyncResult = _asyncResults.GetValue(key);

                    if (asyncResult?.ServiceRequestType == KsiServiceRequestType.AggregatorConfig)
                    {
                        list.Add(asyncResult);
                    }
                }
                break;

            case KsiServiceResponsePayloadType.ExtenderConfig:
                // return all async results of all extender configuration requests
                foreach (ulong key in keys)
                {
                    TcpKsiServiceAsyncResult asyncResult = _asyncResults.GetValue(key);

                    if (asyncResult?.ServiceRequestType == KsiServiceRequestType.ExtenderConfig)
                    {
                        list.Add(asyncResult);
                    }
                }
                break;

            case KsiServiceResponsePayloadType.Error:
                foreach (ulong key in keys)
                {
                    TcpKsiServiceAsyncResult asyncResult = _asyncResults.GetValue(key);

                    if (asyncResult != null)
                    {
                        list.Add(asyncResult);
                    }
                }
                break;

            default:
                throw new KsiServiceProtocolException("Unhandled payload type.");
            }

            return(list);
        }