Inheritance: IDisposable
Exemple #1
0
        public void SendFiles(string localPath)
        {
            Program.Assert(State == AppLogicState.WaitingForDropFile);
            Program.Assert(Directory.Exists(localPath) || File.Exists(localPath));

            using (UdpClient udpFileSender = new UdpClient(0))
            {
                udpFileSender.Client.ReceiveTimeout = UDP_RECEIVE_TIME_OUT;

                byte[] bytes =
                    UDP_PROTOCOL_STEP_0.Concat(
                        Encoding.Default.GetBytes(localPath.Substring(localPath.LastIndexOf(Path.DirectorySeparatorChar) + 1))
                    ).ToArray();
                udpFileSender.Send(bytes, bytes.Length, new IPEndPoint(IPAddress.Broadcast, UDP_FINDER_PORT));

                IPEndPoint receiverEndPoint = null;
                try
                {
                    bytes = udpFileSender.Receive(ref receiverEndPoint);
                }
                catch { bytes = null;  }
                if (bytes == null || bytes[0] != UDP_PROTOCOL_STEP_1[0]) return;

                udpFileSender.Send(UDP_PROTOCOL_STEP_2, 1, receiverEndPoint);
                State = AppLogicState.SendingFile;

                Program.Assert(m_tcpServer == null && m_tcpClient == null);

                m_tcpServer = new TcpListener(
                    Dns.GetHostAddresses(Dns.GetHostName()).Where(i => i.ToString().StartsWith("192.168.")).ToArray()[0], 0);
                m_tcpServer.Start();
                udpFileSender.Send(Program.ToByteArray(((IPEndPoint)m_tcpServer.LocalEndpoint).Port), 4, receiverEndPoint);

                m_tcpClient = m_tcpServer.AcceptTcpClient();

                Program.Assert(m_fileTransSender == null);
                m_fileTransSender = new FileTransSender(localPath, new BufferedStream(m_tcpClient.GetStream()));
                m_fileTransSender.Aborted += Program.WrapThreadSafeDelegate(() =>
                {
                    if (ErrorHandler != null) ErrorHandler("传输中断!"); OnTransformEnd();
                });
                m_fileTransSender.TransCompleted += Program.WrapThreadSafeDelegate((Action<string>)(s =>
                    {
                        if (s != null && ErrorHandler != null) ErrorHandler(s); OnTransformEnd();
                    }));
                m_fileTransSender.FileTransing += Program.WrapThreadSafeDelegate((Action<string>)(s =>
                {
                    m_transingFile = s;
                    if (ProgressChanged != null) ProgressChanged(m_fileTransSender.TransedFileCount * 100 / m_fileTransSender.FileCount);
                }));
                m_fileTransSender.Work();

                m_timer = new Stopwatch();
                m_timer.Start();
            }
        }
Exemple #2
0
        private void OnTransformEnd()
        {
            switch (State)
            {
                case AppLogicState.SendingFile:
                    m_tcpClient.Close(); m_tcpClient = null;
                    m_tcpServer.Stop(); m_tcpServer = null;
                    m_fileTransSender.Dispose();  m_fileTransSender = null;

                    break;
                case AppLogicState.ReceivingFile:
                    m_tcpClient.Close(); m_tcpClient = null;
                    m_fileTransReceiver.Dispose();  m_fileTransReceiver = null;

                    break;
                default:
                    break;
            }

            State = AppLogicState.WaitingForDropFile;
            if (m_udpFileReceiverResult.IsCompleted)
            {
                m_udpFileReceiverResult = m_udpFileReceiver.BeginReceive(
                    o => Program.WrapThreadSafeDelegate(this.UDPProtocol_0_1)(), null);
            }

            m_transingFile = string.Empty;
            m_timer = null;
        }
        public void SendFiles(string localPath)
        {
            Program.Assert(State == AppLogicState.WaitingForDropFile);
            Program.Assert(Directory.Exists(localPath) || File.Exists(localPath));

            using (UdpClient udpFileSender = new UdpClient(0))
            {
                udpFileSender.Client.ReceiveTimeout = UDP_RECEIVE_TIME_OUT;

                byte[] bytes =
                    UDP_PROTOCOL_STEP_0.Concat(
                        Encoding.Default.GetBytes(localPath.Substring(localPath.LastIndexOf(Path.DirectorySeparatorChar) + 1))
                        ).ToArray();
                udpFileSender.Send(bytes, bytes.Length, new IPEndPoint(IPAddress.Broadcast, UDP_FINDER_PORT));

                IPEndPoint receiverEndPoint = null;
                try
                {
                    bytes = udpFileSender.Receive(ref receiverEndPoint);
                }
                catch { bytes = null; }
                if (bytes == null || bytes[0] != UDP_PROTOCOL_STEP_1[0])
                {
                    return;
                }

                udpFileSender.Send(UDP_PROTOCOL_STEP_2, 1, receiverEndPoint);
                State = AppLogicState.SendingFile;

                Program.Assert(m_tcpServer == null && m_tcpClient == null);

                m_tcpServer = new TcpListener(
                    Dns.GetHostAddresses(Dns.GetHostName()).Where(i => i.ToString().StartsWith("192.168.")).ToArray()[0], 0);
                m_tcpServer.Start();
                udpFileSender.Send(Program.ToByteArray(((IPEndPoint)m_tcpServer.LocalEndpoint).Port), 4, receiverEndPoint);

                m_tcpClient = m_tcpServer.AcceptTcpClient();

                Program.Assert(m_fileTransSender == null);
                m_fileTransSender          = new FileTransSender(localPath, new BufferedStream(m_tcpClient.GetStream()));
                m_fileTransSender.Aborted += Program.WrapThreadSafeDelegate(() =>
                {
                    if (ErrorHandler != null)
                    {
                        ErrorHandler("传输中断!");
                    }
                    OnTransformEnd();
                });
                m_fileTransSender.TransCompleted += Program.WrapThreadSafeDelegate((Action <string>)(s =>
                {
                    if (s != null && ErrorHandler != null)
                    {
                        ErrorHandler(s);
                    }
                    OnTransformEnd();
                }));
                m_fileTransSender.FileTransing += Program.WrapThreadSafeDelegate((Action <string>)(s =>
                {
                    m_transingFile = s;
                    if (ProgressChanged != null)
                    {
                        ProgressChanged(m_fileTransSender.TransedFileCount * 100 / m_fileTransSender.FileCount);
                    }
                }));
                m_fileTransSender.Work();

                m_timer = new Stopwatch();
                m_timer.Start();
            }
        }