TestLLUDPServer.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Nini.Config;
  32. using OpenMetaverse.Packets;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.ClientStack.LindenUDP;
  35. namespace OpenSim.Tests.Common
  36. {
  37. /// <summary>
  38. /// This class enables regression testing of the LLUDPServer by allowing us to intercept outgoing data.
  39. /// </summary>
  40. public class TestLLUDPServer : LLUDPServer
  41. {
  42. public List<Packet> PacketsSent { get; private set; }
  43. public TestLLUDPServer(IPAddress listenIP, ref uint port, int proxyPortOffsetParm, bool allow_alternate_port, IConfigSource configSource, AgentCircuitManager circuitManager)
  44. : base(listenIP, ref port, proxyPortOffsetParm, allow_alternate_port, configSource, circuitManager)
  45. {
  46. PacketsSent = new List<Packet>();
  47. }
  48. public override void SendAckImmediate(IPEndPoint remoteEndpoint, PacketAckPacket ack)
  49. {
  50. PacketsSent.Add(ack);
  51. }
  52. public override void SendPacket(
  53. LLUDPClient udpClient, Packet packet, ThrottleOutPacketType category, bool allowSplitting, UnackedPacketMethod method)
  54. {
  55. PacketsSent.Add(packet);
  56. }
  57. public void ClientOutgoingPacketHandler(IClientAPI client, bool resendUnacked, bool sendAcks, bool sendPing)
  58. {
  59. m_resendUnacked = resendUnacked;
  60. m_sendAcks = sendAcks;
  61. m_sendPing = sendPing;
  62. ClientOutgoingPacketHandler(client);
  63. }
  64. //// /// <summary>
  65. //// /// The chunks of data to pass to the LLUDPServer when it calls EndReceive
  66. //// /// </summary>
  67. //// protected Queue<ChunkSenderTuple> m_chunksToLoad = new Queue<ChunkSenderTuple>();
  68. //
  69. //// protected override void BeginReceive()
  70. //// {
  71. //// if (m_chunksToLoad.Count > 0 && m_chunksToLoad.Peek().BeginReceiveException)
  72. //// {
  73. //// ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
  74. //// reusedEpSender = tuple.Sender;
  75. //// throw new SocketException();
  76. //// }
  77. //// }
  78. //
  79. //// protected override bool EndReceive(out int numBytes, IAsyncResult result, ref EndPoint epSender)
  80. //// {
  81. //// numBytes = 0;
  82. ////
  83. //// //m_log.Debug("Queue size " + m_chunksToLoad.Count);
  84. ////
  85. //// if (m_chunksToLoad.Count <= 0)
  86. //// return false;
  87. ////
  88. //// ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
  89. //// RecvBuffer = tuple.Data;
  90. //// numBytes = tuple.Data.Length;
  91. //// epSender = tuple.Sender;
  92. ////
  93. //// return true;
  94. //// }
  95. //
  96. //// public override void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
  97. //// {
  98. //// // Don't do anything just yet
  99. //// }
  100. //
  101. // /// <summary>
  102. // /// Signal that this chunk should throw an exception on Socket.BeginReceive()
  103. // /// </summary>
  104. // /// <param name="epSender"></param>
  105. // public void LoadReceiveWithBeginException(EndPoint epSender)
  106. // {
  107. // ChunkSenderTuple tuple = new ChunkSenderTuple(epSender);
  108. // tuple.BeginReceiveException = true;
  109. // m_chunksToLoad.Enqueue(tuple);
  110. // }
  111. //
  112. // /// <summary>
  113. // /// Load some data to be received by the LLUDPServer on the next receive call
  114. // /// </summary>
  115. // /// <param name="data"></param>
  116. // /// <param name="epSender"></param>
  117. // public void LoadReceive(byte[] data, EndPoint epSender)
  118. // {
  119. // m_chunksToLoad.Enqueue(new ChunkSenderTuple(data, epSender));
  120. // }
  121. //
  122. // /// <summary>
  123. // /// Load a packet to be received by the LLUDPServer on the next receive call
  124. // /// </summary>
  125. // /// <param name="packet"></param>
  126. // public void LoadReceive(Packet packet, EndPoint epSender)
  127. // {
  128. // LoadReceive(packet.ToBytes(), epSender);
  129. // }
  130. //
  131. // /// <summary>
  132. // /// Calls the protected asynchronous result method. This fires out all data chunks currently queued for send
  133. // /// </summary>
  134. // /// <param name="result"></param>
  135. // public void ReceiveData(IAsyncResult result)
  136. // {
  137. // // Doesn't work the same way anymore
  138. //// while (m_chunksToLoad.Count > 0)
  139. //// OnReceivedData(result);
  140. // }
  141. }
  142. /// <summary>
  143. /// Record the data and sender tuple
  144. /// </summary>
  145. public class ChunkSenderTuple
  146. {
  147. public byte[] Data;
  148. public EndPoint Sender;
  149. public bool BeginReceiveException;
  150. public ChunkSenderTuple(byte[] data, EndPoint sender)
  151. {
  152. Data = data;
  153. Sender = sender;
  154. }
  155. public ChunkSenderTuple(EndPoint sender)
  156. {
  157. Sender = sender;
  158. }
  159. }
  160. }