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

Contents of /main/reTurn/ChannelManager.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9000 - (show annotations) (download)
Mon Jan 31 17:38:27 2011 UTC (8 years, 9 months ago) by sgodin
File MIME type: text/plain
File size: 4434 byte(s)
-fixed memory leak - thanks to Khnykin Evgeniy
1 #include "ChannelManager.hxx"
2 #include <rutil/Random.hxx>
3 #include <rutil/WinLeakCheck.hxx>
4 #include <rutil/Logger.hxx>
5 #include "ReTurnSubsystem.hxx"
6
7 #define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
8 #define TURN_CHANNEL_BINDING_LIFETIME_SECONDS 600 // 10 minuntes
9 //#define TURN_CHANNEL_BINDING_LIFETIME_SECONDS 60 // TESTING only
10
11 using namespace std;
12
13 namespace reTurn {
14
15 ChannelManager::ChannelManager()
16 {
17 // make starting channel number random
18 int randInt = resip::Random::getRandom();
19 mNextChannelNumber = MIN_CHANNEL_NUM + (unsigned short)(randInt % (MAX_CHANNEL_NUM-MIN_CHANNEL_NUM+1));
20 }
21
22 ChannelManager::~ChannelManager()
23 {
24 // Cleanup RemotePeer Memory
25 TupleRemotePeerMap::iterator it;
26 for(it = mTupleRemotePeerMap.begin(); it != mTupleRemotePeerMap.end(); it++)
27 {
28 delete it->second;
29 }
30 }
31
32 unsigned short
33 ChannelManager::getNextChannelNumber()
34 {
35 if(mNextChannelNumber == MAX_CHANNEL_NUM)
36 {
37 mNextChannelNumber = MIN_CHANNEL_NUM;
38 }
39 else
40 {
41 mNextChannelNumber++;
42 }
43 return mNextChannelNumber;
44 }
45
46 RemotePeer*
47 ChannelManager::createChannelBinding(const StunTuple& peerTuple)
48 {
49 return createChannelBinding(peerTuple, getNextChannelNumber());
50 }
51
52 RemotePeer*
53 ChannelManager::createChannelBinding(const StunTuple& peerTuple, unsigned short channel)
54 {
55 assert(findRemotePeerByPeerAddress(peerTuple) == 0);
56
57 // Create New RemotePeer
58 RemotePeer* remotePeer = new RemotePeer(peerTuple, channel, TURN_CHANNEL_BINDING_LIFETIME_SECONDS);
59
60 // Add RemoteAddress to the appropriate maps
61 mTupleRemotePeerMap[peerTuple] = remotePeer;
62 mChannelRemotePeerMap[channel] = remotePeer;
63 return remotePeer;
64 }
65
66 RemotePeer*
67 ChannelManager::findRemotePeerByChannel(unsigned short channelNumber)
68 {
69 ChannelRemotePeerMap::iterator it = mChannelRemotePeerMap.find(channelNumber);
70 if(it != mChannelRemotePeerMap.end())
71 {
72 if(!it->second->isExpired())
73 {
74 return it->second;
75 }
76 else
77 {
78 // cleanup expired channel binding
79 mTupleRemotePeerMap.erase(it->second->getPeerTuple());
80 delete it->second;
81 mChannelRemotePeerMap.erase(it);
82 }
83 }
84 return 0;
85 }
86
87 RemotePeer*
88 ChannelManager::findRemotePeerByPeerAddress(const StunTuple& peerAddress)
89 {
90 // Find RemotePeer
91 TupleRemotePeerMap::iterator it = mTupleRemotePeerMap.find(peerAddress);
92 if(it != mTupleRemotePeerMap.end())
93 {
94 if(!it->second->isExpired())
95 {
96 return it->second;
97 }
98 else
99 {
100 // cleanup expired channel binding
101 mChannelRemotePeerMap.erase(it->second->getChannel());
102 delete it->second;
103 mTupleRemotePeerMap.erase(it);
104 }
105 }
106 return 0;
107 }
108
109 } // namespace
110
111
112 /* ====================================================================
113
114 Copyright (c) 2007-2008, Plantronics, Inc.
115 All rights reserved.
116
117 Redistribution and use in source and binary forms, with or without
118 modification, are permitted provided that the following conditions are
119 met:
120
121 1. Redistributions of source code must retain the above copyright
122 notice, this list of conditions and the following disclaimer.
123
124 2. Redistributions in binary form must reproduce the above copyright
125 notice, this list of conditions and the following disclaimer in the
126 documentation and/or other materials provided with the distribution.
127
128 3. Neither the name of Plantronics nor the names of its contributors
129 may be used to endorse or promote products derived from this
130 software without specific prior written permission.
131
132 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
133 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
134 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
135 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
136 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
137 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
138 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
139 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
140 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
141 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
142 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
143
144 ==================================================================== */
145

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