PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace/ReactorCore/StreamSocketClientConnector.cs

https://bitbucket.org/VahidN/interlace
C# | 144 lines | 95 code | 24 blank | 25 comment | 3 complexity | bca42180e0c3c9b4d172afb8b6f615f1 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  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 Computer Consultancy Pty Ltd 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Net;
  30. using System.Net.Sockets;
  31. using System.Text;
  32. using Interlace.Utilities;
  33. #endregion
  34. namespace Interlace.ReactorCore
  35. {
  36. class StreamSocketClientConnector
  37. {
  38. Socket _socket = null;
  39. Reactor _reactor;
  40. IProtocolFactory _factory = null;
  41. public StreamSocketClientConnector(Reactor reactor)
  42. {
  43. _reactor = reactor;
  44. }
  45. public void Connect(IProtocolFactory factory, string addressString, int port)
  46. {
  47. IPAddress address;
  48. if (IPAddress.TryParse(addressString, out address))
  49. {
  50. Connect(factory, address, port);
  51. }
  52. else
  53. {
  54. IAsyncResult result = Dns.BeginGetHostEntry(addressString, null,
  55. new Pair<string, int>(addressString, port));
  56. _reactor.AddResult(result, ResolveCompleted, 0, factory);
  57. }
  58. }
  59. void ResolveCompleted(IAsyncResult result, object state)
  60. {
  61. IProtocolFactory factory = state as IProtocolFactory;
  62. Pair<string, int> pair = result.AsyncState as Pair<string, int>;
  63. IPHostEntry entry;
  64. try
  65. {
  66. entry = Dns.EndGetHostEntry(result);
  67. }
  68. catch (SocketException e)
  69. {
  70. factory.ConnectionFailed(new DnsResolutionException(string.Format(
  71. "Address resolution for the address \"{0}\" failed.",
  72. pair.First), e));
  73. return;
  74. }
  75. if (entry.AddressList.Length == 0)
  76. {
  77. factory.ConnectionFailed(new DnsResolutionException(string.Format(
  78. "Address resolution for the address \"{0}\" failed.",
  79. pair.First)));
  80. }
  81. else
  82. {
  83. Connect(factory, entry.AddressList[0], pair.Second);
  84. }
  85. }
  86. public void Connect(IProtocolFactory factory, IPAddress address, int port)
  87. {
  88. Connect(factory, new IPEndPoint(address, port));
  89. }
  90. public void Connect(IProtocolFactory factory, IPEndPoint peer)
  91. {
  92. _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  93. _factory = factory;
  94. _factory.StartedConnecting();
  95. IAsyncResult result = _socket.BeginConnect(peer, null, null);
  96. _reactor.AddResult(result, ConnectCompleted);
  97. }
  98. void ConnectCompleted(IAsyncResult result, object state)
  99. {
  100. try
  101. {
  102. _socket.EndConnect(result);
  103. }
  104. catch (SocketException ex)
  105. {
  106. _factory.ConnectionFailed(ex);
  107. return;
  108. }
  109. catch (ObjectDisposedException ex)
  110. {
  111. _factory.ConnectionFailed(ex);
  112. return;
  113. }
  114. Protocol protocol = _factory.BuildProtocol();
  115. SocketConnection connection = new StreamSocketConnection(_reactor, protocol);
  116. connection.StartConnection(_socket, protocol.ReceiveBufferSize);
  117. protocol.MakeConnection(connection);
  118. }
  119. }
  120. }