summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/page08.html
blob: 19a74c6869023d1eed2024c67f2bf84e4b4ab579 (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
<!-- $Id$ -->
<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> "<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&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 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>