/beep/transport/tcp/TCPSessionCreator.cs

https://github.com/BDizzle/BeepForNet · C# · 220 lines · 132 code · 21 blank · 67 comment · 6 complexity · 4e5cebddbb32510e28e788a265bbdcae MD5 · raw file

  1. /*
  2. * TCPSessionCreator.java $Revision: 1.1 $ $Date: 2004/09/21 08:47:00 $
  3. *
  4. * Copyright (c) 2001 Invisible Worlds, Inc. All rights reserved.
  5. * Copyright (c) 2001 Huston Franklin. All rights reserved.
  6. *
  7. * The contents of this file are subject to the Blocks Public License (the
  8. * "License"); You may not use this file except in compliance with the License.
  9. *
  10. * You may obtain a copy of the License at http://www.beepcore.org/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. */
  18. using System;
  19. using BEEPException = beepcore.beep.core.BEEPException;
  20. using ProfileRegistry = beepcore.beep.core.ProfileRegistry;
  21. namespace beepcore.beep.transport.tcp
  22. {
  23. /// <summary>This class provides a means for applications or other libraries to create
  24. /// a TCP-based BEEP Session with another BEEP peer.</summary>
  25. /// <author>Eric Dixon</author>
  26. /// <author>Huston Franklin</author>
  27. /// <author>Jay Kint</author>
  28. /// <author>Scott Pead</author>
  29. /// <version>$Revision: 1.1 $, $Date: 2004/09/21 08:47:00 $</version>
  30. public class TCPSessionCreator
  31. {
  32. // Constants
  33. private const int DEFAULT_TABLE_SIZE = 4;
  34. private const int DEFAULT_BACKLOG_SIZE = 100;
  35. private const string ERR_TCP_SOCKET_FAILURE = "Unable to create a TCP socket";
  36. private const string ERR_BIND_FAILURE = "Bind Failed";
  37. private const string ERR_CONNECT_FAILURE = "Connect Failed";
  38. private const string ERR_LISTEN_FAILURE = "Accept Failed";
  39. // Data
  40. private static System.Collections.Hashtable listenerSockets = null;
  41. /// <summary>Method initiate</summary>
  42. /// <param name="host"></param>
  43. /// <param name="port"></param>
  44. /// <exception cref="BEEPException" />
  45. public static TCPSession initiate(System.Net.IPAddress host, int port)
  46. {
  47. try
  48. {
  49. return TCPSession.createInitiator(new System.Net.Sockets.TcpClient(host.ToString(), port), new ProfileRegistry());
  50. }
  51. catch (System.IO.IOException x)
  52. {
  53. throw new BEEPException(x);
  54. }
  55. }
  56. /// <summary>Method initiate</summary>
  57. /// <param name="host"></param>
  58. /// <param name="port"></param>
  59. /// <param name="registry"></param>
  60. /// <param name="servername"></param>
  61. /// <exception cref="BEEPException" />
  62. public static TCPSession initiate(System.Net.IPAddress host, int port, ProfileRegistry registry, string servername)
  63. {
  64. try
  65. {
  66. return TCPSession.createInitiator(new System.Net.Sockets.TcpClient(host.ToString(), port), registry, servername);
  67. }
  68. catch (System.IO.IOException x)
  69. {
  70. throw new BEEPException(x);
  71. }
  72. }
  73. /// <summary>Method initiate</summary>
  74. /// <param name="host"></param>
  75. /// <param name="port"></param>
  76. /// <param name="registry"></param>
  77. /// <exception cref="BEEPException" />
  78. public static TCPSession initiate(System.Net.IPAddress host, int port, ProfileRegistry registry)
  79. {
  80. return initiate(host, port, registry, null);
  81. }
  82. /// <summary>Method initiate</summary>
  83. /// <param name="host"></param>
  84. /// <param name="port"></param>
  85. /// <exception cref="BEEPException" />
  86. public static TCPSession initiate(string host, int port)
  87. {
  88. try
  89. {
  90. return initiate(System.Net.Dns.GetHostByName(host).AddressList[0], port);
  91. }
  92. catch (System.Exception)
  93. {
  94. throw new BEEPException("Unable to connect, unkown host");
  95. }
  96. }
  97. /// <summary>Method initiate</summary>
  98. /// <param name="host"></param>
  99. /// <param name="port"></param>
  100. /// <param name="registry"></param>
  101. /// <exception cref="BEEPException" />
  102. public static TCPSession initiate(string host, int port, ProfileRegistry registry)
  103. {
  104. try
  105. {
  106. return initiate(System.Net.Dns.GetHostByName(host).AddressList[0], port, registry);
  107. }
  108. catch (System.Exception)
  109. {
  110. throw new BEEPException("Unable to connect, unkown host");
  111. }
  112. }
  113. public static TCPSession initiate(string host, int port, ProfileRegistry registry, string servername)
  114. {
  115. try
  116. {
  117. return initiate(System.Net.Dns.GetHostByName(host).AddressList[0], port, registry, servername);
  118. }
  119. catch (System.Exception x)
  120. {
  121. throw new BEEPException("Unable to connect, unkown host", x);
  122. }
  123. }
  124. /// <summary>Method listen</summary>
  125. /// <param name="port"></param>
  126. /// <param name="registry"></param>
  127. /// <exception cref="BEEPException" />
  128. public static TCPSession listen(int port, ProfileRegistry registry)
  129. {
  130. return listen((System.Net.IPAddress)null, port, registry);
  131. }
  132. /// <summary>Method listen</summary>
  133. /// <param name="localInterface"></param>
  134. /// <param name="port"></param>
  135. /// <param name="registry"></param>
  136. /// <exception cref="BEEPException" />
  137. public static TCPSession listen(System.Net.IPAddress localInterface, int port, ProfileRegistry registry)
  138. {
  139. System.Net.Sockets.TcpListener socket = null;
  140. System.Net.Sockets.TcpClient peer = null;
  141. if (listenerSockets == null)
  142. {
  143. listenerSockets = new System.Collections.Hashtable(DEFAULT_TABLE_SIZE);
  144. }
  145. socket = (System.Net.Sockets.TcpListener) listenerSockets[System.Convert.ToString(port)];
  146. // Bind if we're not listening on this port
  147. if (socket == null)
  148. {
  149. // Bind to interface/port pair
  150. try
  151. {
  152. System.Net.Sockets.TcpListener temp_socket;
  153. temp_socket = new System.Net.Sockets.TcpListener(
  154. new System.Net.IPEndPoint(
  155. localInterface!=null ? localInterface : System.Net.IPAddress.Any,
  156. port));
  157. temp_socket.Start();
  158. socket = temp_socket;
  159. listenerSockets[port.ToString()] = socket;
  160. }
  161. catch (System.Exception x)
  162. {
  163. throw new BEEPException(x);
  164. }
  165. }
  166. // Listen
  167. try
  168. {
  169. peer = socket.AcceptTcpClient();
  170. return TCPSession.createListener(peer, registry);
  171. }
  172. catch (System.Exception e)
  173. {
  174. throw new BEEPException(e);
  175. }
  176. }
  177. /// <summary>Method listen</summary>
  178. /// <param name="port"></param>
  179. /// <param name="registry"></param>
  180. /// <param name="localInterface"></param>
  181. /// <exception cref="BEEPException" />
  182. public static TCPSession listen(string localInterface, int port, ProfileRegistry registry)
  183. {
  184. try
  185. {
  186. System.Net.IPAddress addr = null;
  187. if ((object) localInterface != null)
  188. {
  189. addr = System.Net.Dns.GetHostByName(localInterface).AddressList[0];
  190. }
  191. return listen(addr, port, registry);
  192. }
  193. catch (System.Exception x)
  194. {
  195. throw new BEEPException(x);
  196. }
  197. }
  198. }
  199. }