2 ** Example program from The ACE Programmer's Guide, Chapter 8.
3 ** Copyright 2003 Addison-Wesley. All Rights Reserved.
6 #include "HA_Proactive_Status.h"
7 #include "ace/Log_Msg.h"
8 #include "ace/Message_Block.h"
9 #include "ace/Proactor.h"
10 #include "ace/os_include/arpa/os_inet.h"
12 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
14 // Listing 1 code/ch08
16 HA_Proactive_Service::open (ACE_HANDLE h
, ACE_Message_Block
&)
19 if (this->reader_
.open (*this) != 0 ||
20 this->writer_
.open (*this) != 0 )
22 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"),
23 ACE_TEXT ("HA_Proactive_Service open")));
28 ACE_Message_Block
*mb
= 0;
29 ACE_NEW_NORETURN (mb
, ACE_Message_Block (1024));
30 if (this->reader_
.read (*mb
, mb
->space ()) != 0)
32 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"),
33 ACE_TEXT ("HA_Proactive_Service begin read")));
39 // mb is now controlled by Proactor framework.
44 // Listing 2 code/ch08
46 HA_Proactive_Service::handle_read_stream
47 (const ACE_Asynch_Read_Stream::Result
&result
)
49 ACE_Message_Block
&mb
= result
.message_block ();
50 if (!result
.success () || result
.bytes_transferred () == 0)
57 if (this->writer_
.write (mb
, mb
.length ()) != 0)
61 ACE_TEXT ("starting write")));
66 ACE_Message_Block
*new_mb
;
67 ACE_NEW_NORETURN (new_mb
, ACE_Message_Block (1024));
68 this->reader_
.read (*new_mb
, new_mb
->space ());
75 // Listing 3 code/ch08
77 HA_Proactive_Service::handle_write_stream
78 (const ACE_Asynch_Write_Stream::Result
&result
)
80 result
.message_block ().release ();
85 // The network address check only works for BSD-ish systems. This
86 // sort of network number accessor should be added to ACE_INET_Addr
88 #if defined (ACE_WIN32)
90 HA_Proactive_Acceptor::validate_connection
91 (const ACE_Asynch_Accept::Result
&,
99 // Listing 4 code/ch08
101 HA_Proactive_Acceptor::validate_connection (
102 const ACE_Asynch_Accept::Result
&,
103 const ACE_INET_Addr
& remote
,
104 const ACE_INET_Addr
& local
)
106 struct in_addr
*remote_addr
=
107 reinterpret_cast<struct in_addr
*> (remote
.get_addr ());
108 struct in_addr
*local_addr
=
109 reinterpret_cast<struct in_addr
*> (local
.get_addr ());
110 if (inet_netof (*local_addr
) == inet_netof (*remote_addr
))
117 #endif /* ACE_WIN32 */
120 ACE_TMAIN (int, ACE_TCHAR
*[])
122 // Listing 5 code/ch08
123 ACE_INET_Addr listen_addr
; // Set up with listen port
124 HA_Proactive_Acceptor aio_acceptor
;
125 if (0 != aio_acceptor
.open (listen_addr
,
128 ACE_DEFAULT_ASYNCH_BACKLOG
,
131 1)) // validate_new_connection
132 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
133 ACE_TEXT ("acceptor open")), 1);
137 // Listing 6 code/ch08
138 ACE_INET_Addr peer_addr
; // Set up peer addr
139 ACE_Asynch_Connector
<HA_Proactive_Service
> aio_connect
;
140 aio_connect
.connect (peer_addr
);
144 // Listing 7 code/ch08
145 ACE_Proactor::instance ()->proactor_run_event_loop ();
153 ACE_TMAIN (int, ACE_TCHAR
*[])
155 ACE_DEBUG ((LM_DEBUG
,
156 ACE_TEXT ("This example requires asynchronous I/O support.\n")));
160 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */