TCPD.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Reflection;
  33. using System.Text;
  34. using System.Threading;
  35. using log4net;
  36. namespace OpenSim.GridLaunch.GUI.Network
  37. {
  38. public class TCPD : IGUI, IDisposable
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private List<Client> Clients = new List<Client>();
  42. private readonly int defaultPort = 7998;
  43. private TcpListener tcpListener;
  44. private Thread listenThread;
  45. private Thread clientThread;
  46. private List<string> Apps = new List<string>();
  47. internal string currentApp = "";
  48. private bool quitTyped = false;
  49. public TCPD()
  50. {
  51. Program.AppCreated += Program_AppCreated;
  52. Program.AppRemoved += Program_AppRemoved;
  53. Program.AppConsoleOutput += Program_AppConsoleOutput;
  54. Program.Command.CommandLine += Command_CommandLine;
  55. }
  56. ~TCPD()
  57. {
  58. Dispose();
  59. }
  60. private bool isDisposed = false;
  61. ///<summary>
  62. ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  63. ///</summary>
  64. ///<filterpriority>2</filterpriority>
  65. public void Dispose()
  66. {
  67. if (isDisposed)
  68. return;
  69. isDisposed = true;
  70. tcpd_Stop();
  71. }
  72. public void StartGUI()
  73. {
  74. // We are starting
  75. tcpd_Start();
  76. }
  77. public void StopGUI()
  78. {
  79. // We are stopping
  80. tcpd_Stop();
  81. }
  82. #region GridLaunch Events
  83. private void Command_CommandLine(string application, string command, string arguments)
  84. {
  85. // If command is a number then someone might be trying to change console: /1, /2, etc.
  86. int currentAppNum = 0;
  87. if (int.TryParse(command, out currentAppNum))
  88. if (currentAppNum <= Apps.Count)
  89. {
  90. currentApp = Apps[currentAppNum - 1];
  91. TCPWriteToAll("Changed console to app: " + currentApp + Environment.NewLine);
  92. }
  93. else
  94. TCPWriteToAll("Unable to change to app number: " + currentAppNum + Environment.NewLine);
  95. // Has user typed quit?
  96. if (command.ToLower() == "quit")
  97. quitTyped = true;
  98. // Has user typed /list?
  99. if (command.ToLower() == "list")
  100. {
  101. TCPWriteToAll("/0 Log console");
  102. for (int i = 1; i <= Apps.Count; i++)
  103. {
  104. TCPWriteToAll(string.Format("/{0} {1}", i, Apps[i - 1]));
  105. }
  106. }
  107. }
  108. void Program_AppCreated(string App)
  109. {
  110. TCPWriteToAll("Started: " + App);
  111. if (!Apps.Contains(App))
  112. Apps.Add(App);
  113. }
  114. void Program_AppRemoved(string App)
  115. {
  116. TCPWriteToAll("Stopped: " + App);
  117. if (Apps.Contains(App))
  118. Apps.Remove(App);
  119. }
  120. private void Program_AppConsoleOutput(string App, string Text)
  121. {
  122. TCPWriteToAll(App, Text);
  123. }
  124. #endregion
  125. private void tcpd_Start()
  126. {
  127. listenThread = new Thread(new ThreadStart(ListenForClients));
  128. listenThread.Name = "TCPDThread";
  129. listenThread.IsBackground = true;
  130. listenThread.Start();
  131. while (!quitTyped)
  132. {
  133. Thread.Sleep(500);
  134. }
  135. //clientThread = new Thread(new ThreadStart(ProcessClients));
  136. //clientThread.Name = "TCPClientThread";
  137. //clientThread.IsBackground = true;
  138. ////clientThread.Start();
  139. }
  140. private void tcpd_Stop()
  141. {
  142. StopThread(listenThread);
  143. StopThread(clientThread);
  144. }
  145. private void ListenForClients()
  146. {
  147. int Port = 0;
  148. int.TryParse(Program.Settings["TCPPort"], out Port);
  149. if (Port < 1)
  150. Port = defaultPort;
  151. m_log.Info("Starting TCP Server on port " + Port);
  152. this.tcpListener = new TcpListener(IPAddress.Any, Port);
  153. this.tcpListener.Start();
  154. while (true)
  155. {
  156. // Blocks until a client has connected to the server
  157. TcpClient tcpClient = this.tcpListener.AcceptTcpClient();
  158. Client client = new Client(this, tcpClient);
  159. lock (Clients)
  160. {
  161. Clients.Add(client);
  162. }
  163. System.Threading.Thread.Sleep(500);
  164. }
  165. }
  166. private static void StopThread(Thread t)
  167. {
  168. if (t != null)
  169. {
  170. m_log.Debug("Stopping thread " + t.Name);
  171. try
  172. {
  173. if (t.IsAlive)
  174. t.Abort();
  175. t.Join(2000);
  176. t = null;
  177. }
  178. catch (Exception ex)
  179. {
  180. m_log.Error("Exception stopping thread: " + ex.ToString());
  181. }
  182. }
  183. }
  184. private void TCPWriteToAll(string app, string text)
  185. {
  186. TCPWriteToAll(text);
  187. }
  188. private void TCPWriteToAll(string text)
  189. {
  190. foreach (Client c in new ArrayList(Clients))
  191. {
  192. try
  193. {
  194. c.Write(text);
  195. } catch (Exception ex)
  196. {
  197. m_log.Error("Exception writing to TCP: " + ex.ToString());
  198. }
  199. }
  200. }
  201. }
  202. }