TestLLUDPServer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  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 OpenSimulator Project 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 DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Net;
  30. using System.Net.Sockets;
  31. using OpenMetaverse.Packets;
  32. namespace OpenSim.Region.ClientStack.LindenUDP.Tests
  33. {
  34. /// <summary>
  35. /// This class enables synchronous testing of the LLUDPServer by allowing us to load our own data into the end
  36. /// receive event
  37. /// </summary>
  38. public class TestLLUDPServer : LLUDPServer
  39. {
  40. /// <summary>
  41. /// The chunks of data to pass to the LLUDPServer when it calls EndReceive
  42. /// </summary>
  43. protected Queue<ChunkSenderTuple> m_chunksToLoad = new Queue<ChunkSenderTuple>();
  44. protected override void BeginReceive()
  45. {
  46. if (m_chunksToLoad.Count > 0 && m_chunksToLoad.Peek().BeginReceiveException)
  47. {
  48. ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
  49. reusedEpSender = tuple.Sender;
  50. throw new SocketException();
  51. }
  52. }
  53. protected override bool EndReceive(out int numBytes, IAsyncResult result, ref EndPoint epSender)
  54. {
  55. numBytes = 0;
  56. //m_log.Debug("Queue size " + m_chunksToLoad.Count);
  57. if (m_chunksToLoad.Count <= 0)
  58. return false;
  59. ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
  60. RecvBuffer = tuple.Data;
  61. numBytes = tuple.Data.Length;
  62. epSender = tuple.Sender;
  63. return true;
  64. }
  65. public override void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
  66. {
  67. // Don't do anything just yet
  68. }
  69. /// <summary>
  70. /// Signal that this chunk should throw an exception on Socket.BeginReceive()
  71. /// </summary>
  72. /// <param name="epSender"></param>
  73. public void LoadReceiveWithBeginException(EndPoint epSender)
  74. {
  75. ChunkSenderTuple tuple = new ChunkSenderTuple(epSender);
  76. tuple.BeginReceiveException = true;
  77. m_chunksToLoad.Enqueue(tuple);
  78. }
  79. /// <summary>
  80. /// Load some data to be received by the LLUDPServer on the next receive call
  81. /// </summary>
  82. /// <param name="data"></param>
  83. /// <param name="epSender"></param>
  84. public void LoadReceive(byte[] data, EndPoint epSender)
  85. {
  86. m_chunksToLoad.Enqueue(new ChunkSenderTuple(data, epSender));
  87. }
  88. /// <summary>
  89. /// Load a packet to be received by the LLUDPServer on the next receive call
  90. /// </summary>
  91. /// <param name="packet"></param>
  92. public void LoadReceive(Packet packet, EndPoint epSender)
  93. {
  94. LoadReceive(packet.ToBytes(), epSender);
  95. }
  96. /// <summary>
  97. /// Calls the protected asynchronous result method. This fires out all data chunks currently queued for send
  98. /// </summary>
  99. /// <param name="result"></param>
  100. public void ReceiveData(IAsyncResult result)
  101. {
  102. while (m_chunksToLoad.Count > 0)
  103. OnReceivedData(result);
  104. }
  105. /// <summary>
  106. /// Has a circuit with the given code been established?
  107. /// </summary>
  108. /// <param name="circuitCode"></param>
  109. /// <returns></returns>
  110. public bool HasCircuit(uint circuitCode)
  111. {
  112. lock (clientCircuits_reverse)
  113. {
  114. return clientCircuits_reverse.ContainsKey(circuitCode);
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// Record the data and sender tuple
  120. /// </summary>
  121. public class ChunkSenderTuple
  122. {
  123. public byte[] Data;
  124. public EndPoint Sender;
  125. public bool BeginReceiveException;
  126. public ChunkSenderTuple(byte[] data, EndPoint sender)
  127. {
  128. Data = data;
  129. Sender = sender;
  130. }
  131. public ChunkSenderTuple(EndPoint sender)
  132. {
  133. Sender = sender;
  134. }
  135. }
  136. }