Esempio n. 1
0
        private void SDFileUploadThread(object obj)
        {
            IPAddress   localaddr = IPAddress.Parse("0.0.0.0");
            TcpListener tcpserver = new TcpListener(localaddr, 0);

            tcpserver.Start();
            IPEndPoint localep = (IPEndPoint)tcpserver.LocalEndpoint;

            WorkProcessHandler h = new WorkProcessHandler(UploadFileProcess);

            ThreadParam ap = (ThreadParam)obj;

            DeviceListener.Device d = deviceListener.Find(ap.id);
            if (d == null)
            {
                return;
            }
            string fname = System.IO.Path.GetFileName(ap.filename);
            //int ret = deviceListener.FilePlayStart(d, "0.0.0.0", localep.Port, ap.streamtype, ap.volume, null);
            int ret = deviceListener.SDCardUploadFile(d, "0.0.0.0", localep.Port, fname);

            if (ret != 0)
            {
                return;
            }
            TcpClient tc = null;

            try
            {
                long starttime = DateTime.Now.Ticks;

                while ((DateTime.Now.Ticks - starttime) < 3000 * 10000)//3000ms
                {
                    if (tcpserver.Pending())
                    {
                        tc = tcpserver.AcceptTcpClient();
                        break;
                    }
                }
                if (tc != null)
                {
                    FileStream fs = File.OpenRead(ap.filename);
                    if (fs != null)
                    {
                        bool          eof        = false;
                        long          writebytes = 0;
                        byte[]        fbs        = new byte[1024];
                        NetworkStream tcs        = tc.GetStream();
                        while (!eof)
                        {
                            if (tcs.CanWrite)
                            {
                                int readbytes = fs.Read(fbs, 0, fbs.Length);

                                tcs.Write(fbs, 0, readbytes);
                                writebytes += readbytes;

                                if (readbytes < fbs.Length)
                                {
                                    eof = true;
                                }

                                int process = (int)(100 * writebytes / fs.Length);

                                this.BeginInvoke(h, d, ap.filename, false, process);
                            }
                        }
                    }
                    tc.Close();
                }

                this.BeginInvoke(h, d, ap.filename, true, 0);
                dataThread = null;
            }
            catch (ThreadAbortException abortException)
            {
                //this.BeginInvoke(h, ap.filename, true, 0);
                if (tc != null)
                {
                    tc.Close();
                }

                dataThread = null;
            }
        }