SocketAsyncTest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections;
  3. using System.Threading;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using NUnit.Framework;
  7. namespace MonoTests.System.Net.Sockets
  8. {
  9. [TestFixture]
  10. public class SocketAsyncTest
  11. {
  12. Socket serverSocket;
  13. Socket clientSocket;
  14. ManualResetEvent readyEvent;
  15. ManualResetEvent mainEvent;
  16. Exception error;
  17. void SetUp ()
  18. {
  19. readyEvent = new ManualResetEvent (false);
  20. mainEvent = new ManualResetEvent (false);
  21. ThreadPool.QueueUserWorkItem (_ => DoWork ());
  22. readyEvent.WaitOne ();
  23. if (error != null)
  24. throw error;
  25. clientSocket = new Socket (
  26. AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  27. clientSocket.Connect (serverSocket.LocalEndPoint);
  28. clientSocket.NoDelay = true;
  29. }
  30. [TestFixtureTearDown]
  31. public void TearDown ()
  32. {
  33. if (serverSocket != null)
  34. serverSocket.Close ();
  35. readyEvent.Close ();
  36. mainEvent.Close ();
  37. }
  38. void DoWork ()
  39. {
  40. try {
  41. serverSocket = new Socket (
  42. AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  43. serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  44. serverSocket.Listen (1);
  45. var async = new SocketAsyncEventArgs ();
  46. async.Completed += (s,e) => OnAccepted (e);
  47. if (!serverSocket.AcceptAsync (async))
  48. OnAccepted (async);
  49. } catch (Exception e) {
  50. error = e;
  51. } finally {
  52. readyEvent.Set ();
  53. }
  54. }
  55. void OnAccepted (SocketAsyncEventArgs e)
  56. {
  57. var acceptSocket = e.AcceptSocket;
  58. try {
  59. var header = new byte [4];
  60. acceptSocket.Receive (header);
  61. if ((header [0] != 0x12) || (header [1] != 0x34) ||
  62. (header [2] != 0x56) || (header [3] != 0x78))
  63. throw new InvalidOperationException ();
  64. } catch (Exception ex) {
  65. error = ex;
  66. return;
  67. }
  68. var recvAsync = new SocketAsyncEventArgs ();
  69. recvAsync.Completed += (sender, args) => OnReceived (args);
  70. recvAsync.SetBuffer (new byte [4], 0, 4);
  71. if (!acceptSocket.ReceiveAsync (recvAsync))
  72. OnReceived (recvAsync);
  73. mainEvent.Set ();
  74. }
  75. void OnReceived (SocketAsyncEventArgs e)
  76. {
  77. if (e.SocketError != SocketError.Success)
  78. error = new SocketException ((int) e.SocketError);
  79. else if (e.Buffer [0] != 0x9a)
  80. error = new InvalidOperationException ();
  81. mainEvent.Set ();
  82. }
  83. [Test]
  84. [Category("Test")]
  85. #if FEATURE_NO_BSD_SOCKETS
  86. [ExpectedException (typeof (PlatformNotSupportedException))]
  87. #endif
  88. public void SendAsync ()
  89. {
  90. SetUp ();
  91. var buffer = new byte [] { 0x12, 0x34, 0x56, 0x78 };
  92. var m = new ManualResetEvent (false);
  93. var e = new SocketAsyncEventArgs ();
  94. e.SetBuffer (buffer, 0, buffer.Length);
  95. e.Completed += (s,o) => {
  96. if (o.SocketError != SocketError.Success)
  97. error = new SocketException ((int)o.SocketError);
  98. m.Set ();
  99. };
  100. bool res = clientSocket.SendAsync (e);
  101. if (res) {
  102. if (!m.WaitOne (1500))
  103. Assert.Fail ("Timeout #1");
  104. }
  105. if (!mainEvent.WaitOne (1500))
  106. Assert.Fail ("Timeout #2");
  107. if (error != null)
  108. throw error;
  109. m.Reset ();
  110. mainEvent.Reset ();
  111. buffer [0] = 0x9a;
  112. buffer [1] = 0xbc;
  113. buffer [2] = 0xde;
  114. buffer [3] = 0xff;
  115. res = clientSocket.SendAsync (e);
  116. if (res) {
  117. if (!m.WaitOne (1500))
  118. Assert.Fail ("Timeout #3");
  119. }
  120. if (!mainEvent.WaitOne (1500))
  121. Assert.Fail ("Timeout #4");
  122. if (error != null)
  123. throw error;
  124. }
  125. }
  126. }