diff options
author | Jeff Ames | 2008-03-04 06:02:48 +0000 |
---|---|---|
committer | Jeff Ames | 2008-03-04 06:02:48 +0000 |
commit | 75f6f6e3ffeca74ca75f80685e243103fe97cf98 (patch) | |
tree | 7541cdfce74eb7378e468a70680216b76a60188a /ThirdParty/3Di/LoadBalancer | |
parent | Merged 3Di code that provides scene and avatar serialization, and plugin supp... (diff) | |
download | opensim-SC-75f6f6e3ffeca74ca75f80685e243103fe97cf98.zip opensim-SC-75f6f6e3ffeca74ca75f80685e243103fe97cf98.tar.gz opensim-SC-75f6f6e3ffeca74ca75f80685e243103fe97cf98.tar.bz2 opensim-SC-75f6f6e3ffeca74ca75f80685e243103fe97cf98.tar.xz |
Update svn properties. Minor formatting cleanup.
Diffstat (limited to '')
-rw-r--r-- | ThirdParty/3Di/LoadBalancer/LoadBalancerPlugin.cs | 579 | ||||
-rw-r--r-- | ThirdParty/3Di/LoadBalancer/TcpClient.cs | 406 | ||||
-rw-r--r-- | ThirdParty/3Di/LoadBalancer/TcpServer.cs | 379 |
3 files changed, 680 insertions, 684 deletions
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 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (c) Contributors, http://opensimulator.org/ | 2 | * Copyright (c) Contributors, http://opensimulator.org/ |
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | 3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. |
4 | * | 4 | * |
5 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: | 6 | * modification, are permitted provided that the following conditions are met: |
7 | * * Redistributions of source code must retain the above copyright | 7 | * * Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. | 8 | * notice, this list of conditions and the following disclaimer. |
9 | * * Redistributions in binary form must reproduce the above copyright | 9 | * * Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the | 10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. | 11 | * documentation and/or other materials provided with the distribution. |
12 | * * Neither the name of the OpenSim Project nor the | 12 | * * Neither the name of the OpenSim Project nor the |
13 | * names of its contributors may be used to endorse or promote products | 13 | * names of its contributors may be used to endorse or promote products |
14 | * derived from this software without specific prior written permission. | 14 | * derived from this software without specific prior written permission. |
15 | * | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | 16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY |
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | 19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * | 26 | */ |
27 | */ | ||
28 | 27 | ||
29 | using System; | 28 | using System; |
30 | using System.IO; | 29 | using System.IO; |
@@ -72,7 +71,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
72 | private int proxyOffset; | 71 | private int proxyOffset; |
73 | private string proxyURL; | 72 | private string proxyURL; |
74 | private SceneManager sceneManager; | 73 | private SceneManager sceneManager; |
75 | private string serializeDir; | 74 | private string serializeDir; |
76 | 75 | ||
77 | private TcpServer mTcpServer; | 76 | private TcpServer mTcpServer; |
78 | private TcpClient mTcpClient; | 77 | private TcpClient mTcpClient; |
@@ -81,7 +80,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
81 | { | 80 | { |
82 | m_log.Info("[BALANCER] "+"Entering Initialize()"); | 81 | m_log.Info("[BALANCER] "+"Entering Initialize()"); |
83 | 82 | ||
84 | StartTcpServer(); | 83 | StartTcpServer(); |
85 | ClientView.SynchronizeClient = new ClientView.SynchronizeClientHandler(SynchronizePackets); | 84 | ClientView.SynchronizeClient = new ClientView.SynchronizeClientHandler(SynchronizePackets); |
86 | AsynchronousSocketListener.PacketHandler = new AsynchronousSocketListener.PacketRecieveHandler(SynchronizePacketRecieve); | 85 | AsynchronousSocketListener.PacketHandler = new AsynchronousSocketListener.PacketRecieveHandler(SynchronizePacketRecieve); |
87 | 86 | ||
@@ -113,10 +112,10 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
113 | private void StartTcpServer() | 112 | private void StartTcpServer() |
114 | { | 113 | { |
115 | Thread server_thread = new Thread(new ThreadStart( | 114 | Thread server_thread = new Thread(new ThreadStart( |
116 | delegate { | 115 | delegate { |
117 | mTcpServer = new TcpServer(10001); | 116 | mTcpServer = new TcpServer(10001); |
118 | mTcpServer.start(); | 117 | mTcpServer.start(); |
119 | })); | 118 | })); |
120 | server_thread.Start(); | 119 | server_thread.Start(); |
121 | } | 120 | } |
122 | 121 | ||
@@ -126,67 +125,67 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
126 | 125 | ||
127 | private XmlRpcResponse GetStatus(XmlRpcRequest request) | 126 | private XmlRpcResponse GetStatus(XmlRpcRequest request) |
128 | { | 127 | { |
129 | XmlRpcResponse response = new XmlRpcResponse(); | 128 | XmlRpcResponse response = new XmlRpcResponse(); |
130 | try | 129 | try |
131 | { | 130 | { |
132 | m_log.Info("[BALANCER] "+"Entering RegionStatus()"); | 131 | m_log.Info("[BALANCER] "+"Entering RegionStatus()"); |
133 | 132 | ||
134 | int src_port = (int)request.Params[0]; | 133 | int src_port = (int)request.Params[0]; |
135 | Scene scene = null; | 134 | Scene scene = null; |
136 | // try to get the scene object | 135 | // try to get the scene object |
137 | RegionInfo src_region = SearchRegionFromPortNum(src_port); | 136 | RegionInfo src_region = SearchRegionFromPortNum(src_port); |
138 | if (sceneManager.TryGetScene(src_region.RegionID, out scene) == false) | 137 | if (sceneManager.TryGetScene(src_region.RegionID, out scene) == false) |
139 | { | 138 | { |
140 | m_log.Error("[BALANCER] "+"The Scene is not found"); | 139 | m_log.Error("[BALANCER] "+"The Scene is not found"); |
141 | return response; | 140 | return response; |
142 | } | 141 | } |
143 | // serialization of client's informations | 142 | // serialization of client's informations |
144 | List<ScenePresence> presences = scene.GetScenePresences(); | 143 | List<ScenePresence> presences = scene.GetScenePresences(); |
145 | int get_scene_presence = presences.Count; | 144 | int get_scene_presence = presences.Count; |
146 | int get_scene_presence_filter = 0; | 145 | int get_scene_presence_filter = 0; |
147 | foreach (ScenePresence pre in presences) | 146 | foreach (ScenePresence pre in presences) |
148 | { | 147 | { |
149 | ClientView client = (ClientView) pre.ControllingClient; | 148 | ClientView client = (ClientView) pre.ControllingClient; |
150 | //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) { | 149 | //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) { |
151 | if(client.PacketProcessingEnabled==true) { | 150 | if(client.PacketProcessingEnabled==true) { |
152 | get_scene_presence_filter++; | 151 | get_scene_presence_filter++; |
153 | } | 152 | } |
154 | } | 153 | } |
155 | List<ScenePresence> avatars = scene.GetAvatars(); | 154 | List<ScenePresence> avatars = scene.GetAvatars(); |
156 | int get_avatar = avatars.Count; | 155 | int get_avatar = avatars.Count; |
157 | int get_avatar_filter = 0; | 156 | int get_avatar_filter = 0; |
158 | string avatar_names = ""; | 157 | string avatar_names = ""; |
159 | foreach (ScenePresence pre in avatars) | 158 | foreach (ScenePresence pre in avatars) |
160 | { | 159 | { |
161 | ClientView client = (ClientView) pre.ControllingClient; | 160 | ClientView client = (ClientView) pre.ControllingClient; |
162 | //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) { | 161 | //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) { |
163 | if(client.PacketProcessingEnabled==true) { | 162 | if(client.PacketProcessingEnabled==true) { |
164 | get_avatar_filter++; | 163 | get_avatar_filter++; |
165 | avatar_names += pre.Firstname + " " + pre.Lastname + "; "; | 164 | avatar_names += pre.Firstname + " " + pre.Lastname + "; "; |
166 | } | 165 | } |
167 | } | 166 | } |
168 | 167 | ||
169 | Hashtable responseData = new Hashtable(); | 168 | Hashtable responseData = new Hashtable(); |
170 | responseData["get_scene_presence_filter"] = get_scene_presence_filter; | 169 | responseData["get_scene_presence_filter"] = get_scene_presence_filter; |
171 | responseData["get_scene_presence"] = get_scene_presence; | 170 | responseData["get_scene_presence"] = get_scene_presence; |
172 | responseData["get_avatar_filter"] = get_avatar_filter; | 171 | responseData["get_avatar_filter"] = get_avatar_filter; |
173 | responseData["get_avatar"] = get_avatar; | 172 | responseData["get_avatar"] = get_avatar; |
174 | responseData["avatar_names"] = avatar_names; | 173 | responseData["avatar_names"] = avatar_names; |
175 | response.Value = responseData; | 174 | response.Value = responseData; |
176 | 175 | ||
177 | m_log.Info("[BALANCER] "+"Exiting RegionStatus()"); | 176 | m_log.Info("[BALANCER] "+"Exiting RegionStatus()"); |
178 | } | 177 | } |
179 | catch (Exception e) | 178 | catch (Exception e) |
180 | { | 179 | { |
181 | m_log.Error("[BALANCER] "+e.ToString()); | 180 | m_log.Error("[BALANCER] "+e.ToString()); |
182 | m_log.Error("[BALANCER] "+e.StackTrace); | 181 | m_log.Error("[BALANCER] "+e.StackTrace); |
183 | } | 182 | } |
184 | return response; | 183 | return response; |
185 | } | 184 | } |
186 | 185 | ||
187 | private XmlRpcResponse SerializeRegion(XmlRpcRequest request) | 186 | private XmlRpcResponse SerializeRegion(XmlRpcRequest request) |
188 | { | 187 | { |
189 | try | 188 | try |
190 | { | 189 | { |
191 | m_log.Info("[BALANCER] "+"Entering SerializeRegion()"); | 190 | m_log.Info("[BALANCER] "+"Entering SerializeRegion()"); |
192 | 191 | ||
@@ -431,7 +430,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
431 | //scene.Backup(); | 430 | //scene.Backup(); |
432 | 431 | ||
433 | m_log.InfoFormat("[BALANCER] "+"region serialization completed [{0}]", | 432 | m_log.InfoFormat("[BALANCER] "+"region serialization completed [{0}]", |
434 | src_region.RegionID.ToString()); | 433 | src_region.RegionID.ToString()); |
435 | } | 434 | } |
436 | 435 | ||
437 | private void SerializeClient(int idx, Scene scene, ScenePresence pre, string export_dir) | 436 | private void SerializeClient(int idx, Scene scene, ScenePresence pre, string export_dir) |
@@ -499,7 +498,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
499 | DeserializeClient(dst_region, import_dir); | 498 | DeserializeClient(dst_region, import_dir); |
500 | 499 | ||
501 | m_log.InfoFormat("[BALANCER] "+"region deserialization completed [{0}]", | 500 | m_log.InfoFormat("[BALANCER] "+"region deserialization completed [{0}]", |
502 | dst_region.ToString()); | 501 | dst_region.ToString()); |
503 | } | 502 | } |
504 | catch (Exception e) | 503 | catch (Exception e) |
505 | { | 504 | { |
@@ -527,19 +526,19 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
527 | 526 | ||
528 | // restore the scene presence | 527 | // restore the scene presence |
529 | /* | 528 | /* |
530 | files = Directory.GetFiles(import_dir, "Presence_*.bin"); | 529 | files = Directory.GetFiles(import_dir, "Presence_*.bin"); |
531 | Array.Sort(files); | 530 | Array.Sort(files); |
532 | |||
533 | foreach (string filename in files) | ||
534 | { | ||
535 | sp = (ScenePresence)Util.DeserializeFromFile(filename); | ||
536 | Console.WriteLine("agent id = {0}", sp.m_uuid); | ||
537 | 531 | ||
538 | scene.m_restorePresences.Add(sp.m_uuid, sp); | 532 | foreach (string filename in files) |
539 | File.Delete(filename); | 533 | { |
534 | sp = (ScenePresence)Util.DeserializeFromFile(filename); | ||
535 | Console.WriteLine("agent id = {0}", sp.m_uuid); | ||
540 | 536 | ||
541 | m_log.InfoFormat("[BALANCER] "+"scene presence deserialized [{0}]", sp.m_uuid); | 537 | scene.m_restorePresences.Add(sp.m_uuid, sp); |
542 | } | 538 | File.Delete(filename); |
539 | |||
540 | m_log.InfoFormat("[BALANCER] "+"scene presence deserialized [{0}]", sp.m_uuid); | ||
541 | } | ||
543 | */ | 542 | */ |
544 | for (int i = 0; ; i++) | 543 | for (int i = 0; ; i++) |
545 | { | 544 | { |
@@ -576,18 +575,18 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
576 | 575 | ||
577 | udpserv.RestoreClient(agentdata, data.userEP, data.proxyEP); | 576 | udpserv.RestoreClient(agentdata, data.userEP, data.proxyEP); |
578 | 577 | ||
579 | // waiting for the scene-presense restored | 578 | // waiting for the scene-presense restored |
580 | lock (scene.m_restorePresences) | 579 | lock (scene.m_restorePresences) |
581 | { | 580 | { |
582 | Monitor.Wait(scene.m_restorePresences, 3000); | 581 | Monitor.Wait(scene.m_restorePresences, 3000); |
583 | } | 582 | } |
584 | 583 | ||
585 | if (scene.ClientManager.TryGetClient(circuit_code, out controller)) | 584 | if (scene.ClientManager.TryGetClient(circuit_code, out controller)) |
586 | { | 585 | { |
587 | m_log.InfoFormat("[BALANCER] " + "get client [{0}]", circuit_code); | 586 | m_log.InfoFormat("[BALANCER] " + "get client [{0}]", circuit_code); |
588 | controller.SetClientInfo(data); | 587 | controller.SetClientInfo(data); |
589 | } | 588 | } |
590 | 589 | ||
591 | File.Delete(fname); | 590 | File.Delete(fname); |
592 | 591 | ||
593 | m_log.InfoFormat("[BALANCER] " + "client info deserialized [{0}]", circuit_code); | 592 | m_log.InfoFormat("[BALANCER] " + "client info deserialized [{0}]", circuit_code); |
@@ -622,7 +621,7 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
622 | Console.WriteLine("scene found."); | 621 | Console.WriteLine("scene found."); |
623 | 622 | ||
624 | if ((sceneManager.CurrentScene != null) | 623 | if ((sceneManager.CurrentScene != null) |
625 | && (sceneManager.CurrentScene.RegionInfo.RegionID == killScene.RegionInfo.RegionID)) | 624 | && (sceneManager.CurrentScene.RegionInfo.RegionID == killScene.RegionInfo.RegionID)) |
626 | { | 625 | { |
627 | sceneManager.TrySetCurrentScene(".."); | 626 | sceneManager.TrySetCurrentScene(".."); |
628 | } | 627 | } |
@@ -671,10 +670,10 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
671 | { | 670 | { |
672 | // stopping clientview thread | 671 | // stopping clientview thread |
673 | if (((ClientView)controller).PacketProcessingEnabled) | 672 | if (((ClientView)controller).PacketProcessingEnabled) |
674 | { | 673 | { |
675 | controller.Stop(); | 674 | controller.Stop(); |
676 | ((ClientView)controller).PacketProcessingEnabled = false; | 675 | ((ClientView)controller).PacketProcessingEnabled = false; |
677 | } | 676 | } |
678 | // teminateing clientview thread | 677 | // teminateing clientview thread |
679 | controller.Terminate(); | 678 | controller.Terminate(); |
680 | m_log.Info("[BALANCER] "+"client thread stopped"); | 679 | m_log.Info("[BALANCER] "+"client thread stopped"); |
@@ -698,90 +697,90 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer | |||
698 | 697 | ||
699 | private XmlRpcResponse SplitRegion(XmlRpcRequest request) | 698 | private XmlRpcResponse SplitRegion(XmlRpcRequest request) |
700 | { | 699 | { |
701 | try | 700 | try |
702 | { | 701 | { |
703 | int myID = (int) request.Params[0]; | 702 | int myID = (int) request.Params[0]; |
704 | int numRegions = (int) request.Params[1]; | 703 | int numRegions = (int) request.Params[1]; |
705 | regionPortList = new int[numRegions]; | 704 | regionPortList = new int[numRegions]; |
706 | sceneURL = new string[numRegions]; | 705 | sceneURL = new string[numRegions]; |
707 | tcpClientList = new TcpClient[numRegions]; | 706 | tcpClientList = new TcpClient[numRegions]; |
708 | 707 | ||
709 | for(int i=0; i<numRegions; i++) | 708 | for(int i=0; i<numRegions; i++) |
710 | { | 709 | { |
711 | regionPortList[i]=(int) request.Params[i+2]; | 710 | regionPortList[i]=(int) request.Params[i+2]; |
712 | sceneURL[i]=(string) request.Params[i+2+numRegions]; | 711 | sceneURL[i]=(string) request.Params[i+2+numRegions]; |
713 | } | 712 | } |
714 | 713 | ||
715 | string hostname; | 714 | string hostname; |
716 | 715 | ||
717 | for(int i=0; i<numRegions; i++) | 716 | for(int i=0; i<numRegions; i++) |
718 | { | 717 | { |
719 | hostname = sceneURL[i].Split(new char[] { '/', ':' })[3]; | 718 | hostname = sceneURL[i].Split(new char[] { '/', ':' })[3]; |
720 | m_log.InfoFormat("[SPLITSCENE] "+"creating tcp client host:{0}", hostname); | 719 | m_log.InfoFormat("[SPLITSCENE] "+"creating tcp client host:{0}", hostname); |
721 | tcpClientList[i] = new TcpClient(hostname, 10001); | 720 | tcpClientList[i] = new TcpClient(hostname, 10001); |
722 | } | 721 | } |
723 | 722 | ||
724 | bool isMaster = (myID == 0); | 723 | bool isMaster = (myID == 0); |
725 | 724 | ||
726 | isLocalNeighbour = new bool[numRegions]; | 725 | isLocalNeighbour = new bool[numRegions]; |
727 | for(int i=0; i<numRegions; i++) isLocalNeighbour[i] = (sceneURL[i] == sceneURL[myID]); | 726 | for(int i=0; i<numRegions; i++) isLocalNeighbour[i] = (sceneURL[i] == sceneURL[myID]); |
728 | 727 | ||
729 | RegionInfo region = SearchRegionFromPortNum(regionPortList[myID]); | 728 | RegionInfo region = SearchRegionFromPortNum(regionPortList[myID]); |
730 | 729 | ||
731 | //Console.WriteLine("\n === SplitRegion {0}\n", region.RegionID); | 730 | //Console.WriteLine("\n === SplitRegion {0}\n", region.RegionID); |
732 | 731 | ||
733 | Scene scene; | 732 | Scene scene; |
734 | if (sceneManager.TryGetScene(region.RegionID, out scene)) | 733 | if (sceneManager.TryGetScene(region.RegionID, out scene)) |
735 | { | 734 | { |
736 | // Disable event updates, backups etc in the slave(s) | 735 | // Disable event updates, backups etc in the slave(s) |
737 | if (isMaster) { | 736 | if (isMaster) { |
738 | scene.Region_Status = RegionStatus.Up; | 737 | scene.Region_Status = RegionStatus.Up; |
739 | } | 738 | } |
740 | else | 739 | else |
741 | { | 740 | { |
742 | scene.Region_Status = RegionStatus.SlaveScene; | 741 | scene.Region_Status = RegionStatus.SlaveScene; |
743 | } | 742 | } |
744 | |||
745 | //Console.WriteLine("=== SplitRegion {0}: Scene found, status {1}", region.RegionID, scene.Region_Status); | ||
746 | 743 | ||
747 | // Disabling half of the avatars in master, and the other half in slave | 744 | //Console.WriteLine("=== SplitRegion {0}: Scene found, status {1}", region.RegionID, scene.Region_Status); |
748 | 745 | ||
749 | int i = 0; | 746 | // Disabling half of the avatars in master, and the other half in slave |
750 | List<ScenePresence> presences = scene.GetScenePresences(); | ||
751 | presences.Sort(); | ||
752 | foreach (ScenePresence pre in presences) | ||
753 | { | ||
754 | // Divide the presences evenly over the set of subscenes | ||
755 | ClientView client = (ClientView) pre.ControllingClient; | ||
756 | client.PacketProcessingEnabled = (( (i + myID) % sceneURL.Length) == 0); | ||
757 | 747 | ||
758 | m_log.InfoFormat("[SPLITSCENE] === SplitRegion {0}: SP.PacketEnabled {1}", region.RegionID, client.PacketProcessingEnabled); | 748 | int i = 0; |
749 | List<ScenePresence> presences = scene.GetScenePresences(); | ||
750 | presences.Sort(); | ||
751 | foreach (ScenePresence pre in presences) | ||
752 | { | ||
753 | // Divide the presences evenly over the set of subscenes | ||
754 | ClientView client = (ClientView) pre.ControllingClient; | ||
755 | client.PacketProcessingEnabled = (( (i + myID) % sceneURL.Length) == 0); | ||
759 | 756 | ||
760 | if (!client.PacketProcessingEnabled) | 757 | m_log.InfoFormat("[SPLITSCENE] === SplitRegion {0}: SP.PacketEnabled {1}", region.RegionID, client.PacketProcessingEnabled); |
761 | { | ||
762 | // stopping clientview thread | ||
763 | client.Stop(); | ||
764 | } | ||
765 | 758 | ||
766 | ++i; | 759 | if (!client.PacketProcessingEnabled) |
767 | } | 760 | { |
761 | // stopping clientview thread | ||
762 | client.Stop(); | ||
763 | } | ||
768 | 764 | ||
769 | scene.splitID = myID; | 765 | ++i; |
770 | scene.SynchronizeScene = new Scene.SynchronizeSceneHandler(SynchronizeScenes); | ||
771 | isSplit = true; | ||
772 | } | 766 | } |
773 | else | ||
774 | { | ||
775 | m_log.Error("[SPLITSCENE] "+String.Format("Scene not found {0}", region.RegionID)); | ||
776 | } | ||
777 | } | ||
778 | catch (Exception e) | ||
779 | { | ||
780 | m_log.Error("[SPLITSCENE] "+e.ToString()); | ||
781 | m_log.Error("[SPLITSCENE] "+e.StackTrace); | ||
782 | } | ||
783 | 767 | ||
784 | return new XmlRpcResponse(); | 768 | scene.splitID = myID; |
769 | scene.SynchronizeScene = new Scene.SynchronizeSceneHandler(SynchronizeScenes); | ||
770 | isSplit = true; | ||
771 | } | ||
772 | else | ||
773 | { | ||
774 | m_log.Error("[SPLITSCENE] "+String.Format("Scene not found {0}", region.RegionID)); | ||
775 | } | ||
776 | } | ||
777 | catch (Exception e) | ||
778 | { | ||
779 | m_log.Error("[SPLITSCENE] "+e.ToString()); | ||
780 | m_log.Error("[SPLITSCENE] "+e.StackTrace); | ||
781 | } | ||
782 | |||
783 | return new XmlRpcResponse(); | ||
785 | } | 784 | } |
786 | 785 | ||
787 | private XmlRpcResponse MergeRegions(XmlRpcRequest request) | 786 | private XmlRpcResponse MergeRegions(XmlRpcRequest request) |
@@ -794,40 +793,40 @@ presences.Sort(); | |||
794 | string src_url = (string) request.Params[0]; | 793 | string src_url = (string) request.Params[0]; |
795 | int src_port = (int) request.Params[1]; | 794 | int src_port = (int) request.Params[1]; |
796 | 795 | ||
797 | RegionInfo region = SearchRegionFromPortNum(src_port); | 796 | RegionInfo region = SearchRegionFromPortNum(src_port); |
798 | 797 | ||
799 | simMain.ProxyCommand(region.proxyUrl, "BlockClientMessages", src_url, src_port + proxyOffset); | 798 | simMain.ProxyCommand(region.proxyUrl, "BlockClientMessages", src_url, src_port + proxyOffset); |
800 | 799 | ||
801 | Scene scene; | 800 | Scene scene; |
802 | if (sceneManager.TryGetScene(region.RegionID, out scene)) | 801 | if (sceneManager.TryGetScene(region.RegionID, out scene)) |
803 | { | 802 | { |
804 | isSplit = false; | 803 | isSplit = false; |
805 | 804 | ||
806 | scene.SynchronizeScene = null; | 805 | scene.SynchronizeScene = null; |
807 | scene.Region_Status = RegionStatus.Up; | 806 | scene.Region_Status = RegionStatus.Up; |
808 | 807 | ||
809 | List<ScenePresence> presences = scene.GetScenePresences(); | 808 | List<ScenePresence> presences = scene.GetScenePresences(); |
810 | foreach (ScenePresence pre in presences) | 809 | foreach (ScenePresence pre in presences) |
811 | { | 810 | { |
812 | ClientView client = (ClientView) pre.ControllingClient; | 811 | ClientView client = (ClientView) pre.ControllingClient; |
813 | if (!client.PacketProcessingEnabled) | 812 | if (!client.PacketProcessingEnabled) |
814 | { | 813 | { |
815 | client.Restart(); | 814 | client.Restart(); |
816 | client.PacketProcessingEnabled = true; | 815 | client.PacketProcessingEnabled = true; |
817 | } | 816 | } |
818 | } | 817 | } |
819 | } | 818 | } |
820 | 819 | ||
821 | // Delete the slave scenes | 820 | // Delete the slave scenes |
822 | for(int i=1; i<sceneURL.Length; i++) | 821 | for(int i=1; i<sceneURL.Length; i++) |
823 | { | 822 | { |
824 | string url = (sceneURL[i].Split('/')[2]).Split(':')[0]; // get URL part from EP | 823 | string url = (sceneURL[i].Split('/')[2]).Split(':')[0]; // get URL part from EP |
825 | simMain.ProxyCommand(region.proxyUrl, "DeleteRegion", regionPortList[i] + proxyOffset, url); | 824 | simMain.ProxyCommand(region.proxyUrl, "DeleteRegion", regionPortList[i] + proxyOffset, url); |
826 | Thread.Sleep(1000); | 825 | Thread.Sleep(1000); |
827 | simMain.XmlRpcCommand(sceneURL[i], "TerminateRegion", regionPortList[i]); // TODO: need + proxyOffset? | 826 | simMain.XmlRpcCommand(sceneURL[i], "TerminateRegion", regionPortList[i]); // TODO: need + proxyOffset? |
828 | } | 827 | } |
829 | 828 | ||
830 | simMain.ProxyCommand(region.proxyUrl, "UnblockClientMessages", src_url, src_port + proxyOffset); | 829 | simMain.ProxyCommand(region.proxyUrl, "UnblockClientMessages", src_url, src_port + proxyOffset); |
831 | } | 830 | } |
832 | catch (Exception e) | 831 | catch (Exception e) |
833 | { | 832 | { |
@@ -835,7 +834,7 @@ presences.Sort(); | |||
835 | m_log.Error("[BALANCER] "+e.StackTrace); | 834 | m_log.Error("[BALANCER] "+e.StackTrace); |
836 | throw e; | 835 | throw e; |
837 | } | 836 | } |
838 | 837 | ||
839 | return new XmlRpcResponse(); | 838 | return new XmlRpcResponse(); |
840 | } | 839 | } |
841 | 840 | ||
@@ -870,11 +869,11 @@ presences.Sort(); | |||
870 | { | 869 | { |
871 | ScenePresence pre = scene.GetScenePresences().Find(delegate(ScenePresence x) { return x.UUID == scenePresenceID; }); | 870 | ScenePresence pre = scene.GetScenePresences().Find(delegate(ScenePresence x) { return x.UUID == scenePresenceID; }); |
872 | 871 | ||
873 | if (pre == null) | 872 | if (pre == null) |
874 | { | 873 | { |
875 | m_log.ErrorFormat("[SPLITSCENE] [LocalUpdatePhysics] ScenePresence is missing... ({0})", scenePresenceID.ToString()); | 874 | m_log.ErrorFormat("[SPLITSCENE] [LocalUpdatePhysics] ScenePresence is missing... ({0})", scenePresenceID.ToString()); |
876 | return; | 875 | return; |
877 | } | 876 | } |
878 | 877 | ||
879 | // m_log.Info("[SPLITSCENE] "+"LocalUpdatePhysics [region:{0}, client:{1}]", | 878 | // m_log.Info("[SPLITSCENE] "+"LocalUpdatePhysics [region:{0}, client:{1}]", |
880 | // regionID.ToString(), pre.UUID.ToString()); | 879 | // regionID.ToString(), pre.UUID.ToString()); |
@@ -905,56 +904,56 @@ presences.Sort(); | |||
905 | // Because data changes by the physics simulation when the client doesn't move, | 904 | // Because data changes by the physics simulation when the client doesn't move, |
906 | // if MovementFlag is false, It is necessary to synchronize. | 905 | // if MovementFlag is false, It is necessary to synchronize. |
907 | //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) | 906 | //if(pre.MovementFlag!=0 && client.PacketProcessingEnabled==true) |
908 | if(client.PacketProcessingEnabled==true) | 907 | if(client.PacketProcessingEnabled==true) |
909 | { | 908 | { |
910 | //m_log.Info("[SPLITSCENE] "+String.Format("Client moving in {0} {1}", scene.RegionInfo.RegionID, pre.AbsolutePosition)); | 909 | //m_log.Info("[SPLITSCENE] "+String.Format("Client moving in {0} {1}", scene.RegionInfo.RegionID, pre.AbsolutePosition)); |
911 | 910 | ||
912 | for (int i = 0; i < sceneURL.Length; i++) | 911 | for (int i = 0; i < sceneURL.Length; i++) |
913 | { | 912 | { |
914 | if (i == scene.splitID) | 913 | if (i == scene.splitID) |
915 | { | 914 | { |
916 | continue; | 915 | continue; |
917 | } | 916 | } |
918 | 917 | ||
919 | if(isLocalNeighbour[i]) | 918 | if(isLocalNeighbour[i]) |
920 | { | 919 | { |
921 | //m_log.Info("[SPLITSCENE] "+"Synchronize ScenePresence (Local) [region:{0}=>{1}, client:{2}]", | 920 | //m_log.Info("[SPLITSCENE] "+"Synchronize ScenePresence (Local) [region:{0}=>{1}, client:{2}]", |
922 | // scene.RegionInfo.RegionID, regionPortList[i], pre.UUID.ToString()); | 921 | // scene.RegionInfo.RegionID, regionPortList[i], pre.UUID.ToString()); |
923 | LocalUpdatePhysics(regionPortList[i], pre.UUID, pre.AbsolutePosition, pre.Velocity, pre.PhysicsActor.Flying); | 922 | LocalUpdatePhysics(regionPortList[i], pre.UUID, pre.AbsolutePosition, pre.Velocity, pre.PhysicsActor.Flying); |
924 | } | 923 | } |
925 | else | 924 | else |
926 | { | 925 | { |
927 | //m_log.Info("[SPLITSCENE] "+"Synchronize ScenePresence (Remote) [region port:{0}, client:{1}, position:{2}, velocity:{3}, flying:{4}]", | 926 | //m_log.Info("[SPLITSCENE] "+"Synchronize ScenePresence (Remote) [region port:{0}, client:{1}, position:{2}, velocity:{3}, flying:{4}]", |
928 | // regionPortList[i], pre.UUID.ToString(), pre.AbsolutePosition.ToString(), | 927 | // regionPortList[i], pre.UUID.ToString(), pre.AbsolutePosition.ToString(), |
929 | // pre.Velocity.ToString(), pre.PhysicsActor.Flying); | 928 | // pre.Velocity.ToString(), pre.PhysicsActor.Flying); |
930 | 929 | ||
931 | 930 | ||
932 | simMain.XmlRpcCommand(sceneURL[i], "UpdatePhysics", | 931 | simMain.XmlRpcCommand(sceneURL[i], "UpdatePhysics", |
933 | regionPortList[i], pre.UUID.GetBytes(), | 932 | regionPortList[i], pre.UUID.GetBytes(), |
934 | pre.AbsolutePosition.GetBytes(), pre.Velocity.GetBytes(), | 933 | pre.AbsolutePosition.GetBytes(), pre.Velocity.GetBytes(), |
935 | pre.PhysicsActor.Flying); | 934 | pre.PhysicsActor.Flying); |
936 | 935 | ||
937 | /* | 936 | /* |
938 | byte[] buff = new byte[12+12+1]; | 937 | byte[] buff = new byte[12+12+1]; |
939 | 938 | ||
940 | Buffer.BlockCopy(pre.AbsolutePosition.GetBytes(), 0, buff, 0, 12); | 939 | Buffer.BlockCopy(pre.AbsolutePosition.GetBytes(), 0, buff, 0, 12); |
941 | Buffer.BlockCopy(pre.Velocity.GetBytes(), 0, buff, 12, 12); | 940 | Buffer.BlockCopy(pre.Velocity.GetBytes(), 0, buff, 12, 12); |
942 | buff[24] = (byte)((pre.PhysicsActor.Flying)?1:0); | 941 | buff[24] = (byte)((pre.PhysicsActor.Flying)?1:0); |
943 | 942 | ||
944 | // create header | 943 | // create header |
945 | InternalPacketHeader header = new InternalPacketHeader(); | 944 | InternalPacketHeader header = new InternalPacketHeader(); |
946 | 945 | ||
947 | header.type = 1; | 946 | header.type = 1; |
948 | header.throttlePacketType = 0; | 947 | header.throttlePacketType = 0; |
949 | header.numbytes = buff.Length; | 948 | header.numbytes = buff.Length; |
950 | header.agent_id = pre.UUID.UUID; | 949 | header.agent_id = pre.UUID.UUID; |
951 | header.region_port = regionPortList[i]; | 950 | header.region_port = regionPortList[i]; |
952 | 951 | ||
953 | //Send | 952 | //Send |
954 | tcpClientList[i].send(header, buff); | 953 | tcpClientList[i].send(header, buff); |
955 | */ | 954 | */ |
956 | } | 955 | } |
957 | } | 956 | } |
958 | } | 957 | } |
959 | // ++i; | 958 | // ++i; |
960 | } | 959 | } |
@@ -968,41 +967,41 @@ presences.Sort(); | |||
968 | return false; | 967 | return false; |
969 | } | 968 | } |
970 | 969 | ||
971 | Scene localScene = (Scene)scene; | 970 | Scene localScene = (Scene)scene; |
972 | 971 | ||
973 | for (int i = 0; i < sceneURL.Length; i++) | 972 | for (int i = 0; i < sceneURL.Length; i++) |
974 | { | 973 | { |
975 | if (i == localScene.splitID) | 974 | if (i == localScene.splitID) |
976 | { | ||
977 | continue; | ||
978 | } | ||
979 | |||
980 | if(isLocalNeighbour[i]) | ||
981 | { | 975 | { |
982 | //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Local) [type:{0}, client:{1}]", | 976 | continue; |
983 | // packet.Type.ToString(), agentID.ToString()); | ||
984 | LocalUpdatePacket(regionPortList[i], agentID, packet, throttlePacketType); | ||
985 | } | 977 | } |
986 | else | 978 | |
979 | if(isLocalNeighbour[i]) | ||
987 | { | 980 | { |
988 | //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Remote) [type:{0}, client:{1}]", | 981 | //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Local) [type:{0}, client:{1}]", |
989 | // packet.Type.ToString(), agentID.ToString()); | 982 | // packet.Type.ToString(), agentID.ToString()); |
990 | // to bytes | 983 | LocalUpdatePacket(regionPortList[i], agentID, packet, throttlePacketType); |
991 | byte[] buff = packet.ToBytes(); | 984 | } |
985 | else | ||
986 | { | ||
987 | //m_log.Info("[SPLITSCENE] "+"Synchronize Packet (Remote) [type:{0}, client:{1}]", | ||
988 | // packet.Type.ToString(), agentID.ToString()); | ||
989 | // to bytes | ||
990 | byte[] buff = packet.ToBytes(); | ||
992 | 991 | ||
993 | // create header | 992 | // create header |
994 | InternalPacketHeader header = new InternalPacketHeader(); | 993 | InternalPacketHeader header = new InternalPacketHeader(); |
995 | 994 | ||
996 | header.type = 0; | 995 | header.type = 0; |
997 | header.throttlePacketType = (int)throttlePacketType; | 996 | header.throttlePacketType = (int)throttlePacketType; |
998 | header.numbytes = buff.Length; | 997 | header.numbytes = buff.Length; |
999 | header.agent_id = agentID.UUID; | 998 | header.agent_id = agentID.UUID; |
1000 | header.region_port = regionPortList[i]; | 999 | header.region_port = regionPortList[i]; |
1001 | 1000 | ||
1002 | //Send | 1001 | //Send |
1003 | tcpClientList[i].send(header, buff); | 1002 | tcpClientList[i].send(header, buff); |
1004 | 1003 | ||
1005 | PacketPool.Instance.ReturnPacket(packet); | 1004 | PacketPool.Instance.ReturnPacket(packet); |
1006 | } | 1005 | } |
1007 | } | 1006 | } |
1008 | 1007 | ||
@@ -1022,20 +1021,20 @@ presences.Sort(); | |||
1022 | { | 1021 | { |
1023 | ScenePresence pre = scene.GetScenePresences().Find(delegate(ScenePresence x) { return x.UUID == agentID; }); | 1022 | ScenePresence pre = scene.GetScenePresences().Find(delegate(ScenePresence x) { return x.UUID == agentID; }); |
1024 | 1023 | ||
1025 | if (pre == null) | 1024 | if (pre == null) |
1026 | { | 1025 | { |
1027 | m_log.ErrorFormat("[SPLITSCENE] [LocalUpdatePacket] ScenePresence is missing... ({0})", agentID.ToString()); | 1026 | m_log.ErrorFormat("[SPLITSCENE] [LocalUpdatePacket] ScenePresence is missing... ({0})", agentID.ToString()); |
1028 | return; | 1027 | return; |
1029 | } | 1028 | } |
1030 | 1029 | ||
1031 | if (((ClientView)pre.ControllingClient).PacketProcessingEnabled==true) | 1030 | if (((ClientView)pre.ControllingClient).PacketProcessingEnabled==true) |
1032 | { | 1031 | { |
1033 | pre.ControllingClient.OutPacket(packet, throttlePacketType); | 1032 | pre.ControllingClient.OutPacket(packet, throttlePacketType); |
1034 | } | 1033 | } |
1035 | else | 1034 | else |
1036 | { | 1035 | { |
1037 | PacketPool.Instance.ReturnPacket(packet); | 1036 | PacketPool.Instance.ReturnPacket(packet); |
1038 | } | 1037 | } |
1039 | } | 1038 | } |
1040 | } | 1039 | } |
1041 | 1040 | ||
@@ -1061,22 +1060,22 @@ presences.Sort(); | |||
1061 | // packetEnd = buff.Length; | 1060 | // packetEnd = buff.Length; |
1062 | 1061 | ||
1063 | try | 1062 | try |
1064 | { | 1063 | { |
1065 | //m_log.Info("[SPLITSCENE] "+"PacketPool.Instance : {0}", (PacketPool.Instance == null)?"null":"not null"); | 1064 | //m_log.Info("[SPLITSCENE] "+"PacketPool.Instance : {0}", (PacketPool.Instance == null)?"null":"not null"); |
1066 | //m_log.Info("[SPLITSCENE] "+"buff length={0}", buff.Length); | 1065 | //m_log.Info("[SPLITSCENE] "+"buff length={0}", buff.Length); |
1067 | 1066 | ||
1068 | packet = PacketPool.Instance.GetPacket(buff, ref packetEnd, zero); | 1067 | packet = PacketPool.Instance.GetPacket(buff, ref packetEnd, zero); |
1069 | 1068 | ||
1070 | LocalUpdatePacket(header.region_port, new LLUUID(header.agent_id), | 1069 | LocalUpdatePacket(header.region_port, new LLUUID(header.agent_id), |
1071 | packet, (ThrottleOutPacketType)header.throttlePacketType); | 1070 | packet, (ThrottleOutPacketType)header.throttlePacketType); |
1072 | } | 1071 | } |
1073 | catch (Exception e) | 1072 | catch (Exception e) |
1074 | { | 1073 | { |
1075 | m_log.Error("[SPLITSCENE] "+e.ToString()); | 1074 | m_log.Error("[SPLITSCENE] "+e.ToString()); |
1076 | m_log.Error("[SPLITSCENE] "+e.StackTrace); | 1075 | m_log.Error("[SPLITSCENE] "+e.StackTrace); |
1077 | } | 1076 | } |
1078 | 1077 | ||
1079 | break; | 1078 | break; |
1080 | 1079 | ||
1081 | case 1: | 1080 | case 1: |
1082 | 1081 | ||
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 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (c) Contributors, http://opensimulator.org/ | 2 | * Copyright (c) Contributors, http://opensimulator.org/ |
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | 3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. |
4 | * | 4 | * |
5 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: | 6 | * modification, are permitted provided that the following conditions are met: |
7 | * * Redistributions of source code must retain the above copyright | 7 | * * Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. | 8 | * notice, this list of conditions and the following disclaimer. |
9 | * * Redistributions in binary form must reproduce the above copyright | 9 | * * Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the | 10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. | 11 | * documentation and/or other materials provided with the distribution. |
12 | * * Neither the name of the OpenSim Project nor the | 12 | * * Neither the name of the OpenSim Project nor the |
13 | * names of its contributors may be used to endorse or promote products | 13 | * names of its contributors may be used to endorse or promote products |
14 | * derived from this software without specific prior written permission. | 14 | * derived from this software without specific prior written permission. |
15 | * | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | 16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY |
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | 19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * | 26 | */ |
27 | */ | ||
28 | 27 | ||
29 | using System; | 28 | using System; |
30 | using System.IO; | 29 | using System.IO; |
@@ -35,196 +34,197 @@ using System.Text; | |||
35 | using System.Runtime.Serialization.Formatters.Binary; | 34 | using System.Runtime.Serialization.Formatters.Binary; |
36 | 35 | ||
37 | namespace OpenSim.ApplicationPlugins.LoadBalancer { | 36 | namespace OpenSim.ApplicationPlugins.LoadBalancer { |
38 | public class AsynchronousClient { | 37 | public class AsynchronousClient { |
39 | private static ManualResetEvent connectDone = new ManualResetEvent(false); | 38 | private static ManualResetEvent connectDone = new ManualResetEvent(false); |
40 | private static ManualResetEvent sendDone = new ManualResetEvent(false); | 39 | private static ManualResetEvent sendDone = new ManualResetEvent(false); |
41 | private static ManualResetEvent receiveDone = new ManualResetEvent(false); | 40 | private static ManualResetEvent receiveDone = new ManualResetEvent(false); |
42 | private static String response = String.Empty; | 41 | private static String response = String.Empty; |
43 | 42 | ||
44 | public static Socket StartClient(string hostname, int port) { | 43 | public static Socket StartClient(string hostname, int port) { |
45 | try { | 44 | try { |
46 | IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname); | 45 | IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname); |
47 | IPAddress ipAddress = ipHostInfo.AddressList[0]; | 46 | IPAddress ipAddress = ipHostInfo.AddressList[0]; |
48 | IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); | 47 | IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); |
49 | 48 | ||
50 | Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | 49 | Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
51 | client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client); | 50 | client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client); |
52 | connectDone.WaitOne(); | 51 | connectDone.WaitOne(); |
53 | /* | 52 | /* |
54 | Send(client,"This is a test<EOF>"); | 53 | Send(client,"This is a test<EOF>"); |
55 | sendDone.WaitOne(); | 54 | sendDone.WaitOne(); |
56 | Receive(client); | 55 | Receive(client); |
57 | receiveDone.WaitOne(); | 56 | receiveDone.WaitOne(); |
58 | client.Shutdown(SocketShutdown.Both); | 57 | client.Shutdown(SocketShutdown.Both); |
59 | client.Close(); | 58 | client.Close(); |
60 | */ | 59 | */ |
61 | return client; | 60 | return client; |
62 | } catch (Exception e) { | 61 | } catch (Exception e) { |
63 | Console.WriteLine(e.ToString()); | 62 | Console.WriteLine(e.ToString()); |
64 | throw new Exception("socket error !!"); | 63 | throw new Exception("socket error !!"); |
65 | } | 64 | } |
66 | } | 65 | } |
67 | 66 | ||
68 | private static void ConnectCallback(IAsyncResult ar) { | 67 | private static void ConnectCallback(IAsyncResult ar) { |
69 | try { | 68 | try { |
70 | Socket client = (Socket) ar.AsyncState; | 69 | Socket client = (Socket) ar.AsyncState; |
71 | client.EndConnect(ar); | 70 | client.EndConnect(ar); |
72 | Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); | 71 | Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); |
73 | connectDone.Set(); | 72 | connectDone.Set(); |
74 | } catch (Exception e) { | 73 | } catch (Exception e) { |
75 | Console.WriteLine(e.ToString()); | 74 | Console.WriteLine(e.ToString()); |
76 | } | 75 | } |
77 | } | 76 | } |
78 | 77 | ||
79 | /* | 78 | /* |
80 | public static void Receive(Socket client) { | 79 | public static void Receive(Socket client) { |
81 | try { | 80 | try { |
82 | StateObject state = new StateObject(); | 81 | StateObject state = new StateObject(); |
83 | state.workSocket = client; | 82 | state.workSocket = client; |
84 | client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); | 83 | client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); |
85 | } catch (Exception e) { | 84 | } catch (Exception e) { |
86 | Console.WriteLine(e.ToString()); | 85 | Console.WriteLine(e.ToString()); |
87 | } | 86 | } |
88 | } | 87 | } |
89 | 88 | ||
90 | private static void ReceiveCallback( IAsyncResult ar ) { | 89 | private static void ReceiveCallback( IAsyncResult ar ) { |
91 | try { | 90 | try { |
92 | StateObject state = (StateObject) ar.AsyncState; | 91 | StateObject state = (StateObject) ar.AsyncState; |
93 | Socket client = state.workSocket; | 92 | Socket client = state.workSocket; |
94 | 93 | ||
95 | int bytesRead = client.EndReceive(ar); | 94 | int bytesRead = client.EndReceive(ar); |
96 | if (bytesRead > 0) { | 95 | if (bytesRead > 0) { |
97 | state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead)); | 96 | state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead)); |
98 | client.BeginReceive(state.buffer,0,StateObject.BufferSize,0, new AsyncCallback(ReceiveCallback), state); | 97 | client.BeginReceive(state.buffer,0,StateObject.BufferSize,0, new AsyncCallback(ReceiveCallback), state); |
99 | } else { | 98 | } else { |
100 | if (state.sb.Length > 1) { | 99 | if (state.sb.Length > 1) { |
101 | response = state.sb.ToString(); | 100 | response = state.sb.ToString(); |
102 | } | 101 | } |
103 | receiveDone.Set(); | 102 | receiveDone.Set(); |
104 | } | 103 | } |
105 | } catch (Exception e) { | 104 | } catch (Exception e) { |
106 | Console.WriteLine(e.ToString()); | 105 | Console.WriteLine(e.ToString()); |
107 | } | 106 | } |
108 | } | 107 | } |
109 | */ | 108 | */ |
110 | public static void Send(Socket client, byte[] byteData) { | 109 | public static void Send(Socket client, byte[] byteData) { |
111 | client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); | 110 | client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); |
112 | } | 111 | } |
113 | 112 | ||
114 | private static void SendCallback(IAsyncResult ar) { | 113 | private static void SendCallback(IAsyncResult ar) { |
115 | try { | 114 | try { |
116 | Socket client = (Socket) ar.AsyncState; | 115 | Socket client = (Socket) ar.AsyncState; |
117 | int bytesSent = client.EndSend(ar); | 116 | int bytesSent = client.EndSend(ar); |
118 | //Console.WriteLine("Sent {0} bytes to server.", bytesSent); | 117 | //Console.WriteLine("Sent {0} bytes to server.", bytesSent); |
119 | sendDone.Set(); | 118 | sendDone.Set(); |
120 | } catch (Exception e) { | 119 | } catch (Exception e) { |
121 | Console.WriteLine(e.ToString()); | 120 | Console.WriteLine(e.ToString()); |
122 | } | 121 | } |
123 | } | 122 | } |
124 | } | 123 | } |
125 | 124 | ||
126 | public class InternalPacketHeader | 125 | public class InternalPacketHeader |
127 | { | ||
128 | private byte[] buffer = new byte[32]; | ||
129 | public int type; | ||
130 | public int throttlePacketType; | ||
131 | public int numbytes; | ||
132 | public Guid agent_id; | ||
133 | public int region_port; | ||
134 | |||
135 | public void FromBytes(byte[] bytes) | ||
136 | { | 126 | { |
137 | int i = 0; // offset | 127 | private byte[] buffer = new byte[32]; |
138 | try | 128 | public int type; |
129 | public int throttlePacketType; | ||
130 | public int numbytes; | ||
131 | public Guid agent_id; | ||
132 | public int region_port; | ||
133 | |||
134 | public void FromBytes(byte[] bytes) | ||
139 | { | 135 | { |
140 | this.type = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); | 136 | int i = 0; // offset |
141 | this.throttlePacketType = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); | 137 | try |
142 | this.numbytes = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); | 138 | { |
143 | this.agent_id = new Guid( | 139 | this.type = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); |
144 | bytes[i++] | (bytes[i++] << 8) | (bytes[i++] << 16) | bytes[i++] << 24, | 140 | this.throttlePacketType = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); |
145 | (short)(bytes[i++] | (bytes[i++] << 8)), | 141 | this.numbytes = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); |
146 | (short)(bytes[i++] | (bytes[i++] << 8)), | 142 | this.agent_id = new Guid( |
147 | bytes[i++], bytes[i++], bytes[i++], bytes[i++], | 143 | bytes[i++] | (bytes[i++] << 8) | (bytes[i++] << 16) | bytes[i++] << 24, |
148 | bytes[i++], bytes[i++], bytes[i++], bytes[i++]); | 144 | (short)(bytes[i++] | (bytes[i++] << 8)), |
149 | this.region_port = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); | 145 | (short)(bytes[i++] | (bytes[i++] << 8)), |
146 | bytes[i++], bytes[i++], bytes[i++], bytes[i++], | ||
147 | bytes[i++], bytes[i++], bytes[i++], bytes[i++]); | ||
148 | this.region_port = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); | ||
149 | } | ||
150 | catch (Exception) | ||
151 | { | ||
152 | throw new Exception("bad format!!!"); | ||
153 | } | ||
150 | } | 154 | } |
151 | catch (Exception) | 155 | |
156 | public byte[] ToBytes() | ||
152 | { | 157 | { |
153 | throw new Exception("bad format!!!"); | 158 | int i = 0; |
159 | this.buffer[i++] = (byte)(this.type % 256); | ||
160 | this.buffer[i++] = (byte)((this.type >> 8) % 256); | ||
161 | this.buffer[i++] = (byte)((this.type >> 16) % 256); | ||
162 | this.buffer[i++] = (byte)((this.type >> 24) % 256); | ||
163 | |||
164 | this.buffer[i++] = (byte)(this.throttlePacketType % 256); | ||
165 | this.buffer[i++] = (byte)((this.throttlePacketType >> 8) % 256); | ||
166 | this.buffer[i++] = (byte)((this.throttlePacketType >> 16) % 256); | ||
167 | this.buffer[i++] = (byte)((this.throttlePacketType >> 24) % 256); | ||
168 | |||
169 | this.buffer[i++] = (byte)(this.numbytes % 256); | ||
170 | this.buffer[i++] = (byte)((this.numbytes >> 8) % 256); | ||
171 | this.buffer[i++] = (byte)((this.numbytes >> 16) % 256); | ||
172 | this.buffer[i++] = (byte)((this.numbytes >> 24) % 256); | ||
173 | |||
174 | // no endian care | ||
175 | Buffer.BlockCopy(agent_id.ToByteArray(), 0, this.buffer, i, 16); i += 16; | ||
176 | |||
177 | this.buffer[i++] = (byte)(this.region_port % 256); | ||
178 | this.buffer[i++] = (byte)((this.region_port >> 8) % 256); | ||
179 | this.buffer[i++] = (byte)((this.region_port >> 16) % 256); | ||
180 | this.buffer[i++] = (byte)((this.region_port >> 24) % 256); | ||
181 | |||
182 | return this.buffer; | ||
154 | } | 183 | } |
155 | } | 184 | } |
156 | 185 | ||
157 | public byte[] ToBytes() | 186 | public class TcpClient { |
158 | { | 187 | |
159 | int i = 0; | 188 | public static int internalPacketHeaderSize = 4*4 + 16*1; |
160 | this.buffer[i++] = (byte)(this.type % 256); | 189 | |
161 | this.buffer[i++] = (byte)((this.type >> 8) % 256); | 190 | private string mHostname; |
162 | this.buffer[i++] = (byte)((this.type >> 16) % 256); | 191 | private int mPort; |
163 | this.buffer[i++] = (byte)((this.type >> 24) % 256); | 192 | private Socket mConnection; |
164 | 193 | public TcpClient(string hostname, int port) { | |
165 | this.buffer[i++] = (byte)(this.throttlePacketType % 256); | 194 | this.mHostname = hostname; |
166 | this.buffer[i++] = (byte)((this.throttlePacketType >> 8) % 256); | 195 | this.mPort = port; |
167 | this.buffer[i++] = (byte)((this.throttlePacketType >> 16) % 256); | 196 | this.mConnection = null; |
168 | this.buffer[i++] = (byte)((this.throttlePacketType >> 24) % 256); | 197 | } |
169 | 198 | public void connect() { | |
170 | this.buffer[i++] = (byte)(this.numbytes % 256); | 199 | this.mConnection = AsynchronousClient.StartClient(mHostname, mPort); |
171 | this.buffer[i++] = (byte)((this.numbytes >> 8) % 256); | 200 | } |
172 | this.buffer[i++] = (byte)((this.numbytes >> 16) % 256); | ||
173 | this.buffer[i++] = (byte)((this.numbytes >> 24) % 256); | ||
174 | |||
175 | // no endian care | ||
176 | Buffer.BlockCopy(agent_id.ToByteArray(), 0, this.buffer, i, 16); i += 16; | ||
177 | |||
178 | this.buffer[i++] = (byte)(this.region_port % 256); | ||
179 | this.buffer[i++] = (byte)((this.region_port >> 8) % 256); | ||
180 | this.buffer[i++] = (byte)((this.region_port >> 16) % 256); | ||
181 | this.buffer[i++] = (byte)((this.region_port >> 24) % 256); | ||
182 | |||
183 | return this.buffer; | ||
184 | } | ||
185 | } | ||
186 | public class TcpClient { | ||
187 | |||
188 | public static int internalPacketHeaderSize = 4*4 + 16*1; | ||
189 | |||
190 | private string mHostname; | ||
191 | private int mPort; | ||
192 | private Socket mConnection; | ||
193 | public TcpClient(string hostname, int port) { | ||
194 | this.mHostname = hostname; | ||
195 | this.mPort = port; | ||
196 | this.mConnection = null; | ||
197 | } | ||
198 | public void connect() { | ||
199 | this.mConnection = AsynchronousClient.StartClient(mHostname, mPort); | ||
200 | } | ||
201 | /* | 201 | /* |
202 | public void recevie() { | 202 | public void recevie() { |
203 | if (mConnection == null) { | 203 | if (mConnection == null) { |
204 | throw new Exception("client not initialized"); | 204 | throw new Exception("client not initialized"); |
205 | } | 205 | } |
206 | try | 206 | try |
207 | { | 207 | { |
208 | AsynchronousClient.Receive(this.mConnection); | 208 | AsynchronousClient.Receive(this.mConnection); |
209 | } | 209 | } |
210 | catch (Exception e) | 210 | catch (Exception e) |
211 | { | 211 | { |
212 | Console.WriteLine(e.ToString()); | 212 | Console.WriteLine(e.ToString()); |
213 | mConnection = null; | 213 | mConnection = null; |
214 | } | 214 | } |
215 | } | 215 | } |
216 | */ | 216 | */ |
217 | public void send(InternalPacketHeader header, byte[] packet) { | 217 | public void send(InternalPacketHeader header, byte[] packet) { |
218 | 218 | ||
219 | lock (this) | 219 | lock (this) |
220 | { | 220 | { |
221 | 221 | ||
222 | if (mConnection == null) { | 222 | if (mConnection == null) { |
223 | // throw new Exception("client not initialized"); | 223 | // throw new Exception("client not initialized"); |
224 | connect(); | 224 | connect(); |
225 | } | 225 | } |
226 | 226 | ||
227 | AsynchronousClient.Send(this.mConnection, header.ToBytes()); | 227 | AsynchronousClient.Send(this.mConnection, header.ToBytes()); |
228 | 228 | ||
229 | /* | 229 | /* |
230 | for (int i = 0; i < 10; i++) | 230 | for (int i = 0; i < 10; i++) |
@@ -233,8 +233,8 @@ for (int i = 0; i < 10; i++) | |||
233 | } | 233 | } |
234 | Console.WriteLine(""); | 234 | Console.WriteLine(""); |
235 | */ | 235 | */ |
236 | AsynchronousClient.Send(this.mConnection, packet); | 236 | AsynchronousClient.Send(this.mConnection, packet); |
237 | } | 237 | } |
238 | } | 238 | } |
239 | } | 239 | } |
240 | } | 240 | } |
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 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (c) Contributors, http://opensimulator.org/ | 2 | * Copyright (c) Contributors, http://opensimulator.org/ |
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | 3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. |
4 | * | 4 | * |
5 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: | 6 | * modification, are permitted provided that the following conditions are met: |
7 | * * Redistributions of source code must retain the above copyright | 7 | * * Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. | 8 | * notice, this list of conditions and the following disclaimer. |
9 | * * Redistributions in binary form must reproduce the above copyright | 9 | * * Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the | 10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. | 11 | * documentation and/or other materials provided with the distribution. |
12 | * * Neither the name of the OpenSim Project nor the | 12 | * * Neither the name of the OpenSim Project nor the |
13 | * names of its contributors may be used to endorse or promote products | 13 | * names of its contributors may be used to endorse or promote products |
14 | * derived from this software without specific prior written permission. | 14 | * derived from this software without specific prior written permission. |
15 | * | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | 16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY |
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | 19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * | 26 | */ |
27 | */ | ||
28 | 27 | ||
29 | using System; | 28 | using System; |
30 | using System.IO; | 29 | using System.IO; |
@@ -38,182 +37,180 @@ using OpenSim.Framework.Console; | |||
38 | 37 | ||
39 | namespace OpenSim.ApplicationPlugins.LoadBalancer { | 38 | namespace OpenSim.ApplicationPlugins.LoadBalancer { |
40 | 39 | ||
41 | public class StateObject { | 40 | public class StateObject { |
42 | public Socket workSocket = null; | 41 | public Socket workSocket = null; |
43 | public const int BufferSize = 2048; | 42 | public const int BufferSize = 2048; |
44 | public byte[] buffer = new byte[BufferSize]; | 43 | public byte[] buffer = new byte[BufferSize]; |
45 | public MemoryStream ms_ptr = new MemoryStream(); | 44 | public MemoryStream ms_ptr = new MemoryStream(); |
46 | public InternalPacketHeader header = null; | 45 | public InternalPacketHeader header = null; |
47 | } | 46 | } |
48 | 47 | ||
49 | public class AsynchronousSocketListener { | 48 | public class AsynchronousSocketListener { |
50 | public static string data = null; | 49 | public static string data = null; |
51 | public static ManualResetEvent allDone = new ManualResetEvent(false); | 50 | public static ManualResetEvent allDone = new ManualResetEvent(false); |
52 | 51 | ||
53 | #region KIRYU | 52 | #region KIRYU |
54 | public delegate void PacketRecieveHandler(InternalPacketHeader header, byte[] buff); | 53 | public delegate void PacketRecieveHandler(InternalPacketHeader header, byte[] buff); |
55 | public static PacketRecieveHandler PacketHandler = null; | 54 | public static PacketRecieveHandler PacketHandler = null; |
56 | #endregion | 55 | #endregion |
57 | 56 | ||
58 | public AsynchronousSocketListener() { } | 57 | public AsynchronousSocketListener() { } |
59 | 58 | ||
60 | public static void StartListening(int port) { | 59 | public static void StartListening(int port) { |
61 | IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); | 60 | IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); |
62 | IPAddress ipAddress = ipHostInfo.AddressList[0]; | 61 | IPAddress ipAddress = ipHostInfo.AddressList[0]; |
63 | IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); | 62 | IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); |
64 | 63 | ||
65 | Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); | 64 | Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); |
66 | try { | 65 | try { |
67 | listener.Bind(localEndPoint); | 66 | listener.Bind(localEndPoint); |
68 | listener.Listen(100); | 67 | listener.Listen(100); |
69 | while (true) { | 68 | while (true) { |
70 | allDone.Reset(); | 69 | allDone.Reset(); |
71 | listener.BeginAccept( new AsyncCallback(AcceptCallback), listener ); | 70 | listener.BeginAccept( new AsyncCallback(AcceptCallback), listener ); |
72 | allDone.WaitOne(); | 71 | allDone.WaitOne(); |
73 | } | 72 | } |
74 | } catch (Exception e) { | 73 | } catch (Exception e) { |
75 | Console.WriteLine(e.ToString()); | 74 | Console.WriteLine(e.ToString()); |
76 | } | 75 | } |
77 | /* | 76 | /* |
78 | Console.WriteLine("\nPress ENTER to continue..."); | 77 | Console.WriteLine("\nPress ENTER to continue..."); |
79 | Console.Read(); | 78 | Console.Read(); |
80 | */ | 79 | */ |
81 | } | 80 | } |
82 | 81 | ||
83 | public static void AcceptCallback(IAsyncResult ar) { | 82 | public static void AcceptCallback(IAsyncResult ar) { |
84 | allDone.Set(); | 83 | allDone.Set(); |
85 | Socket listener = (Socket) ar.AsyncState; | 84 | Socket listener = (Socket) ar.AsyncState; |
86 | Socket handler = listener.EndAccept(ar); | 85 | Socket handler = listener.EndAccept(ar); |
87 | StateObject state = new StateObject(); | 86 | StateObject state = new StateObject(); |
88 | state.workSocket = handler; | 87 | state.workSocket = handler; |
89 | handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); | 88 | handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); |
90 | } | 89 | } |
91 | 90 | ||
92 | public static void ReadCallback(IAsyncResult ar) { | 91 | public static void ReadCallback(IAsyncResult ar) { |
93 | String content = String.Empty; | 92 | String content = String.Empty; |
94 | StateObject state = (StateObject) ar.AsyncState; | 93 | StateObject state = (StateObject) ar.AsyncState; |
95 | Socket handler = state.workSocket; | 94 | Socket handler = state.workSocket; |
96 | 95 | ||
97 | try | 96 | try |
98 | { | 97 | { |
99 | 98 | int bytesRead = handler.EndReceive(ar); | |
100 | int bytesRead = handler.EndReceive(ar); | 99 | |
101 | 100 | //MainLog.Instance.Verbose("TCPSERVER", "Received packet [{0}]", bytesRead); | |
102 | //MainLog.Instance.Verbose("TCPSERVER", "Received packet [{0}]", bytesRead); | 101 | |
103 | 102 | if (bytesRead > 0) { | |
104 | if (bytesRead > 0) { | 103 | state.ms_ptr.Write(state.buffer, 0, bytesRead); |
105 | state.ms_ptr.Write(state.buffer, 0, bytesRead); | 104 | } |
106 | } | 105 | else |
107 | else | 106 | { |
108 | { | 107 | //MainLog.Instance.Verbose("TCPSERVER", "Connection terminated"); |
109 | //MainLog.Instance.Verbose("TCPSERVER", "Connection terminated"); | 108 | return; |
110 | return; | 109 | } |
111 | } | 110 | |
112 | 111 | long rest_size = state.ms_ptr.Length; | |
113 | long rest_size = state.ms_ptr.Length; | 112 | long current_pos = 0; |
114 | long current_pos = 0; | 113 | while (rest_size > TcpClient.internalPacketHeaderSize) { |
115 | while (rest_size > TcpClient.internalPacketHeaderSize) { | 114 | |
116 | 115 | if ((state.header == null) && (rest_size >= TcpClient.internalPacketHeaderSize)) | |
117 | if ((state.header == null) && (rest_size >= TcpClient.internalPacketHeaderSize)) | 116 | { |
118 | { | 117 | //MainLog.Instance.Verbose("TCPSERVER", "Processing header"); |
119 | //MainLog.Instance.Verbose("TCPSERVER", "Processing header"); | 118 | |
120 | 119 | // reading header | |
121 | // reading header | 120 | state.header = new InternalPacketHeader(); |
122 | state.header = new InternalPacketHeader(); | 121 | |
123 | 122 | byte[] headerbytes = new byte[TcpClient.internalPacketHeaderSize]; | |
124 | byte[] headerbytes = new byte[TcpClient.internalPacketHeaderSize]; | 123 | state.ms_ptr.Position = current_pos; |
125 | state.ms_ptr.Position = current_pos; | 124 | state.ms_ptr.Read(headerbytes, 0, TcpClient.internalPacketHeaderSize); |
126 | state.ms_ptr.Read(headerbytes, 0, TcpClient.internalPacketHeaderSize); | 125 | state.ms_ptr.Seek(0, SeekOrigin.End); |
127 | state.ms_ptr.Seek(0, SeekOrigin.End); | 126 | state.header.FromBytes(headerbytes); |
128 | state.header.FromBytes(headerbytes); | 127 | } |
129 | } | 128 | |
130 | 129 | if ((state.header != null) && (rest_size >= state.header.numbytes + TcpClient.internalPacketHeaderSize)) | |
131 | if ((state.header != null) && (rest_size >= state.header.numbytes + TcpClient.internalPacketHeaderSize)) | 130 | { |
132 | { | 131 | //MainLog.Instance.Verbose("TCPSERVER", "Processing body"); |
133 | //MainLog.Instance.Verbose("TCPSERVER", "Processing body"); | 132 | |
134 | 133 | // reading body | |
135 | // reading body | 134 | byte[] packet = new byte[state.header.numbytes]; |
136 | byte[] packet = new byte[state.header.numbytes]; | 135 | state.ms_ptr.Position = current_pos + TcpClient.internalPacketHeaderSize; |
137 | state.ms_ptr.Position = current_pos + TcpClient.internalPacketHeaderSize; | 136 | state.ms_ptr.Read(packet, 0, state.header.numbytes); |
138 | state.ms_ptr.Read(packet, 0, state.header.numbytes); | ||
139 | 137 | ||
140 | /* | 138 | /* |
141 | for(int i=0; i<state.header.numbytes; i++) { | 139 | for(int i=0; i<state.header.numbytes; i++) { |
142 | System.Console.Write(packet[i] + " "); | 140 | System.Console.Write(packet[i] + " "); |
143 | } | 141 | } |
144 | System.Console.WriteLine(); | 142 | System.Console.WriteLine(); |
145 | */ | 143 | */ |
146 | 144 | ||
147 | state.ms_ptr.Seek(0, SeekOrigin.End); | 145 | state.ms_ptr.Seek(0, SeekOrigin.End); |
148 | // call loadbarancer function | 146 | // call loadbarancer function |
149 | if (PacketHandler != null) | 147 | if (PacketHandler != null) |
150 | { | 148 | { |
151 | //MainLog.Instance.Verbose("TCPSERVER", "calling PacketHandler"); | 149 | //MainLog.Instance.Verbose("TCPSERVER", "calling PacketHandler"); |
152 | PacketHandler(state.header, packet); | 150 | PacketHandler(state.header, packet); |
153 | } | 151 | } |
154 | else | 152 | else |
155 | { | 153 | { |
156 | //MainLog.Instance.Verbose("TCPSERVER", "PacketHandler not found"); | 154 | //MainLog.Instance.Verbose("TCPSERVER", "PacketHandler not found"); |
157 | } | 155 | } |
158 | 156 | ||
159 | int read_size = state.header.numbytes + TcpClient.internalPacketHeaderSize; | 157 | int read_size = state.header.numbytes + TcpClient.internalPacketHeaderSize; |
160 | state.header = null; | 158 | state.header = null; |
161 | 159 | ||
162 | rest_size -= read_size; | 160 | rest_size -= read_size; |
163 | current_pos += read_size; | 161 | current_pos += read_size; |
164 | 162 | ||
165 | if (rest_size < TcpClient.internalPacketHeaderSize) { | 163 | if (rest_size < TcpClient.internalPacketHeaderSize) { |
166 | 164 | ||
167 | byte[] rest_bytes = new byte[rest_size]; | 165 | byte[] rest_bytes = new byte[rest_size]; |
168 | state.ms_ptr.Position = read_size; | 166 | state.ms_ptr.Position = read_size; |
169 | state.ms_ptr.Read(rest_bytes, 0, (int)rest_size); | 167 | state.ms_ptr.Read(rest_bytes, 0, (int)rest_size); |
170 | state.ms_ptr.Close(); | 168 | state.ms_ptr.Close(); |
171 | state.ms_ptr = new MemoryStream(); | 169 | state.ms_ptr = new MemoryStream(); |
172 | state.ms_ptr.Write(rest_bytes, 0, (int)rest_size); | 170 | state.ms_ptr.Write(rest_bytes, 0, (int)rest_size); |
173 | break; | 171 | break; |
174 | } | 172 | } |
175 | } | 173 | } |
176 | 174 | ||
177 | } // while (true) | 175 | } // while (true) |
178 | 176 | ||
179 | } | 177 | } |
180 | catch (Exception e) | 178 | catch (Exception e) |
181 | { | 179 | { |
182 | //MainLog.Instance.Verbose("TCPSERVER", e.ToString()); | 180 | //MainLog.Instance.Verbose("TCPSERVER", e.ToString()); |
183 | //MainLog.Instance.Verbose("TCPSERVER", e.StackTrace); | 181 | //MainLog.Instance.Verbose("TCPSERVER", e.StackTrace); |
184 | } | 182 | } |
185 | 183 | ||
186 | handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); | 184 | handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); |
187 | } | 185 | } |
188 | 186 | ||
189 | private static void Send(Socket handler, String data) { | 187 | private static void Send(Socket handler, String data) { |
190 | byte[] byteData = Encoding.ASCII.GetBytes(data); | 188 | byte[] byteData = Encoding.ASCII.GetBytes(data); |
191 | handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler); | 189 | handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler); |
192 | } | 190 | } |
193 | 191 | ||
194 | private static void SendCallback(IAsyncResult ar) { | 192 | private static void SendCallback(IAsyncResult ar) { |
195 | try { | 193 | try { |
196 | Socket handler = (Socket) ar.AsyncState; | 194 | Socket handler = (Socket) ar.AsyncState; |
197 | int bytesSent = handler.EndSend(ar); | 195 | int bytesSent = handler.EndSend(ar); |
198 | //Console.WriteLine("Sent {0} bytes to client.", bytesSent); | 196 | //Console.WriteLine("Sent {0} bytes to client.", bytesSent); |
199 | handler.Shutdown(SocketShutdown.Both); | 197 | handler.Shutdown(SocketShutdown.Both); |
200 | handler.Close(); | 198 | handler.Close(); |
201 | } catch (Exception e) { | 199 | } catch (Exception e) { |
202 | Console.WriteLine(e.ToString()); | 200 | Console.WriteLine(e.ToString()); |
203 | } | 201 | } |
204 | } | 202 | } |
205 | } | 203 | } |
206 | 204 | ||
207 | public class TcpServer { | 205 | public class TcpServer { |
208 | private int mPort = 11000; | 206 | private int mPort = 11000; |
209 | public TcpServer() { | 207 | public TcpServer() { |
210 | } | 208 | } |
211 | public TcpServer(int port) { | 209 | public TcpServer(int port) { |
212 | mPort = port; | 210 | mPort = port; |
213 | } | 211 | } |
214 | public void start() { | 212 | public void start() { |
215 | AsynchronousSocketListener.StartListening(mPort); | 213 | AsynchronousSocketListener.StartListening(mPort); |
216 | } | 214 | } |
217 | } | 215 | } |
218 | } | 216 | } |
219 | |||