Ejemplo n.º 1
0
        /// <summary>
        /// End TCP request
        /// </summary>
        /// <param name="ar">TCP KSI service async result</param>
        /// <returns></returns>
        protected byte[] EndRequest(IAsyncResult ar)
        {
            TcpKsiServiceAsyncResult asyncResult = ar as TcpKsiServiceAsyncResult;

            if (asyncResult == null)
            {
                throw new KsiServiceProtocolException("Invalid IAsyncResult.");
            }

            try
            {
                if (_isDisposed)
                {
                    throw new KsiServiceProtocolException("TCP KSI service protocol is disposed.");
                }

                if (asyncResult.IsDisposed)
                {
                    throw new KsiServiceProtocolException("Provided async result is already disposed. Possibly using the same async result twice when ending request.");
                }

                if (!asyncResult.IsCompleted)
                {
                    if (!asyncResult.AsyncWaitHandle.WaitOne(_requestTimeOut))
                    {
                        Logger.Debug("Request timed out. Waiting asyncResult.AsyncWaitHandle in EndRequest timed out.");
                        throw new KsiServiceProtocolException("Request timed out.");
                    }
                }

                if (asyncResult.HasError)
                {
                    Logger.Warn("{0} (request id: {1}){2}{3}", asyncResult.Error.Message, asyncResult.RequestId, Environment.NewLine, asyncResult.Error);
                    throw asyncResult.Error;
                }

                Logger.Debug("TCP service protocol returning {0} bytes (request id: {1}).", asyncResult.ResultStream.Length, asyncResult.RequestId);

                return(asyncResult.ResultStream.ToArray());
            }
            finally
            {
                asyncResult.Dispose();
            }
        }