Program.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * C# ESL managed examples
  3. *
  4. * Version: MPL 1.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * Contributor:
  17. * Diego Toro <dftoro@yahoo.com>
  18. */
  19. using System;
  20. using System.Net;
  21. using System.Net.Sockets;
  22. using System.Threading;
  23. namespace ManagedEslTest
  24. {
  25. [Flags]
  26. public enum enLogLevel
  27. {
  28. EMERG = 0,
  29. ALERT,
  30. CRIT,
  31. ERROR,
  32. WARNING,
  33. NOTICE,
  34. INFO,
  35. DEBUG
  36. }
  37. class Program
  38. {
  39. public static readonly int ESL_SUCCESS = 1;
  40. static void Main(string[] args)
  41. {
  42. /*
  43. * The next lines are usefull to view simples C# examples about how ESL managed works.
  44. * Active only a mode uncomment it: inbound, outbound sync or outbound async.
  45. * If you active two o more on this application you may have problems.
  46. * Remember modify your dialplan for testing outbound modes (see OutboundModeSync and OutboundModeAsync members)
  47. */
  48. ThreadPool.QueueUserWorkItem(new WaitCallback(InboundMode));
  49. //ThreadPool.QueueUserWorkItem(new WaitCallback(OutboundModeSync));
  50. //ThreadPool.QueueUserWorkItem(new WaitCallback(OutboundModeAsync));
  51. Console.ReadLine();
  52. }
  53. /// <summary>
  54. /// Example using event socket in "Inbound" mode
  55. /// </summary>
  56. /// <param name="stateInfo">Object state info</param>
  57. static void InboundMode(Object stateInfo)
  58. {
  59. //Initializes a new instance of ESLconnection, and connects to the host $host on the port $port, and supplies $password to freeswitch
  60. ESLconnection eslConnection = new ESLconnection("127.0.0.1", "8021", "ClueCon");
  61. if (eslConnection.Connected() != ESL_SUCCESS)
  62. {
  63. Console.WriteLine("Error connecting to FreeSwitch");
  64. return;
  65. }
  66. //Set log level
  67. //ESL.eslSetLogLevel((int)enLogLevel.DEBUG);
  68. // Subscribe to all events
  69. ESLevent eslEvent = eslConnection.SendRecv("event plain ALL");
  70. if (eslEvent == null)
  71. {
  72. Console.WriteLine("Error subscribing to all events");
  73. return;
  74. }
  75. //Turns an event into colon-separated 'name: value' pairs. The format parameter isn't used
  76. Console.WriteLine(eslEvent.Serialize(String.Empty));
  77. // Grab Events until process is killed
  78. while (eslConnection.Connected() == ESL_SUCCESS)
  79. {
  80. eslEvent = eslConnection.RecvEvent();
  81. Console.WriteLine(eslEvent.Serialize(String.Empty));
  82. }
  83. }
  84. /// <summary>
  85. /// Example using event socket in "Outbound" mode synchronic
  86. /// </summary>
  87. /// <param name="stateInfo">Object state info</param>
  88. static void OutboundModeSync(Object stateInfo)
  89. {
  90. /* add next line to a dialplan
  91. <action application="socket" data="localhost:8022 sync full"/>
  92. */
  93. TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8022);
  94. try
  95. {
  96. tcpListener.Start();
  97. Console.WriteLine("OutboundModeSync, waiting for a connection...");
  98. while (true)
  99. {
  100. Socket sckClient = tcpListener.AcceptSocket();
  101. //Initializes a new instance of ESLconnection, and connects to the host $host on the port $port, and supplies $password to freeswitch
  102. ESLconnection eslConnection = new ESLconnection(sckClient.Handle.ToInt32());
  103. Console.WriteLine("Execute(\"answer\")");
  104. eslConnection.Execute("answer", String.Empty, String.Empty);
  105. Console.WriteLine("Execute(\"playback\")");
  106. eslConnection.Execute("playback", "music/8000/suite-espanola-op-47-leyenda.wav", String.Empty);
  107. Console.WriteLine("Execute(\"hangup\")");
  108. eslConnection.Execute("hangup", String.Empty, String.Empty);
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. Console.WriteLine(ex);
  114. }
  115. finally
  116. {
  117. tcpListener.Stop();
  118. }
  119. }
  120. /// <summary>
  121. /// Example using event socket in "Outbound" mode asynchronic
  122. /// </summary>
  123. /// <param name="stateInfo">Object state info</param>
  124. static void OutboundModeAsync(Object stateInfo)
  125. {
  126. /* add next line to a dialplan
  127. <action application="socket" data="localhost:8022 async full" />
  128. */
  129. TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8022);
  130. try
  131. {
  132. tcpListener.Start();
  133. Console.WriteLine("OutboundModeAsync, waiting for connections...");
  134. while (true)
  135. {
  136. tcpListener.BeginAcceptSocket((asyncCallback) =>
  137. {
  138. TcpListener tcpListened = (TcpListener)asyncCallback.AsyncState;
  139. Socket sckClient = tcpListened.EndAcceptSocket(asyncCallback);
  140. //Initializes a new instance of ESLconnection, and connects to the host $host on the port $port, and supplies $password to freeswitch
  141. ESLconnection eslConnection = new ESLconnection(sckClient.Handle.ToInt32());
  142. ESLevent eslEvent = eslConnection.GetInfo();
  143. string strUuid = eslEvent.GetHeader("UNIQUE-ID", -1);
  144. eslConnection.SendRecv("myevents");
  145. eslConnection.SendRecv("divert_events on");
  146. eslConnection.Execute("answer", String.Empty, String.Empty);
  147. eslConnection.Execute("playback", "music/8000/suite-espanola-op-47-leyenda.wav", String.Empty);
  148. while (eslConnection.Connected() == ESL_SUCCESS)
  149. {
  150. eslEvent = eslConnection.RecvEvent();
  151. Console.WriteLine(eslEvent.Serialize(String.Empty));
  152. }
  153. sckClient.Close();
  154. Console.WriteLine("Connection closed uuid:{0}", strUuid);
  155. }, tcpListener);
  156. Thread.Sleep(50);
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. Console.WriteLine(ex);
  162. }
  163. finally
  164. {
  165. tcpListener.Stop();
  166. }
  167. }
  168. }
  169. }