Exemplo n.º 1
0
        private void DataReceived(IAsyncResult ar)
        {
            UdpClient  c = (UdpClient)((UdpState)ar.AsyncState).c;
            IPEndPoint wantedIpEndPoint   = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
            IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            Byte[] receiveBytes   = System.Text.Encoding.Default.GetBytes("".ToString());
            Regex  regexMyBidInfo = new Regex(@"<MYBIDINFO>(.*)</MYBIDINFO>");

            try
            {
                // parse UDP packages and update the price and tiem in BIDContext
                receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);
                string recvInfo = ASCIIEncoding.ASCII.GetString(receiveBytes);
                if (regexMyBidInfo.IsMatch(recvInfo))
                {
                    var      myBidInfo      = regexMyBidInfo.Match(recvInfo).Groups[1].ToString();
                    string[] myBidInfoArray = myBidInfo.Split(new Char[] { '^' });
                    DateTime dtUpdate       = Convert.ToDateTime(myBidInfoArray[0]);
                    int      price          = Convert.ToInt32(myBidInfoArray[1]);

                    // 收到一个udp包就去更新 这样时间就能够保持一致
                    this._ctx.UpdateState(price, dtUpdate);
                    BIDLogger.GetLogger(null).Append("It is a new info:" + recvInfo);
                    //if (price > this._ctx.BaseAmount)
                    //{
                    //    this._ctx.UpdateState(price, dtUpdate);
                    //    BIDLogger.GetLogger(null).Append("It is a new info:" + recvInfo);
                    //}
                    //else
                    //{
                    //    BIDLogger.GetLogger(null).Append("It is an old info:" + recvInfo);
                    //}
                }
            }
            catch (Exception)
            {
                //c.Close();
                //c.Client.Close();
                // 点多次开始拍卖此处有坑 需要做null的判定
                if (c != null && c.Client != null)
                {
                    c.Client.Shutdown(SocketShutdown.Both);
                }
                return;
            }

            // Check sender
            bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address)) || wantedIpEndPoint.Address.Equals(IPAddress.Any);
            bool isRightPort = (wantedIpEndPoint.Port == receivedIpEndPoint.Port) || wantedIpEndPoint.Port == 0;

            if (isRightHost && isRightPort)
            {
                // Convert data to ASCII and print in console
                string receivedText = ASCIIEncoding.ASCII.GetString(receiveBytes);
            }

            // Restart listening for udp data packages
            c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);
        }
Exemplo n.º 2
0
        public static Bitmap DownloadImage(string imageUrl)
        {
            int    retries = 6;
            Bitmap bitmap  = null;

            while ((bitmap == null) && (retries-- > 0))
            {
                try
                {
                    WebClient client = new WebClient();
                    Stream    stream = client.OpenRead(imageUrl);
                    bitmap = new Bitmap(stream);
                    stream.Flush();
                    stream.Close();
                }
                catch (Exception)
                {
                    BIDLogger.GetLogger(null).Append("DownloadImage retry");

                    //这个WebClient应该自己默认有超时时间的 1秒吧
//                  Thread.Sleep(500);
                    continue;
                }
            }
            return(bitmap);
        }
Exemplo n.º 3
0
        public static BIDLogger GetLogger(string name = null)
        {
            if (name == null)
            {
                name = "Default";
            }
            BIDLogger bIDLogger = null;

            BIDLogger._cache.TryGetValue(name, out bIDLogger);
            if (bIDLogger == null)
            {
                lock (BIDLogger._cache)
                {
                    bIDLogger = new BIDLogger(name);
                    BIDLogger._cache[name] = bIDLogger;
                }
            }
            return(bIDLogger);
        }
Exemplo n.º 4
0
        private static string DecryptString(string encryString)
        {
            try
            {
                string privateKeyXML = BIDWebConfig.privateKeyXML;
                var    rsa           = new RSACryptoServiceProvider();
                rsa.FromXmlString(privateKeyXML);

                // Decrypt string, convert to correct encoding
                byte[] encryptedBytes  = Convert.FromBase64String(encryString);
                var    decryptedBytes  = rsa.Decrypt(encryptedBytes, false);
                var    decryptedString = Encoding.UTF8.GetString(decryptedBytes);
                return(decryptedString);
            }
            catch (Exception ex)
            {
                BIDLogger.GetLogger(null).Append("解析license文件出错" + ex.ToString());
                return(null);
            }
        }
