Example #1
0
        public AuctionFlow(ScrapBrowser browser, ScrapParameter param, bool login_mode)
            : base(browser, param, login_mode)
        {
            logger.Info("Action initialize");

            FlowMap.Add("Member/SignIn/LogOn", Login);
            FlowMap.Add("Home/Home", Home);
            FlowMap.Add("membership/MyInfo/MyInfoComp", Profile);
            FlowMap.Add("Escrow/Delivery/BuyDecision", BuyDecision);
            FlowMap.Add("Member/Settle/IacSettleDetail", LacSettleDetail);
            FlowMap.Add("Escrow/Delivery/GeneralDelivery", GeneralDelivery);
            FlowMap.Add("Escrow/Delivery/Sending", Sending);
            FlowMap.Add("Areas/Manual/SellerGuide", ScrapEnd);
            FlowMap.Add("Escrow/Claim/ReturnRequestManagement", ReturnRequestManagement);
            FlowMap.Add("Sell/Items/ItemsMng", ItemsMng);
            FlowMap.Add("Sell/Items/GetItemMngList", GetItemMngList);
            FlowMap.Add("Member/CustomerService/CSManagement", CSManagement);

            DownloadMap.Add("BuyDecisionExcel", BuyDecisionExcel);
            DownloadMap.Add("IacRemitListExcelDownload", LacRemitListExcelDownload);
            DownloadMap.Add("GeneralDeliveryExcel", GeneralDeliveryExcel);
            DownloadMap.Add("SendingExcel", SendingExcel);
            DownloadMap.Add("ExcelDownload", ExcelDownload);

            base.ReflectFlyweightKeys.Add(typeof(BuyDecisionExcel));
            base.ReflectFlyweightKeys.Add(typeof(LacRemitListExcel));
            base.ReflectFlyweightKeys.Add(typeof(GeneralDeliveryExcel));
            base.ReflectFlyweightKeys.Add(typeof(SendingExcel));
            base.ReflectFlyweightKeys.Add(typeof(ReturnRequest));
            base.ReflectFlyweightKeys.ForEach(type =>
            {
                ReflectFlyweight.Add(type, new List <FieldInfo>(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)));
            });
        }
Example #2
0
    public static void DownLoad(object param)
    {
        ThreadParam      tParam = param as ThreadParam;
        ManualResetEvent close  = tParam.close;
        AutoResetEvent   Wait   = tParam.waitfor;

        try
        {
            while (bContinue)
            {
                Wait.WaitOne();
                HttpRequest req    = null;
                string      strURI = null;
                while (bContinue)
                {
                    req    = null;
                    strURI = null;
                    lock (RequestMap)
                    {
                        if (RequestMap.Count == 0)
                        {
                            break;
                        }

                        bool bFindOrder = false;
                        foreach (KeyValuePair <string, HttpRequest> each in RequestMap)
                        {
                            if (req == null)
                            {
                                req = each.Value;
                            }
                            if (strURI == null)
                            {
                                strURI = each.Key;
                            }

                            if (each.Value != null)
                            {
                                if (each.Value.order != RequestStatus.Pause)
                                {
                                    req        = each.Value;
                                    strURI     = each.Key;
                                    bFindOrder = true;
                                    break;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }

                        if (strURI != null)
                        {
                            //may be is retry
                            lock (DownloadMap)
                            {
                                if (!DownloadMap.ContainsKey(strURI))
                                {
                                    DownloadMap.Add(strURI, req);
                                }
                            }
                            RequestMap.Remove(strURI);
                        }
                    }

                    Socket sClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                    //http1.1 download start
                    string strHost         = "";
                    string strPort         = "";
                    string strRelativePath = "";

                    if (!ParseURI(strURI, ref strHost, ref strPort, ref strRelativePath))
                    {
                        req.error = "404";
                        req.callback(ref req);
                        break;
                    }
                    else
                    {
                        EndPoint server = new IPEndPoint(IPAddress.Parse(strHost), System.Convert.ToInt32(strPort));
                        try
                        {
                            connectDone.Reset();
                            sClient.BeginConnect(server, new AsyncCallback(ConnectCallback), sClient);
                            connectDone.WaitOne(2000, false);
                            if (!sClient.Connected)
                            {
                                Log.LogInfo("connect time out");
                                //repush to download again
                                lock (RequestMap)
                                {
                                    if (!RequestMap.ContainsKey(strURI))
                                    {
                                        req.error = null;
                                        req.order = 0;
                                        RequestMap.Add(strURI, req);
                                    }
                                }
                                continue;
                            }
                        }
                        catch
                        {
                            req.error = "connect failed";
                            req.callback(ref req);

                            //repush to download again
                            lock (RequestMap)
                            {
                                if (!RequestMap.ContainsKey(strURI))
                                {
                                    req.error = null;
                                    req.order = 0;
                                    RequestMap.Add(strURI, req);
                                }
                            }
                            continue;
                        }

                        if (!sClient.Connected)
                        {
                            req.error = "connect failed";
                            req.callback(ref req);

                            //repush to download again
                            lock (RequestMap)
                            {
                                if (!RequestMap.ContainsKey(strURI))
                                {
                                    req.error = null;
                                    req.order = 0;
                                    RequestMap.Add(strURI, req);
                                }
                            }
                            continue;
                        }
                        string strSend = string.Format(strHttpVer, strRelativePath, strHost, strPort, req.cbstart);
                        Log.LogInfo("send packet:" + strSend);
                        byte[] bySend = System.Text.Encoding.UTF8.GetBytes(string.Format(strHttpVer, strRelativePath, strHost, strPort, req.cbstart));
                        sClient.Send(bySend);
                        int nByteRecved = 0;
                        int nNewLine    = 0;
                        //recv http head
                        MemoryStream ms         = new MemoryStream();
                        byte[]       byteRecved = new byte[1];
                        while (true)
                        {
                            if (!bContinue)
                            {
                                break;
                            }
                            try
                            {
                                nByteRecved = sClient.Receive(byteRecved, 1, 0);
                            }
                            catch (Exception exp)
                            {
                                break;
                            }
                            if (nByteRecved <= 0)
                            {
                                break;
                            }
                            ms.Write(byteRecved, 0, 1);
                            if (byteRecved[0] == '\n')
                            {
                                nNewLine++;
                                if (System.Text.Encoding.UTF8.GetString(ms.GetBuffer()).Contains("\r\n\r\n"))
                                {
                                    break;
                                }
                            }
                        }
                        if (!sClient.Connected || !bContinue)
                        {
                            req.error = "recv interrupt";
                            req.callback(ref req);
                            lock (RequestMap)
                            {
                                if (!RequestMap.ContainsKey(strURI))
                                {
                                    req.error = null;
                                    req.order = 0;
                                    RequestMap.Add(strURI, req);
                                }
                            }
                            continue;
                        }
                        nByteRecved = 0;
                        string strHead = System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
                        Log.LogInfo("http recv:" + strHead);
                        string strHeadLower = strHead.ToLower();
                        //check http1.1 return code
                        int      nReturnCode    = 0;
                        long     nContentLength = 0;
                        int      nRangeStart    = 0;
                        int      nRangeStop     = 0;
                        string[] strResponse    = new string[nNewLine];
                        char[]   split          = { '\n' };
                        strResponse = strHeadLower.Split(split);
                        for (int i = 0; i < strResponse.Length; ++i)
                        {
                            if (strResponse[i].Contains("http/"))
                            {
                                string strStatus = strResponse[i];
                                nReturnCode = System.Convert.ToInt32(strStatus.Substring(9, 3));
                                Log.LogInfo("http result:" + nReturnCode.ToString());
                            }
                            else if (strResponse[i].Contains("content-length:"))
                            {
                                string   strLength = strResponse[i];
                                string[] strSplit  = strLength.Split(new char[] { ' ' }, 2);
                                nContentLength = System.Convert.ToInt64(strSplit[1]);
                                if (req.cbstart == 0)
                                {
                                    req.cbsize = nContentLength;
                                    req.callback(ref req);
                                }
                            }
                            else if (strResponse[i].Contains("tranfer-encoding:chunked"))
                            {
                                Log.LogInfo("error !!! can not read chunked data");
                            }

                            if (nReturnCode != 0 && nContentLength != 0)
                            {
                                break;
                            }
                        }

                        ms.Close();
                        ms = null;

                        if (nReturnCode != 206 && nReturnCode != 200)
                        {
                            req.error = nReturnCode.ToString();
                            req.callback(ref req);
                            lock (RequestMap)
                            {
                                if (!RequestMap.ContainsKey(strURI))
                                {
                                    req.error = null;
                                    req.order = 0;
                                    RequestMap.Add(strURI, req);
                                }
                            }

                            sClient.Close();
                            continue;
                        }

                        FileStream fs = File.Open(req.strLocalPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
                        if (req.cbstart != 0)
                        {
                            fs.Seek(req.cbstart, SeekOrigin.Begin);
                        }

                        //calc kbps

                        //total recved / total time => kbps
                        long   nTotalRecved = req.cbstart;
                        int    nLoop        = 0;
                        byte[] memory       = new byte[10 * 1024];
                        while (true)
                        {
                            long nTickBegin = DateTime.Now.Ticks;
                            if (!bContinue)
                            {
                                break;
                            }
                            try
                            {
                                nByteRecved = sClient.Receive(memory, 10 * 1024, SocketFlags.None);
                            }
                            catch (Exception exp)
                            {
                                break;
                            }
                            if (nByteRecved <= 0)
                            {
                                break;
                            }
                            nLoop++;

                            //Loader.LogErr("recv bytes:" + nByteRecved.ToString());
                            fs.Write(memory, 0, nByteRecved);
                            fs.Flush();
                            nTotalRecved  += nByteRecved;
                            req.cboffset   = nTotalRecved;
                            req.loadBytes += nByteRecved;
                            if (nTotalRecved == nContentLength)
                            {
                                break;
                            }
                            if (nLoop >= 10)
                            {
                                req.callback(ref req);
                                nLoop = 0;
                            }
                        }

                        sClient.Close();
                        //Loader.LogErr("file transfer result:" + nByteRecved.ToString());
                        req.cboffset = fs.Seek(0, SeekOrigin.Current);
                        fs.Flush();
                        fs.Close();
                        req.callback(ref req);
                        req.bDone = true;
                    }
                }
            }
            if (close != null)
            {
                Log.LogInfo("thread quit signal open");
                close.Set();
                close = null;
            }
        }
        catch (Exception exp)
        {
            Debug.LogError(exp.Message + "|" + exp.StackTrace + " download thread crashed");
        }
        finally
        {
            if (close != null)
            {
                close.Set();
                close = null;
            }
        }
    }