Example #1
0
        private void SendCustomFile()
        {
            FileStream   fs = null;
            BinaryReader br = null;

            Byte[] userData = null;
            var    dest     = this.GetDest();

            try
            {
                // Create the reader for data.
                fs = new FileStream(_fileNameToSend, FileMode.Open, FileAccess.Read);
                br = new BinaryReader(fs);

                for (int n = 0; n < _sendTimes; n++)
                {
                    try
                    {
                        // 发送文件起始标志与扩展名
                        if (_fileBeginFlagEnabled)
                        {
                            userData = RsspEncoding.ToNetString(CustomFile.StartFlag + Path.GetExtension(_fileNameToSend));

                            var outPkg = new OutgoingPackage(dest, userData);
                            _nodeComm.Send(outPkg);
                        }

                        // Read and send data
                        fs.Position = 0;
                        do
                        {
                            userData = br.ReadBytes(_packetSize);

                            var outPkg = new OutgoingPackage(dest, userData);
                            _nodeComm.Send(outPkg);

                            // 如果文件发送完成,则立即结束
                            if (userData.Length < _packetSize)
                            {
                                break;
                            }

                            // 发送间隔
                            if (_sendExitEvent.WaitOne(_sendInterval, false))
                            {
                                break;
                            }
                        } while (fs.Length != fs.Position);
                    }
                    finally
                    {
                        userData = RsspEncoding.ToNetString(CustomFile.EndFlag);

                        // 发送文件传送结束标志
                        if (_fileBeginFlagEnabled)
                        {
                            var outPkg = new OutgoingPackage(dest, userData);
                            _nodeComm.Send(outPkg);
                        }
                    }
                }
            }
            catch (Exception /*ex*/)
            {
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }

                _sendThread = null;
            }
        }