TestLLUDPServer.cs 6.3 KB

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