1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
** $Id: HA_Proactive_Status.cpp 91626 2010-09-07 10:59:20Z johnnyw $
**
** Example program from The ACE Programmer's Guide, Chapter 8.
** Copyright 2003 Addison-Wesley. All Rights Reserved.
*/
#include "HA_Proactive_Status.h"
#include "ace/Log_Msg.h"
#include "ace/Message_Block.h"
#include "ace/Proactor.h"
#include "ace/os_include/arpa/os_inet.h"
#if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
// Listing 1 code/ch08
void
HA_Proactive_Service::open (ACE_HANDLE h, ACE_Message_Block&)
{
this->handle (h);
if (this->reader_.open (*this) != 0 ||
this->writer_.open (*this) != 0 )
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
ACE_TEXT ("HA_Proactive_Service open")));
delete this;
return;
}
ACE_Message_Block *mb = 0;
ACE_NEW_NORETURN (mb, ACE_Message_Block (1024));
if (this->reader_.read (*mb, mb->space ()) != 0)
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
ACE_TEXT ("HA_Proactive_Service begin read")));
mb->release ();
delete this;
return;
}
// mb is now controlled by Proactor framework.
return;
}
// Listing 1
// Listing 2 code/ch08
void
HA_Proactive_Service::handle_read_stream
(const ACE_Asynch_Read_Stream::Result &result)
{
ACE_Message_Block &mb = result.message_block ();
if (!result.success () || result.bytes_transferred () == 0)
{
mb.release ();
delete this;
}
else
{
if (this->writer_.write (mb, mb.length ()) != 0)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("starting write")));
mb.release ();
}
else
{
ACE_Message_Block *new_mb;
ACE_NEW_NORETURN (new_mb, ACE_Message_Block (1024));
this->reader_.read (*new_mb, new_mb->space ());
}
}
return;
}
// Listing 2
// Listing 3 code/ch08
void
HA_Proactive_Service::handle_write_stream
(const ACE_Asynch_Write_Stream::Result &result)
{
result.message_block ().release ();
return;
}
// Listing 3
// The network address check only works for BSD-ish systems. This
// sort of network number accessor should be added to ACE_INET_Addr
// at some point...
#if defined (ACE_WIN32)
int
HA_Proactive_Acceptor::validate_connection
(const ACE_Asynch_Accept::Result&,
const ACE_INET_Addr&,
const ACE_INET_Addr&)
{
return 0;
}
#else
// Listing 4 code/ch08
int
HA_Proactive_Acceptor::validate_connection (
const ACE_Asynch_Accept::Result&,
const ACE_INET_Addr& remote,
const ACE_INET_Addr& local)
{
struct in_addr *remote_addr =
reinterpret_cast<struct in_addr*> (remote.get_addr ());
struct in_addr *local_addr =
reinterpret_cast<struct in_addr*> (local.get_addr ());
if (inet_netof (*local_addr) == inet_netof (*remote_addr))
return 0;
return -1;
}
// Listing 4
#endif /* ACE_WIN32 */
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
// Listing 5 code/ch08
ACE_INET_Addr listen_addr; // Set up with listen port
HA_Proactive_Acceptor aio_acceptor;
if (0 != aio_acceptor.open (listen_addr,
0, // bytes_to_read
0, // pass_addresses
ACE_DEFAULT_ASYNCH_BACKLOG,
1, // reuse_addr
0, // proactor
1)) // validate_new_connection
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
ACE_TEXT ("acceptor open")), 1);
// Listing 5
#if 0
// Listing 6 code/ch08
ACE_INET_Addr peer_addr; // Set up peer addr
ACE_Asynch_Connector<HA_Proactive_Service> aio_connect;
aio_connect.connect (peer_addr);
// Listing 6
#endif
// Listing 7 code/ch08
ACE_Proactor::instance ()->proactor_run_event_loop ();
// Listing 7
return 0;
}
#else
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("This example requires asynchronous I/O support.\n")));
return 1;
}
#endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */
|