首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用TIdTCPServer进行多播(Indy9+Delphi7)

使用TIdTCPServer进行多播(Indy9+Delphi7)
EN

Stack Overflow用户
提问于 2012-07-13 02:18:31
回答 1查看 2.1K关注 0票数 1

我知道我下面描述的不是技术上的多播,但我缺乏一个更好的描述我正在尝试做的事情。

我目前有一个带有OnExecute的TIdTCPServer,它读取传入的消息,将响应消息生成到线程安全队列中,然后调用一个函数来遍历队列并将消息发送回客户端。这部分代码正在运行,并允许客户端请求数据集。

当客户端更改服务器上的对象时,我希望将此更改通知所有客户端。代码当前会将通知消息添加到每个连接的每个队列中。我的问题是OnExecute没有循环,所以在队列上发送消息的调用直到服务器接收到来自客户端的消息时才会被调用。

有没有办法让OnExecute循环?或者一种触发连接OnExecute的方法,我知道我已经为其排队消息了?

我的过程TWinSocketSession对象有一个对连接的引用,并包含传出消息队列的代码。它还有一个名为SendMessages的过程,该过程遍历队列并调用Connection.Write。下面是我的OnExecute过程。

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-13 05:09:17

是的,TIdTCPServer.OnExecute event IS a looped event。入站消息到达时触发的是,而不是。无论连接实际在做什么,它都会在连接的生命周期内以连续循环的方式触发。根据需要实现阻塞行为是事件处理程序的责任。您的代码使用指定的25ms超时调用ReadLn(),因此它不会阻止事件处理程序及时退出,因此它可以立即重新进入。如果您的事件处理程序被阻止及时退出,那么您的代码中的其他地方就会出现死锁问题。

然而,我建议进行以下更改:

代码语言:javascript
复制
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事件中直接为连接分配一个新队列,这将会更好地工作。OnDisconnect事件可以在客户端断开连接时释放队列。

代码语言:javascript
复制
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;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11458095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档