另外关于virtual,如果父类有的话,子类是可以不加的 ACE_SPIPE_Acceptor的注释看得我有点疑惑,我拿楼主的代码按照注释的说法修改了一下,
不过确实没有被ACE_Reactor framework正确的demultiplex。
/**
* @class ACE_SPIPE_Acceptor
*
* @brief A factory class that produces ACE_SPIPE_Stream objects.
*
* ACE_SPIPE_Acceptor is a factory class that accepts SPIPE connections.
* Each accepted connection produces an ACE_SPIPE_Stream object.
*
* @warning Windows: Works only on Windows NT 4 and higher. To use this
* class with the ACE_Reactor framework, note that the handle to
* demultiplex on is an event handle and should be registered with the
* ACE_Reactor::register_handler (ACE_Event_Handler *, ACE_HANDLE) method.
*
* @warning Works on non-Windows platforms only when @c ACE_HAS_STREAM_PIPES
* is defined.
*
*/
class ACE_Export ACE_SPIPE_Acceptor : public ACE_SPIPE 按照楼主的代码简单改了一下,可以跑通了。
class Pipe_Event_Read : public ACE_Event_Handler
{
public:
Pipe_Event_Read()
{
cout << "Pipe_Event_Read()" << endl;
}
virtual ACE_HANDLE get_handle (void) const
{
return new_stream.get_handle();
}
virtual int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0)
{
cout << "Pipe_Event_Read::handle_signal" << endl;
return handle_input();
}
virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE)
{
cout << "Pipe_Event_Read::handle_input" << endl;
char buf = {0};
int n = 0;
if( (n = new_stream.recv_n(buf, 9)) > 0 )
{
//收数据
int len = ACE_OS::atoi(buf);
ACE_DEBUG((LM_DEBUG, "Pipe_Event_Read ::recv:%d %s\n", len, buf));
char *content = new char;
ACE_OS::memset(content, 0, len+1);
new_stream.recv(content, len);
ACE_DEBUG((LM_DEBUG, "Pipe_Event_Read ::content:%s\n", content));
ACE_OS::sleep(1);
//往主进程里写
int nRet =new_stream.send(buf, 9);
nRet =new_stream.send(content, len);
cout << "Pipe_Event_Read ::child send over"<<nRet<<"\n";
delete [] content;
ACE_OS::memset(buf, 0, 100);
}
else
{
cout << "Pipe_Event_Read ::ACE_INVALID_HANDLE"<<"\n";
return -1;
}
return 0;
}
ACE_SPIPE_Acceptor acceptor;
ACE_SPIPE_Stream new_stream;
};
子进程:
const char* pipe_name = argv;
Pipe_Event_Read per;
if( per.acceptor.open(ACE_SPIPE_Addr(pipe_name)) == -1 )
ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%p\n"), ACE_TEXT("open")), 1);
if(per.acceptor.accept(per.new_stream, 0) == -1)
ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%p\n"), ACE_TEXT("acceptor")), 1);
cout << "acceptor.accept succ " << per.new_stream.get_handle()<< " "
<<per.acceptor.get_handle()<< endl;
per.new_stream.send_n("acb",3);
ACE_Reactor::instance()->register_handler(&per,per.new_stream.get_handle());
ACE_Reactor::instance()->run_reactor_event_loop(); 再次感谢,实验成功了
不过对我的应用来说,还是有问题,大致是这样的:
有2个进程A和B,建立了一个ACE_SPIPE_Stream,这个stream保持到进程结束
A的线程1发送数据给B,B进行一定处理后返回数据给A
A的线程2接收,使用了ACE_Reactor来监控数据的到来
程序运行后,会阻塞与线程2的接收 不会呀!
两端都可以用Reactor呀。
进程A干嘛要另外起一个线程专门进行接收? 我有些不了解,windows下为什么要用SPIPE,win下对pipe的支持不是很努力吧。
就用socket多好?
页:
1
[2]