blob: a6371dcc60ad95253b3ce375c92e424347605e56 (
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
95
96
97
|
<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 Protocol_Task implementation takes care of the open(), close(),
put() and svc() methods so that derivatives can concentrate on the
send() and recv() methods. After a while you find that most
ACE_Task<> derivatives look very similar in the four basic methods and
only need one or two additional to do any real work.
<HR>
<PRE>
<font color=red>// $Id$</font>
<font color=blue>#include</font> "<font color=green>Protocol_Task.h</font>"
<font color=red>// Construct the object and remember the thread count.</font>
<font color=#008888>Protocol_Task::Protocol_Task</font>(void)
{
;
}
<font color=#008888>Protocol_Task::~Protocol_Task</font>(void)
{
;
}
int <font color=#008888>Protocol_Task::open</font>(void *arg)
{
ACE_UNUSED_ARG(arg);
return(0);
}
int <font color=#008888>Protocol_Task::close</font>(u_long flags)
{
return 0;
}
<font color=red>/* When a message is put() onto the task, it's time to process() some data.
*/</font>
int <font color=#008888>Protocol_Task::put</font>(ACE_Message_Block *message,ACE_Time_Value *timeout)
{
return this->process(message,timeout);
}
<font color=red>/* Return an error since we don't want the task to ever be activated.
*/</font>
int <font color=#008888>Protocol_Task::svc</font>(void)
{
return -1;
}
<font color=red>/* There's nothing really magic about process(). We just decide if
we're moving data upstream or downstream and invoke the appropriate
virtual function to handle it.
*/</font>
int <font color=#008888>Protocol_Task::process</font>(ACE_Message_Block * message, ACE_Time_Value *timeout)
{
if( this->is_writer() )
{
return this->send(message,timeout);
}
return this->recv(message,timeout);
}
<font color=red>/* We must insist that derivatives provide a meaningful overload for
these methods. It's fairly common for ACE object methods to return
an error when an overload is expected but the method cannot be
safely made pure virtual.
*/</font>
int <font color=#008888>Protocol_Task::send</font>(ACE_Message_Block *message,
ACE_Time_Value *timeout)
{
return -1;
}
int <font color=#008888>Protocol_Task::recv</font>(ACE_Message_Block * message,
ACE_Time_Value *timeout)
{
return -1;
}
</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page14.html">Continue This Tutorial</A>]</CENTER>
|