Add new channel function send_message
[asterisk-bristuff.git] / channels / h323 / compat_h323.cxx
blobeec7361b244837d4f5e7c436c702b5b625aa13b7
1 #ifndef _GNU_SOURCE
2 #define _GNU_SOURCE
3 #endif
4 /*
5 * ast_h323.cpp
7 * OpenH323 Channel Driver for ASTERISK PBX.
8 * By Jeremy McNamara
9 * For The NuFone Network
11 * chan_h323 has been derived from code created by
12 * Michael Manousos and Mark Spencer
14 * This file is part of the chan_h323 driver for Asterisk
16 * chan_h323 is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * chan_h323 is distributed WITHOUT ANY WARRANTY; without even
22 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
23 * PURPOSE. See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Version Info: $Id$
31 #include <ptlib.h>
32 #include <h323.h>
33 #include <transports.h>
35 #include "ast_h323.h"
37 #if VERSION(OPENH323_MAJOR,OPENH323_MINOR,OPENH323_BUILD) < VERSION(1,17,3)
38 MyH323TransportTCP::MyH323TransportTCP(
39 H323EndPoint & endpoint,
40 PIPSocket::Address binding,
41 BOOL listen)
42 : H323TransportTCP(endpoint, binding, listen)
46 BOOL MyH323TransportTCP::Connect()
48 if (IsListening())
49 return TRUE;
51 PTCPSocket * socket = new PTCPSocket(remotePort);
52 Open(socket);
54 channelPointerMutex.StartRead();
56 socket->SetReadTimeout(10000/*endpoint.GetSignallingChannelConnectTimeout()*/);
58 localPort = endpoint.GetNextTCPPort();
59 WORD firstPort = localPort;
60 for (;;) {
61 PTRACE(4, "H323TCP\tConnecting to "
62 << remoteAddress << ':' << remotePort
63 << " (local port=" << localPort << ')');
64 if (socket->Connect(localAddress, localPort, remoteAddress))
65 break;
67 int errnum = socket->GetErrorNumber();
68 if (localPort == 0 || (errnum != EADDRINUSE && errnum != EADDRNOTAVAIL)) {
69 PTRACE(1, "H323TCP\tCould not connect to "
70 << remoteAddress << ':' << remotePort
71 << " (local port=" << localPort << ") - "
72 << socket->GetErrorText() << '(' << errnum << ')');
73 channelPointerMutex.EndRead();
74 return SetErrorValues(socket->GetErrorCode(), errnum);
77 localPort = endpoint.GetNextTCPPort();
78 if (localPort == firstPort) {
79 PTRACE(1, "H323TCP\tCould not bind to any port in range " <<
80 endpoint.GetTCPPortBase() << " to " << endpoint.GetTCPPortMax());
81 channelPointerMutex.EndRead();
82 return SetErrorValues(socket->GetErrorCode(), errnum);
86 socket->SetReadTimeout(PMaxTimeInterval);
88 channelPointerMutex.EndRead();
90 return OnOpen();
92 #endif
94 BOOL MyH323TransportUDP::DiscoverGatekeeper(H323Gatekeeper &gk, H323RasPDU &pdu, const H323TransportAddress &address)
96 PThread *thd = PThread::Current();
98 /* If we run in OpenH323's thread use it instead of creating new one */
99 if (thd)
100 return H323TransportUDP::DiscoverGatekeeper(gk, pdu, address);
102 /* Make copy of arguments to pass them into thread */
103 discoverGatekeeper = &gk;
104 discoverPDU = &pdu;
105 discoverAddress = &address;
107 /* Assume discovery thread isn't finished */
108 discoverReady = FALSE;
110 /* Create discovery thread */
111 thd = PThread::Create(PCREATE_NOTIFIER(DiscoverMain), 0,
112 PThread::NoAutoDeleteThread,
113 PThread::NormalPriority,
114 "GkDiscovery:%x");
116 /* Wait until discovery thread signal us its finished */
117 for(;;) {
118 discoverMutex.Wait();
119 if (discoverReady) /* Thread has been finished */
120 break;
121 discoverMutex.Signal();
123 discoverMutex.Signal();
125 /* Cleanup/delete thread */
126 thd->WaitForTermination();
127 delete thd;
129 return discoverResult;
132 void MyH323TransportUDP::DiscoverMain(PThread &thread, INT arg)
134 PWaitAndSignal m(discoverMutex);
136 discoverResult = H323TransportUDP::DiscoverGatekeeper(*discoverGatekeeper, *discoverPDU, *discoverAddress);
137 discoverReady = TRUE;