/// <summary> /// 获得虚拟路径 /// </summary> /// <param name="localPath">本地路径</param> /// <returns>返回相对路径,失败返回NULL</returns> internal string MapLocalPathToVirtualPath(string localDir) { if (!localDir.EndsWith(localPathSpliter.ToString())) { localDir += localPathSpliter.ToString(); } if (!HomeDir.EndsWith(localPathSpliter.ToString())) { HomeDir += localPathSpliter.ToString(); } if (localDir == HomeDir) { return(Root); } else { //Unix上的文件名需要大小写匹配 if (localDir.IndexOf(HomeDir) == -1) { NetDebuger.PrintErrorMessage(this, "MAP LOCAL DIR:" + localDir + " TO HOME DIR:" + HomeDir + " FAIL"); return(Root); } else { localDir = localDir.Replace(HomeDir, Root); //把本地路径的分隔符号,替换为虚拟路径的分隔符号 return(localDir.Replace(new string(localPathSpliter, 1), new string(virtualPathSpliter, 1)). Replace(new string(virtualPathSpliter, 2), new string(virtualPathSpliter, 1))); } } }
private void StartUpFtpServer(string HostIP) { try { m_ftpServer.Stop(); ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"); ManagementObjectCollection nics = query.Get(); foreach (ManagementObject nic in nics) { { if (getIpBaseNum((nic["IPSubnet"] as String[])[0], (nic["IPAddress"] as String[])[0]) == getIpBaseNum((nic["IPSubnet"] as String[])[0], HostIP)) { m_ftpServer.PasvIPSetting = (nic["IPAddress"] as String[])[0]; break; } else { m_ftpServer.PasvIPSetting = null; } } } m_ftpServer.Start(); //Console.WriteLine("Press enter to exit..."); //Console.ReadLine(); //m_ftpServer.Stop(); } catch (System.Exception e) { NetDebuger.PrintErrorMessage("FATAL ERROR:" + e.Message); } }
/// <summary> /// 通讯错误事件 /// </summary> /// <param name="session"></param> /// <param name="e"></param> internal protected virtual void ReportError(TSession session, Exception e) { if (e is SocketException) { SocketException se = e as SocketException; string msg = string.Format("SocketException Code:{0}, Native Code:{1}", se.ErrorCode, se.NativeErrorCode); NetDebuger.PrintErrorMessage(session, msg); } NetDebuger.PrintErrorMessage(session, e.ToString()); }
private void SendFile(object arg) { FtpCommand cmd = arg as FtpCommand; if (cmd.Parameters.Count == 0) { throw new SyntaxException(); } CheckDataConnExist(); try { Statue = FtpSessionStatue.Download; bool isFile; string localPath = GetLocalPath(cmd.Parameters[0], FtpOption.Download, out isFile); if (isFile) { Response("150 Opening BINARY mode data connection for file transfer."); if (dataConn.SendFile(localPath, restartPos)) { Response("226 Transfer complete."); } else { Response("426 Connection closed; transfer aborted."); } } else { throw new FileNotFoundException(cmd.Parameters[0]); } } catch (FtpException) { throw; } catch (Exception e) { NetDebuger.PrintErrorMessage(this, e.ToString()); throw new InternalException(e.Message); } finally { CloseDataConn(); Statue = FtpSessionStatue.Wait; } }
private void STOR(FtpCommand cmd) { CheckLogin(); if (cmd.Parameters.Count == 0) { throw new SyntaxException(); } CheckDataConnExist(); restartPos = 0; try { Statue = FtpSessionStatue.Upload; bool isFile; string localPath = GetLocalPath(cmd.Parameters[0], FtpOption.Upload, out isFile); dataConn.MaxLengthOfUpload = user.MaxUploadFileLength; dataConn.RecvFile = new FileStream(localPath, FileMode.Create); Response("150 Opening BINARY mode data connection for file transfer."); if (dataConn.ReceiveFile(localPath, restartPos)) { Response("226 Transfer complete."); } else { NetDebuger.PrintErrorMessage(this, dataConn.AsyncHelper.Exception.Message); Response("426 Connection closed; transfer aborted."); } } catch (FtpException) { throw; } catch (Exception e) { NetDebuger.PrintErrorMessage(this, e.ToString()); throw new InternalException("store file"); } finally { CloseDataConn(); Statue = FtpSessionStatue.Wait; } }
/// <summary> /// 分发FTP请求 /// </summary> /// <param name="cmdText"></param> internal void SwitchFtpRequest(string cmdText) { NetDebuger.PrintErrorMessage(this, cmdText); if (string.IsNullOrEmpty(cmdText)) { return; } FtpCommand cmd; int index = cmdText.IndexOf(' '); //把命令和参数部分分开 if (index != -1) { //把命令转成大写 cmd = new FtpCommand(cmdText.Substring(0, index).ToUpper()); cmd.Parameters.Add(cmdText.Substring(index + 1)); } else { //把命令转成大写 cmd = new FtpCommand(cmdText.ToUpper()); } //会话重新开始计时 TimeCounter.Reset(); try { if (!ftpHandlers.ContainsKey(cmd.Command)) { throw new CommandNotImplementedException(); } //分发各个命令 ftpHandlers[cmd.Command].Invoke(cmd); } catch (FtpException e) { Response(e.Message); } catch (Exception e) { NetDebuger.PrintErrorMessage(this, e.ToString()); } }
protected internal override void ReportError(FtpSession session, Exception e) { NetDebuger.PrintErrorMessage(e.ToString()); }
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { NetDebuger.PrintErrorMessage("UNHANDLED ERROR:" + e.ExceptionObject.ToString()); }
/// <summary> /// Called when [connect server failed]. /// </summary> /// <param name="e">The e.</param> protected override void OnConnectServerFailed(Exception e) { base.OnConnectServerFailed(e); NetDebuger.PrintErrorMessage(Session, e.ToString()); }