PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace/ReactorCore/StreamSocketConnection.cs

https://bitbucket.org/VahidN/interlace
C# | 216 lines | 158 code | 33 blank | 25 comment | 6 complexity | bdd01f443c487e57e5e806b911e277c5 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. #endregion
  33. namespace Interlace.ReactorCore
  34. {
  35. public class StreamSocketConnection : SocketConnection
  36. {
  37. public StreamSocketConnection(IReactor reactor, Protocol protocol)
  38. : base(reactor, protocol)
  39. {
  40. }
  41. public IPEndPoint RemoteEndPoint
  42. {
  43. get
  44. {
  45. try
  46. {
  47. return _socket.RemoteEndPoint as IPEndPoint;
  48. }
  49. catch (SocketException)
  50. {
  51. return null;
  52. }
  53. catch (ObjectDisposedException)
  54. {
  55. return null;
  56. }
  57. }
  58. }
  59. public IPEndPoint LocalEndPoint
  60. {
  61. get
  62. {
  63. try
  64. {
  65. return _socket.LocalEndPoint as IPEndPoint;
  66. }
  67. catch (SocketException)
  68. {
  69. return null;
  70. }
  71. catch (ObjectDisposedException)
  72. {
  73. return null;
  74. }
  75. }
  76. }
  77. protected override void ContinueReceive()
  78. {
  79. IAsyncResult result;
  80. try
  81. {
  82. result = _socket.BeginReceive(_receiveBuffer, 0,
  83. _receiveBuffer.Length, SocketFlags.None, null, null);
  84. }
  85. catch (SocketException ex)
  86. {
  87. HandleSocketException(ex);
  88. return;
  89. }
  90. catch (ObjectDisposedException ex)
  91. {
  92. HandleSocketClosed(ex);
  93. return;
  94. }
  95. _reactor.AddResult(result, ReceiveCompleted);
  96. }
  97. void ReceiveCompleted(IAsyncResult result, object state)
  98. {
  99. int bytesReceived;
  100. try
  101. {
  102. bytesReceived = _socket.EndReceive(result);
  103. }
  104. catch (SocketException ex)
  105. {
  106. HandleSocketException(ex);
  107. return;
  108. }
  109. catch (ObjectDisposedException ex)
  110. {
  111. HandleSocketClosed(ex);
  112. return;
  113. }
  114. if (bytesReceived == 0)
  115. {
  116. HandleSocketReadFailed();
  117. return;
  118. }
  119. try
  120. {
  121. _protocol.DataReceived(_socket.RemoteEndPoint as IPEndPoint, _receiveBuffer, 0, bytesReceived);
  122. }
  123. catch (Exception ex)
  124. {
  125. HandleProtocolException(ex);
  126. }
  127. ContinueReceive();
  128. }
  129. protected override void ContinueSend()
  130. {
  131. IAsyncResult result;
  132. try
  133. {
  134. result = _socket.BeginSend(_transmitBuffer.Data.Array, _transmitBuffer.Data.Offset,
  135. _transmitBuffer.Data.Count, SocketFlags.None, null, null);
  136. }
  137. catch (SocketException ex)
  138. {
  139. HandleSocketException(ex);
  140. return;
  141. }
  142. catch (ObjectDisposedException ex)
  143. {
  144. HandleSocketClosed(ex);
  145. return;
  146. }
  147. _reactor.AddResult(result, SendCompleted);
  148. }
  149. void SendCompleted(IAsyncResult result, object state)
  150. {
  151. int bytesSent;
  152. try
  153. {
  154. bytesSent = _socket.EndSend(result);
  155. }
  156. catch (SocketException ex)
  157. {
  158. HandleSocketException(ex);
  159. return;
  160. }
  161. catch (ObjectDisposedException ex)
  162. {
  163. HandleSocketClosed(ex);
  164. return;
  165. }
  166. if (bytesSent == 0)
  167. {
  168. HandleSocketReadFailed();
  169. return;
  170. }
  171. _transmitBuffer = _transmitBuffer.WithConsumedBytes(bytesSent);
  172. if (_transmitBuffer.Data.Count == 0)
  173. {
  174. _isTransmitting = false;
  175. KickSendQueue();
  176. }
  177. else
  178. {
  179. ContinueSend();
  180. }
  181. }
  182. }
  183. }