#include "ace/OS_main.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Reactor.h"
#include "ace/Process.h"
#include "ace/SOCK_Dgram.h"
#include "ace/INET_Addr.h"
#include "ace/Log_Msg.h"
class Dgram_Endpoint : public ACE_Event_Handler
{
public:
Dgram_Endpoint (const ACE_INET_Addr &local_addr);
virtual ACE_HANDLE get_handle (void) const;
virtual int handle_input (ACE_HANDLE handle);
virtual int handle_timeout (const ACE_Time_Value & tv,const void *arg = 0);
virtual int handle_close (ACE_HANDLE handle,ACE_Reactor_Mask close_mask);
virtual int handle_signal (int signum,siginfo_t*,ucontext_t*);
int send (const char *buf,size_t len,const ACE_INET_Addr &);
private:
ACE_SOCK_Dgram endpoint_;
};
int Dgram_Endpoint::send (const char *buf,const ACE_INET_Addr &addr)
{
return this->endpoint_.send (buf,len,addr);
}
Dgram_Endpoint::Dgram_Endpoint (const ACE_INET_Addr &local_addr)
: endpoint_(local_addr)
{
}
ACE_HANDLE Dgram_Endpoint::get_handle (void) const
{
return this->endpoint_.get_handle ();
}
int Dgram_Endpoint::handle_close (ACE_HANDLE handle,ACE_Reactor_Mask)
{
ACE_DEBUG((LM_DEBUG,"************handle_close***********/n"));
ACE_UNUSED_ARG (handle);
this->endpoint_.close();
delete this;
return 0;
}
int Dgram_Endpoint::handle_input (ACE_HANDLE)
{
char buf[BUFSIZ];
ACE_INET_Addr from_addr;
char address[32];
ACE_DEBUG ((LM_DEBUG,"(%P|%t) activity occurred on handle %d!/n",this->endpoint_.get_handle ()));
ssize_t n = this->endpoint_.recv (buf,sizeof buf,from_addr);
memset(address,sizeof(address));
from_addr.addr_to_string(address,sizeof(address),1);
if (n == -1)
ACE_ERROR ((LM_ERROR,"%p/n","handle_input"));
else
ACE_DEBUG ((LM_DEBUG,"[%d]bytes from[%s] received:%s/n",n,address,buf));
return 0;
}
int Dgram_Endpoint::handle_timeout (const ACE_Time_Value &,
const void *)
{
ACE_DEBUG ((LM_DEBUG,"(%P|%t) timed out for endpoint/n"));
return 0;
}
int Dgram_Endpoint::handle_signal (int signum,siginfo_t* siginfo,ucontext_t* context)
{
return ACE_Event_Handler::handle_signal (signum,siginfo,context);
}
int ACE_TMAIN (int argc,ACE_TCHAR *argv[])
{
ACE_INET_Addr local_addr(10101);
Dgram_Endpoint *endpoint;
ACE_NEW_RETURN (endpoint,Dgram_Endpoint (local_addr),-1);
// Read data from other side.
if (ACE_Reactor::instance ()->register_handler(endpoint,ACE_Event_Handler::READ_MASK) == -1)
{
ACE_ERROR_RETURN ((LM_ERROR,"ACE_Reactor::register_handler"),-1);
}
#if 0
if (-1 == ACE_Reactor::instance()->register_handler(SIGINT,endpoint))
{
ACE_ERROR_RETURN((LM_ERROR,"fail to register SIGINT handler"),-1);
}
#endif
ACE_Reactor::instance()->run_event_loop();
/*
while(1)
{
ACE_Time_Value tv (10,0);
if (ACE_Reactor::instance ()->handle_events (tv) <= 0) { ACE_ERROR_RETURN ((LM_DEBUG,"(%P|%t) %p/n","handle_events"),-1); } } */ return 0;}
原文链接:https://www.f2er.com/react/308582.html