コード例 #1
0
        private void button15_Click(object sender, EventArgs e)
        {
            label8.Text = "---";
            label9.Text = "---";
            if (DeviceID == null)
            {
                MessageBox.Show("device is not login.");
                return;
            }
            DeviceListener.Device d = deviceListener.Find(DeviceID);

            string pakfilename = checkBox2.Text;

            d = deviceListener.Find(DeviceID);
            if ((checkBox2.Checked) && (File.Exists(pakfilename)))
            {
                IPAddress   localaddr = IPAddress.Parse("0.0.0.0");
                TcpListener tcpserver = new TcpListener(localaddr, 0);
                tcpserver.Start();
                IPEndPoint localep = (IPEndPoint)tcpserver.LocalEndpoint;

                TcpClient tc        = null;
                int       starttime = System.Environment.TickCount;
                try
                {
                    int ret = deviceListener.FirmwareUpdate(d, "0.0.0.0", localep.Port, pakfilename, "data");
                    if (ret == 0)
                    {
                        while ((System.Environment.TickCount - starttime) < 3000 * 10000)//3000ms
                        {
                            if (tcpserver.Pending())
                            {
                                tc = tcpserver.AcceptTcpClient();
                                break;
                            }
                            Application.DoEvents();
                        }

                        if (tc != null)
                        {
                            NetworkStream tcs = tc.GetStream();
                            FileStream    fs  = File.OpenRead(pakfilename);
                            if (fs != null)
                            {
                                bool   eof        = false;
                                long   writebytes = 0;
                                byte[] fbs        = new byte[1024];

                                while (!eof)
                                {
                                    if (tcs.CanWrite)
                                    {
                                        int readbytes = fs.Read(fbs, 0, fbs.Length);
                                        tcs.Write(fbs, 0, readbytes);
                                        writebytes += readbytes;
                                        if (writebytes >= fs.Length)
                                        {
                                            eof = true;
                                        }
                                        int process = (int)(100 * writebytes / fs.Length);

                                        label9.Text = process.ToString() + "%";

                                        Application.DoEvents();
                                    }
                                }
                            }
                            int timer = System.Environment.TickCount;
                            tc.Client.Shutdown(System.Net.Sockets.SocketShutdown.Send);

                            byte[] rb   = new byte[256];
                            int    rlen = tcs.Read(rb, 0, rb.Length);
                            while (rlen > 0)
                            {
                                rlen = tcs.Read(rb, 0, rb.Length);
                            }
                            tc.Close();
                            int t = System.Environment.TickCount - timer;
                        }
                    }
                }
                finally
                {
                    tcpserver.Stop();
                    tcpserver = null;
                }
            }

            Thread.Sleep(1000);
            string binfilename = checkBox1.Text;

            if ((checkBox1.Checked) && (File.Exists(binfilename)))
            {
                IPAddress   localaddr = IPAddress.Parse("0.0.0.0");
                TcpListener tcpserver = new TcpListener(localaddr, 0);
                tcpserver.Start();
                IPEndPoint localep = (IPEndPoint)tcpserver.LocalEndpoint;

                TcpClient tc        = null;
                int       starttime = System.Environment.TickCount;
                try
                {
                    int ret = deviceListener.FirmwareUpdate(d, "0.0.0.0", localep.Port, binfilename, "bin");
                    if (ret == 0)
                    {
                        while ((System.Environment.TickCount - starttime) < 3000 * 10000)//3000ms
                        {
                            if (tcpserver.Pending())
                            {
                                tc = tcpserver.AcceptTcpClient();
                                break;
                            }
                            Application.DoEvents();
                        }

                        if (tc != null)
                        {
                            NetworkStream tcs = tc.GetStream();
                            FileStream    fs  = File.OpenRead(binfilename);
                            if (fs != null)
                            {
                                bool   eof        = false;
                                long   writebytes = 0;
                                byte[] fbs        = new byte[1024];

                                while (!eof)
                                {
                                    if (tcs.CanWrite)
                                    {
                                        int readbytes = fs.Read(fbs, 0, fbs.Length);
                                        tcs.Write(fbs, 0, readbytes);
                                        writebytes += readbytes;
                                        if (writebytes >= fs.Length)
                                        {
                                            eof = true;
                                        }
                                        int process = (int)(100 * writebytes / fs.Length);

                                        label8.Text = process.ToString() + "%";

                                        Application.DoEvents();
                                    }
                                    Thread.Sleep(10);
                                }
                            }

                            int timer = System.Environment.TickCount;
                            tc.Client.Shutdown(System.Net.Sockets.SocketShutdown.Send);

                            byte[] rb   = new byte[256];
                            int    rlen = tcs.Read(rb, 0, rb.Length);
                            while (rlen > 0)
                            {
                                rlen = tcs.Read(rb, 0, rb.Length);
                            }
                            tc.Close();
                            int t = System.Environment.TickCount - timer;
                            //label8.Text = t.ToString();
                        }
                    }
                }
                finally
                {
                    tcpserver.Stop();
                    tcpserver = null;
                }
            }

            d = deviceListener.Find(DeviceID);
            deviceListener.Apply(d);
            PlaySuccessNotify();

            button2_Click_1(null, null);
        }