|
发表于 2010-3-31 17:36:46
|
显示全部楼层
按照楼主的代码简单改了一下,可以跑通了。
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[100] = {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[len+1];
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[1];
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(); |
|