<!-- page08.html,v 1.16 2003/08/19 15:08:26 schmidt Exp --> <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="James CE Johnson"> <TITLE>ACE Tutorial 015</TITLE> </HEAD> <BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F"> <CENTER><B><FONT SIZE=+2>ACE Tutorial 015</FONT></B></CENTER> <CENTER><B><FONT SIZE=+2>Building a protocol stream</FONT></B></CENTER> <P> <HR WIDTH="100%"> The Handler object is our event handler. You can use either ACE_Event_Handler or ACE_Svc_Handler<> for the baseclass. I generally prefer the latter since it takes care of some housekeeping that I would otherwise be responsible for. <P> The class declaration is taken almost exactly from a previous tutorial. A good design will have a simple handler object that will collect data from the peer and pass it along to another object for processing. Again, keep it simple and delegate authority. <HR> <PRE> <font color=red>// page08.html,v 1.16 2003/08/19 15:08:26 schmidt Exp</font> <font color=blue>#ifndef</font> <font color=purple>HANDLER_H</font> <font color=blue>#define</font> <font color=purple>HANDLER_H</font> <font color=blue>#include</font> "<A HREF="../../../ace/Svc_Handler.h">ace/Svc_Handler.h</A>" <font color=blue>#if !defined</font> (<font color=purple>ACE_LACKS_PRAGMA_ONCE</font>) <font color=blue># pragma</font> <font color=purple>once</font> <font color=blue>#endif</font> <font color=red>/* ACE_LACKS_PRAGMA_ONCE */</font> <font color=blue>#include</font> "<A HREF="../../../ace/SOCK_Stream.h">ace/SOCK_Stream.h</A>" <font color=blue>#include</font> "<font color=green>Protocol_Stream.h</font>" <font color=red>/* Just your basic event handler. We use ACE_Svc_Handler<> as a baseclass so that it can maintain the peer() and other details for us. We're not going to activate() this object, so we can get away with the NULL synch choice. */</font> class Handler : public ACE_Svc_Handler <ACE_SOCK_STREAM, ACE_NULL_SYNCH> { public: Handler (void); ~Handler (void); <font color=red>// Called by the acceptor when we're created in response to a client</font> <font color=red>// connection.</font> int open (void *); <font color=red>// Called when it's time for us to be deleted. We take care of</font> <font color=red>// removing ourselves from the reactor and shutting down the peer()</font> <font color=red>// connectin.</font> void destroy (void); <font color=red>// Called when it's time for us to go away. There are subtle</font> <font color=red>// differences between destroy() and close() so don't try to use</font> <font color=red>// either for all cases.</font> int close (u_long); protected: <font color=red>// Respond to peer() activity.</font> int handle_input (ACE_HANDLE); <font color=red>// This will be called when handle_input() returns a failure code.</font> <font color=red>// That's our signal that it's time to begin the shutdown process.</font> int handle_close (ACE_HANDLE, ACE_Reactor_Mask mask); private: <font color=red>// Like the Client, we have to abide by the protocol requirements.</font> <font color=red>// We use a local Protocol_Stream object to take care of those</font> <font color=red>// details. For us, I/O then just becomes a matter of interacting</font> <font color=red>// with the stream.</font> Protocol_Stream stream_; Protocol_Stream &stream (void) { return this->stream_; } }; <font color=blue>#endif</font> <font color=red>/* HANDLER_H */</font> </PRE> <P><HR WIDTH="100%"> <CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page09.html">Continue This Tutorial</A>]</CENTER>