aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/Linden/UDP/Tests/TestLLUDPServer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ClientStack/Linden/UDP/Tests/TestLLUDPServer.cs')
-rw-r--r--OpenSim/Region/ClientStack/Linden/UDP/Tests/TestLLUDPServer.cs153
1 files changed, 153 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/Linden/UDP/Tests/TestLLUDPServer.cs b/OpenSim/Region/ClientStack/Linden/UDP/Tests/TestLLUDPServer.cs
new file mode 100644
index 0000000..f98586d
--- /dev/null
+++ b/OpenSim/Region/ClientStack/Linden/UDP/Tests/TestLLUDPServer.cs
@@ -0,0 +1,153 @@
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
28using System;
29using System.Collections.Generic;
30using System.Net;
31using System.Net.Sockets;
32using OpenMetaverse.Packets;
33
34namespace 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
47 protected override void BeginReceive()
48 {
49 if (m_chunksToLoad.Count > 0 && m_chunksToLoad.Peek().BeginReceiveException)
50 {
51 ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
52 reusedEpSender = tuple.Sender;
53 throw new SocketException();
54 }
55 }
56
57 protected override bool EndReceive(out int numBytes, IAsyncResult result, ref EndPoint epSender)
58 {
59 numBytes = 0;
60
61 //m_log.Debug("Queue size " + m_chunksToLoad.Count);
62
63 if (m_chunksToLoad.Count <= 0)
64 return false;
65
66 ChunkSenderTuple tuple = m_chunksToLoad.Dequeue();
67 RecvBuffer = tuple.Data;
68 numBytes = tuple.Data.Length;
69 epSender = tuple.Sender;
70
71 return true;
72 }
73
74 public override void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
75 {
76 // Don't do anything just yet
77 }
78
79 /// <summary>
80 /// Signal that this chunk should throw an exception on Socket.BeginReceive()
81 /// </summary>
82 /// <param name="epSender"></param>
83 public void LoadReceiveWithBeginException(EndPoint epSender)
84 {
85 ChunkSenderTuple tuple = new ChunkSenderTuple(epSender);
86 tuple.BeginReceiveException = true;
87 m_chunksToLoad.Enqueue(tuple);
88 }
89
90 /// <summary>
91 /// Load some data to be received by the LLUDPServer on the next receive call
92 /// </summary>
93 /// <param name="data"></param>
94 /// <param name="epSender"></param>
95 public void LoadReceive(byte[] data, EndPoint epSender)
96 {
97 m_chunksToLoad.Enqueue(new ChunkSenderTuple(data, epSender));
98 }
99
100 /// <summary>
101 /// Load a packet to be received by the LLUDPServer on the next receive call
102 /// </summary>
103 /// <param name="packet"></param>
104 public void LoadReceive(Packet packet, EndPoint epSender)
105 {
106 LoadReceive(packet.ToBytes(), epSender);
107 }
108
109 /// <summary>
110 /// Calls the protected asynchronous result method. This fires out all data chunks currently queued for send
111 /// </summary>
112 /// <param name="result"></param>
113 public void ReceiveData(IAsyncResult result)
114 {
115 while (m_chunksToLoad.Count > 0)
116 OnReceivedData(result);
117 }
118
119 /// <summary>
120 /// Has a circuit with the given code been established?
121 /// </summary>
122 /// <param name="circuitCode"></param>
123 /// <returns></returns>
124 public bool HasCircuit(uint circuitCode)
125 {
126 lock (clientCircuits_reverse)
127 {
128 return clientCircuits_reverse.ContainsKey(circuitCode);
129 }
130 }
131 }
132
133 /// <summary>
134 /// Record the data and sender tuple
135 /// </summary>
136 public class ChunkSenderTuple
137 {
138 public byte[] Data;
139 public EndPoint Sender;
140 public bool BeginReceiveException;
141
142 public ChunkSenderTuple(byte[] data, EndPoint sender)
143 {
144 Data = data;
145 Sender = sender;
146 }
147
148 public ChunkSenderTuple(EndPoint sender)
149 {
150 Sender = sender;
151 }
152 }
153}