예제 #1
0
        public void load_history()
        {
            ltData   = new LtData(this);
            gridList = new BindingList <LottoryItem>();

            if (ltData.load_history() < 0)
            {
                MessageBox.Show("Load history data failed\n");
            }
            ltData.clone_history(gridList);
        }
예제 #2
0
파일: update.cs 프로젝트: cnzoupeng/NewLt
        public static void get_new(object ltd)
        {
            LtData ltData = (LtData)ltd;

            int tryTimes = 3;
            int start    = 2003001;
            int stop     = 2020999;

            byte[] byteHead;
            byte[] byteData;


            if (ltData.ltSet.Count > 0)
            {
                start  = ((LottoryItem)ltData.ltSet[ltData.ltSet.Count - 1]).id;
                start += 1;
            }
RETRY:
            try
            {
                TcpClient tclient = new TcpClient();
                tclient.Connect("42.121.193.75", 5000);
                NetworkStream stream = tclient.GetStream();

                ReqHisData req = new ReqHisData();
                req.head.key   = 0;
                req.head.magic = IPAddress.HostToNetworkOrder((int)LtNet.LT_MAGIC);
                req.head.job   = IPAddress.HostToNetworkOrder((int)JobId.JOB_HISTORY);
                req.head.len   = IPAddress.HostToNetworkOrder(Marshal.SizeOf(req));
                req.req.start  = IPAddress.HostToNetworkOrder(start);
                req.req.stop   = IPAddress.HostToNetworkOrder(stop);

                stream.Write(StructToBytes(req), 0, Marshal.SizeOf(req));
                stream.Flush();

                //读取头部
                ResData res      = new ResData();
                int     headSize = Marshal.SizeOf(res);
                byteHead = new byte[headSize];
                int count = stream.Read(byteHead, 0, headSize);
                if (count < headSize)
                {
                    stream.Close();
                    tclient.Close();
                    MessageBox.Show("更新数据失败:\n");
                    return;
                }

                res            = (ResData)BytesToStruct(byteHead, res.GetType());
                res.head.key   = IPAddress.NetworkToHostOrder(res.head.key);
                res.head.magic = IPAddress.NetworkToHostOrder(res.head.magic);
                res.head.job   = IPAddress.NetworkToHostOrder(res.head.job);
                res.head.len   = IPAddress.NetworkToHostOrder(res.head.len);
                res.result     = IPAddress.NetworkToHostOrder(res.result);
                if (res.head.magic != LtNet.LT_MAGIC || res.head.job != (int)JobId.JOB_HISTORY || res.head.len < headSize)
                {
                    stream.Close();
                    tclient.Close();
                    MessageBox.Show("更新数据失败:错误的响应\n");
                    return;
                }

                if (res.result != 0)
                {
                    stream.Close();
                    tclient.Close();
                    MessageBox.Show("更新数据失败:服务器错误\n");
                    return;
                }

                if (res.head.len == headSize)
                {
                    stream.Close();
                    tclient.Close();
                    return;
                }

                int dataLen = res.head.len - headSize;
                byteData = new byte[dataLen];
                int recvLen = 0;

                while (recvLen < dataLen)
                {
                    int recvCount = stream.Read(byteData, recvLen, dataLen - recvLen);
                    if (recvCount <= 0)
                    {
                        stream.Close();
                        tclient.Close();
                        MessageBox.Show("更新数据失败:\n");
                        return;
                    }
                    recvLen += recvCount;
                }
            }
            catch (System.Exception ex)
            {
                tryTimes--;
                if (tryTimes > 0)
                {
                    goto RETRY;
                }

                //MessageBox.Show("更新数据失败:" + ex.Message);
                return;
            }

            StreamWriter  objWriter = new StreamWriter(DEFINE.HISTORY_FILE, true);
            ASCIIEncoding encoding  = new ASCIIEncoding();
            string        strLt     = encoding.GetString(byteData);
            int           itemAdd   = 0;

            //objWriter.Write(strLt);

            string[] items = strLt.Split('\n');
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i].Length < 10)
                {
                    continue;
                }

                LottoryItem item = new LottoryItem();
                if (string_to_item(items[i], item) == 0)
                {
                    ltData.ltSet.Add(item);
                    //if (i < (items.Length - 1))
                    {
                        items[i] += "\n";
                    }
                    objWriter.Write(items[i]);
                    itemAdd++;
                }
            }
            objWriter.Close();

            if (itemAdd > 0)
            {
                Thread.Sleep(1000);
                ltData.form.otherUpdateDatas();
            }
        }