TestLLUDPServer.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. public void ClientOutgoingPacketHandler(IClientAPI client, bool resendUnacked, bool sendAcks, bool sendPing)
  57. {
  58. m_resendUnacked = resendUnacked;
  59. m_sendAcks = sendAcks;
  60. m_sendPing = sendPing;
  61. ClientOutgoingPacketHandler(client);
  62. }
  63. //// /// <summary>
  64. //// /// The chunks of data to pass to the LLUDPServer when it calls EndReceive
  65. //// /// </summary>
  66. //// protected Queue<ChunkSenderTuple> m_chunksToLoad = new Queue<ChunkSenderTuple>();
  67. //
  68. //// protected override void BeginReceive()
  69. //// {
  70. //// if (m_chunksToLoad.Count > 0 && m_chunksToLoad.Peek().BeginReceiveException)
  71. //// {
  72. //// ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
  73. //// reusedEpSender = tuple.Sender;
  74. //// throw new SocketException();
  75. //// }
  76. //// }
  77. //
  78. //// protected override bool EndReceive(out int numBytes, IAsyncResult result, ref EndPoint epSender)
  79. //// {
  80. //// numBytes = 0;
  81. ////
  82. //// //m_log.Debug("Queue size " + m_chunksToLoad.Count);
  83. ////
  84. //// if (m_chunksToLoad.Count <= 0)
  85. //// return false;
  86. ////
  87. //// ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
  88. //// RecvBuffer = tuple.Data;
  89. //// numBytes = tuple.Data.Length;
  90. //// epSender = tuple.Sender;
  91. ////
  92. //// return true;
  93. //// }
  94. //
  95. //// public override void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
  96. //// {
  97. //// // Don't do anything just yet
  98. //// }
  99. //
  100. // /// <summary>
  101. // /// Signal that this chunk should throw an exception on Socket.BeginReceive()
  102. // /// </summary>
  103. // /// <param name="epSender"></param>
  104. // public void LoadReceiveWithBeginException(EndPoint epSender)
  105. // {
  106. // ChunkSenderTuple tuple = new ChunkSenderTuple(epSender);
  107. // tuple.BeginReceiveException = true;
  108. // m_chunksToLoad.Enqueue(tuple);
  109. // }
  110. //
  111. // /// <summary>
  112. // /// Load some data to be received by the LLUDPServer on the next receive call
  113. // /// </summary>
  114. // /// <param name="data"></param>
  115. // /// <param name="epSender"></param>
  116. // public void LoadReceive(byte[] data, EndPoint epSender)
  117. // {
  118. // m_chunksToLoad.Enqueue(new ChunkSenderTuple(data, epSender));
  119. // }
  120. //
  121. // /// <summary>
  122. // /// Load a packet to be received by the LLUDPServer on the next receive call
  123. // /// </summary>
  124. // /// <param name="packet"></param>
  125. // public void LoadReceive(Packet packet, EndPoint epSender)
  126. // {
  127. // LoadReceive(packet.ToBytes(), epSender);
  128. // }
  129. //
  130. // /// <summary>
  131. // /// Calls the protected asynchronous result method. This fires out all data chunks currently queued for send
  132. // /// </summary>
  133. // /// <param name="result"></param>
  134. // public void ReceiveData(IAsyncResult result)
  135. // {
  136. // // Doesn't work the same way anymore
  137. //// while (m_chunksToLoad.Count > 0)
  138. //// OnReceivedData(result);
  139. // }
  140. }
  141. /// <summary>
  142. /// Record the data and sender tuple
  143. /// </summary>
  144. public class ChunkSenderTuple
  145. {
  146. public byte[] Data;
  147. public EndPoint Sender;
  148. public bool BeginReceiveException;
  149. public ChunkSenderTuple(byte[] data, EndPoint sender)
  150. {
  151. Data = data;
  152. Sender = sender;
  153. }
  154. public ChunkSenderTuple(EndPoint sender)
  155. {
  156. Sender = sender;
  157. }
  158. }
  159. }