Fixed dependency
[opal.git] / src / iax2 / transmit.cxx
blobc4622f06ca483b583abbd9ef6dc0d280019b91ce
1 /*
3 * Inter Asterisk Exchange 2
4 *
5 * Class to implement the transmitter, which sends all packets for all calls.
6 *
7 * Open Phone Abstraction Library (OPAL)
9 * Copyright (c) 2005 Indranet Technologies Ltd.
11 * The contents of this file are subject to the Mozilla Public License
12 * Version 1.0 (the "License"); you may not use this file except in
13 * compliance with the License. You may obtain a copy of the License at
14 * http://www.mozilla.org/MPL/
16 * Software distributed under the License is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
18 * the License for the specific language governing rights and limitations
19 * under the License.
21 * The Original Code is Open Phone Abstraction Library.
23 * The Initial Developer of the Original Code is Indranet Technologies Ltd.
25 * The author of this code is Derek J Smithies
28 * $Log$
29 * Revision 1.7 2007/08/07 03:10:18 dereksmithies
30 * Modify comments. Reduce some of the verbosity of the PTRACE messages.
32 * Revision 1.6 2007/01/23 02:10:39 dereksmithies
33 * Handle Vnak frames correctly. Handle iseqno and oseqno correctly.
35 * Revision 1.5 2007/01/11 03:02:16 dereksmithies
36 * Remove the previous audio buffering code, and switch to using the jitter
37 * buffer provided in Opal. Reduce the verbosity of the log mesasges.
39 * Revision 1.4 2007/01/09 03:32:23 dereksmithies
40 * Tidy up and improve the close down process - make it more robust.
41 * Alter level of several PTRACE statements. Add Terminate() method to transmitter and receiver.
43 * Revision 1.3 2006/08/09 03:46:39 dereksmithies
44 * Add ability to register to a remote Asterisk box. The iaxProcessor class is split
45 * into a callProcessor and a regProcessor class.
46 * Big thanks to Stephen Cook, (sitiveni@gmail.com) for this work.
48 * Revision 1.2 2005/08/26 03:07:38 dereksmithies
49 * Change naming convention, so all class names contain the string "IAX2"
51 * Revision 1.1 2005/07/30 07:01:33 csoutheren
52 * Added implementation of IAX2 (Inter Asterisk Exchange 2) protocol
53 * Thanks to Derek Smithies of Indranet Technologies Ltd. for
54 * writing and contributing this code
61 #include <ptlib.h>
63 #ifdef P_USE_PRAGMA
64 #pragma implementation "transmit.h"
65 #endif
67 #include <iax2/transmit.h>
69 #define new PNEW
71 IAX2Transmit::IAX2Transmit(IAX2EndPoint & _newEndpoint, PUDPSocket & _newSocket)
72 : PThread(1000, NoAutoDeleteThread),
73 ep(_newEndpoint),
74 sock(_newSocket)
76 sendNowFrames.Initialise();
77 ackingFrames.Initialise();
79 keepGoing = TRUE;
81 PTRACE(6,"Constructor - IAX2 Transmitter");
82 Resume();
85 IAX2Transmit::~IAX2Transmit()
87 sendNowFrames.AllowDeleteObjects();
88 ackingFrames.AllowDeleteObjects();
89 PTRACE(5, "IAX2Transmit\tDestructor finished");
92 void IAX2Transmit::Terminate()
94 keepGoing = FALSE;
95 activate.Signal();
98 void IAX2Transmit::SendFrame(IAX2Frame *newFrame)
100 PTRACE(5,"Process request to send frame " << newFrame->IdString());
102 sendNowFrames.AddNewFrame(newFrame);
104 activate.Signal();
107 void IAX2Transmit::PurgeMatchingFullFrames(IAX2Frame *newFrame)
109 if (!PIsDescendant(newFrame, IAX2FullFrame))
110 return;
112 PTRACE(4, "Purge frames matching received " << newFrame->IdString());
113 ackingFrames.DeleteMatchingSendFrame((IAX2FullFrame *)newFrame);
116 void IAX2Transmit::SendVnakRequestedFrames(IAX2FullFrameProtocol &src)
118 ackingFrames.SendVnakRequestedFrames(src);
122 void IAX2Transmit::Main()
124 SetThreadName("IAX2Transmit");
125 while(keepGoing) {
126 activate.Wait();
128 if (!keepGoing)
129 break;
131 ReportLists();
132 ProcessAckingList();
134 ProcessSendList();
136 PTRACE(3, " End of the Transmit thread.");
139 void IAX2Transmit::ProcessAckingList()
141 IAX2FrameList framesToSend;
142 framesToSend.Initialise();
144 ackingFrames.GetResendFramesDeleteOldFrames(framesToSend);
146 framesToSend.MarkAllAsResent();
148 sendNowFrames.GrabContents(framesToSend);
151 void IAX2Transmit::ReportLists()
153 PTRACE(5, "IAX2Transmit\tSend now frames is: ");
154 sendNowFrames.ReportList();
155 PTRACE(5, "IAX2Transmit\tAckingFrames is:");
156 ackingFrames.ReportList();
159 void IAX2Transmit::ProcessSendList()
161 for(;;) {
162 IAX2Frame * active = sendNowFrames.GetLastFrame();
163 if (active == NULL)
164 break;
166 BOOL isFullFrame = FALSE;
167 if (PIsDescendant(active, IAX2FullFrame)) {
168 isFullFrame = TRUE;
169 IAX2FullFrame *f= (IAX2FullFrame *)active;
170 if (f->DeleteFrameNow()) {
171 PTRACE(6, "This frame has timed out, so do not transmit" << f->IdString());
172 delete f;
173 continue;
177 if (!active->TransmitPacket(sock)) {
178 PTRACE(4, "Delete " << active->IdString() << " as transmit failed.");
179 delete active;
180 continue;
183 if (!isFullFrame) {
184 PTRACE(4, "Delete this frame as it is a mini frame, and continue" << active->IdString());
185 delete active;
186 continue;
189 IAX2FullFrame *f= (IAX2FullFrame *)active;
190 if (f->IsAckFrame() || f->IsVnakFrame()) {
191 delete f;
192 continue;
195 if (!active->CanRetransmitFrame()) {
196 delete f;
197 continue;
200 ackingFrames.AddNewFrame(active);
204 /* The comment below is magic for those who use emacs to edit this file. */
205 /* With the comment below, the tab key does auto indent to 2 spaces. */
208 * Local Variables:
209 * mode:c
210 * c-file-style:linux
211 * c-basic-offset:2
212 * End: