/[resiprocate]/main/reTurn/AsyncSocketBase.cxx
ViewVC logotype

Contents of /main/reTurn/AsyncSocketBase.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11159 - (show annotations) (download)
Fri Apr 25 14:03:32 2014 UTC (5 years, 6 months ago) by sgodin
File MIME type: text/plain
File size: 6160 byte(s)
-modified asio and boost include file ordering to avoid multiply defined symbol errors on linking
 -in preparation for upgrade of asio drop and support for latest boost 
 -include asio/ssh.hpp everywhere asio.hpp is included
 -include boost headers before others, to ensure we are not redefining stdint definitions in some includes
  and not others

1 #include "AsyncSocketBase.hxx"
2 #include "AsyncSocketBaseHandler.hxx"
3 #include <rutil/WinLeakCheck.hxx>
4 #include <rutil/Logger.hxx>
5 #include "ReTurnSubsystem.hxx"
6
7 #define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
8
9 using namespace std;
10
11 #define NO_CHANNEL ((unsigned short)-1)
12
13 namespace reTurn {
14
15 AsyncSocketBase::AsyncSocketBase(asio::io_service& ioService) :
16 mIOService(ioService),
17 mReceiving(false),
18 mConnected(false),
19 mAsyncSocketBaseHandler(0)
20 {
21 }
22
23 AsyncSocketBase::~AsyncSocketBase()
24 {
25 if(mAsyncSocketBaseHandler) mAsyncSocketBaseHandler->onSocketDestroyed();
26 }
27
28 void
29 AsyncSocketBase::send(const StunTuple& destination, boost::shared_ptr<DataBuffer>& data)
30 {
31 mIOService.dispatch(boost::bind(&AsyncSocketBase::doSend, shared_from_this(), destination, data, 0));
32 }
33
34 void
35 AsyncSocketBase::send(const StunTuple& destination, unsigned short channel, boost::shared_ptr<DataBuffer>& data)
36 {
37 mIOService.post(boost::bind(&AsyncSocketBase::doSend, shared_from_this(), destination, channel, data, 0));
38 }
39
40 void
41 AsyncSocketBase::doSend(const StunTuple& destination, boost::shared_ptr<DataBuffer>& data, unsigned int bufferStartPos)
42 {
43 doSend(destination, NO_CHANNEL, data, bufferStartPos);
44 }
45
46 void
47 AsyncSocketBase::doSend(const StunTuple& destination, unsigned short channel, boost::shared_ptr<DataBuffer>& data, unsigned int bufferStartPos)
48 {
49 bool writeInProgress = !mSendDataQueue.empty();
50 if(channel == NO_CHANNEL)
51 {
52 boost::shared_ptr<DataBuffer> empty;
53 mSendDataQueue.push_back(SendData(destination, empty, data, bufferStartPos));
54 }
55 else
56 {
57 // Add Turn Framing
58 boost::shared_ptr<DataBuffer> frame = allocateBuffer(4);
59 channel = htons(channel);
60 memcpy(&(*frame)[0], &channel, 2);
61 unsigned short msgsize = htons((unsigned short)data->size());
62 memcpy(&(*frame)[2], (void*)&msgsize, 2); // UDP doesn't need size - but shouldn't hurt to send it anyway
63
64 mSendDataQueue.push_back(SendData(destination, frame, data, bufferStartPos));
65 }
66 if (!writeInProgress)
67 {
68 sendFirstQueuedData();
69 }
70 }
71
72 void
73 AsyncSocketBase::handleSend(const asio::error_code& e)
74 {
75 if(!e)
76 {
77 onSendSuccess();
78 }
79 else
80 {
81 DebugLog(<< "handleSend with error: " << e);
82 onSendFailure(e);
83 }
84
85 // TODO - check if closed here, and if so don't try and send more
86 // Clear this data from the queue and see if there is more data to send
87 mSendDataQueue.pop_front();
88 if (!mSendDataQueue.empty())
89 {
90 sendFirstQueuedData();
91 }
92 }
93
94 void
95 AsyncSocketBase::sendFirstQueuedData()
96 {
97 std::vector<asio::const_buffer> bufs;
98 if(mSendDataQueue.front().mFrameData.get() != 0) // If we have frame data
99 {
100 bufs.push_back(asio::buffer(mSendDataQueue.front().mFrameData->data(), mSendDataQueue.front().mFrameData->size()));
101 }
102 bufs.push_back(asio::buffer(mSendDataQueue.front().mData->data()+mSendDataQueue.front().mBufferStartPos, mSendDataQueue.front().mData->size()-mSendDataQueue.front().mBufferStartPos));
103 transportSend(mSendDataQueue.front().mDestination, bufs);
104 }
105
106 void
107 AsyncSocketBase::receive()
108 {
109 mIOService.post(boost::bind(&AsyncSocketBase::doReceive, shared_from_this()));
110 }
111
112 void
113 AsyncSocketBase::doReceive()
114 {
115 if(!mReceiving)
116 {
117 mReceiving=true;
118 mReceiveBuffer = allocateBuffer(RECEIVE_BUFFER_SIZE);
119 transportReceive();
120 }
121 }
122
123 void
124 AsyncSocketBase::framedReceive()
125 {
126 mIOService.post(boost::bind(&AsyncSocketBase::doFramedReceive, shared_from_this()));
127 }
128
129 void
130 AsyncSocketBase::doFramedReceive()
131 {
132 if(!mReceiving)
133 {
134 mReceiving=true;
135 mReceiveBuffer = allocateBuffer(RECEIVE_BUFFER_SIZE);
136 transportFramedReceive();
137 }
138 }
139
140 void
141 AsyncSocketBase::handleReceive(const asio::error_code& e, std::size_t bytesTransferred)
142 {
143 mReceiving = false;
144
145 if(!e)
146 {
147 // Handoff received buffer to appliction, and prepare receive buffer for next call
148 mReceiveBuffer->truncate(bytesTransferred);
149 onReceiveSuccess(getSenderEndpointAddress(), getSenderEndpointPort(), mReceiveBuffer);
150 }
151 else
152 {
153 DebugLog(<< "handleReceive with error: " << e);
154 onReceiveFailure(e);
155 }
156 }
157
158 void
159 AsyncSocketBase::close()
160 {
161 mIOService.post(boost::bind(&AsyncSocketBase::transportClose, shared_from_this()));
162 }
163
164 boost::shared_ptr<DataBuffer>
165 AsyncSocketBase::allocateBuffer(unsigned int size)
166 {
167 return boost::shared_ptr<DataBuffer>(new DataBuffer(size));
168 }
169
170 } // namespace
171
172
173 /* ====================================================================
174
175 Copyright (c) 2007-2008, Plantronics, Inc.
176 All rights reserved.
177
178 Redistribution and use in source and binary forms, with or without
179 modification, are permitted provided that the following conditions are
180 met:
181
182 1. Redistributions of source code must retain the above copyright
183 notice, this list of conditions and the following disclaimer.
184
185 2. Redistributions in binary form must reproduce the above copyright
186 notice, this list of conditions and the following disclaimer in the
187 documentation and/or other materials provided with the distribution.
188
189 3. Neither the name of Plantronics nor the names of its contributors
190 may be used to endorse or promote products derived from this
191 software without specific prior written permission.
192
193 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
194 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
195 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
197 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
198 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
199 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
200 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
201 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
202 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
203 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
204
205 ==================================================================== */
206

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27