![](/img/trans.png)
[英]Delphi Rio 10.3.3 with Indy 10.6.2.5366 TIdTCPServer thread problem
[英]Multicasting with TIdTCPServer (Indy 9 + Delphi 7)
我知道我在下面描述的並不是技術上的多播,但我對我想要做的事情缺乏更好的描述。
我目前有一個帶有OnExecute的TIdTCPServer,它讀取傳入的消息,將響應消息生成到線程安全隊列中,然后調用一個函數來逐步執行隊列並將消息發送回客戶端。 這部分代碼正在運行,並允許客戶端請求數據集。
當客戶端更改服務器上的對象時,我希望所有客戶端都收到有關此更改的通知。 代碼當前將通知消息添加到每個連接的每個隊列。 我的問題是OnExecute沒有循環,因此在服務器從客戶端收到消息之前,不會調用在隊列上發送消息的調用。
有沒有辦法讓OnExecute循環? 或者為連接觸發OnExecute的方法我知道我已將消息排隊了?
我的過程TWinSocketSession對象具有對連接的引用,並包含傳出消息Queue的代碼。 它還有一個名為SendMessages的過程,它遍歷隊列並調用Connection.Write。 下面是我的OnExecute程序。
procedure TWinServerSession.IdTCPServer1Execute(AThread: TIdPeerThread);
Var
i: Integer;
strMessage: String;
begin
//find the Socket Session for connection
for i := 0 to m_usClientCount do
begin
if m_mWinSocketSession[i]<>Nil then
if ( m_mWinSocketSession[i].Connection = Athread.Connection ) then break;
end;
//read the message
strMessage := m_mWinSocketSession[i].Connection.ReadLn(#0,25,-1);
//parse the message and populate the Queue with outgoing messages
m_mWinSocketSession[i].ParseInString(strMessage);
//send all of the outgoing messages
m_mWinSocketSession[i].SendMessages;
end;
是的, TIdTCPServer.OnExecute
事件是一個循環事件。 入站消息到達時不會觸發它。 無論連接實際在做什么,它都會在連續的生命周期內以連續循環方式觸發。 它是事件處理程序根據需要實現阻塞行為的責任。 您的代碼調用了ReadLn()
並指定了25ms的超時,因此它不會阻止事件處理程序及時退出,因此它可以立即重新輸入。 如果阻止事件處理程序及時退出,那么代碼中的其他地方就會出現死鎖問題。
不過,我會建議以下更改:
procedure TWinServerSession.IdTCPServer1Connect(AThread: TIdPeerThread);
var
i: Integer;
begin
for i := 0 to m_usClientCount do
begin
if (m_mWinSocketSession[i] = nil) then
begin
m_mWinSocketSession[i] := TWinSocketSession.Create;
m_mWinSocketSession[i].Connection := AThread.Connection;
// for easier access in the other events...
AThread.Data := m_mWinSocketSession[i];
Exit;
end;
end;
// cannot start a new session
AThread.Connection.Disconnect;
end;
procedure TWinServerSession.IdTCPServer1Disconnect(AThread: TIdPeerThread);
var
session: TWinSocketSession;
i: Integer;
begin
session := TWinSocketSession(AThread.Data);
AThread.Data := nil;
if session <> nil then
begin
for i := 0 to m_usClientCount do
begin
if m_mWinSocketSession[i] = session then
begin
m_mWinSocketSession[i] := nil;
Break;
end;
end;
session.Free;
end;
end;
procedure TWinServerSession.IdTCPServer1Execute(AThread: TIdPeerThread);
var
session: TWinSocketSession;
strMessage: String;
begin
session := TWinSocketSession(AThread.Data);
//read a message
strMessage := AThread.Connection.ReadLn(#0, 25, -1);
if not AThread.Connection.ReadLnTimedOut then
begin
//parse the message and populate the Queue with outgoing messages
session.ParseInString(strMessage);
end;
//send all of the outgoing messages
session.SendMessages;
end;
如果您可以完全消除m_mWinSocketSession列表並且只是在OnConnect
事件中將新隊列直接分配給Connection,那么這將更好。 當客戶端斷開連接時, OnDisconnect
事件可以釋放隊列。
procedure TWinServerSession.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
AThread.Data := TWinSocketSession.Create;
TWinSocketSession(AThread.Data).Connection := AThread.Connection;
end;
procedure TWinServerSession.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
session := TWinSocketSession(AThread.Data);
AThread.Data := nil;
session.Free;
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.