aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/3Di/LoadBalancer/TcpClient.cs
diff options
context:
space:
mode:
Diffstat (limited to 'ThirdParty/3Di/LoadBalancer/TcpClient.cs')
-rw-r--r--ThirdParty/3Di/LoadBalancer/TcpClient.cs406
1 files changed, 203 insertions, 203 deletions
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
29using System; 28using System;
30using System.IO; 29using System.IO;
@@ -35,196 +34,197 @@ using System.Text;
35using System.Runtime.Serialization.Formatters.Binary; 34using System.Runtime.Serialization.Formatters.Binary;
36 35
37namespace OpenSim.ApplicationPlugins.LoadBalancer { 36namespace 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
126public 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/*
230for (int i = 0; i < 10; i++) 230for (int i = 0; i < 10; i++)
@@ -233,8 +233,8 @@ for (int i = 0; i < 10; i++)
233} 233}
234Console.WriteLine(""); 234Console.WriteLine("");
235*/ 235*/
236 AsynchronousClient.Send(this.mConnection, packet); 236 AsynchronousClient.Send(this.mConnection, packet);
237 } 237 }
238 } 238 }
239 } 239 }
240} 240}