aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/3Di/LoadBalancer/TcpServer.cs
diff options
context:
space:
mode:
authorJeff Ames2008-03-04 06:02:48 +0000
committerJeff Ames2008-03-04 06:02:48 +0000
commit75f6f6e3ffeca74ca75f80685e243103fe97cf98 (patch)
tree7541cdfce74eb7378e468a70680216b76a60188a /ThirdParty/3Di/LoadBalancer/TcpServer.cs
parentMerged 3Di code that provides scene and avatar serialization, and plugin supp... (diff)
downloadopensim-SC_OLD-75f6f6e3ffeca74ca75f80685e243103fe97cf98.zip
opensim-SC_OLD-75f6f6e3ffeca74ca75f80685e243103fe97cf98.tar.gz
opensim-SC_OLD-75f6f6e3ffeca74ca75f80685e243103fe97cf98.tar.bz2
opensim-SC_OLD-75f6f6e3ffeca74ca75f80685e243103fe97cf98.tar.xz
Update svn properties. Minor formatting cleanup.
Diffstat (limited to 'ThirdParty/3Di/LoadBalancer/TcpServer.cs')
-rw-r--r--ThirdParty/3Di/LoadBalancer/TcpServer.cs379
1 files changed, 188 insertions, 191 deletions
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
29using System; 28using System;
30using System.IO; 29using System.IO;
@@ -38,182 +37,180 @@ using OpenSim.Framework.Console;
38 37
39namespace OpenSim.ApplicationPlugins.LoadBalancer { 38namespace 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