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.cs225
1 files changed, 0 insertions, 225 deletions
diff --git a/ThirdParty/3Di/LoadBalancer/TcpClient.cs b/ThirdParty/3Di/LoadBalancer/TcpClient.cs
deleted file mode 100644
index 1acba24..0000000
--- a/ThirdParty/3Di/LoadBalancer/TcpClient.cs
+++ /dev/null
@@ -1,225 +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
28using System;
29using System.IO;
30using System.Net;
31using System.Net.Sockets;
32using System.Threading;
33
34namespace OpenSim.ApplicationPlugins.LoadBalancer
35{
36 public class AsynchronousClient
37 {
38 private static ManualResetEvent connectDone = new ManualResetEvent(false);
39 private static ManualResetEvent sendDone = new ManualResetEvent(false);
40
41 public static Socket StartClient(string hostname, int port)
42 {
43 try
44 {
45 IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname);
46 IPAddress ipAddress = ipHostInfo.AddressList[0];
47 IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
48
49 Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
50 client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
51 connectDone.WaitOne();
52 /*
53 Send(client,"This is a test<EOF>");
54 sendDone.WaitOne();
55 Receive(client);
56 receiveDone.WaitOne();
57 client.Shutdown(SocketShutdown.Both);
58 client.Close();
59 */
60 return client;
61 }
62 catch (Exception e)
63 {
64 Console.WriteLine(e.ToString());
65 throw new Exception("socket error !!");
66 }
67 }
68
69 private static void ConnectCallback(IAsyncResult ar)
70 {
71 try
72 {
73 Socket client = (Socket) ar.AsyncState;
74 client.EndConnect(ar);
75 Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString());
76 connectDone.Set();
77 }
78 catch (Exception e)
79 {
80 Console.WriteLine(e.ToString());
81 }
82 }
83
84 public static void Send(Socket client, byte[] byteData)
85 {
86 client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
87 }
88
89 private static void SendCallback(IAsyncResult ar)
90 {
91 try
92 {
93 Socket client = (Socket) ar.AsyncState;
94 int bytesSent = client.EndSend(ar);
95 if (bytesSent > 0)
96 {
97 //Console.WriteLine("Sent {0} bytes to server.", bytesSent);
98 }
99 sendDone.Set();
100 }
101 catch (Exception e)
102 {
103 Console.WriteLine(e.ToString());
104 }
105 }
106 }
107
108 public class InternalPacketHeader
109 {
110 public Guid agent_id;
111 private byte[] buffer = new byte[32];
112 public int numbytes;
113 public int region_port;
114 public int throttlePacketType;
115 public int type;
116
117 public void FromBytes(byte[] bytes)
118 {
119 MemoryStream memstr = new MemoryStream(bytes);
120 memstr.Seek(0, SeekOrigin.Begin);
121 BinaryReader binread = new BinaryReader(memstr);
122
123 type = binread.ReadInt32();
124 throttlePacketType = binread.ReadInt32();
125 numbytes = binread.ReadInt32();
126 agent_id = new Guid(binread.ReadBytes(16));
127 region_port = binread.ReadInt32();
128
129 binread.Close();
130 }
131
132 public byte[] ToBytes()
133 {
134 int i = 0;
135 buffer[i++] = (byte) (type % 256);
136 buffer[i++] = (byte) ((type >> 8) % 256);
137 buffer[i++] = (byte) ((type >> 16) % 256);
138 buffer[i++] = (byte) ((type >> 24) % 256);
139
140 buffer[i++] = (byte) (throttlePacketType % 256);
141 buffer[i++] = (byte) ((throttlePacketType >> 8) % 256);
142 buffer[i++] = (byte) ((throttlePacketType >> 16) % 256);
143 buffer[i++] = (byte) ((throttlePacketType >> 24) % 256);
144
145 buffer[i++] = (byte) (numbytes % 256);
146 buffer[i++] = (byte) ((numbytes >> 8) % 256);
147 buffer[i++] = (byte) ((numbytes >> 16) % 256);
148 buffer[i++] = (byte) ((numbytes >> 24) % 256);
149
150 // no endian care
151 Buffer.BlockCopy(agent_id.ToByteArray(), 0, buffer, i, 16);
152 i += 16;
153
154 buffer[i++] = (byte) (region_port % 256);
155 buffer[i++] = (byte) ((region_port >> 8) % 256);
156 buffer[i++] = (byte) ((region_port >> 16) % 256);
157 buffer[i++] = (byte) ((region_port >> 24) % 256);
158
159 return buffer;
160 }
161 }
162
163 public class TcpClient
164 {
165 public static int internalPacketHeaderSize = 4 * 4 + 16 * 1;
166 private Socket mConnection;
167
168 private string mHostname;
169 private int mPort;
170
171 public TcpClient(string hostname, int port)
172 {
173 mHostname = hostname;
174 mPort = port;
175 mConnection = null;
176 }
177
178 public void connect()
179 {
180 mConnection = AsynchronousClient.StartClient(mHostname, mPort);
181 }
182
183/*
184 public void receive()
185 {
186 if (mConnection == null)
187 {
188 throw new Exception("client not initialized");
189 }
190 try
191 {
192 AsynchronousClient.Receive(this.mConnection);
193 }
194 catch (Exception e)
195 {
196 Console.WriteLine(e.ToString());
197 mConnection = null;
198 }
199 }
200*/
201
202 public void send(InternalPacketHeader header, byte[] packet)
203 {
204 lock (this)
205 {
206 if (mConnection == null)
207 {
208// throw new Exception("client not initialized");
209 connect();
210 }
211
212 AsynchronousClient.Send(mConnection, header.ToBytes());
213
214/*
215for (int i = 0; i < 10; i++)
216{
217 Console.Write(packet[i] + " ");
218}
219Console.WriteLine("");
220*/
221 AsynchronousClient.Send(mConnection, packet);
222 }
223 }
224 }
225} \ No newline at end of file