You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.8 KiB
C#

using Estsh.Client.Common.CostomTraceListener;
using Estsh.Client.Common.FileUtil;
using SAEA.FTP;
using SAEA.FTP.Core;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
namespace Estsh.File.Server
{
public partial class FileServer : Form
{
private string sOldFile = string.Empty;
private FTPServer server;
private readonly int MAX_LINES = 1000;
private int _port = 8899;
private int _capacity = 1000;
public FileServer()
{
InitializeComponent();
string ServerName = string.Empty;
try
{
ServerName = ConfigurationManager.AppSettings["ServerName"];
}
catch { }
this.Text = string.Format("[{0}] ", ServerName) + this.Text + " Ver: " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
notifyIcon1.Text = this.Text;
try
{
_port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"].Trim());
_capacity = Convert.ToInt32(ConfigurationManager.AppSettings["Capacity"].Trim());
}
catch { }
Trace.Listeners.Add(new RichTextBoxTraceListener(rtbState, MAX_LINES));
//Trace.Listeners.Add(new TextFileTraceListener());
//Trace.Listeners.Add(new SQLiteTraceListener(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log\\log.db")));
// ³ö·¢¿ªÊ¼Ê¼þ
cmd_Start_Click(null, null);
}
public void InitFtpServer()
{
ServerConfig _serverConfig = new ServerConfig();
_serverConfig.IP = "127.0.0.1";
_serverConfig.Port = (ushort)_port;
FTPServerConfigManager.Save();
var ftpServer = new FTPServer(_serverConfig.IP, _serverConfig.Port, _serverConfig.BufferSize);
//ftpServer.OnLog += _ftpServer_OnLog;
ftpServer.Start();
//server = new FtpServer();
//server.Port = _port;
//server.Capacity = _capacity;
//server.HeartBeatPeriod = 120000; // 120s
//FtpUser user = new FtpUser("rock");
//user.Password = "8623";
//user.AllowWrite = true;
//user.HomeDir = AppDomain.CurrentDomain.BaseDirectory;
//user.MaxConnectionCount = 1000;
//user.MaxUploadFileLength = 1024 * 1024 * 20; // 20M
//server.AddUser(user);
//server.AnonymousUser.HomeDir = AppDomain.CurrentDomain.BaseDirectory;
//server.AnonymousUser.AllowRead = false;
//server.Start();
}
private void cmd_Start_Click(object sender, EventArgs e)
{
cmd_Start.Enabled = false;
cmd_Stop.Enabled = true;
fileSystemWatcher1.Path = AppDomain.CurrentDomain.BaseDirectory;
fileSystemWatcher1.IncludeSubdirectories = true;
fileSystemWatcher1.EnableRaisingEvents = true;
rtbState.AppendText("Initialize File Server.\n");
InitFtpServer();
rtbState.AppendText("Server Start...\n");
this.WindowState = FormWindowState.Minimized;
}
private void FileServer_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.cmd_Stop.Enabled)
{
MessageBox.Show("Please Click [Stop] Button First !");
e.Cancel = true;
cmd_Stop.Focus();
}
else
{
if (server != null)
{
if (server.Running)
{
server.Stop();
server.Dispose();
server = null;
}
}
}
}
private void cmd_Stop_Click(object sender, EventArgs e)
{
cmd_Start.Enabled = true;
cmd_Stop.Enabled = false;
fileSystemWatcher1.EnableRaisingEvents = false;
if (server != null)
{
if (server.Running)
{
server.Stop();
server.Dispose();
server = null;
}
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
this.BringToFront();
}
private void FileServer_Deactivate(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
}
}
private void HandleFileEvent(object sender, FileSystemEventArgs e)
{
string sTempPath = string.Empty;
int iPos = 0;
sTempPath = Path.GetDirectoryName(e.FullPath);
// Skip Log Directory
//if (sTempPath.Substring(sTempPath.Length - 3, 3) == "Log")
// return;
// Èç¹û·¾¶Öаüº¬ Log ×Ö·û²¢ÇÒÔÚ¸ùĿ¼£¬¾ÍÌø¹ý
if (sTempPath.Contains("Log") && sTempPath.Replace(AppDomain.CurrentDomain.BaseDirectory, string.Empty).Substring(0, 3) == "Log")
return;
if (sOldFile != e.FullPath)
{
if (!Directory.Exists(e.FullPath))
{
sOldFile = e.FullPath;
tim_Delay.Enabled = true;
sTempPath = e.FullPath.Replace(fileSystemWatcher1.Path, "");
iPos = sTempPath.IndexOf("\\");
if (iPos > 0)
{
sTempPath = sTempPath.Substring(0, iPos);
rtbState.AppendText(e.FullPath + " " + sTempPath + "\n");
// ÑÓʱ 100 ºÁÃ룬·ÀÖ¹¶ÁÈ¡²»µ½ÎļþµÄÊôÐÔ
Thread.Sleep(100);
FileInfos fileInfos = new FileInfos();
FileManager.GetLocalFileInfo(fileSystemWatcher1.Path, sTempPath, ref fileInfos);
fileSystemWatcher1.EnableRaisingEvents = false;
FileManager.SaveFileListsToXml(fileSystemWatcher1.Path + "\\" + sTempPath + ".xml",
fileInfos);
fileSystemWatcher1.EnableRaisingEvents = true;
}
else
{
rtbState.AppendText(sTempPath + "\n");
}
}
}
}
private void tim_Delay_Tick(object sender, EventArgs e)
{
sOldFile = string.Empty;
tim_Delay.Enabled = false;
}
}
}