Exemplo n.º 5
0
        private void frmMain_Load(object sender, EventArgs e)
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bid.log");

            BIDLogger.GetLogger(null).AddHandler(new FileLogger(path));
            base.ClientSize = new Size(this.pnControl.Width + 900, 700);
            this.wbMain.Navigate("about:blank");
            this.wbMain.ScrollBarsEnabled = false;
            this.tmUI.Enabled             = true;
            this.AutoFireTimeSecond       = this.random.Next(54, 57); //生成54 - 57秒的随机数
            this.AutoFireTime            += this.AutoFireTimeSecond.ToString();

            // Load License
            //try
            //{
            //    string licenseText = System.IO.File.ReadAllText(@"license.dat");
            //    Tuple<string, int> resTuple = EncryptionHelper.ParserLicense(licenseText);
            //    this._ctx.BIDNumberMore = resTuple.Item2;
            //    if (resTuple.Item1 != "0")
            //    {
            //        System.Windows.Forms.MessageBox.Show(resTuple.Item1);
            //        Application.Exit();
            //    }
            //}
            //catch (System.IO.FileNotFoundException ex)
            //{
            //    System.Windows.Forms.MessageBox.Show("license文件不存在!");
            //    BIDLogger.GetLogger(null).Append(ex.ToString());
            //    Application.Exit();
            //}
            //catch (Exception ex)
            //{
            //    System.Windows.Forms.MessageBox.Show("未知错误 请换一台干净的Windows 7电脑");
            //    BIDLogger.GetLogger(null).Append(ex.ToString());
            //    Application.Exit();
            //}
        }
Exemplo n.º 6
0
        public void ProxyThread()
        {
            while (true)
            {
                UdpClient udpClient = null;
                try
                {
                    // Automatically assigned local IPv4 port
                    udpClient = new UdpClient(0);
                    IPEndPoint epUdpServer = new IPEndPoint(IPAddress.Parse(BIDWebConfig.yayaServer), 990);
                    UdpState   state       = new UdpState(udpClient, epUdpServer);

                    // UdpClient is not verify the validity of addresses
                    udpClient.Connect(epUdpServer);
                    udpClient.BeginReceive(new AsyncCallback(DataReceived), state);
                    StringBuilder msgSendStringBuilder = new StringBuilder();
                    StringBuilder msgHelloBuilder      = new StringBuilder();
                    msgHelloBuilder.Append("<TYPE>CLIENT</TYPE><BIDNO>");
                    msgHelloBuilder.Append(this._ctx.BIDNumer);
                    msgHelloBuilder.Append("</BIDNO>");
                    msgHelloBuilder.Append("<VCODE>123456</VCODE>");
                    byte[] msgHello = System.Text.Encoding.Default.GetBytes(msgHelloBuilder.ToString());
                    while (true)
                    {
                        // We Must Send Hello Message First
                        udpClient.Send(msgHello, msgHello.Length);
                        Thread.Sleep(100);

                        // Send Price Update Message
                        msgSendStringBuilder.Append("<TYPE>UPDATE</TYPE><INFO>");

                        // 显然此处的发送时间不应该再是本地的时间了:
                        msgSendStringBuilder.Append(this._ctx.UpdateTime.ToString("HH:mm:ss"));
                        msgSendStringBuilder.Append("^");
                        msgSendStringBuilder.Append(this._ctx.BaseAmount);
                        msgSendStringBuilder.Append("</INFO>");
                        byte[] msgSend = System.Text.Encoding.Default.GetBytes(msgSendStringBuilder.ToString());
                        udpClient.Send(msgSend, msgSend.Length);
                        msgSendStringBuilder.Clear();
                        Thread.Sleep(200);
                    }
                }
                catch (SocketException ex)
                {
                    BIDLogger.GetLogger(null).Append(ex.ToString());
                }
                catch (Exception ex)
                {
                    BIDLogger.GetLogger(null).Append(ex.ToString());
                }
                finally
                {
                    if (udpClient != null)
                    {
                        try
                        {
                            udpClient.Close();
                        }
                        catch
                        {
                        }
                    }
                }
                Thread.Sleep(500);
            }
        }
Exemplo n.º 7
0
 private void Log(string str)
 {
     BIDLogger.GetLogger(null).Append(str, 1);
 }