整体的主要是服务端的,唯一比较大的区别来说应该是比example来的更好理解(个人感受)
下面上代码
//acceptor.h
#include "ace/Event_Handler.h"
#include "ace/INET_Addr.h"
#include "ace/Reactor.h"
#include "ace/SOCK_Stream.h"
#include "ace/SOCK_Acceptor.h"
class Handle_data;
class Acceptor: public ACE_Event_Handler
{
public:
Acceptor( ACE_Reactor *r = ACE_Reactor::instance() ):ACE_Event_Handler(r){}
ACE_INT32 open( const ACE_UINT16 port );
ACE_INT32 handle_input( ACE_HANDLE = ACE_INVALID_HANDLE );
ACE_INT32 handle_close( ACE_HANDLE = ACE_INVALID_HANDLE, ACE_Reactor_Mask = 0 );
ACE_HANDLE get_handle() const
{
return acceptor_.get_handle();
}
ACE_SOCK_Acceptor &get_acceptor(){ return acceptor_; }
private:
~Acceptor(){};
private:
ACE_SOCK_Acceptor acceptor_;
};
#include "ace/Log_Msg.h"
#include "acceptor.h"
#include "handle_data.h"
ACE_INT32 Acceptor::open( ACE_UINT16 port )
{
ACE_INET_Addr addr;
addr.set( port, (ACE_UINT32)INADDR_ANY );
if (acceptor_.open( addr ) == -1)
{
ACE_DEBUG( (LM_DEBUG, "accept open error\n") );
return -1;
}
return reactor()->register_handler( this, ACE_Event_Handler::ACCEPT_MASK );
}
ACE_INT32 Acceptor::handle_input( ACE_HANDLE handle )
{
Handle_data *handle_data = 0;
ACE_NEW_RETURN( handle_data, Handle_data( reactor() ), -1 );
//等待链接
if (acceptor_.accept( handle_data->get_peer() ) == -1)
{
ACE_DEBUG( (LM_DEBUG, "accept handle input accept error!\n") );
handle_data->handle_close();
return -1;
}
//获取远程链接地址
else if (handle_data->open() == -1)
{
ACE_DEBUG( (LM_DEBUG, "accept handle input open error!\n") );
handle_data->handle_close();
return -1;
}
else
{
ACE_DEBUG( (LM_DEBUG, "accept handle input ok!\n") );
return 0;
}
}
ACE_INT32 Acceptor::handle_close( ACE_HANDLE handle, ACE_Reactor_Mask mask )
{
acceptor_.close();
delete this;
ACE_DEBUG( (LM_DEBUG, "accept handle close ok!\n") );
return 0;
}
handle_data.h
#include "ace/Event_Handler.h"
#include "ace/SOCK_Stream.h"
#include "ace/INET_Addr.h"
#include "ace/Reactor.h"
#include "ace/Time_Value.h"
class Handle_data: public ACE_Event_Handler
{
public:
Handle_data( ACE_Reactor *r = ACE_Reactor::instance() ):ACE_Event_Handler(r) {}
ACE_INT32 open( );
ACE_INT32 handle_input( ACE_HANDLE = ACE_INVALID_HANDLE );
ACE_INT32 handle_close( ACE_HANDLE = ACE_INVALID_HANDLE, ACE_Reactor_Mask mask = 0 );
ACE_INT32 handle_output (ACE_HANDLE fd = ACE_INVALID_HANDLE);
ACE_HANDLE get_handle( ) const
{
return peer_.get_handle();
}
ACE_SOCK_Stream &get_peer()
{
return peer_;
}
private:
~Handle_data(){ACE_DEBUG( (LM_DEBUG, "handle data ~dctor .\n") );};
private:
ACE_SOCK_Stream peer_;
};
handle_data.cpp
#include "ace/SOCK_Stream.h"
#include "ace/Reactor.h"
#include "ace/Event_Handler.h"
#include "ace/Log_Msg.h"
#include "ace/Timer_Queue.h"
#include "handle_data.h"
#include <iostream>
ACE_INT32 Handle_data::open( )
{
ACE_INT32 ret = 0;
ACE_INET_Addr remote_addr;
get_peer().get_remote_addr( remote_addr );
ACE_DEBUG( (LM_DEBUG, "the remote addr is %s\n", remote_addr.get_host_addr()) );
//注册句柄
//ACE_Event_Handler::ACCEPT_MASK
ret = reactor()->register_handler( this, ACE_Event_Handler::READ_MASK| ACE_Event_Handler::WRITE_MASK);
if (ret != -1)
{
ACE_DEBUG( (LM_DEBUG, "handle data register ok!\n") );
}
return ret;
}
ACE_INT32 Handle_data::handle_close( ACE_HANDLE , ACE_Reactor_Mask )
{
get_peer().close();
ACE_DEBUG( (LM_DEBUG, "handle data close.\n") );
delete this;
return 0;
}
//数据接收
ACE_INT32 Handle_data::handle_input( ACE_HANDLE )
{
ACE_INT8 buf[512] = {0};
ACE_INT32 len;
len = get_peer().recv( buf, 500);
if (len > 0)
{
ACE_DEBUG( (LM_DEBUG, "recv data %s, len:%d.\n", buf, len) );
return 0;
}
else if (len == 0)
{
ACE_DEBUG( (LM_DEBUG, "recv data len is 0, client exit.\n") );
return -1;
}
else
{
ACE_DEBUG( (LM_DEBUG, "recv data error len < 0" ) );
return -1;
}
}
//数据发送
ACE_INT32 Handle_data::handle_output( ACE_HANDLE fd /*= ACE_INVALID_HANDLE*/ )
{
get_peer().send_n("hello Client\n",12);
return 0;
}
main.cpp
#include "ace/Log_Msg.h"
#include "acceptor.h"
#include "handle_data.h"
ACE_INT32 main( ACE_INT32 argc, char **argv )
{
ACE_UINT16 port = 0;
/*if (argc < 2)
return -1;*/
//port = ACE_OS::atoi(argv[1]);
port = 60000;
Acceptor *accept;
ACE_NEW_RETURN( accept, Acceptor( ACE_Reactor::instance () ),-1 );
if (accept->open( port ) == -1)
{
accept->handle_close();
ACE_DEBUG( (LM_DEBUG, "main open error!\n") );
return -1;
}
if (ACE_Reactor::run_event_loop() ==-1)
{
accept->handle_close();
ACE_DEBUG( (LM_DEBUG, "main run event loop error!\n") );
return -1;
}
accept->handle_close();
return 0;
}