/* UDP_Peer_Event_Handler.h
*/
#ifndef _UDP_PEER_EVENT_HANDLER_H_
#define _UDP_PEER_EVENT_HANDLER_H_
#include "ace/SOCK_Dgram.h"
#include "ace/INET_Addr.h"
class UDP_Peer_Event_Handler : public ACE_Event_Handler
{
typedef ACE_Event_Handler super;
public:
UDP_Peer_Event_Handler (const ACE_INET_Addr &local_addr);
virtual ~UDP_Peer_Event_Handler();
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* siginfo, ucontext_t* context);
int send (const char *buf, size_t len, const ACE_INET_Addr &addr);
private:
ACE_SOCK_Dgram gram_;
};
#endif // _UDP_PEER_EVENT_HANDLER_H_
/* UDP_Peer_Event_Handler.cpp
*/
#include "UDP_Peer_Event_Handler.h"
UDP_Peer_Event_Handler::UDP_Peer_Event_Handler (const ACE_INET_Addr &local_addr)
: gram_(local_addr)
{
ACE_DEBUG((LM_DEBUG, "UDP_Peer_Event_Handler constructor was called.\n"));
}
UDP_Peer_Event_Handler::~UDP_Peer_Event_Handler()
{
ACE_DEBUG((LM_DEBUG, "UDP_Peer_Event_Handler destructor was called.\n"));
this->gram_.close();
}
ACE_HANDLE UDP_Peer_Event_Handler::get_handle (void) const
{
return this->gram_.get_handle();
}
int UDP_Peer_Event_Handler::handle_close (ACE_HANDLE handle, ACE_Reactor_Mask mask)
{
ACE_DEBUG((LM_DEBUG, "************handle_close***********\n"));
if (mask == ACE_Event_Handler::WRITE_MASK)
return 0;
return super::handle_close(handle, mask);
}
int UDP_Peer_Event_Handler::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->gram_.get_handle ()));
ssize_t nbytes = this->gram_.recv (buf, sizeof(buf), from_addr);
if (nbytes == -1)
{
ACE_ERROR ((LM_ERROR,"%p","handle_input error/n"));
return -1;
}
ACE_DEBUG ((LM_DEBUG, "[%d]bytes from[%s] received:%s/n", nbytes, address, buf));
return 0;
}
int UDP_Peer_Event_Handler::handle_timeout (const ACE_Time_Value &,const void *)
{
ACE_DEBUG ((LM_DEBUG,"(%P|%t) timed out for udp/n"));
return 0;
}
int UDP_Peer_Event_Handler::handle_signal (int signum, siginfo_t* siginfo, ucontext_t* context)
{
return ACE_Event_Handler::handle_signal (signum, siginfo, context);
}
int UDP_Peer_Event_Handler::send (const char *buf,size_t len,const ACE_INET_Addr &addr)
{
return this->gram_.send (buf, len, addr);
}
/* ACE_UDP_Server.cpp
*/
#include "UDP_Peer_Event_Handler.h"
#include "ace/OS_main.h"
#include "ace/Reactor.h"
static const u_short SERVER_PORT = 10101;
int ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_INET_Addr local_addr(SERVER_PORT);
UDP_Peer_Event_Handler *udp;
ACE_NEW_RETURN(udp, UDP_Peer_Event_Handler(local_addr), -1);
if (ACE_Reactor::instance()->register_handler(udp, ACE_Event_Handler::READ_MASK) == -1)
{
ACE_ERROR_RETURN((LM_ERROR, "ACE_Reactor::register_handler"), -1);
}
ACE_Reactor::instance()->run_event_loop();
return 0;
}
/* ACE_UDP_Client.cpp
*/
#include "UDP_Peer_Event_Handler.h"
#include "ace/OS_main.h"
#include "ace/Reactor.h"
static const u_short SERVER_PORT = 10101;
static const char *SERVER_HOST_NAME = "127.0.0.1";
int ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_INET_Addr local_addr((u_short)0);
UDP_Peer_Event_Handler *udp;
ACE_NEW_RETURN(udp, UDP_Peer_Event_Handler(local_addr), -1);
ACE_INET_Addr remote_addr(SERVER_PORT, SERVER_HOST_NAME);
int ret = udp->send("hello", 5, remote_addr); // SEND MESSAGE
if (ACE_Reactor::instance()->register_handler(udp, ACE_Event_Handler::WRITE_MASK) == -1)
{
ACE_ERROR_RETURN((LM_ERROR, "ACE_Reactor::register_handler"), -1);
}
ACE_Reactor::instance()->run_event_loop();
return 0;
}