From ee3c428040fd026a92f3dd9a43ab4f88dec38181 Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Thu, 23 Oct 2008 17:16:13 +0000 Subject: * Refactor LLUDPServer slightly so that unit tests can pass in data synchronously. Shouldn't be any functional change --- .../Region/ClientStack/LindenUDP/LLUDPServer.cs | 81 +++++++++++++--------- .../LindenUDP/Tests/BasicCircuitTests.cs | 4 +- .../ClientStack/LindenUDP/Tests/TestLLUDPServer.cs | 54 +++++++++++++++ 3 files changed, 105 insertions(+), 34 deletions(-) create mode 100755 OpenSim/Region/ClientStack/LindenUDP/Tests/TestLLUDPServer.cs (limited to 'OpenSim') diff --git a/OpenSim/Region/ClientStack/LindenUDP/LLUDPServer.cs b/OpenSim/Region/ClientStack/LindenUDP/LLUDPServer.cs index e3ded7f..22086f8 100644 --- a/OpenSim/Region/ClientStack/LindenUDP/LLUDPServer.cs +++ b/OpenSim/Region/ClientStack/LindenUDP/LLUDPServer.cs @@ -180,39 +180,9 @@ namespace OpenSim.Region.ClientStack.LindenUDP { Packet packet = null; int numBytes = 1; - bool ok = false; EndPoint epSender = new IPEndPoint(IPAddress.Any, 0); - try - { - numBytes = m_socket.EndReceiveFrom(result, ref epSender); - ok = true; - } - catch (SocketException e) - { - // TODO : Actually only handle those states that we have control over, re-throw everything else, - // TODO: implement cases as we encounter them. - //m_log.Error("[[CLIENT]: ]: Connection Error! - " + e.ToString()); - switch (e.SocketErrorCode) - { - case SocketError.AlreadyInProgress: - return; - - case SocketError.NetworkReset: - case SocketError.ConnectionReset: - break; - - default: - throw; - } - } - catch (ObjectDisposedException e) - { - m_log.DebugFormat("[CLIENT]: ObjectDisposedException: Object {0} disposed.", e.ObjectName); - // Uhh, what object, and why? this needs better handling. - } - - if (ok) + if (EndReceive(out numBytes, result, ref epSender)) { // Make sure we are getting zeroes when running off the // end of grab / degrab packets from old clients @@ -297,7 +267,10 @@ namespace OpenSim.Region.ClientStack.LindenUDP } } - private void BeginReceive() + /// + /// Begin an asynchronous receive of the next bit of raw data + /// + protected virtual void BeginReceive() { try { @@ -315,6 +288,50 @@ namespace OpenSim.Region.ClientStack.LindenUDP ResetEndPoint(); } } + + /// + /// Finish the process of asynchronously receiving the next bit of raw data + /// + /// The number of bytes received. Will return 0 if no bytes were recieved + /// + /// The sender of the data + /// + protected virtual bool EndReceive(out int numBytes, IAsyncResult result, ref EndPoint epSender) + { + bool hasReceivedOkay = false; + numBytes = 0; + + try + { + numBytes = m_socket.EndReceiveFrom(result, ref epSender); + hasReceivedOkay = true; + } + catch (SocketException e) + { + // TODO : Actually only handle those states that we have control over, re-throw everything else, + // TODO: implement cases as we encounter them. + //m_log.Error("[[CLIENT]: ]: Connection Error! - " + e.ToString()); + switch (e.SocketErrorCode) + { + case SocketError.AlreadyInProgress: + return hasReceivedOkay; + + case SocketError.NetworkReset: + case SocketError.ConnectionReset: + break; + + default: + throw; + } + } + catch (ObjectDisposedException e) + { + m_log.DebugFormat("[CLIENT]: ObjectDisposedException: Object {0} disposed.", e.ObjectName); + // Uhh, what object, and why? this needs better handling. + } + + return hasReceivedOkay; + } private void ResetEndPoint() { diff --git a/OpenSim/Region/ClientStack/LindenUDP/Tests/BasicCircuitTests.cs b/OpenSim/Region/ClientStack/LindenUDP/Tests/BasicCircuitTests.cs index 3b3eb91..cf24e58 100644 --- a/OpenSim/Region/ClientStack/LindenUDP/Tests/BasicCircuitTests.cs +++ b/OpenSim/Region/ClientStack/LindenUDP/Tests/BasicCircuitTests.cs @@ -44,10 +44,10 @@ namespace OpenSim.Region.ClientStack.LindenUDP [Test] public void TestAddClient() { - IClientNetworkServer llUdpServer = new LLUDPServer(); + TestLLUDPServer testLLUDPServer = new TestLLUDPServer(); uint port = 666; - llUdpServer.Initialise(null, ref port, -1, false, new ClientStackUserSettings(), null, null); + testLLUDPServer.Initialise(null, ref port, -1, false, new ClientStackUserSettings(), null, null); //UseCircuitCodePacket uccp = new UseCircuitCodePacket(); //llUdpServer.epS diff --git a/OpenSim/Region/ClientStack/LindenUDP/Tests/TestLLUDPServer.cs b/OpenSim/Region/ClientStack/LindenUDP/Tests/TestLLUDPServer.cs new file mode 100755 index 0000000..5e7cbca --- /dev/null +++ b/OpenSim/Region/ClientStack/LindenUDP/Tests/TestLLUDPServer.cs @@ -0,0 +1,54 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Net; +using OpenSim.Framework; +using OpenSim.Framework.Communications.Cache; + +namespace OpenSim.Region.ClientStack.LindenUDP +{ + /// + /// This class enables synchronous testing of the LLUDPServer by allowing us to load our own data into the end + /// receive event + /// + public class TestLLUDPServer : LLUDPServer + { + protected override void BeginReceive() + { + // Do nothing + } + + protected override bool EndReceive(out int numBytes, IAsyncResult result, ref EndPoint epSender) + { + // TODO: Return a packet loaded in by a test + numBytes = 0; + + return true; + } + } +} -- cgit v1.1