aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/3Di/LoadBalancer/TcpClient.cs
diff options
context:
space:
mode:
authorJohan Berntsson2008-03-04 05:31:54 +0000
committerJohan Berntsson2008-03-04 05:31:54 +0000
commit279e0061c515ee0a03036bef68eea9738273d785 (patch)
tree4502228eb7b87a760e0b0e67aded9d1d870d0bed /ThirdParty/3Di/LoadBalancer/TcpClient.cs
parentAdded copyright heaaders. Minor cleanup. (diff)
downloadopensim-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/TcpClient.cs')
-rw-r--r--ThirdParty/3Di/LoadBalancer/TcpClient.cs240
1 files changed, 240 insertions, 0 deletions
diff --git a/ThirdParty/3Di/LoadBalancer/TcpClient.cs b/ThirdParty/3Di/LoadBalancer/TcpClient.cs
new file mode 100644
index 0000000..9f62d33
--- /dev/null
+++ b/ThirdParty/3Di/LoadBalancer/TcpClient.cs
@@ -0,0 +1,240 @@
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
29using System;
30using System.IO;
31using System.Net;
32using System.Net.Sockets;
33using System.Threading;
34using System.Text;
35using System.Runtime.Serialization.Formatters.Binary;
36
37namespace OpenSim.ApplicationPlugins.LoadBalancer {
38 public class AsynchronousClient {
39 private static ManualResetEvent connectDone = new ManualResetEvent(false);
40 private static ManualResetEvent sendDone = new ManualResetEvent(false);
41 private static ManualResetEvent receiveDone = new ManualResetEvent(false);
42 private static String response = String.Empty;
43
44 public static Socket StartClient(string hostname, int port) {
45 try {
46 IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname);
47 IPAddress ipAddress = ipHostInfo.AddressList[0];
48 IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
49
50 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
51 client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client);
52 connectDone.WaitOne();
53 /*
54 Send(client,"This is a test<EOF>");
55 sendDone.WaitOne();
56 Receive(client);
57 receiveDone.WaitOne();
58 client.Shutdown(SocketShutdown.Both);
59 client.Close();
60 */
61 return client;
62 } catch (Exception e) {
63 Console.WriteLine(e.ToString());
64 throw new Exception("socket error !!");
65 }
66 }
67
68 private static void ConnectCallback(IAsyncResult ar) {
69 try {
70 Socket client = (Socket) ar.AsyncState;
71 client.EndConnect(ar);
72 Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString());
73 connectDone.Set();
74 } catch (Exception e) {
75 Console.WriteLine(e.ToString());
76 }
77 }
78
79/*
80 public static void Receive(Socket client) {
81 try {
82 StateObject state = new StateObject();
83 state.workSocket = client;
84 client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
85 } catch (Exception e) {
86 Console.WriteLine(e.ToString());
87 }
88 }
89
90 private static void ReceiveCallback( IAsyncResult ar ) {
91 try {
92 StateObject state = (StateObject) ar.AsyncState;
93 Socket client = state.workSocket;
94
95 int bytesRead = client.EndReceive(ar);
96 if (bytesRead > 0) {
97 state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
98 client.BeginReceive(state.buffer,0,StateObject.BufferSize,0, new AsyncCallback(ReceiveCallback), state);
99 } else {
100 if (state.sb.Length > 1) {
101 response = state.sb.ToString();
102 }
103 receiveDone.Set();
104 }
105 } catch (Exception e) {
106 Console.WriteLine(e.ToString());
107 }
108 }
109*/
110 public static void Send(Socket client, byte[] byteData) {
111 client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
112 }
113
114 private static void SendCallback(IAsyncResult ar) {
115 try {
116 Socket client = (Socket) ar.AsyncState;
117 int bytesSent = client.EndSend(ar);
118 //Console.WriteLine("Sent {0} bytes to server.", bytesSent);
119 sendDone.Set();
120 } catch (Exception e) {
121 Console.WriteLine(e.ToString());
122 }
123 }
124 }
125
126public 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 {
137 int i = 0; // offset
138 try
139 {
140 this.type = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24));
141 this.throttlePacketType = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24));
142 this.numbytes = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24));
143 this.agent_id = new Guid(
144 bytes[i++] | (bytes[i++] << 8) | (bytes[i++] << 16) | bytes[i++] << 24,
145 (short)(bytes[i++] | (bytes[i++] << 8)),
146 (short)(bytes[i++] | (bytes[i++] << 8)),
147 bytes[i++], bytes[i++], bytes[i++], bytes[i++],
148 bytes[i++], bytes[i++], bytes[i++], bytes[i++]);
149 this.region_port = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24));
150 }
151 catch (Exception)
152 {
153 throw new Exception("bad format!!!");
154 }
155 }
156
157 public byte[] ToBytes()
158 {
159 int i = 0;
160 this.buffer[i++] = (byte)(this.type % 256);
161 this.buffer[i++] = (byte)((this.type >> 8) % 256);
162 this.buffer[i++] = (byte)((this.type >> 16) % 256);
163 this.buffer[i++] = (byte)((this.type >> 24) % 256);
164
165 this.buffer[i++] = (byte)(this.throttlePacketType % 256);
166 this.buffer[i++] = (byte)((this.throttlePacketType >> 8) % 256);
167 this.buffer[i++] = (byte)((this.throttlePacketType >> 16) % 256);
168 this.buffer[i++] = (byte)((this.throttlePacketType >> 24) % 256);
169
170 this.buffer[i++] = (byte)(this.numbytes % 256);
171 this.buffer[i++] = (byte)((this.numbytes >> 8) % 256);
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/*
202 public void recevie() {
203 if (mConnection == null) {
204 throw new Exception("client not initialized");
205 }
206 try
207 {
208 AsynchronousClient.Receive(this.mConnection);
209 }
210 catch (Exception e)
211 {
212 Console.WriteLine(e.ToString());
213 mConnection = null;
214 }
215 }
216*/
217 public void send(InternalPacketHeader header, byte[] packet) {
218
219 lock (this)
220 {
221
222 if (mConnection == null) {
223// throw new Exception("client not initialized");
224 connect();
225 }
226
227 AsynchronousClient.Send(this.mConnection, header.ToBytes());
228
229/*
230for (int i = 0; i < 10; i++)
231{
232 Console.Write(packet[i] + " ");
233}
234Console.WriteLine("");
235*/
236 AsynchronousClient.Send(this.mConnection, packet);
237 }
238 }
239 }
240}