summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/page14.html
blob: 1624577470e76a2cc7daabcfe51e26b8ed183fe8 (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
<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 implementation of Xmit isn't too complicated.  If we choose to
combine it with the Recv task we simply lift the recv() method from
that object and drop it into this one.
<P>
Note that close() must decide if it's being called when the stream is
shutdown or when it's svc() method exits.  Since we tell the baseclass 
not to use any threads it's a safe bet that flags will always be
non-zero.  Still, it's good practice to plan for the future by
checking the value.
<P>
Note also that when we send the data we prefix it with the data size.
This let's our sibling Recv ensure that an entire block is received
together.  This can be very important for compression and encryption
processes which typically work better with blocks of data instead of
streams of data.
<HR>
<PRE>

<font color=red>// $Id$</font>

<font color=blue>#ifndef</font> <font color=purple>XMIT_H</font>
<font color=blue>#define</font> <font color=purple>XMIT_h</font>

<font color=blue>#include</font> "<font color=green>Protocol_Task.h</font>"

<font color=red>// Forward reference reduces <font color=blue>#include</font> dependencies</font>
class ACE_SOCK_Stream;

<font color=red>/* A class suitable for sending data to a peer from within an
   ACE_Stream.
 */</font>
class Xmit : public Protocol_Task
{
public:

    typedef Protocol_Task inherited;

     <font color=red>// We must be given a valid peer when constructed.  Without that</font>
     <font color=red>// we don't know who to send data to.</font>
    Xmit( ACE_SOCK_Stream & _peer );

    ~Xmit(void);

     <font color=red>// As you know, close() will be called in a couple of ways by the</font>
     <font color=red>// ACE framework.  We use that opportunity to terminate the</font>
     <font color=red>// connection to the peer.</font>
    int close(u_long flags);

protected:

    ACE_SOCK_Stream & peer(void)
        {
            return this->peer_;
        }

     <font color=red>// Send the data to the peer.  By now it will have been</font>
     <font color=red>// completely protocol-ized by other tasks in the stream.</font>
    int send(ACE_Message_Block *message,
             ACE_Time_Value *timeout);

private:
     <font color=red>// A representation of the peer we're talking to.</font>
    ACE_SOCK_Stream & peer_;
};

<font color=blue>#endif</font> <font color=red>// XMIT_H</font>
</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page15.html">Continue This Tutorial</A>]</CENTER>