Example #1
0
        void EndProcessRequest(IAsyncResult result)
        {
#if DEBUG
            int workerAvailable         = threadPool.AvailableThreads;
            int completionPortAvailable = threadPool.MaxThreads;
            int activeRequests          = completionPortAvailable - workerAvailable;

            Debug.WriteLine(string.Format("EndProcessRequest: Pooled:{0} Id:{1} out of {2}/{3} ({4} Requests Active, {5} Requests Pending)", Thread.CurrentThread.IsThreadPoolThread, Thread.CurrentThread.ManagedThreadId, workerAvailable, completionPortAvailable, activeRequests, threadPool.RequestQueueLength));
#endif

            BitCoinResult.End(result, this, "getwork");
        }
Example #2
0
        new public static void End(IAsyncResult result, object owner, string operationId)
        {
            BitCoinResult asyncResult = result as BitCoinResult;

            if (asyncResult == null)
            {
                throw new ArgumentException(
                          "Result passed represents an operation not supported " +
                          "by this framework.",
                          "result");
            }

            // Wait until operation has completed
            AsyncResultNoResult.End(result, owner, operationId);

            // Write the result to client (if above didn't throw)
            if (asyncResult.m_result != null && asyncResult.m_context.Response.IsClientConnected)
            {
                try
                {
                    var outgoingResponse = asyncResult.m_context.Response;

                    // write the response to stream
                    outgoingResponse.ContentEncoding = Encoding.UTF8;
                    outgoingResponse.ContentType     = asyncResult.m_result.ContentType;
                    outgoingResponse.StatusCode      = asyncResult.m_result.HttpStatusCode;

                    if (asyncResult.m_result.HttpStatusCode == 401)
                    {
                        outgoingResponse.AddHeader("WWW-Authenticate",
                                                   "BASIC Realm=TopBit API (http://api.topb.it)");
                    }

                    if (outgoingResponse.StatusCode == 200)
                    {
                        outgoingResponse.Write(asyncResult.m_result.ToJson());
                    }

                    outgoingResponse.End();
                }
                catch (Exception)
                {
                    // socket connection from mining client is interrupted.
                }
            }
        }
Example #3
0
        /// <remarks>
        /// 以下设计将保证 bitcoin 网关处理尽可能多的请求
        /// getwork:
        ///     远程调用,使用异步处理
        /// submitwork:
        ///     当本地验证未通过则直接返回
        ///     当本地验证通过时使用远程调用,由于不需要考虑结果,仍然可以立即返回
        /// </remarks>
        IAsyncResult BeginProcessRequest(object sender, EventArgs e, AsyncCallback cb, object extraData)
        {
#if DEBUG
            int workerAvailable         = threadPool.AvailableThreads;
            int completionPortAvailable = threadPool.MaxThreads;
            int activeRequests          = completionPortAvailable - workerAvailable;

            Debug.WriteLine(string.Format("BeginProcessRequest: {0} {1} out of {2}/{3} ({4} Requests Active)", Thread.CurrentThread.IsThreadPoolThread, Thread.CurrentThread.ManagedThreadId, workerAvailable, completionPortAvailable, activeRequests));

            //attach the debugger if query string contains 'break=yes'
            BreakIfRequested();
#endif
            HttpContext context = ((HttpApplication)sender).Context;

            //  prepare async context
            var result = new BitCoinResult(cb, context, extraData, this, "getwork");

            try
            {
                var message = Message.Parse(context.Request);
                if (message != null && message.Method == "getwork")
                {
                    // api.topb.it only deals 'getwork' request
                    result.Process(threadPool, message);
                }
                else
                {
                    result.SetResult(UnknownMethod);
                    result.Complete(null, true);
                }
            }
            catch (Exception error)
            {
                logger.Fatal("BeginProcessRequest - " + error.Message, error);

#if DEBUG
                Debug.WriteLine(string.Format("Exception in BeginProcessRequest: {0}", error.Message));
#endif
                result.SetResult(InternalError);
                result.Complete(null, true);
            }

            //  ok, return it
            return(result);
        }