From 75f6f6e3ffeca74ca75f80685e243103fe97cf98 Mon Sep 17 00:00:00 2001 From: Jeff Ames Date: Tue, 4 Mar 2008 06:02:48 +0000 Subject: Update svn properties. Minor formatting cleanup. --- ThirdParty/3Di/LoadBalancer/LoadBalancerPlugin.cs | 579 ++++++++++----------- ThirdParty/3Di/LoadBalancer/TcpClient.cs | 406 +++++++-------- ThirdParty/3Di/LoadBalancer/TcpServer.cs | 379 +++++++------- .../ServerPlugin/RegionMonitorPlugin.cs | 2 +- ThirdParty/3Di/RegionProxy/RegionProxyPlugin.cs | 55 +- 5 files changed, 708 insertions(+), 713 deletions(-) (limited to 'ThirdParty/3Di') diff --git a/ThirdParty/3Di/LoadBalancer/LoadBalancerPlugin.cs b/ThirdParty/3Di/LoadBalancer/LoadBalancerPlugin.cs index 6812777..f58e0ee 100644 --- a/ThirdParty/3Di/LoadBalancer/LoadBalancerPlugin.cs +++ b/ThirdParty/3Di/LoadBalancer/LoadBalancerPlugin.cs @@ -1,30 +1,29 @@ /* -* 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. -* -*/ + * 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.IO; @@ -72,7 +71,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer private int proxyOffset; private string proxyURL; private SceneManager sceneManager; - private string serializeDir; + private string serializeDir; private TcpServer mTcpServer; private TcpClient mTcpClient; @@ -81,7 +80,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { m_log.Info("[BALANCER] "+"Entering Initialize()"); - StartTcpServer(); + StartTcpServer(); ClientView.SynchronizeClient = new ClientView.SynchronizeClientHandler(SynchronizePackets); AsynchronousSocketListener.PacketHandler = new AsynchronousSocketListener.PacketRecieveHandler(SynchronizePacketRecieve); @@ -113,10 +112,10 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer private void StartTcpServer() { Thread server_thread = new Thread(new ThreadStart( - delegate { - mTcpServer = new TcpServer(10001); - mTcpServer.start(); - })); + delegate { + mTcpServer = new TcpServer(10001); + mTcpServer.start(); + })); server_thread.Start(); } @@ -126,67 +125,67 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer private XmlRpcResponse GetStatus(XmlRpcRequest request) { - XmlRpcResponse response = new XmlRpcResponse(); - try + XmlRpcResponse response = new XmlRpcResponse(); + try { m_log.Info("[BALANCER] "+"Entering RegionStatus()"); int src_port = (int)request.Params[0]; - Scene scene = null; - // try to get the scene object - RegionInfo src_region = SearchRegionFromPortNum(src_port); - if (sceneManager.TryGetScene(src_region.RegionID, out scene) == false) - { - m_log.Error("[BALANCER] "+"The Scene is not found"); - return response; - } - // serialization of client's informations + Scene scene = null; + // try to get the scene object + RegionInfo src_region = SearchRegionFromPortNum(src_port); + if (sceneManager.TryGetScene(src_region.RegionID, out scene) == false) + { + m_log.Error("[BALANCER] "+"The Scene is not found"); + return response; + } + // serialization of client's informations List presences = scene.GetScenePresences(); - int get_scene_presence = presences.Count; - int get_scene_presence_filter = 0; - foreach (ScenePresence pre in presences) + int get_scene_presence = presences.Count; + int get_scene_presence_filter = 0; + foreach (ScenePresence pre in presences) { ClientView client = (ClientView) pre.ControllingClient; //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) { - if(client.PacketProcessingEnabled==true) { - get_scene_presence_filter++; - } + if(client.PacketProcessingEnabled==true) { + get_scene_presence_filter++; + } } - List avatars = scene.GetAvatars(); - int get_avatar = avatars.Count; - int get_avatar_filter = 0; - string avatar_names = ""; - foreach (ScenePresence pre in avatars) + List avatars = scene.GetAvatars(); + int get_avatar = avatars.Count; + int get_avatar_filter = 0; + string avatar_names = ""; + foreach (ScenePresence pre in avatars) { ClientView client = (ClientView) pre.ControllingClient; //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) { - if(client.PacketProcessingEnabled==true) { - get_avatar_filter++; - avatar_names += pre.Firstname + " " + pre.Lastname + "; "; - } + if(client.PacketProcessingEnabled==true) { + get_avatar_filter++; + avatar_names += pre.Firstname + " " + pre.Lastname + "; "; + } } - - Hashtable responseData = new Hashtable(); - responseData["get_scene_presence_filter"] = get_scene_presence_filter; - responseData["get_scene_presence"] = get_scene_presence; - responseData["get_avatar_filter"] = get_avatar_filter; - responseData["get_avatar"] = get_avatar; - responseData["avatar_names"] = avatar_names; - response.Value = responseData; - - m_log.Info("[BALANCER] "+"Exiting RegionStatus()"); + + Hashtable responseData = new Hashtable(); + responseData["get_scene_presence_filter"] = get_scene_presence_filter; + responseData["get_scene_presence"] = get_scene_presence; + responseData["get_avatar_filter"] = get_avatar_filter; + responseData["get_avatar"] = get_avatar; + responseData["avatar_names"] = avatar_names; + response.Value = responseData; + + m_log.Info("[BALANCER] "+"Exiting RegionStatus()"); } catch (Exception e) { m_log.Error("[BALANCER] "+e.ToString()); m_log.Error("[BALANCER] "+e.StackTrace); } - return response; + return response; } private XmlRpcResponse SerializeRegion(XmlRpcRequest request) { - try + try { m_log.Info("[BALANCER] "+"Entering SerializeRegion()"); @@ -431,7 +430,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer //scene.Backup(); m_log.InfoFormat("[BALANCER] "+"region serialization completed [{0}]", - src_region.RegionID.ToString()); + src_region.RegionID.ToString()); } private void SerializeClient(int idx, Scene scene, ScenePresence pre, string export_dir) @@ -499,7 +498,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer DeserializeClient(dst_region, import_dir); m_log.InfoFormat("[BALANCER] "+"region deserialization completed [{0}]", - dst_region.ToString()); + dst_region.ToString()); } catch (Exception e) { @@ -527,19 +526,19 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer // restore the scene presence /* - files = Directory.GetFiles(import_dir, "Presence_*.bin"); - Array.Sort(files); - - foreach (string filename in files) - { - sp = (ScenePresence)Util.DeserializeFromFile(filename); - Console.WriteLine("agent id = {0}", sp.m_uuid); + files = Directory.GetFiles(import_dir, "Presence_*.bin"); + Array.Sort(files); - scene.m_restorePresences.Add(sp.m_uuid, sp); - File.Delete(filename); + foreach (string filename in files) + { + sp = (ScenePresence)Util.DeserializeFromFile(filename); + Console.WriteLine("agent id = {0}", sp.m_uuid); - m_log.InfoFormat("[BALANCER] "+"scene presence deserialized [{0}]", sp.m_uuid); - } + scene.m_restorePresences.Add(sp.m_uuid, sp); + File.Delete(filename); + + m_log.InfoFormat("[BALANCER] "+"scene presence deserialized [{0}]", sp.m_uuid); + } */ for (int i = 0; ; i++) { @@ -576,18 +575,18 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer udpserv.RestoreClient(agentdata, data.userEP, data.proxyEP); - // waiting for the scene-presense restored - lock (scene.m_restorePresences) - { - Monitor.Wait(scene.m_restorePresences, 3000); - } - - if (scene.ClientManager.TryGetClient(circuit_code, out controller)) - { - m_log.InfoFormat("[BALANCER] " + "get client [{0}]", circuit_code); - controller.SetClientInfo(data); - } - + // waiting for the scene-presense restored + lock (scene.m_restorePresences) + { + Monitor.Wait(scene.m_restorePresences, 3000); + } + + if (scene.ClientManager.TryGetClient(circuit_code, out controller)) + { + m_log.InfoFormat("[BALANCER] " + "get client [{0}]", circuit_code); + controller.SetClientInfo(data); + } + File.Delete(fname); m_log.InfoFormat("[BALANCER] " + "client info deserialized [{0}]", circuit_code); @@ -622,7 +621,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer Console.WriteLine("scene found."); if ((sceneManager.CurrentScene != null) - && (sceneManager.CurrentScene.RegionInfo.RegionID == killScene.RegionInfo.RegionID)) + && (sceneManager.CurrentScene.RegionInfo.RegionID == killScene.RegionInfo.RegionID)) { sceneManager.TrySetCurrentScene(".."); } @@ -671,10 +670,10 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { // stopping clientview thread if (((ClientView)controller).PacketProcessingEnabled) - { + { controller.Stop(); ((ClientView)controller).PacketProcessingEnabled = false; - } + } // teminateing clientview thread controller.Terminate(); m_log.Info("[BALANCER] "+"client thread stopped"); @@ -698,90 +697,90 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer private XmlRpcResponse SplitRegion(XmlRpcRequest request) { - try - { - int myID = (int) request.Params[0]; - int numRegions = (int) request.Params[1]; - regionPortList = new int[numRegions]; - sceneURL = new string[numRegions]; - tcpClientList = new TcpClient[numRegions]; - - for(int i=0; i presences = scene.GetScenePresences(); -presences.Sort(); - foreach (ScenePresence pre in presences) - { - // Divide the presences evenly over the set of subscenes - ClientView client = (ClientView) pre.ControllingClient; - client.PacketProcessingEnabled = (( (i + myID) % sceneURL.Length) == 0); + // Disabling half of the avatars in master, and the other half in slave - m_log.InfoFormat("[SPLITSCENE] === SplitRegion {0}: SP.PacketEnabled {1}", region.RegionID, client.PacketProcessingEnabled); + int i = 0; + List presences = scene.GetScenePresences(); + presences.Sort(); + foreach (ScenePresence pre in presences) + { + // Divide the presences evenly over the set of subscenes + ClientView client = (ClientView) pre.ControllingClient; + client.PacketProcessingEnabled = (( (i + myID) % sceneURL.Length) == 0); - if (!client.PacketProcessingEnabled) - { - // stopping clientview thread - client.Stop(); - } + m_log.InfoFormat("[SPLITSCENE] === SplitRegion {0}: SP.PacketEnabled {1}", region.RegionID, client.PacketProcessingEnabled); - ++i; - } + if (!client.PacketProcessingEnabled) + { + // stopping clientview thread + client.Stop(); + } - scene.splitID = myID; - scene.SynchronizeScene = new Scene.SynchronizeSceneHandler(SynchronizeScenes); - isSplit = true; + ++i; } - else - { - m_log.Error("[SPLITSCENE] "+String.Format("Scene not found {0}", region.RegionID)); - } - } - catch (Exception e) - { - m_log.Error("[SPLITSCENE] "+e.ToString()); - m_log.Error("[SPLITSCENE] "+e.StackTrace); - } - return new XmlRpcResponse(); + scene.splitID = myID; + scene.SynchronizeScene = new Scene.SynchronizeSceneHandler(SynchronizeScenes); + isSplit = true; + } + else + { + m_log.Error("[SPLITSCENE] "+String.Format("Scene not found {0}", region.RegionID)); + } + } + catch (Exception e) + { + m_log.Error("[SPLITSCENE] "+e.ToString()); + m_log.Error("[SPLITSCENE] "+e.StackTrace); + } + + return new XmlRpcResponse(); } private XmlRpcResponse MergeRegions(XmlRpcRequest request) @@ -794,40 +793,40 @@ presences.Sort(); string src_url = (string) request.Params[0]; int src_port = (int) request.Params[1]; - RegionInfo region = SearchRegionFromPortNum(src_port); - - simMain.ProxyCommand(region.proxyUrl, "BlockClientMessages", src_url, src_port + proxyOffset); - - Scene scene; - if (sceneManager.TryGetScene(region.RegionID, out scene)) - { - isSplit = false; - - scene.SynchronizeScene = null; - scene.Region_Status = RegionStatus.Up; - - List presences = scene.GetScenePresences(); - foreach (ScenePresence pre in presences) - { - ClientView client = (ClientView) pre.ControllingClient; - if (!client.PacketProcessingEnabled) - { - client.Restart(); - client.PacketProcessingEnabled = true; - } - } - } - - // Delete the slave scenes - for(int i=1; i presences = scene.GetScenePresences(); + foreach (ScenePresence pre in presences) + { + ClientView client = (ClientView) pre.ControllingClient; + if (!client.PacketProcessingEnabled) + { + client.Restart(); + client.PacketProcessingEnabled = true; + } + } + } + + // Delete the slave scenes + for(int i=1; i{1}, client:{2}]", - // scene.RegionInfo.RegionID, regionPortList[i], pre.UUID.ToString()); - LocalUpdatePhysics(regionPortList[i], pre.UUID, pre.AbsolutePosition, pre.Velocity, pre.PhysicsActor.Flying); - } - else - { - //m_log.Info("[SPLITSCENE] "+"Synchronize ScenePresence (Remote) [region port:{0}, client:{1}, position:{2}, velocity:{3}, flying:{4}]", - // regionPortList[i], pre.UUID.ToString(), pre.AbsolutePosition.ToString(), - // pre.Velocity.ToString(), pre.PhysicsActor.Flying); - - - simMain.XmlRpcCommand(sceneURL[i], "UpdatePhysics", - regionPortList[i], pre.UUID.GetBytes(), - pre.AbsolutePosition.GetBytes(), pre.Velocity.GetBytes(), - pre.PhysicsActor.Flying); + for (int i = 0; i < sceneURL.Length; i++) + { + if (i == scene.splitID) + { + continue; + } + + if(isLocalNeighbour[i]) + { + //m_log.Info("[SPLITSCENE] "+"Synchronize ScenePresence (Local) [region:{0}=>{1}, client:{2}]", + // scene.RegionInfo.RegionID, regionPortList[i], pre.UUID.ToString()); + LocalUpdatePhysics(regionPortList[i], pre.UUID, pre.AbsolutePosition, pre.Velocity, pre.PhysicsActor.Flying); + } + else + { + //m_log.Info("[SPLITSCENE] "+"Synchronize ScenePresence (Remote) [region port:{0}, client:{1}, position:{2}, velocity:{3}, flying:{4}]", + // regionPortList[i], pre.UUID.ToString(), pre.AbsolutePosition.ToString(), + // pre.Velocity.ToString(), pre.PhysicsActor.Flying); + + + simMain.XmlRpcCommand(sceneURL[i], "UpdatePhysics", + regionPortList[i], pre.UUID.GetBytes(), + pre.AbsolutePosition.GetBytes(), pre.Velocity.GetBytes(), + pre.PhysicsActor.Flying); /* - byte[] buff = new byte[12+12+1]; + byte[] buff = new byte[12+12+1]; - Buffer.BlockCopy(pre.AbsolutePosition.GetBytes(), 0, buff, 0, 12); - Buffer.BlockCopy(pre.Velocity.GetBytes(), 0, buff, 12, 12); - buff[24] = (byte)((pre.PhysicsActor.Flying)?1:0); + Buffer.BlockCopy(pre.AbsolutePosition.GetBytes(), 0, buff, 0, 12); + Buffer.BlockCopy(pre.Velocity.GetBytes(), 0, buff, 12, 12); + buff[24] = (byte)((pre.PhysicsActor.Flying)?1:0); - // create header - InternalPacketHeader header = new InternalPacketHeader(); + // create header + InternalPacketHeader header = new InternalPacketHeader(); - header.type = 1; - header.throttlePacketType = 0; - header.numbytes = buff.Length; - header.agent_id = pre.UUID.UUID; - header.region_port = regionPortList[i]; + header.type = 1; + header.throttlePacketType = 0; + header.numbytes = buff.Length; + header.agent_id = pre.UUID.UUID; + header.region_port = regionPortList[i]; - //Send - tcpClientList[i].send(header, buff); + //Send + tcpClientList[i].send(header, buff); */ - } - } + } + } } // ++i; } @@ -968,41 +967,41 @@ presences.Sort(); return false; } - Scene localScene = (Scene)scene; + Scene localScene = (Scene)scene; - for (int i = 0; i < sceneURL.Length; i++) - { - if (i == localScene.splitID) - { - continue; - } - - if(isLocalNeighbour[i]) + for (int i = 0; i < sceneURL.Length; i++) + { + if (i == localScene.splitID) { - //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Local) [type:{0}, client:{1}]", - // packet.Type.ToString(), agentID.ToString()); - LocalUpdatePacket(regionPortList[i], agentID, packet, throttlePacketType); + continue; } - else + + if(isLocalNeighbour[i]) { - //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Remote) [type:{0}, client:{1}]", - // packet.Type.ToString(), agentID.ToString()); - // to bytes - byte[] buff = packet.ToBytes(); + //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Local) [type:{0}, client:{1}]", + // packet.Type.ToString(), agentID.ToString()); + LocalUpdatePacket(regionPortList[i], agentID, packet, throttlePacketType); + } + else + { + //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Remote) [type:{0}, client:{1}]", + // packet.Type.ToString(), agentID.ToString()); + // to bytes + byte[] buff = packet.ToBytes(); - // create header - InternalPacketHeader header = new InternalPacketHeader(); + // create header + InternalPacketHeader header = new InternalPacketHeader(); - header.type = 0; - header.throttlePacketType = (int)throttlePacketType; - header.numbytes = buff.Length; - header.agent_id = agentID.UUID; - header.region_port = regionPortList[i]; + header.type = 0; + header.throttlePacketType = (int)throttlePacketType; + header.numbytes = buff.Length; + header.agent_id = agentID.UUID; + header.region_port = regionPortList[i]; - //Send - tcpClientList[i].send(header, buff); + //Send + tcpClientList[i].send(header, buff); - PacketPool.Instance.ReturnPacket(packet); + PacketPool.Instance.ReturnPacket(packet); } } @@ -1022,20 +1021,20 @@ presences.Sort(); { ScenePresence pre = scene.GetScenePresences().Find(delegate(ScenePresence x) { return x.UUID == agentID; }); - if (pre == null) - { + if (pre == null) + { m_log.ErrorFormat("[SPLITSCENE] [LocalUpdatePacket] ScenePresence is missing... ({0})", agentID.ToString()); return; - } - - if (((ClientView)pre.ControllingClient).PacketProcessingEnabled==true) - { - pre.ControllingClient.OutPacket(packet, throttlePacketType); - } - else - { - PacketPool.Instance.ReturnPacket(packet); - } + } + + if (((ClientView)pre.ControllingClient).PacketProcessingEnabled==true) + { + pre.ControllingClient.OutPacket(packet, throttlePacketType); + } + else + { + PacketPool.Instance.ReturnPacket(packet); + } } } @@ -1061,22 +1060,22 @@ presences.Sort(); // packetEnd = buff.Length; try - { + { //m_log.Info("[SPLITSCENE] "+"PacketPool.Instance : {0}", (PacketPool.Instance == null)?"null":"not null"); //m_log.Info("[SPLITSCENE] "+"buff length={0}", buff.Length); packet = PacketPool.Instance.GetPacket(buff, ref packetEnd, zero); LocalUpdatePacket(header.region_port, new LLUUID(header.agent_id), - packet, (ThrottleOutPacketType)header.throttlePacketType); - } + packet, (ThrottleOutPacketType)header.throttlePacketType); + } catch (Exception e) - { + { m_log.Error("[SPLITSCENE] "+e.ToString()); m_log.Error("[SPLITSCENE] "+e.StackTrace); - } + } - break; + break; case 1: diff --git a/ThirdParty/3Di/LoadBalancer/TcpClient.cs b/ThirdParty/3Di/LoadBalancer/TcpClient.cs index 9f62d33..2e4cdc7 100644 --- a/ThirdParty/3Di/LoadBalancer/TcpClient.cs +++ b/ThirdParty/3Di/LoadBalancer/TcpClient.cs @@ -1,30 +1,29 @@ /* -* 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. -* -*/ + * 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.IO; @@ -35,196 +34,197 @@ using System.Text; using System.Runtime.Serialization.Formatters.Binary; namespace OpenSim.ApplicationPlugins.LoadBalancer { - public class AsynchronousClient { - private static ManualResetEvent connectDone = new ManualResetEvent(false); - private static ManualResetEvent sendDone = new ManualResetEvent(false); - private static ManualResetEvent receiveDone = new ManualResetEvent(false); - private static String response = String.Empty; - - public static Socket StartClient(string hostname, int port) { - try { - IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname); - IPAddress ipAddress = ipHostInfo.AddressList[0]; - IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); - - Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client); - connectDone.WaitOne(); - /* - Send(client,"This is a test"); - sendDone.WaitOne(); - Receive(client); - receiveDone.WaitOne(); - client.Shutdown(SocketShutdown.Both); - client.Close(); - */ - return client; - } catch (Exception e) { - Console.WriteLine(e.ToString()); - throw new Exception("socket error !!"); - } - } - - private static void ConnectCallback(IAsyncResult ar) { - try { - Socket client = (Socket) ar.AsyncState; - client.EndConnect(ar); - Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); - connectDone.Set(); - } catch (Exception e) { - Console.WriteLine(e.ToString()); - } - } + public class AsynchronousClient { + private static ManualResetEvent connectDone = new ManualResetEvent(false); + private static ManualResetEvent sendDone = new ManualResetEvent(false); + private static ManualResetEvent receiveDone = new ManualResetEvent(false); + private static String response = String.Empty; + + public static Socket StartClient(string hostname, int port) { + try { + IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname); + IPAddress ipAddress = ipHostInfo.AddressList[0]; + IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); + + Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client); + connectDone.WaitOne(); + /* + Send(client,"This is a test"); + sendDone.WaitOne(); + Receive(client); + receiveDone.WaitOne(); + client.Shutdown(SocketShutdown.Both); + client.Close(); + */ + return client; + } catch (Exception e) { + Console.WriteLine(e.ToString()); + throw new Exception("socket error !!"); + } + } + + private static void ConnectCallback(IAsyncResult ar) { + try { + Socket client = (Socket) ar.AsyncState; + client.EndConnect(ar); + Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); + connectDone.Set(); + } catch (Exception e) { + Console.WriteLine(e.ToString()); + } + } /* - public static void Receive(Socket client) { - try { - StateObject state = new StateObject(); - state.workSocket = client; - client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); - } catch (Exception e) { - Console.WriteLine(e.ToString()); - } - } - - private static void ReceiveCallback( IAsyncResult ar ) { - try { - StateObject state = (StateObject) ar.AsyncState; - Socket client = state.workSocket; - - int bytesRead = client.EndReceive(ar); - if (bytesRead > 0) { - state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead)); - client.BeginReceive(state.buffer,0,StateObject.BufferSize,0, new AsyncCallback(ReceiveCallback), state); - } else { - if (state.sb.Length > 1) { - response = state.sb.ToString(); - } - receiveDone.Set(); - } - } catch (Exception e) { - Console.WriteLine(e.ToString()); - } - } + public static void Receive(Socket client) { + try { + StateObject state = new StateObject(); + state.workSocket = client; + client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); + } catch (Exception e) { + Console.WriteLine(e.ToString()); + } + } + + private static void ReceiveCallback( IAsyncResult ar ) { + try { + StateObject state = (StateObject) ar.AsyncState; + Socket client = state.workSocket; + + int bytesRead = client.EndReceive(ar); + if (bytesRead > 0) { + state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead)); + client.BeginReceive(state.buffer,0,StateObject.BufferSize,0, new AsyncCallback(ReceiveCallback), state); + } else { + if (state.sb.Length > 1) { + response = state.sb.ToString(); + } + receiveDone.Set(); + } + } catch (Exception e) { + Console.WriteLine(e.ToString()); + } + } */ - public static void Send(Socket client, byte[] byteData) { - client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); - } - - private static void SendCallback(IAsyncResult ar) { - try { - Socket client = (Socket) ar.AsyncState; - int bytesSent = client.EndSend(ar); - //Console.WriteLine("Sent {0} bytes to server.", bytesSent); - sendDone.Set(); - } catch (Exception e) { - Console.WriteLine(e.ToString()); - } - } - } - -public class InternalPacketHeader -{ - private byte[] buffer = new byte[32]; - public int type; - public int throttlePacketType; - public int numbytes; - public Guid agent_id; - public int region_port; - - public void FromBytes(byte[] bytes) + public static void Send(Socket client, byte[] byteData) { + client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); + } + + private static void SendCallback(IAsyncResult ar) { + try { + Socket client = (Socket) ar.AsyncState; + int bytesSent = client.EndSend(ar); + //Console.WriteLine("Sent {0} bytes to server.", bytesSent); + sendDone.Set(); + } catch (Exception e) { + Console.WriteLine(e.ToString()); + } + } + } + + public class InternalPacketHeader { - int i = 0; // offset - try + private byte[] buffer = new byte[32]; + public int type; + public int throttlePacketType; + public int numbytes; + public Guid agent_id; + public int region_port; + + public void FromBytes(byte[] bytes) { - this.type = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); - this.throttlePacketType = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); - this.numbytes = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); - this.agent_id = new Guid( - bytes[i++] | (bytes[i++] << 8) | (bytes[i++] << 16) | bytes[i++] << 24, - (short)(bytes[i++] | (bytes[i++] << 8)), - (short)(bytes[i++] | (bytes[i++] << 8)), - bytes[i++], bytes[i++], bytes[i++], bytes[i++], - bytes[i++], bytes[i++], bytes[i++], bytes[i++]); - this.region_port = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); + int i = 0; // offset + try + { + this.type = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); + this.throttlePacketType = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); + this.numbytes = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); + this.agent_id = new Guid( + bytes[i++] | (bytes[i++] << 8) | (bytes[i++] << 16) | bytes[i++] << 24, + (short)(bytes[i++] | (bytes[i++] << 8)), + (short)(bytes[i++] | (bytes[i++] << 8)), + bytes[i++], bytes[i++], bytes[i++], bytes[i++], + bytes[i++], bytes[i++], bytes[i++], bytes[i++]); + this.region_port = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); + } + catch (Exception) + { + throw new Exception("bad format!!!"); + } } - catch (Exception) + + public byte[] ToBytes() { - throw new Exception("bad format!!!"); + int i = 0; + this.buffer[i++] = (byte)(this.type % 256); + this.buffer[i++] = (byte)((this.type >> 8) % 256); + this.buffer[i++] = (byte)((this.type >> 16) % 256); + this.buffer[i++] = (byte)((this.type >> 24) % 256); + + this.buffer[i++] = (byte)(this.throttlePacketType % 256); + this.buffer[i++] = (byte)((this.throttlePacketType >> 8) % 256); + this.buffer[i++] = (byte)((this.throttlePacketType >> 16) % 256); + this.buffer[i++] = (byte)((this.throttlePacketType >> 24) % 256); + + this.buffer[i++] = (byte)(this.numbytes % 256); + this.buffer[i++] = (byte)((this.numbytes >> 8) % 256); + this.buffer[i++] = (byte)((this.numbytes >> 16) % 256); + this.buffer[i++] = (byte)((this.numbytes >> 24) % 256); + + // no endian care + Buffer.BlockCopy(agent_id.ToByteArray(), 0, this.buffer, i, 16); i += 16; + + this.buffer[i++] = (byte)(this.region_port % 256); + this.buffer[i++] = (byte)((this.region_port >> 8) % 256); + this.buffer[i++] = (byte)((this.region_port >> 16) % 256); + this.buffer[i++] = (byte)((this.region_port >> 24) % 256); + + return this.buffer; } } - public byte[] ToBytes() - { - int i = 0; - this.buffer[i++] = (byte)(this.type % 256); - this.buffer[i++] = (byte)((this.type >> 8) % 256); - this.buffer[i++] = (byte)((this.type >> 16) % 256); - this.buffer[i++] = (byte)((this.type >> 24) % 256); - - this.buffer[i++] = (byte)(this.throttlePacketType % 256); - this.buffer[i++] = (byte)((this.throttlePacketType >> 8) % 256); - this.buffer[i++] = (byte)((this.throttlePacketType >> 16) % 256); - this.buffer[i++] = (byte)((this.throttlePacketType >> 24) % 256); - - this.buffer[i++] = (byte)(this.numbytes % 256); - this.buffer[i++] = (byte)((this.numbytes >> 8) % 256); - this.buffer[i++] = (byte)((this.numbytes >> 16) % 256); - this.buffer[i++] = (byte)((this.numbytes >> 24) % 256); - - // no endian care - Buffer.BlockCopy(agent_id.ToByteArray(), 0, this.buffer, i, 16); i += 16; - - this.buffer[i++] = (byte)(this.region_port % 256); - this.buffer[i++] = (byte)((this.region_port >> 8) % 256); - this.buffer[i++] = (byte)((this.region_port >> 16) % 256); - this.buffer[i++] = (byte)((this.region_port >> 24) % 256); - - return this.buffer; - } -} - public class TcpClient { - - public static int internalPacketHeaderSize = 4*4 + 16*1; - - private string mHostname; - private int mPort; - private Socket mConnection; - public TcpClient(string hostname, int port) { - this.mHostname = hostname; - this.mPort = port; - this.mConnection = null; - } - public void connect() { - this.mConnection = AsynchronousClient.StartClient(mHostname, mPort); - } + public class TcpClient { + + public static int internalPacketHeaderSize = 4*4 + 16*1; + + private string mHostname; + private int mPort; + private Socket mConnection; + public TcpClient(string hostname, int port) { + this.mHostname = hostname; + this.mPort = port; + this.mConnection = null; + } + public void connect() { + this.mConnection = AsynchronousClient.StartClient(mHostname, mPort); + } /* - public void recevie() { - if (mConnection == null) { - throw new Exception("client not initialized"); - } - try - { - AsynchronousClient.Receive(this.mConnection); - } - catch (Exception e) - { - Console.WriteLine(e.ToString()); - mConnection = null; - } - } + public void recevie() { + if (mConnection == null) { + throw new Exception("client not initialized"); + } + try + { + AsynchronousClient.Receive(this.mConnection); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + mConnection = null; + } + } */ - public void send(InternalPacketHeader header, byte[] packet) { + public void send(InternalPacketHeader header, byte[] packet) { - lock (this) + lock (this) { - if (mConnection == null) { -// throw new Exception("client not initialized"); - connect(); - } + if (mConnection == null) { +// throw new Exception("client not initialized"); + connect(); + } - AsynchronousClient.Send(this.mConnection, header.ToBytes()); + AsynchronousClient.Send(this.mConnection, header.ToBytes()); /* for (int i = 0; i < 10; i++) @@ -233,8 +233,8 @@ for (int i = 0; i < 10; i++) } Console.WriteLine(""); */ - AsynchronousClient.Send(this.mConnection, packet); + AsynchronousClient.Send(this.mConnection, packet); } - } - } + } + } } diff --git a/ThirdParty/3Di/LoadBalancer/TcpServer.cs b/ThirdParty/3Di/LoadBalancer/TcpServer.cs index ee8bcba..4e651b6 100644 --- a/ThirdParty/3Di/LoadBalancer/TcpServer.cs +++ b/ThirdParty/3Di/LoadBalancer/TcpServer.cs @@ -1,30 +1,29 @@ /* -* 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. -* -*/ + * 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.IO; @@ -38,182 +37,180 @@ using OpenSim.Framework.Console; namespace OpenSim.ApplicationPlugins.LoadBalancer { - public class StateObject { - public Socket workSocket = null; - public const int BufferSize = 2048; - public byte[] buffer = new byte[BufferSize]; - public MemoryStream ms_ptr = new MemoryStream(); - public InternalPacketHeader header = null; - } + public class StateObject { + public Socket workSocket = null; + public const int BufferSize = 2048; + public byte[] buffer = new byte[BufferSize]; + public MemoryStream ms_ptr = new MemoryStream(); + public InternalPacketHeader header = null; + } - public class AsynchronousSocketListener { - public static string data = null; - public static ManualResetEvent allDone = new ManualResetEvent(false); + public class AsynchronousSocketListener { + public static string data = null; + public static ManualResetEvent allDone = new ManualResetEvent(false); #region KIRYU public delegate void PacketRecieveHandler(InternalPacketHeader header, byte[] buff); public static PacketRecieveHandler PacketHandler = null; #endregion - public AsynchronousSocketListener() { } - - public static void StartListening(int port) { - IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); - IPAddress ipAddress = ipHostInfo.AddressList[0]; - IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); - - Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); - try { - listener.Bind(localEndPoint); - listener.Listen(100); - while (true) { - allDone.Reset(); - listener.BeginAccept( new AsyncCallback(AcceptCallback), listener ); - allDone.WaitOne(); - } - } catch (Exception e) { - Console.WriteLine(e.ToString()); - } - /* - Console.WriteLine("\nPress ENTER to continue..."); - Console.Read(); - */ - } - - public static void AcceptCallback(IAsyncResult ar) { - allDone.Set(); - Socket listener = (Socket) ar.AsyncState; - Socket handler = listener.EndAccept(ar); - StateObject state = new StateObject(); - state.workSocket = handler; - handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); - } - - public static void ReadCallback(IAsyncResult ar) { - String content = String.Empty; - StateObject state = (StateObject) ar.AsyncState; - Socket handler = state.workSocket; + public AsynchronousSocketListener() { } + + public static void StartListening(int port) { + IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); + IPAddress ipAddress = ipHostInfo.AddressList[0]; + IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); + + Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); + try { + listener.Bind(localEndPoint); + listener.Listen(100); + while (true) { + allDone.Reset(); + listener.BeginAccept( new AsyncCallback(AcceptCallback), listener ); + allDone.WaitOne(); + } + } catch (Exception e) { + Console.WriteLine(e.ToString()); + } + /* + Console.WriteLine("\nPress ENTER to continue..."); + Console.Read(); + */ + } + + public static void AcceptCallback(IAsyncResult ar) { + allDone.Set(); + Socket listener = (Socket) ar.AsyncState; + Socket handler = listener.EndAccept(ar); + StateObject state = new StateObject(); + state.workSocket = handler; + handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); + } + + public static void ReadCallback(IAsyncResult ar) { + String content = String.Empty; + StateObject state = (StateObject) ar.AsyncState; + Socket handler = state.workSocket; try { - - int bytesRead = handler.EndReceive(ar); - - //MainLog.Instance.Verbose("TCPSERVER", "Received packet [{0}]", bytesRead); - - if (bytesRead > 0) { - state.ms_ptr.Write(state.buffer, 0, bytesRead); - } - else - { - //MainLog.Instance.Verbose("TCPSERVER", "Connection terminated"); - return; - } - - long rest_size = state.ms_ptr.Length; - long current_pos = 0; - while (rest_size > TcpClient.internalPacketHeaderSize) { - - if ((state.header == null) && (rest_size >= TcpClient.internalPacketHeaderSize)) - { - //MainLog.Instance.Verbose("TCPSERVER", "Processing header"); - - // reading header - state.header = new InternalPacketHeader(); - - byte[] headerbytes = new byte[TcpClient.internalPacketHeaderSize]; - state.ms_ptr.Position = current_pos; - state.ms_ptr.Read(headerbytes, 0, TcpClient.internalPacketHeaderSize); - state.ms_ptr.Seek(0, SeekOrigin.End); - state.header.FromBytes(headerbytes); - } - - if ((state.header != null) && (rest_size >= state.header.numbytes + TcpClient.internalPacketHeaderSize)) - { - //MainLog.Instance.Verbose("TCPSERVER", "Processing body"); - - // reading body - byte[] packet = new byte[state.header.numbytes]; - state.ms_ptr.Position = current_pos + TcpClient.internalPacketHeaderSize; - state.ms_ptr.Read(packet, 0, state.header.numbytes); + int bytesRead = handler.EndReceive(ar); + + //MainLog.Instance.Verbose("TCPSERVER", "Received packet [{0}]", bytesRead); + + if (bytesRead > 0) { + state.ms_ptr.Write(state.buffer, 0, bytesRead); + } + else + { + //MainLog.Instance.Verbose("TCPSERVER", "Connection terminated"); + return; + } + + long rest_size = state.ms_ptr.Length; + long current_pos = 0; + while (rest_size > TcpClient.internalPacketHeaderSize) { + + if ((state.header == null) && (rest_size >= TcpClient.internalPacketHeaderSize)) + { + //MainLog.Instance.Verbose("TCPSERVER", "Processing header"); + + // reading header + state.header = new InternalPacketHeader(); + + byte[] headerbytes = new byte[TcpClient.internalPacketHeaderSize]; + state.ms_ptr.Position = current_pos; + state.ms_ptr.Read(headerbytes, 0, TcpClient.internalPacketHeaderSize); + state.ms_ptr.Seek(0, SeekOrigin.End); + state.header.FromBytes(headerbytes); + } + + if ((state.header != null) && (rest_size >= state.header.numbytes + TcpClient.internalPacketHeaderSize)) + { + //MainLog.Instance.Verbose("TCPSERVER", "Processing body"); + + // reading body + byte[] packet = new byte[state.header.numbytes]; + state.ms_ptr.Position = current_pos + TcpClient.internalPacketHeaderSize; + state.ms_ptr.Read(packet, 0, state.header.numbytes); /* - for(int i=0; i