我正在开发一个ftp服务器(刚刚使用Delphi XE 6和Indy 10),除了关闭应用程序之外都很好。是否有一些客户端连接但没有下载/上传应用程序完成,但如果我打开了一些数据通道(一些客户端下载/上传文件),那么应用程序崩溃(启动一些访问冲突和冻结)。所以问题是:如何安全地断开所有客户端的连接(向服务器以状态421脱机的所有客户端发送某种类型的通知),然后安全地终止所有数据通道。很高兴看到如何安全地关闭SSLConnections实例(我使用IdServerIOHandlerSSLOpenSSL进行SSL连接)。
这是我目前的代码:
procedure TfrmMain.Button3Click(Sender: TObject);
begin
//TThread.Synchronize
TThread.Queue(nil,
procedure
var
vX, vCount: Integer;
vProcContext: TIdFTPServerContext;
begin
with FTPServer.Contexts.LockList Do
try
try
vCount := Count;
Log('Clients connected: ' + IntToStr(vCount));
for vX := vCount - 1 downto 0 do
begin
vProcContext := Items[vX];
//vProcContext := TIdFTPServerContext(Items[vX]);
Assert(vProcContext is TIdFTPServerContext);
if vProcContext = nil then
Continue;
//I add this later but don't work.
vProcContext.Connection.IOHandler.WriteBufferClear;
vProcContext.Connection.IOHandler.InputBuffer.Clear;
vProcContext.Connection.IOHandler.Close;
if vProcContext.Connection.Connected then
vProcContext.Connection.Disconnect;
vProcContext.Connection.IOHandler.WriteLn('421 Server is going offline');
vProcContext.Connection.Disconnect;
//TIdDataChannelAccess(vProcContext.DataChannel).FDataChannel.Disconnect;
vProcContext.KillDataChannel;
TIdYarnOfThread(vProcContext.Yarn).Thread.Terminate;
end;
except
on e : Exception do
begin
Log('Error en btn: ' + e.Message);
raise;
end;
end;
finally
FTPServer.Contexts.UnlockList;
end;
end
);
IdServerIOHandlerSSLOpenSSL.Shutdown;
IdServerIOHandlerSSLOpenSSL.Destroy;
FTPServer.Active := False;
end;
这是我的日志程序:
procedure TfrmMain.Log(LogText: String; AppendDateTime: Boolean = False);
begin
//TThread.Synchronize
TThread.Queue(nil,
procedure
begin
logMemo.Lines.Add('> ' + LogText);
end
);
end;
提前致谢并抱歉我的英语不好:(!