diff options
author | Mike Mazur | 2009-01-28 01:56:04 +0000 |
---|---|---|
committer | Mike Mazur | 2009-01-28 01:56:04 +0000 |
commit | fefe0ff3d9ceaaefccb494ef13a5a98cc0131070 (patch) | |
tree | 01c5057fb9f23c55f07ff102906f5f14d6453118 /ThirdParty/3Di/LoadBalancer/TcpServer.cs | |
parent | Slight cleanup of docs, removing trailing whitespace. (diff) | |
download | opensim-SC-fefe0ff3d9ceaaefccb494ef13a5a98cc0131070.zip opensim-SC-fefe0ff3d9ceaaefccb494ef13a5a98cc0131070.tar.gz opensim-SC-fefe0ff3d9ceaaefccb494ef13a5a98cc0131070.tar.bz2 opensim-SC-fefe0ff3d9ceaaefccb494ef13a5a98cc0131070.tar.xz |
Removing contents of ThirdParty/3Di. The load balancer can now be found
at http://forge.opensimulator.org/gf/project/loadbalancer/
config key: svn.rmdir
Diffstat (limited to 'ThirdParty/3Di/LoadBalancer/TcpServer.cs')
-rw-r--r-- | ThirdParty/3Di/LoadBalancer/TcpServer.cs | 219 |
1 files changed, 0 insertions, 219 deletions
diff --git a/ThirdParty/3Di/LoadBalancer/TcpServer.cs b/ThirdParty/3Di/LoadBalancer/TcpServer.cs deleted file mode 100644 index 376c0a5..0000000 --- a/ThirdParty/3Di/LoadBalancer/TcpServer.cs +++ /dev/null | |||
@@ -1,219 +0,0 @@ | |||
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 | using System; | ||
29 | using System.IO; | ||
30 | using System.Net; | ||
31 | using System.Net.Sockets; | ||
32 | using System.Threading; | ||
33 | |||
34 | namespace OpenSim.ApplicationPlugins.LoadBalancer | ||
35 | { | ||
36 | public class StateObject | ||
37 | { | ||
38 | public const int BufferSize = 2048; | ||
39 | public byte[] buffer = new byte[BufferSize]; | ||
40 | public InternalPacketHeader header = null; | ||
41 | public MemoryStream ms_ptr = new MemoryStream(); | ||
42 | public Socket workSocket = null; | ||
43 | } | ||
44 | |||
45 | public class AsynchronousSocketListener | ||
46 | { | ||
47 | public static ManualResetEvent allDone = new ManualResetEvent(false); | ||
48 | public static string data = null; | ||
49 | |||
50 | #region KIRYU | ||
51 | |||
52 | #region Delegates | ||
53 | |||
54 | public delegate void PacketRecieveHandler(InternalPacketHeader header, byte[] buff); | ||
55 | |||
56 | #endregion | ||
57 | |||
58 | public static PacketRecieveHandler PacketHandler = null; | ||
59 | |||
60 | #endregion | ||
61 | |||
62 | public AsynchronousSocketListener() | ||
63 | { | ||
64 | } | ||
65 | |||
66 | public static void StartListening(int port) | ||
67 | { | ||
68 | IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); | ||
69 | IPAddress ipAddress = ipHostInfo.AddressList[0]; | ||
70 | IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); | ||
71 | |||
72 | Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | ||
73 | try | ||
74 | { | ||
75 | listener.Bind(localEndPoint); | ||
76 | listener.Listen(100); | ||
77 | while (true) | ||
78 | { | ||
79 | allDone.Reset(); | ||
80 | listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); | ||
81 | allDone.WaitOne(); | ||
82 | } | ||
83 | } | ||
84 | catch (Exception e) | ||
85 | { | ||
86 | Console.WriteLine(e.ToString()); | ||
87 | } | ||
88 | /* | ||
89 | Console.WriteLine("\nPress ENTER to continue..."); | ||
90 | Console.Read(); | ||
91 | */ | ||
92 | } | ||
93 | |||
94 | public static void AcceptCallback(IAsyncResult ar) | ||
95 | { | ||
96 | allDone.Set(); | ||
97 | Socket listener = (Socket) ar.AsyncState; | ||
98 | Socket handler = listener.EndAccept(ar); | ||
99 | StateObject state = new StateObject(); | ||
100 | state.workSocket = handler; | ||
101 | handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); | ||
102 | } | ||
103 | |||
104 | public static void ReadCallback(IAsyncResult ar) | ||
105 | { | ||
106 | StateObject state = (StateObject) ar.AsyncState; | ||
107 | Socket handler = state.workSocket; | ||
108 | |||
109 | try | ||
110 | { | ||
111 | int bytesRead = handler.EndReceive(ar); | ||
112 | |||
113 | //MainLog.Instance.Verbose("TCPSERVER", "Received packet [{0}]", bytesRead); | ||
114 | |||
115 | if (bytesRead > 0) | ||
116 | { | ||
117 | state.ms_ptr.Write(state.buffer, 0, bytesRead); | ||
118 | } | ||
119 | else | ||
120 | { | ||
121 | //MainLog.Instance.Verbose("TCPSERVER", "Connection terminated"); | ||
122 | return; | ||
123 | } | ||
124 | |||
125 | long rest_size = state.ms_ptr.Length; | ||
126 | long current_pos = 0; | ||
127 | while (rest_size > TcpClient.internalPacketHeaderSize) | ||
128 | { | ||
129 | if ((state.header == null) && (rest_size >= TcpClient.internalPacketHeaderSize)) | ||
130 | { | ||
131 | //MainLog.Instance.Verbose("TCPSERVER", "Processing header"); | ||
132 | |||
133 | // reading header | ||
134 | state.header = new InternalPacketHeader(); | ||
135 | |||
136 | byte[] headerbytes = new byte[TcpClient.internalPacketHeaderSize]; | ||
137 | state.ms_ptr.Position = current_pos; | ||
138 | state.ms_ptr.Read(headerbytes, 0, TcpClient.internalPacketHeaderSize); | ||
139 | state.ms_ptr.Seek(0, SeekOrigin.End); | ||
140 | state.header.FromBytes(headerbytes); | ||
141 | } | ||
142 | |||
143 | if ((state.header != null) && (rest_size >= state.header.numbytes + TcpClient.internalPacketHeaderSize)) | ||
144 | { | ||
145 | //MainLog.Instance.Verbose("TCPSERVER", "Processing body"); | ||
146 | |||
147 | // reading body | ||
148 | byte[] packet = new byte[state.header.numbytes]; | ||
149 | state.ms_ptr.Position = current_pos + TcpClient.internalPacketHeaderSize; | ||
150 | state.ms_ptr.Read(packet, 0, state.header.numbytes); | ||
151 | |||
152 | /* | ||
153 | for (int i=0; i<state.header.numbytes; i++) | ||
154 | { | ||
155 | System.Console.Write(packet[i] + " "); | ||
156 | } | ||
157 | System.Console.WriteLine(); | ||
158 | */ | ||
159 | |||
160 | state.ms_ptr.Seek(0, SeekOrigin.End); | ||
161 | // call loadbarancer function | ||
162 | if (PacketHandler == null) | ||
163 | { | ||
164 | //MainLog.Instance.Verbose("TCPSERVER", "PacketHandler not found"); | ||
165 | } | ||
166 | else | ||
167 | { | ||
168 | //MainLog.Instance.Verbose("TCPSERVER", "calling PacketHandler"); | ||
169 | PacketHandler(state.header, packet); | ||
170 | } | ||
171 | |||
172 | int read_size = state.header.numbytes + TcpClient.internalPacketHeaderSize; | ||
173 | state.header = null; | ||
174 | |||
175 | rest_size -= read_size; | ||
176 | current_pos += read_size; | ||
177 | |||
178 | if (rest_size < TcpClient.internalPacketHeaderSize) | ||
179 | { | ||
180 | byte[] rest_bytes = new byte[rest_size]; | ||
181 | state.ms_ptr.Position = read_size; | ||
182 | state.ms_ptr.Read(rest_bytes, 0, (int) rest_size); | ||
183 | state.ms_ptr.Close(); | ||
184 | state.ms_ptr = new MemoryStream(); | ||
185 | state.ms_ptr.Write(rest_bytes, 0, (int) rest_size); | ||
186 | break; | ||
187 | } | ||
188 | } | ||
189 | } // while (true) | ||
190 | } | ||
191 | catch (Exception) | ||
192 | { | ||
193 | //MainLog.Instance.Verbose("TCPSERVER", e.ToString()); | ||
194 | //MainLog.Instance.Verbose("TCPSERVER", e.StackTrace); | ||
195 | } | ||
196 | |||
197 | handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); | ||
198 | } | ||
199 | } | ||
200 | |||
201 | public class TcpServer | ||
202 | { | ||
203 | private int mPort = 11000; | ||
204 | |||
205 | public TcpServer() | ||
206 | { | ||
207 | } | ||
208 | |||
209 | public TcpServer(int port) | ||
210 | { | ||
211 | mPort = port; | ||
212 | } | ||
213 | |||
214 | public void start() | ||
215 | { | ||
216 | AsynchronousSocketListener.StartListening(mPort); | ||
217 | } | ||
218 | } | ||
219 | } \ No newline at end of file | ||