PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Framework/SharpMud.Server.Systems.Net.Core/Net/ConnectionListener.cs

http://sharpmud-framework.googlecode.com/
C# | 198 lines | 134 code | 19 blank | 45 comment | 4 complexity | b74a34fd6938e976be2955d1e63e5b8c MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace SharpMud.Net
  5. {
  6. public class ConnectionListener : IConnectionListener, System.Threading.IRequiresExternalPulsing
  7. {
  8. #region Private Variables
  9. private System.Collections.ArrayList _WaitingConnections;
  10. private System.Net.Sockets.TcpListener _TcpListener;
  11. private int _Port;
  12. private System.Net.IPAddress _Address;
  13. private Server.Systems.Net.INetworkSystem _Host;
  14. private log4net.ILog log;
  15. #endregion
  16. #region Constructors
  17. /// <summary>
  18. /// Default constructor. Creates a new, null instance of ConnectionListener.
  19. /// </summary>
  20. private ConnectionListener()
  21. {
  22. _WaitingConnections = new System.Collections.ArrayList();
  23. _Port = 0;
  24. _Address = System.Net.IPAddress.None;
  25. _Host = null;
  26. }
  27. /// <summary>
  28. /// A public constructor for ConnectionListener. Give it the port and the default handler and
  29. /// the connection listner will listen on all local address bindings at that port.
  30. /// </summary>
  31. /// <param name="port">The port to listen on</param>
  32. /// <param name="defaultHandler">The handler that will accept all the new connections generated
  33. /// from the listener.</param>
  34. public ConnectionListener(int port, Server.Systems.Net.INetworkSystem host)
  35. :this(System.Net.IPAddress.Any,port,host)
  36. {
  37. }
  38. /// <summary>
  39. /// A public constructor for ConnectionListener. Give it the local listening address and the local
  40. /// port to listen on, and a handler for new connections made at that endpoint.
  41. /// </summary>
  42. /// <param name="address">The local address to listen on</param>
  43. /// <param name="port">The port to listen on</param>
  44. /// <param name="defaultHandler">The handler that will accpet all the new connections generated from the listener</param>
  45. public ConnectionListener(System.Net.IPAddress address, int port,Server.Systems.Net.INetworkSystem host)
  46. {
  47. if(address==null)
  48. throw new ArgumentNullException("address");
  49. if(port<1024)
  50. throw new ArgumentOutOfRangeException("port",port,"The port of a ConnectionListener cannot be less than 1024.");
  51. if(host==null)
  52. throw new ArgumentNullException("host");
  53. _Port = port;
  54. _Address = address;
  55. _WaitingConnections = new System.Collections.ArrayList();
  56. _TcpListener = new System.Net.Sockets.TcpListener(_Address, _Port);
  57. _Host = host;
  58. log = log4net.LogManager.GetLogger(this.GetType().Name + "[localhost:" + _Port.ToString() + "]");
  59. log.Debug("CREATED");
  60. }
  61. #endregion
  62. /// <summary>
  63. /// This property returns the hosting network system of this ConnectionListener
  64. /// </summary>
  65. public SharpMud.Server.Systems.Net.INetworkSystem Host
  66. {
  67. get
  68. {
  69. return this._Host;
  70. }
  71. }
  72. #region IConnectionListener Members
  73. /// <summary>
  74. /// The list of connections that have had their sockets accepted and are awaiting to be handled
  75. /// </summary>
  76. public INetworkConnection[] WaitingConnections
  77. {
  78. get
  79. {
  80. return (INetworkConnection[])_WaitingConnections.ToArray(typeof(INetworkConnection));
  81. }
  82. }
  83. /// <summary>
  84. /// The local port that is being listened on
  85. /// </summary>
  86. public int Port
  87. {
  88. get
  89. {
  90. return this._Port;
  91. }
  92. }
  93. /// <summary>
  94. /// The local endpoint that is being listened on
  95. /// </summary>
  96. public System.Net.IPEndPoint ListenPoint
  97. {
  98. get
  99. {
  100. return (IPEndPoint)this._TcpListener.LocalEndpoint;
  101. }
  102. }
  103. /// <summary>
  104. /// Removes a connection from the list of new connections that are waiting to be handled
  105. /// </summary>
  106. /// <param name="connection">The connection to remove</param>
  107. public void RemoveWaitingConnection(INetworkConnection connection)
  108. {
  109. this._WaitingConnections.Remove(connection);
  110. }
  111. private event SharpMud.Net.ConnectionListenerEventHandler _ConnectionMade;
  112. /// <summary>
  113. /// This event is raised when a connection is made
  114. /// </summary>
  115. public event SharpMud.Net.ConnectionListenerEventHandler ConnectionMade
  116. {
  117. add
  118. {
  119. _ConnectionMade += value;
  120. }
  121. remove
  122. {
  123. _ConnectionMade -= value;
  124. }
  125. }
  126. /// <summary>
  127. /// Tell the ConnectionListener to do any work that it needs to be told to do, such as check for new c
  128. /// </summary>
  129. public void Pulse()
  130. {
  131. lock(this)
  132. {
  133. bool stillLoop=true;
  134. try
  135. {
  136. stillLoop = _TcpListener.Pending();
  137. }
  138. catch(System.InvalidOperationException e)
  139. {
  140. stillLoop = false;
  141. }
  142. while(stillLoop)
  143. {
  144. NetworkConnection cw = new NetworkConnection(_TcpListener.AcceptSocket());
  145. this._WaitingConnections.Add(cw);
  146. if(this._ConnectionMade!=null)
  147. this._ConnectionMade(this,cw);
  148. try
  149. {
  150. stillLoop = _TcpListener.Pending();
  151. }
  152. catch(System.InvalidOperationException e)
  153. {
  154. stillLoop = false;
  155. }
  156. }
  157. }
  158. }
  159. /// <summary>
  160. /// Tell the ConnectionListener to start listening for new connections
  161. /// </summary>
  162. public void Start()
  163. {
  164. log.Debug("Starting");
  165. this._TcpListener.Start();
  166. log.Info("STARTED");
  167. }
  168. /// <summary>
  169. /// Tell the ConnectionListener to stop listening for new connections
  170. /// </summary>
  171. public void Stop()
  172. {
  173. log.Debug("Stopping");
  174. this._TcpListener.Stop();
  175. log.Info("STOPPED");
  176. }
  177. #endregion
  178. }
  179. }