ACE 调试 异常
程序在调用open时抛出异常,我找不到错误,请哪位好心人帮忙看看
#include"stdafx.h"
#include"ace/Auto_Ptr.h"
#include"ace/Log_Msg.h"
#include"ace/INET_Addr.h"
#include"ace/SOCK_Acceptor.h"
#include"ace/Reactor.h"
#include"ace/Message_Block.h"
#include"ace/Message_Queue.h"
#include"ace/SOCK_Stream.h"
#include "ace/Synch.h"
class ClientAcceptor:public ACE_Event_Handler
{
public:
virtual ~ClientAcceptor();
int open(const ACE_INET_Addr &listen_addr);
virtual ACE_HANDLE get_handle(void)const{return this->acceptor_.get_handle();}
virtual int handle_input(ACE_HANDLE fd=ACE_INVALID_HANDLE);
virtual int handle_close(ACE_HANDLE handle,ACE_Reactor_Mask close_mask);
protected:
ACE_SOCK_Acceptor acceptor_;
};
class ClientService:public ACE_Event_Handler
{
public:
ACE_SOCK_Stream &peer(){return this->sock_;};
int open();
virtual ACE_HANDLE get_handle()const{return this->sock_.get_handle();}
virtual int handle_input(ACE_HANDLE fd=ACE_INVALID_HANDLE);
virtual int handle_output(ACE_HANDLE fd=ACE_INVALID_HANDLE);
virtual int handle_close(ACE_HANDLE handle,ACE_Reactor_Mask close_mask);
private:
ACE_SOCK_Stream sock_;
ACE_Message_Queue<ACE_NULL_SYNCH>output_queue_;
};
#include"stdafx.h"
#include"ClientAcceptor.h"
#define MAXHOSTNAMELEN 128
int ClientAcceptor::open(const ACE_INET_Addr &listen_port)
{
if(this->acceptor_.open(listen_port,1)==-1)
ACE_ERROR_RETURN((LM_ERROR,ACE_TEXT("%p\n"),ACE_TEXT("acceptor.open")),-1);
return this->reactor()->register_handler(this,ACE_Event_Handler::ACCEPT_MASK);
}
int ClientAcceptor::handle_input(ACE_HANDLE)
{
ClientService *client;
ACE_NEW_RETURN(client, ClientService,-1);
auto_ptr<ClientService>p(client);
if(this->acceptor_.accept(client->peer())==-1)
ACE_ERROR_RETURN((LM_ERROR,ACE_TEXT("(%p|%t)%p\n"),ACE_TEXT("Failed to accept")ACE_TEXT("client connection")),-1);
p.release();
client->reactor(this->reactor());
if(client->open()==-1)
client->handle_close(ACE_INVALID_HANDLE,0);
return 0;
}
int ClientService::open(void)
{
ACE_TCHAR peer_name[MAXHOSTNAMELEN];
ACE_INET_Addr peer_addr;
if(this->sock_.get_remote_addr(peer_addr)==0&&peer_addr.addr_to_string(peer_name,MAXHOSTNAMELEN)==0)
ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%p|%t)Connection from %s\n"),peer_name));
return this->reactor()->register_handler(this,ACE_Event_Handler::READ_MASK);
}
int ClientAcceptor::handle_close(ACE_HANDLE,ACE_Reactor_Mask)
{
if(this->acceptor_.get_handle()!=ACE_INVALID_HANDLE)
{
ACE_Reactor_Mask m=ACE_Event_Handler::ACCEPT_MASK|ACE_Event_Handler::DONT_CALL;
this->reactor()->remove_handler(this,m);
}
return 0;
}
ClientAcceptor::~ClientAcceptor()
{
this->handle_close(ACE_INVALID_HANDLE,0);
}
int ClientService::handle_input(ACE_HANDLE)
{
const size_t INPUT_SIZE=4096;
char buffer[INPUT_SIZE];
ssize_t recv_cnt,send_cnt;
if((recv_cnt=this->sock_.recv(buffer,sizeof(buffer)))<=0)
{
ACE_DEBUG((LM_DEBUG,ACE_TEXT("(%p|%t)Connection closed\n")));
return -1;
}
send_cnt=this->sock_.send(buffer,static_cast<size_t>(recv_cnt));
if(send_cnt==recv_cnt)
return 0;
if(send_cnt==-1&&ACE_OS::last_error()!=EWOULDBLOCK)
ACE_ERROR_RETURN((LM_ERROR,ACE_TEXT("(%p|%t)%p\n"),ACE_TEXT("send")),0);
if(send_cnt==-1)
send_cnt=0;
ACE_Message_Block *mb;
size_t remaining=static_cast<size_t>(recv_cnt-send_cnt);
ACE_NEW_RETURN(mb,ACE_Message_Block(&buffer[send_cnt],remaining),-1);
int output_off=this->output_queue_.is_empty();
ACE_Time_Value nowait(ACE_OS::gettimeofday());
if(this->output_queue_.enqueue_tail(mb,&nowait)==-1)
{
ACE_ERROR((LM_ERROR,ACE_TEXT("(%p|%t)%p;discarding data\n"),ACE_TEXT("enqueue failed")));
mb->release();
return 0;
}
if(output_off)
{
return this->reactor()->register_handler(this,ACE_Event_Handler::WRITE_MASK);
}
return 0;
}
int ClientService::handle_output(ACE_HANDLE)
{
ACE_Message_Block *mb;
ACE_Time_Value nowait(ACE_OS::gettimeofday());
while(0==this->output_queue_.dequeue_head(mb,&nowait))
{
ssize_t send_cnt=this->sock_.send(mb->rd_ptr(),mb->length());
if(send_cnt==-1)
ACE_ERROR((LM_ERROR,ACE_TEXT("(%p|%t)%p\n"),ACE_TEXT("send")));
else
mb->rd_ptr(static_cast<size_t>(send_cnt));
if(mb->length()>0)
{
this->output_queue_.enqueue_head(mb);break;
}
mb->release();
}
return (this->output_queue_.is_empty())?-1:0;
}
int ClientService::handle_close(ACE_HANDLE,ACE_Reactor_Mask mask)
{
if(mask==ACE_Event_Handler::WRITE_MASK)return 0;
mask=ACE_Event_Handler::ALL_EVENTS_MASK|ACE_Event_Handler::DONT_CALL;
this->reactor()->remove_handler(this,mask);
this->sock_.close();
this->output_queue_.flush();
delete this;
return 0;
}
int main(int,char*[])
{
ACE_INET_Addr listen_port;
listen_port.set("HAStatus");
ClientAcceptor acceptor;
if(acceptor.open(listen_port)==-1)
return 1;
ACE_Reactor::instance()->run_reactor_event_loop();
//ACE_Reactor::run_event_loop();
return 0;
}
[解决办法]
初始化的时候调用ACE:init()来了吗
在windows平台下 必须调用这个来初始相关的socket
你采用debug模式 ,使用ACEd.lib和ACEd.dll可以看到堆栈
[解决办法]
貌似reactor_成员没有初始化啊。
int ClientAcceptor::open(const ACE_INET_Addr &listen_port)
{
if(this->acceptor_.open(listen_port,1)==-1)
ACE_ERROR_RETURN((LM_ERROR,ACE_TEXT("%p\n"),ACE_TEXT("acceptor.open")),-1);
// 加了下面两行就好了,不知道是不是这个原因
if (this->reactor() == 0)
this->reactor(new ACE_Reactor);
return this->reactor()->register_handler(this,ACE_Event_Handler::ACCEPT_MASK);
}