TestLLUDPServer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 OpenSim 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. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications.Cache;
  34. namespace OpenSim.Region.ClientStack.LindenUDP.Tests
  35. {
  36. /// <summary>
  37. /// This class enables synchronous testing of the LLUDPServer by allowing us to load our own data into the end
  38. /// receive event
  39. /// </summary>
  40. public class TestLLUDPServer : LLUDPServer
  41. {
  42. /// <summary>
  43. /// The chunks of data to pass to the LLUDPServer when it calls EndReceive
  44. /// </summary>
  45. protected Queue<ChunkSenderTuple> m_chunksToLoad = new Queue<ChunkSenderTuple>();
  46. protected override void BeginReceive()
  47. {
  48. if (m_chunksToLoad.Count > 0 && m_chunksToLoad.Peek().BeginReceiveException)
  49. {
  50. ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
  51. reusedEpSender = tuple.Sender;
  52. throw new SocketException();
  53. }
  54. }
  55. protected override bool EndReceive(out int numBytes, IAsyncResult result, ref EndPoint epSender)
  56. {
  57. numBytes = 0;
  58. //System.Console.WriteLine("Queue size " + m_chunksToLoad.Count);
  59. if (m_chunksToLoad.Count <= 0)
  60. return false;
  61. ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
  62. RecvBuffer = tuple.Data;
  63. numBytes = tuple.Data.Length;
  64. epSender = tuple.Sender;
  65. return true;
  66. }
  67. public override void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
  68. {
  69. // Don't do anything just yet
  70. }
  71. /// <summary>
  72. /// Signal that this chunk should throw an exception on Socket.BeginReceive()
  73. /// </summary>
  74. /// <param name="epSender"></param>
  75. public void LoadReceiveWithBeginException(EndPoint epSender)
  76. {
  77. ChunkSenderTuple tuple = new ChunkSenderTuple(epSender);
  78. tuple.BeginReceiveException = true;
  79. m_chunksToLoad.Enqueue(tuple);
  80. }
  81. /// <summary>
  82. /// Load some data to be received by the LLUDPServer on the next receive call
  83. /// </summary>
  84. /// <param name="data"></param>
  85. /// <param name="epSender"></param>
  86. public void LoadReceive(byte[] data, EndPoint epSender)
  87. {
  88. m_chunksToLoad.Enqueue(new ChunkSenderTuple(data, epSender));
  89. }
  90. /// <summary>
  91. /// Load a packet to be received by the LLUDPServer on the next receive call
  92. /// </summary>
  93. /// <param name="packet"></param>
  94. public void LoadReceive(Packet packet, EndPoint epSender)
  95. {
  96. LoadReceive(packet.ToBytes(), epSender);
  97. }
  98. /// <summary>
  99. /// Calls the protected asynchronous result method. This fires out all data chunks currently queued for send
  100. /// </summary>
  101. /// <param name="result"></param>
  102. public void ReceiveData(IAsyncResult result)
  103. {
  104. while (m_chunksToLoad.Count > 0)
  105. OnReceivedData(result);
  106. }
  107. /// <summary>
  108. /// Has a circuit with the given code been established?
  109. /// </summary>
  110. /// <param name="circuitCode"></param>
  111. /// <returns></returns>
  112. public bool HasCircuit(uint circuitCode)
  113. {
  114. lock (clientCircuits_reverse)
  115. {
  116. return clientCircuits_reverse.ContainsKey(circuitCode);
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// Record the data and sender tuple
  122. /// </summary>
  123. public class ChunkSenderTuple
  124. {
  125. public byte[] Data;
  126. public EndPoint Sender;
  127. public bool BeginReceiveException;
  128. public ChunkSenderTuple(byte[] data, EndPoint sender)
  129. {
  130. Data = data;
  131. Sender = sender;
  132. }
  133. public ChunkSenderTuple(EndPoint sender)
  134. {
  135. Sender = sender;
  136. }
  137. }
  138. }