diff options
author | Johan Berntsson | 2008-03-04 05:31:54 +0000 |
---|---|---|
committer | Johan Berntsson | 2008-03-04 05:31:54 +0000 |
commit | 279e0061c515ee0a03036bef68eea9738273d785 (patch) | |
tree | 4502228eb7b87a760e0b0e67aded9d1d870d0bed /ThirdParty/3Di/LoadBalancer/TcpServer.cs | |
parent | Added copyright heaaders. Minor cleanup. (diff) | |
download | opensim-SC_OLD-279e0061c515ee0a03036bef68eea9738273d785.zip opensim-SC_OLD-279e0061c515ee0a03036bef68eea9738273d785.tar.gz opensim-SC_OLD-279e0061c515ee0a03036bef68eea9738273d785.tar.bz2 opensim-SC_OLD-279e0061c515ee0a03036bef68eea9738273d785.tar.xz |
Merged 3Di code that provides scene and avatar serialization, and plugin support for region move/split/merge. See ThirdParty/3Di/README.txt. Unless the new modules are used there should be no noticeable changes when running OpenSim.
Diffstat (limited to 'ThirdParty/3Di/LoadBalancer/TcpServer.cs')
-rw-r--r-- | ThirdParty/3Di/LoadBalancer/TcpServer.cs | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/ThirdParty/3Di/LoadBalancer/TcpServer.cs b/ThirdParty/3Di/LoadBalancer/TcpServer.cs new file mode 100644 index 0000000..ee8bcba --- /dev/null +++ b/ThirdParty/3Di/LoadBalancer/TcpServer.cs | |||
@@ -0,0 +1,219 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | using System; | ||
30 | using System.IO; | ||
31 | using System.Net; | ||
32 | using System.Net.Sockets; | ||
33 | using System.Text; | ||
34 | using System.Threading; | ||
35 | using System.Runtime.Serialization.Formatters.Binary; | ||
36 | |||
37 | using OpenSim.Framework.Console; | ||
38 | |||
39 | namespace OpenSim.ApplicationPlugins.LoadBalancer { | ||
40 | |||
41 | public class StateObject { | ||
42 | public Socket workSocket = null; | ||
43 | public const int BufferSize = 2048; | ||
44 | public byte[] buffer = new byte[BufferSize]; | ||
45 | public MemoryStream ms_ptr = new MemoryStream(); | ||
46 | public InternalPacketHeader header = null; | ||
47 | } | ||
48 | |||
49 | public class AsynchronousSocketListener { | ||
50 | public static string data = null; | ||
51 | public static ManualResetEvent allDone = new ManualResetEvent(false); | ||
52 | |||
53 | #region KIRYU | ||
54 | public delegate void PacketRecieveHandler(InternalPacketHeader header, byte[] buff); | ||
55 | public static PacketRecieveHandler PacketHandler = null; | ||
56 | #endregion | ||
57 | |||
58 | public AsynchronousSocketListener() { } | ||
59 | |||
60 | public static void StartListening(int port) { | ||
61 | IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); | ||
62 | IPAddress ipAddress = ipHostInfo.AddressList[0]; | ||
63 | IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); | ||
64 | |||
65 | Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); | ||
66 | try { | ||
67 | listener.Bind(localEndPoint); | ||
68 | listener.Listen(100); | ||
69 | while (true) { | ||
70 | allDone.Reset(); | ||
71 | listener.BeginAccept( new AsyncCallback(AcceptCallback), listener ); | ||
72 | allDone.WaitOne(); | ||
73 | } | ||
74 | } catch (Exception e) { | ||
75 | Console.WriteLine(e.ToString()); | ||
76 | } | ||
77 | /* | ||
78 | Console.WriteLine("\nPress ENTER to continue..."); | ||
79 | Console.Read(); | ||
80 | */ | ||
81 | } | ||
82 | |||
83 | public static void AcceptCallback(IAsyncResult ar) { | ||
84 | allDone.Set(); | ||
85 | Socket listener = (Socket) ar.AsyncState; | ||
86 | Socket handler = listener.EndAccept(ar); | ||
87 | StateObject state = new StateObject(); | ||
88 | state.workSocket = handler; | ||
89 | handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); | ||
90 | } | ||
91 | |||
92 | public static void ReadCallback(IAsyncResult ar) { | ||
93 | String content = String.Empty; | ||
94 | StateObject state = (StateObject) ar.AsyncState; | ||
95 | Socket handler = state.workSocket; | ||
96 | |||
97 | try | ||
98 | { | ||
99 | |||
100 | int bytesRead = handler.EndReceive(ar); | ||
101 | |||
102 | //MainLog.Instance.Verbose("TCPSERVER", "Received packet [{0}]", bytesRead); | ||
103 | |||
104 | if (bytesRead > 0) { | ||
105 | state.ms_ptr.Write(state.buffer, 0, bytesRead); | ||
106 | } | ||
107 | else | ||
108 | { | ||
109 | //MainLog.Instance.Verbose("TCPSERVER", "Connection terminated"); | ||
110 | return; | ||
111 | } | ||
112 | |||
113 | long rest_size = state.ms_ptr.Length; | ||
114 | long current_pos = 0; | ||
115 | while (rest_size > TcpClient.internalPacketHeaderSize) { | ||
116 | |||
117 | if ((state.header == null) && (rest_size >= TcpClient.internalPacketHeaderSize)) | ||
118 | { | ||
119 | //MainLog.Instance.Verbose("TCPSERVER", "Processing header"); | ||
120 | |||
121 | // reading header | ||
122 | state.header = new InternalPacketHeader(); | ||
123 | |||
124 | byte[] headerbytes = new byte[TcpClient.internalPacketHeaderSize]; | ||
125 | state.ms_ptr.Position = current_pos; | ||
126 | state.ms_ptr.Read(headerbytes, 0, TcpClient.internalPacketHeaderSize); | ||
127 | state.ms_ptr.Seek(0, SeekOrigin.End); | ||
128 | state.header.FromBytes(headerbytes); | ||
129 | } | ||
130 | |||
131 | if ((state.header != null) && (rest_size >= state.header.numbytes + TcpClient.internalPacketHeaderSize)) | ||
132 | { | ||
133 | //MainLog.Instance.Verbose("TCPSERVER", "Processing body"); | ||
134 | |||
135 | // reading body | ||
136 | byte[] packet = new byte[state.header.numbytes]; | ||
137 | state.ms_ptr.Position = current_pos + TcpClient.internalPacketHeaderSize; | ||
138 | state.ms_ptr.Read(packet, 0, state.header.numbytes); | ||
139 | |||
140 | /* | ||
141 | for(int i=0; i<state.header.numbytes; i++) { | ||
142 | System.Console.Write(packet[i] + " "); | ||
143 | } | ||
144 | System.Console.WriteLine(); | ||
145 | */ | ||
146 | |||
147 | state.ms_ptr.Seek(0, SeekOrigin.End); | ||
148 | // call loadbarancer function | ||
149 | if (PacketHandler != null) | ||
150 | { | ||
151 | //MainLog.Instance.Verbose("TCPSERVER", "calling PacketHandler"); | ||
152 | PacketHandler(state.header, packet); | ||
153 | } | ||
154 | else | ||
155 | { | ||
156 | //MainLog.Instance.Verbose("TCPSERVER", "PacketHandler not found"); | ||
157 | } | ||
158 | |||
159 | int read_size = state.header.numbytes + TcpClient.internalPacketHeaderSize; | ||
160 | state.header = null; | ||
161 | |||
162 | rest_size -= read_size; | ||
163 | current_pos += read_size; | ||
164 | |||
165 | if (rest_size < TcpClient.internalPacketHeaderSize) { | ||
166 | |||
167 | byte[] rest_bytes = new byte[rest_size]; | ||
168 | state.ms_ptr.Position = read_size; | ||
169 | state.ms_ptr.Read(rest_bytes, 0, (int)rest_size); | ||
170 | state.ms_ptr.Close(); | ||
171 | state.ms_ptr = new MemoryStream(); | ||
172 | state.ms_ptr.Write(rest_bytes, 0, (int)rest_size); | ||
173 | break; | ||
174 | } | ||
175 | } | ||
176 | |||
177 | } // while (true) | ||
178 | |||
179 | } | ||
180 | catch (Exception e) | ||
181 | { | ||
182 | //MainLog.Instance.Verbose("TCPSERVER", e.ToString()); | ||
183 | //MainLog.Instance.Verbose("TCPSERVER", e.StackTrace); | ||
184 | } | ||
185 | |||
186 | handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); | ||
187 | } | ||
188 | |||
189 | private static void Send(Socket handler, String data) { | ||
190 | byte[] byteData = Encoding.ASCII.GetBytes(data); | ||
191 | handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler); | ||
192 | } | ||
193 | |||
194 | private static void SendCallback(IAsyncResult ar) { | ||
195 | try { | ||
196 | Socket handler = (Socket) ar.AsyncState; | ||
197 | int bytesSent = handler.EndSend(ar); | ||
198 | //Console.WriteLine("Sent {0} bytes to client.", bytesSent); | ||
199 | handler.Shutdown(SocketShutdown.Both); | ||
200 | handler.Close(); | ||
201 | } catch (Exception e) { | ||
202 | Console.WriteLine(e.ToString()); | ||
203 | } | ||
204 | } | ||
205 | } | ||
206 | |||
207 | public class TcpServer { | ||
208 | private int mPort = 11000; | ||
209 | public TcpServer() { | ||
210 | } | ||
211 | public TcpServer(int port) { | ||
212 | mPort = port; | ||
213 | } | ||
214 | public void start() { | ||
215 | AsynchronousSocketListener.StartListening(mPort); | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | |||