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)));
            });
        }
    private static void ENTER_ROOM_REPLY(byte[] bytes)
    {
        EnterRoomReply input = EnterRoomReply.Parser.ParseFrom(bytes);

        if (!input.Ret)
        {
            string msg = "进入战场失败:" + input.ErrMsg;
            UIManager.Instance.SystemTips(msg, PanelSystemTips.MessageType.Error);
            GameRoomManager.Instance.Log("MSG: ENTER_ROOM_REPLY - " + msg);
            if (ClientManager.Instance)
            {
                ClientManager.Instance.StateMachine.TriggerTransition(
                    ConnectionFSMStateEnum.StateEnum.DISCONNECTED_ROOM);
            }

            return;
        }

        // 请求地图数据
        DownloadMap output = new DownloadMap()
        {
            RoomId = input.RoomId,
        };

        GameRoomManager.Instance.SendMsg(ROOM.DownloadMap, output.ToByteArray());

        // 请求地图上的资源变化数据
        DownloadResCell output2 = new DownloadResCell()
        {
            RoomId = input.RoomId,
        };

        GameRoomManager.Instance.SendMsg(ROOM.DownloadResCell, output2.ToByteArray());
        {
            string msg = "成功进入战场!";
            GameRoomManager.Instance.Log($"MSG: ENTER_ROOM_REPLY OK - " + msg);
        }
    }
    private static void DOWNLOAD_MAP(byte[] bytes)
    {
        DownloadMap input     = DownloadMap.Parser.ParseFrom(bytes);
        string      tableName = $"MAP:{input.RoomId}";

        if (!ServerRoomManager.Instance.Redis.CSRedis.Exists(tableName))
        {
            string msg = $"Cannot find the table - {tableName}"; // Redis中没有找到地图表格
            ServerRoomManager.Instance.Log("MSG:DOWNLOAD_MAP - " + msg);
            DownloadMapReply output = new DownloadMapReply()
            {
                Ret    = false,
                ErrMsg = msg,
            };
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadMapReply, output.ToByteArray());
            return;
        }

        //////////////
        // 校验地图的RoomId是否和Redis中保存的一致
        long roomId = ServerRoomManager.Instance.Redis.CSRedis.HGet <long>(tableName, "RoomId");

        if (roomId != input.RoomId)
        {
            string msg = $"Read map data from redis failed! RoomId is not matched! RoomId from client:{input.RoomId} - RoomId from Redis:{roomId}"; // 从Redis中读取地图数据失败!roomId不匹配!传来的RoomId // Redis中保存的RoomId
            ServerRoomManager.Instance.Log("MSG:DOWNLOAD_MAP - " + msg);
            DownloadMapReply output = new DownloadMapReply()
            {
                Ret    = false,
                ErrMsg = msg,
            };
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadMapReply, output.ToByteArray());
            return;
        }
        string roomName = ServerRoomManager.Instance.Redis.CSRedis.HGet <string>(tableName, "RoomName");

        //////////////
        // 计算这张地图是不是我自己创建的
        PlayerInfo pi = ServerRoomManager.Instance.GetPlayer(_args);

        if (pi == null)
        {
            string msg = $"Read map data from redis failed! Player is not found! MapName:{roomName} - RoomId:{roomId}"; // 从Redis中读取地图数据失败!我自己并没有在战场服务器!地图名
            ServerRoomManager.Instance.Log("MSG:DOWNLOAD_MAP - " + msg);
            DownloadMapReply output = new DownloadMapReply()
            {
                Ret    = false,
                ErrMsg = msg,
            };
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadMapReply, output.ToByteArray());
            return;
        }
        long TokenId      = ServerRoomManager.Instance.Redis.CSRedis.HGet <long>(tableName, "Creator");
        bool IsCreateByMe = TokenId == pi.Enter.TokenId;

        //////////////
        // 其他数据
        int maxPlayerCount = ServerRoomManager.Instance.Redis.CSRedis.HGet <int>(tableName, "MaxPlayerCount");

        //////////////
        // 读取地图数据
        byte[] totalData = ServerRoomManager.Instance.Redis.CSRedis.HGet <byte[]>(tableName, "MapData");
        int    totalSize = totalData.Length;

        //////////////
        // 服务器把这份数据留起来自己用——这部分代码暂时无效
        var roomLogic = ServerRoomManager.Instance.GetRoomLogic(roomId);

        if (roomLogic == null)
        {
            string msg = ($"The Battlefield is not created or has been disposed! MapName:{roomName} - RoomId:{roomId}"); // 该战场尚未创建或者已经被销毁!地图名
            ServerRoomManager.Instance.Log("MSG:DOWNLOAD_MAP - " + msg);
            DownloadMapReply output = new DownloadMapReply()
            {
                Ret    = false,
                ErrMsg = msg,
            };
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadMapReply, output.ToByteArray());
            return;
        }
        if (!roomLogic.SetMap(totalData))
        {
            string msg = ($"Map data is not valid, can be currupted! MapName:{roomName} - RoomId:{roomId}"); // 地图数据不合法,可能已经被损坏!地图名
            ServerRoomManager.Instance.Log("MSG:DOWNLOAD_MAP - " + msg);
            DownloadMapReply output = new DownloadMapReply()
            {
                Ret    = false,
                ErrMsg = msg,
            };
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadMapReply, output.ToByteArray());
            return;
        }

        //////////////
        // 把地图数据下发到客户端
        const int CHUNK_SIZE = 900;
        int       remainSize = totalSize;
        int       index      = 0;
        int       position   = 0;

        while (remainSize > CHUNK_SIZE)
        {
            DownloadMapReply output = new DownloadMapReply()
            {
                RoomName       = roomName,
                RoomId         = input.RoomId,
                MaxPlayerCount = maxPlayerCount,
                IsCreatedByMe  = IsCreateByMe,
                IsLastPackage  = false,
                PackageIndex   = index++,
                Ret            = true,
            };
            byte[] sendBytes = new byte[CHUNK_SIZE];
            Array.Copy(totalData, position, sendBytes, 0, CHUNK_SIZE);
            output.MapData = ByteString.CopyFrom(sendBytes);
            position      += CHUNK_SIZE;
            remainSize    -= CHUNK_SIZE;
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadMapReply, output.ToByteArray());
        }

        {
            DownloadMapReply output = new DownloadMapReply()
            {
                RoomName       = roomName,
                RoomId         = input.RoomId,
                MaxPlayerCount = maxPlayerCount,
                IsCreatedByMe  = IsCreateByMe,
                IsLastPackage  = true,
                PackageIndex   = index++,
                Ret            = true,
            };
            byte[] sendBytes = new byte[remainSize];
            Array.Copy(totalData, position, sendBytes, 0, remainSize);
            output.MapData = ByteString.CopyFrom(sendBytes);
            position      += remainSize;
            remainSize    -= remainSize;
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.DownloadMapReply, output.ToByteArray());
        }

        ServerRoomManager.Instance.Log($"MSG: DOWNLOAD_MAP - Download map data succeeded! MapName:{roomName} - Total Map Size:{totalSize}"); // 地图数据下载完成!地图名
    }
Example #4
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;
            }
        }
    }