ACE_Reactor Server一例的增强版
1
/*
***********************************************************************
2 * @file: test02.cpp
3 * @author: AGP
4 * @revise: Stone Jiang <[email protected]> http://www.cppblog.com/ace
5 * 增加了用户可调试性;解决了内存问题,注原作者要求忽略内存问题,关注于
6 Reactor本身
7
8 *********************************************************************** */
9
10 #ifdef _DEBUG
11 #pragma comment (lib, " aced.lib " )
12 #else
13 #pragma comment (lib, " ace.lib " )
14 #endif
15
16 #include " ace/Reactor.h "
17 #include " ace/SOCK_Acceptor.h "
18 #include " ace/os.h "
19
20 #define PORT_NO 19998
21 typedef ACE_SOCK_Acceptor Acceptor;
22 // forward declaration
23 class My_Accept_Handler;
24
25 class My_Input_Handler: public ACE_Event_Handler
26 {
27 public :
28 // construcor
29 My_Input_Handler()
30 {
31 ACE_DEBUG((LM_DEBUG, " Constructor\n " ));
32 }
33
34 ~ My_Input_Handler()
35 {
36 ACE_DEBUG((LM_DEBUG, " Deconstructor\n " ));
37 }
38
39 // Called back to handle any input received
40 int handle_input(ACE_HANDLE)
41 {
42 // receive the data
43 ssize_t recvBytes = peer().recv(data, 12 );
44 if (recvBytes <= 0 )
45 {
46 ACE_DEBUG((LM_DEBUG, " %s\n " , " 客户端断开连接 " ));
47 return - 1 ;
48 }
49 data[recvBytes] = 0 ;
50
51 ACE_DEBUG((LM_DEBUG, " %s\n " ,data));
52
53
54 if (ACE_OS::strcmp(data, " q " ) == 0 )
55 {
56 ACE_DEBUG((LM_DEBUG, " %s\n " , " 客户端要求服务端停止服务 " ));
57 peer().close();
58 ACE_Reactor::instance() -> end_reactor_event_loop();
59 return - 1 ;
60 }
61 peer().send_n(data,recvBytes);
62 // do something with the input received.
63 //
64 // keep yourself registerd with the reator
65 return 0 ;
66 }
67
68 int handle_close(ACE_HANDLE h,ACE_Reactor_Mask m)
69 {
70 delete this ;
71 return 0 ;
72 }
73
74 // Used by the reactor to determine the underlying handle
75 ACE_HANDLE get_handle() const
76 {
77 return this -> peer_.get_handle();
78 }
79
80 // Returns a reference to the underlying stream.
81 ACE_SOCK_Stream & peer()
82 {
83 return this -> peer_;
84 }
85
86 private :
87 ACE_SOCK_Stream peer_;
88 char data [ 12 ];
89 };
90
91 class My_Accept_Handler: public ACE_Event_Handler
92 {
93 public :
94 // Constructor
95 My_Accept_Handler(ACE_Addr & addr)
96 {
97 this -> open(addr);
98 }
99
100 // Open the peer_acceptor so it starts to "listen"
101 // for incoming clients
102 int open(ACE_Addr & addr)
103 {
104 peer_acceptor.open(addr);
105 return 0 ;
106 }
107
108 // Overload the handle input method
109 int handle_input(ACE_HANDLE handle)
110 {
111 // Client has requested connection to server.
112 // Create a handler to handle the connection
113 My_Input_Handler * eh = new My_Input_Handler();
114
115 // Accept the connection "into" the Event Handler
116 if ( this -> peer_acceptor.accept(eh -> peer(), // stream
117 0 , // remote address
118 0 , // timeout
119 1 ) == - 1 ) // restart if interrupted
120 ACE_DEBUG((LM_ERROR, " Error in connection \n " ));
121
122 ACE_DEBUG((LM_DEBUG, " Connection established\n " ));
123
124 // Register the input event handler for reading
125 ACE_Reactor::instance() -> register_handler(eh,ACE_Event_Handler::READ_MASK);
126 const char * msg = " 按q键使服务安全退出\r\n " ;
127 eh -> peer().send_n(msg,strlen(msg) + 1 );
128 // Unregister as the acceptor is not expecting new clients
129 return - 1 ;
130 }
131
132 // Used by the reactor to determine the underlying handle
133 ACE_HANDLE get_handle( void ) const
134 {
135 return this -> peer_acceptor.get_handle();
136 }
137
138 private :
139 Acceptor peer_acceptor;
140 };
141
142 int ACE_TMAIN( int argc, char * argv[])
143 {
144 // Create an address on which to receive connections
145
146 ACE_INET_Addr addr(PORT_NO);
147
148 // Create the Accept Handler which automatically begins to "listen"
149 // for client requests for connections
150 My_Accept_Handler * eh = new My_Accept_Handler(addr);
151
152 // Register the reator to call back when incoming client connects
153 ACE_Reactor::instance() -> register_handler(eh,ACE_Event_Handler::ACCEPT_MASK);
154
155 // Start the event loop
156
157 ACE_Reactor::instance() -> run_reactor_event_loop();
158
159 delete eh;
160
161 return 0 ;
162 }
2 * @file: test02.cpp
3 * @author: AGP
4 * @revise: Stone Jiang <[email protected]> http://www.cppblog.com/ace
5 * 增加了用户可调试性;解决了内存问题,注原作者要求忽略内存问题,关注于
6 Reactor本身
7
8 *********************************************************************** */
9
10 #ifdef _DEBUG
11 #pragma comment (lib, " aced.lib " )
12 #else
13 #pragma comment (lib, " ace.lib " )
14 #endif
15
16 #include " ace/Reactor.h "
17 #include " ace/SOCK_Acceptor.h "
18 #include " ace/os.h "
19
20 #define PORT_NO 19998
21 typedef ACE_SOCK_Acceptor Acceptor;
22 // forward declaration
23 class My_Accept_Handler;
24
25 class My_Input_Handler: public ACE_Event_Handler
26 {
27 public :
28 // construcor
29 My_Input_Handler()
30 {
31 ACE_DEBUG((LM_DEBUG, " Constructor\n " ));
32 }
33
34 ~ My_Input_Handler()
35 {
36 ACE_DEBUG((LM_DEBUG, " Deconstructor\n " ));
37 }
38
39 // Called back to handle any input received
40 int handle_input(ACE_HANDLE)
41 {
42 // receive the data
43 ssize_t recvBytes = peer().recv(data, 12 );
44 if (recvBytes <= 0 )
45 {
46 ACE_DEBUG((LM_DEBUG, " %s\n " , " 客户端断开连接 " ));
47 return - 1 ;
48 }
49 data[recvBytes] = 0 ;
50
51 ACE_DEBUG((LM_DEBUG, " %s\n " ,data));
52
53
54 if (ACE_OS::strcmp(data, " q " ) == 0 )
55 {
56 ACE_DEBUG((LM_DEBUG, " %s\n " , " 客户端要求服务端停止服务 " ));
57 peer().close();
58 ACE_Reactor::instance() -> end_reactor_event_loop();
59 return - 1 ;
60 }
61 peer().send_n(data,recvBytes);
62 // do something with the input received.
63 //
64 // keep yourself registerd with the reator
65 return 0 ;
66 }
67
68 int handle_close(ACE_HANDLE h,ACE_Reactor_Mask m)
69 {
70 delete this ;
71 return 0 ;
72 }
73
74 // Used by the reactor to determine the underlying handle
75 ACE_HANDLE get_handle() const
76 {
77 return this -> peer_.get_handle();
78 }
79
80 // Returns a reference to the underlying stream.
81 ACE_SOCK_Stream & peer()
82 {
83 return this -> peer_;
84 }
85
86 private :
87 ACE_SOCK_Stream peer_;
88 char data [ 12 ];
89 };
90
91 class My_Accept_Handler: public ACE_Event_Handler
92 {
93 public :
94 // Constructor
95 My_Accept_Handler(ACE_Addr & addr)
96 {
97 this -> open(addr);
98 }
99
100 // Open the peer_acceptor so it starts to "listen"
101 // for incoming clients
102 int open(ACE_Addr & addr)
103 {
104 peer_acceptor.open(addr);
105 return 0 ;
106 }
107
108 // Overload the handle input method
109 int handle_input(ACE_HANDLE handle)
110 {
111 // Client has requested connection to server.
112 // Create a handler to handle the connection
113 My_Input_Handler * eh = new My_Input_Handler();
114
115 // Accept the connection "into" the Event Handler
116 if ( this -> peer_acceptor.accept(eh -> peer(), // stream
117 0 , // remote address
118 0 , // timeout
119 1 ) == - 1 ) // restart if interrupted
120 ACE_DEBUG((LM_ERROR, " Error in connection \n " ));
121
122 ACE_DEBUG((LM_DEBUG, " Connection established\n " ));
123
124 // Register the input event handler for reading
125 ACE_Reactor::instance() -> register_handler(eh,ACE_Event_Handler::READ_MASK);
126 const char * msg = " 按q键使服务安全退出\r\n " ;
127 eh -> peer().send_n(msg,strlen(msg) + 1 );
128 // Unregister as the acceptor is not expecting new clients
129 return - 1 ;
130 }
131
132 // Used by the reactor to determine the underlying handle
133 ACE_HANDLE get_handle( void ) const
134 {
135 return this -> peer_acceptor.get_handle();
136 }
137
138 private :
139 Acceptor peer_acceptor;
140 };
141
142 int ACE_TMAIN( int argc, char * argv[])
143 {
144 // Create an address on which to receive connections
145
146 ACE_INET_Addr addr(PORT_NO);
147
148 // Create the Accept Handler which automatically begins to "listen"
149 // for client requests for connections
150 My_Accept_Handler * eh = new My_Accept_Handler(addr);
151
152 // Register the reator to call back when incoming client connects
153 ACE_Reactor::instance() -> register_handler(eh,ACE_Event_Handler::ACCEPT_MASK);
154
155 // Start the event loop
156
157 ACE_Reactor::instance() -> run_reactor_event_loop();
158
159 delete eh;
160
161 return 0 ;
162 }
/************************************************************************
* @file: test02.cpp
* @author: AGP
* @revise: Stone Jiang < [email protected]> http://www.cppblog.com/ace
* 增加了用户可调试性;解决了内存问题,注原作者要求忽略内存问题,关注于
Reactor本身
************************************************************************/