summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/Xmit.cpp
blob: 28be01b1d2543d7956cafd7de423ab60e23df21b (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

// $Id$

#include "Xmit.h"
#include "ace/SOCK_Stream.h"

/* Construct the object with the peer connection and choose not to
   activate ourselves into a dedicated thread.  You might get some
   performance gain by activating but if you really want a
   multi-threaded apprroach you should handle that as a separate
   issue.  Attempting to force threading at this level will likely
   cause more trouble than you want to deal with.
*/
Xmit::Xmit( ACE_SOCK_Stream & _peer )
        : Protocol_Task(), peer_(_peer)
{
}

Xmit::~Xmit(void)
{
}

/* Check to see if we're being closed by the stream (flags != 0) or if
   we're responding to the exit of our svc() method.
*/
int Xmit::close(u_long flags)
{
     // Take care of the baseclass closure.
    int rval = inherited::close(flags);

     // Only if we're being called at the stream shutdown do we close
     // the peer connection.  If, for some reason, we were activated
     // into one or more threads we wouldn't want to close the pipe
     // before all threads had a chance to flush their data.
    if( flags )
    {
        peer().close();
    }

    return( rval );
}

/* Our overload of send() will take care of sending the data to the
   peer.
*/
int Xmit::send(ACE_Message_Block *message, ACE_Time_Value *timeout)
{
    int rval;

    ACE_DEBUG ((LM_INFO, "(%P|%t) Xmit::send() sending (%s)(%d)\n", message->rd_ptr(), message->length() ));

     /* Since we're going to be sending data that may have been
        compressed and encrypted it's probably important for the
        receiver to get an entire "block" instead of having a
        partial read.

        For that reason, we'll send the length of the message block
        (in clear-text) to the peer so that it can then recv_n()
        the entire block contents in one read operation.
     */
    char msize[32];
    sprintf(msize,"%d",message->length());

     // Be sure we send the end-of-string NULL so that Recv will
     // know when to stop assembling the length.
    rval = this->peer().send_n( msize, strlen(msize)+1, 0, timeout );

    if( rval == -1 )
    {
        ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "Xmit::send() Failed to send message size."), -1);
    }

     /* Now we send the actual data.  If you're worried about
        network efficiency then you may choose to create one buffer
        containing msize and the message data and send it all at
        once.
     */
    rval = this->peer().send_n( message->rd_ptr(), message->length(), 0, timeout );

     // Release the message block since we're done with it.
    message->release();

    return( rval );
}