lwfs9999 发表于 2010-3-1 15:40:07

问个handle_events的问题

我用udp跟远程通信,采用了reacotor,不知道为什么register_handler之后,handle_events会立即返回,并且socket可以recv,不过返回值为-1
示例代码如下,是朝一个不存在的ip发udp消息,应该是没有反馈的。
ACE_Reactor* reactor = ACE_Reactor::instance();
ACE_Time_Value tv(2);
ACE_INET_Addr local,remote("192.168.3.3", 6000);
ACE_SOCK_Dgram sock(ocal);
char buffer[] = {"haha"};
sock.send(buffer, sizeof(buffer), remote);
Test test;
reactor->register_handler(sock.get_handle(), &test, ACE_Event_Handler::READ_MASK);
int r = reactor->handle_events(tv);
int t= sock.recv(buffer, sizeof(buffer), remote);
想不明白,希望大家能帮忙看一下,谢谢。

winston 发表于 2010-3-2 16:05:55

搜索examples下面的实例代码:
while (this->finished_ == 0)
    {
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) handling events\n")));
      this->reactor ()->handle_events ();
    }
handle_events 是需要循环使用的。

lwfs9999 发表于 2010-3-3 16:41:49

/**
   * This event loop driver blocks for up to @a max_wait_time before
   * returning.It will return earlier if events occur.Note that
   * @a max_wait_time can be 0, in which case this method blocks
   * indefinitely until events occur.
   *
   * @a max_wait_time is decremented to reflect how much time this call
   * took.For instance, if a time value of 3 seconds is passed to
   * handle_events and an event occurs after 2 seconds,
   * @a max_wait_time will equal 1 second.This can be used if an
   * application wishes to handle events for some fixed amount of
   * time.
   *
   * Returns the total number of timers and I/O ACE_Event_Handlers
   * that were dispatched, 0 if the @a max_wait_time elapsed without
   * dispatching any handlers, or -1 if an error occurs.
handleevents看注释是应该返回发生的事件数。
但是我刚在reactor上注册过,应该不会有任何事件发生啊。

这个问题是在用asnmp的时候遇到的,库的实现中同步get和set的时候handleevents没有放到while里,这里就出了问题。

winston 发表于 2010-3-3 21:01:50

循环使用。把这个放到另外一个处理线程去。
页: [1]
查看完整版本: 问个handle_events的问题