summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/page08.html
blob: ffa7d9c63c99a4c9e086ca7ab342dd7abdd30399 (plain)
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
<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>// $Id$</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> "<font color=green>ace/Svc_Handler.h</font>"

<font color=blue>#if !defined</font> (<font color=purple>ACE_LACKS_PRAGMA_ONCE</font>)
# pragma once
<font color=blue>#endif</font> <font color=red>/* ACE_LACKS_PRAGMA_ONCE */</font>

<font color=blue>#include</font> "<font color=green>ace/SOCK_Stream.h</font>"
<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&lt;> 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 &lt; 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</font>
        <font color=red>// client connection.</font>
    int open (void *);

        <font color=red>// Called when it's time for us to be deleted.  We take care</font>
        <font color=red>// of removing ourselves from the reactor and shutting down</font>
        <font color=red>// the peer() 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</font>
        <font color=red>// use 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</font>
        <font color=red>// code.  That's our signal that it's time to begin the</font>
        <font color=red>// 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</font>
        <font color=red>// requirements.  We use a local Protocol_Stream object to</font>
        <font color=red>// take care of those details.  For us, I/O then just becomes</font>
        <font color=red>// a matter of interacting 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>