/* Ftp Dot Net Main File : contains Events Declarations, enumerations and FtpServer Class (the main class of the project) Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Last modification : 04/20/2004 by Simon FERQUEL */ using System; using System.IO; using System.Data; using System.Collections; using System.Net; using System.Net.Sockets; using System.Threading; using Microsoft.Win32; using System.Security.Permissions; //Afin de lire et 閏rire des donn閑s dans la base de registre, on donne les droits ?l'assembly [assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, All = "HKEY_LOCAL_MACHINE")] namespace SharpFtpServer { /// <summary> /// Argument de l'関鑞ement "Connection" /// </summary> public class ConnectionEventArgs : EventArgs { string m_ip; public string Ip { get{return m_ip;} } public ConnectionEventArgs(string ip) { this.m_ip=ip; } } /// <summary> /// Code retour de lecture de commande /// </summary> public enum ReadReplyCode{ Ok =0, TimeOut =1, UnknownError =2} /// <summary> /// Exception lev閑 lors d'une erreur de lecture de commande /// </summary> public class ReadException : System.Exception { private ReadReplyCode m_ReadReplyCode; public ReadException(ReadReplyCode code,string message) : base(message) { m_ReadReplyCode = code; } public ReadReplyCode ReadReplyCode { get{ return m_ReadReplyCode; } } } /// <summary> /// La classe FtpServer est le module qui 閏oute sur le port ftp et qui cr閑 les instances des FtpSession /// quand un client se connecte /// </summary> public class FtpServer { //Cl?de registre contenant la Configuration du serveur ftp private RegistryKey m_configRk = Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("BluCorp").CreateSubKey("SharpFtpServer"); private TcpListener FTP_Listener = null; // Listener 閏outant les tentatives de connection des clients private Hashtable m_sessionList = null; // Collection contenant toutes les sessions en cours private Hashtable m_sessionThreads = null; private Thread m_listeningThread = null; // Thread repr閟entant la boucle d'閏oute private bool m_listening = false; // Le serveur 閏oute-t'il? private long m_sessID = 0; // Toutes ces propri閠閟 sont des acc鑣 simplifi閟 ?la base de registre. La plupart des param鑤res // sont modifiable sans red閙arrer le serveur. #region Propri閠閟 public string IPAddress { get{ try { return (string)m_configRk.GetValue("IPAddress"); } catch { return "ALL"; } } set{m_configRk.SetValue("IPAddress",value);} } public Language Language { get { string lang=(string)m_configRk.GetValue("Language","fr"); switch(lang.ToLower()) { case "fr" : return Language.fr; case "en" : return Language.en; default : return Language.fr; } } set { switch(value) { case Language.fr : m_configRk.SetValue("Language","fr"); break; case Language.en : m_configRk.SetValue("Language","en"); break; default : m_configRk.SetValue("Language","fr"); break; } } } public int MaxBadCommands { get { try { return Convert.ToInt32((string)m_configRk.GetValue("MaxBadCommands")); } catch { return 30; } } set { try { m_configRk.SetValue("MaxBadCommands",((int)value).ToString()); } catch {} } } public int Port { get { try { return Convert.ToInt32((string)m_configRk.GetValue("Port")); } catch { return 21; } } set { try { m_configRk.SetValue("Port",((int)value).ToString()); } catch {} } } public int MaxThreads { get { try { return Convert.ToInt32((string)m_configRk.GetValue("MaxThreads")); } catch { return 100; } } set { try { m_configRk.SetValue("MaxThreads",((int)value).ToString()); } catch {} } } public int SessionIdleTimeOut { get { try { return Convert.ToInt32((string)m_configRk.GetValue("SessionIdleTimeOut")); } catch { return 800000; } } set { try { m_configRk.SetValue("SessionIdleTimeOut",((int)value).ToString()); } catch {} } } public int CommandIdleTimeOut { get { try { return Convert.ToInt32((string)m_configRk.GetValue("CommandIdleTimeOut")); } catch { return 60000; } } set { try { m_configRk.SetValue("CommandIdleTimeOut",((int)value).ToString()); } catch {} } } #endregion #region Prise en charge des 関鑞ements public delegate void ConnectionEventHandler(object sender, ConnectionEventArgs e); public event EventHandler Started; public event EventHandler Stopped; public event ConnectionEventHandler ClientConnected; public event ConnectionEventHandler ClientDisconected; #endregion /// <summary> /// Constructeur par d閒aut /// </summary> public FtpServer() { } /// <summary> /// M閠hode permettant le d閙arrage du serveur (du thread d'閏oute) /// </summary> public void Start() { try { //Si le serveur n'閏oute pas d閖?.. if(!this.m_listening) { //on r閕nitialise la collection des sessions en cours. this.m_sessionList=new Hashtable(); //ainsi que celle des threads de session this.m_sessionThreads=new Hashtable(); //on initialise le thread this.m_listeningThread=new Thread(new ThreadStart(Listen)); //et on d閙arre la boucle this.m_listeningThread.Start(); //On d閏lenche aussi l'関鑞ement "Started" if(this.Started!=null) { Started(this,EventArgs.Empty); } } } catch{} } /// <summary> /// M閠hode d'arr阾 du thread d'閏oute /// </summary> public void Stop() { try { //on ne stop le thread que si il est en train de tourner if(this.m_listening) { //on stop le thread this.m_listeningThread.Abort(); //on change la variable en cons閝uence this.m_listening=false; //on stop le listener FTP_Listener.Stop(); //et on le d閠ruit FTP_Listener=null; //On arr阾e aussi toutes les sessions en cours for (int i=0; i<m_sessID; i++) { this.RemoveSession(i); } //on d閏lenche l'関鑞ement Stopped if(this.Stopped!=null) { Stopped(this,EventArgs.Empty); } } } catch{} } /// <summary> /// Boucle d'閏oute /// </summary> private void Listen() { //Param鑤rage du TcpListener if(this.IPAddress.ToLower().IndexOf("all") > -1) { FTP_Listener = new TcpListener(System.Net.IPAddress.Any,this.Port); } else { FTP_Listener = new TcpListener(System.Net.IPAddress.Parse(this.IPAddress),this.Port); } //D閙arrage de l'閏oute FTP_Listener.Start(); this.m_listening=true; while(true) { // On v閞ifie d'abord que le nombre dee connections maximum n'a pas 閠?atteind. if(m_sessionList.Count < this.MaxThreads) { // On attend une connection d'un client Socket clientSocket = FTP_Listener.AcceptSocket(); //on d閏lenche l'関鑞ement ClientConnected if(this.ClientConnected!=null) { this.ClientConnected(this,new ConnectionEventArgs(clientSocket.RemoteEndPoint.ToString())); } //on cr閑 la session FtpSession session=new FtpSession(clientSocket,this,this.m_sessID); //on cr閑 le thread qui va traiter les commandes pass閑s par le client Thread clientThread = new Thread(new ThreadStart(session.StartProcessing)); // Ajout de la session ?la collection des sessions en cours. this.m_sessionList.Add(m_sessID,session); // D閎ut du traitement clientThread.Start(); // Ajout du thread ?la collection de thread de session this.m_sessionThreads.Add(m_sessID,clientThread); m_sessID++; } else { Thread.Sleep(100); } } } /// <summary> /// Suppression d'une session de la liste des sessions actives /// </summary> /// <param name="session">Session ?supprimer</param> public void RemoveSession(long sessID){ try { //Evenement ClientDisconnected if(this.ClientDisconected!=null)this.ClientDisconected(this,new ConnectionEventArgs(((FtpSession)this.m_sessionList[sessID]).RemoteEndPoint.ToString())); //Arr阾 du Thread correspondant ((Thread)this.m_sessionThreads[sessID]).Abort(); //suppression du thread this.m_sessionThreads.Remove(sessID); //Cloture de la session ((FtpSession)this.m_sessionList[sessID]).Close(); //suppression de la session this.m_sessionList[sessID]=null; this.m_sessionList.Remove(sessID); } catch{}; } } }
- 1
- 2
- skyhook10262016-05-30还是挺牛的,可以!
- 粉丝: 0
- 资源: 10
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助