4 #include "ace/SOCK_Acceptor.h"
5 #include "ace/SOCK_Stream.h"
6 #include "ace/Acceptor.h"
7 #include "ace/Svc_Handler.h"
8 #include "ace/Reactor.h"
9 #include "ace/TP_Reactor.h"
10 #include "ace/Get_Opt.h"
14 ACE_TCHAR
const * hi_endpoint
= ACE_TEXT ("localhost:12345");
15 ACE_TCHAR
const * lo_endpoint
= ACE_TEXT ("localhost:23456");
18 parse_args (int argc
, ACE_TCHAR
*argv
[]);
20 class Task
: public ACE_Task_Base
23 Task (ACE_TCHAR
const *endpoint
);
28 ACE_TCHAR
const *endpoint_
;
33 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
35 /// Move the test to the real-time class if it is possible.
38 if (parse_args (argc
, argv
) != 0)
41 Task
hi_task (hi_endpoint
);
42 Task
lo_task (lo_endpoint
);
44 hi_task
.activate(rt_class
.thr_sched_class() | THR_NEW_LWP
| THR_JOINABLE
,
45 1, 1, rt_class
.priority_high());
47 lo_task
.activate(rt_class
.thr_sched_class() | THR_NEW_LWP
| THR_JOINABLE
,
48 nthreads
, 1, rt_class
.priority_low());
56 // ****************************************************************
58 class Svc_Handler
: public ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_SYNCH
>
61 Svc_Handler(ACE_Reactor
* reactor
= 0);
63 virtual int handle_input(ACE_HANDLE
);
64 virtual int handle_close (ACE_HANDLE
, ACE_Reactor_Mask
);
67 typedef ACE_Acceptor
<Svc_Handler
,ACE_SOCK_ACCEPTOR
> Acceptor
;
69 Svc_Handler::Svc_Handler (ACE_Reactor
* reactor
)
70 : ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_SYNCH
> (0, 0, reactor
)
72 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) Accepted connection\n"));
76 Svc_Handler::handle_input(ACE_HANDLE h
)
78 const size_t bufsize
= BUFSIZ
;
83 ssize_t n
= this->peer().recv(buf
, bufsize
);
86 "Connection %d closed while reading data\n",
90 if(errno
== EWOULDBLOCK
)
94 "Error on <%d> while reading %p\n",
99 ssize_t k
= this->peer().send(buf
, n
);
102 "Connection <%d> closed while writing\n",
105 } else if (k
== -1) {
106 if(errno
== EWOULDBLOCK
)
110 "Error on <%d> while writing %p\n",
115 "Short write on <%d>\n",
125 Svc_Handler::handle_close(ACE_HANDLE h
, ACE_Reactor_Mask m
)
127 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) Closed connection\n"));
128 this->peer().close();
130 this->ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_SYNCH
>::handle_close (h
,m
);
134 // ****************************************************************
136 Task::Task(ACE_TCHAR
const * endpoint
)
137 : endpoint_ (endpoint
)
138 , reactor_ (new ACE_TP_Reactor
)
140 ACE_INET_Addr
local_sap (endpoint_
);
141 Acceptor
* acceptor
= new Acceptor
;
143 if(acceptor
->open(local_sap
, &reactor_
, ACE_NONBLOCK
) == -1)
145 ACE_ERROR((LM_ERROR
, "Cannot open endpoint <%s>\n", endpoint_
));
152 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) Starting thread for %s\n", endpoint_
));
153 (void) reactor_
.run_reactor_event_loop();
157 // ****************************************************************
160 parse_args (int argc
, ACE_TCHAR
*argv
[])
162 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("h:l:n:r"));
165 while ((c
= get_opts ()) != -1)
169 hi_endpoint
= get_opts
.opt_arg ();
173 lo_endpoint
= get_opts
.opt_arg ();
177 nthreads
= ACE_OS::atoi (get_opts
.opt_arg ());
186 ACE_ERROR_RETURN ((LM_ERROR
,
195 // Indicates successful parsing of the command line