diff options
Diffstat (limited to 'ThirdParty/3Di/LoadBalancer/TcpClient.cs')
-rw-r--r-- | ThirdParty/3Di/LoadBalancer/TcpClient.cs | 139 |
1 files changed, 81 insertions, 58 deletions
diff --git a/ThirdParty/3Di/LoadBalancer/TcpClient.cs b/ThirdParty/3Di/LoadBalancer/TcpClient.cs index 7b5bc79..af678a0 100644 --- a/ThirdParty/3Di/LoadBalancer/TcpClient.cs +++ b/ThirdParty/3Di/LoadBalancer/TcpClient.cs | |||
@@ -30,19 +30,23 @@ using System.Net; | |||
30 | using System.Net.Sockets; | 30 | using System.Net.Sockets; |
31 | using System.Threading; | 31 | using System.Threading; |
32 | 32 | ||
33 | namespace OpenSim.ApplicationPlugins.LoadBalancer { | 33 | namespace OpenSim.ApplicationPlugins.LoadBalancer |
34 | public class AsynchronousClient { | 34 | { |
35 | public class AsynchronousClient | ||
36 | { | ||
35 | private static ManualResetEvent connectDone = new ManualResetEvent(false); | 37 | private static ManualResetEvent connectDone = new ManualResetEvent(false); |
36 | private static ManualResetEvent sendDone = new ManualResetEvent(false); | 38 | private static ManualResetEvent sendDone = new ManualResetEvent(false); |
37 | 39 | ||
38 | public static Socket StartClient(string hostname, int port) { | 40 | public static Socket StartClient(string hostname, int port) |
39 | try { | 41 | { |
42 | try | ||
43 | { | ||
40 | IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname); | 44 | IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname); |
41 | IPAddress ipAddress = ipHostInfo.AddressList[0]; | 45 | IPAddress ipAddress = ipHostInfo.AddressList[0]; |
42 | IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); | 46 | IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); |
43 | 47 | ||
44 | Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | 48 | Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
45 | client.BeginConnect( remoteEP, new AsyncCallback(ConnectCallback), client); | 49 | client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); |
46 | connectDone.WaitOne(); | 50 | connectDone.WaitOne(); |
47 | /* | 51 | /* |
48 | Send(client,"This is a test<EOF>"); | 52 | Send(client,"This is a test<EOF>"); |
@@ -53,19 +57,25 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { | |||
53 | client.Close(); | 57 | client.Close(); |
54 | */ | 58 | */ |
55 | return client; | 59 | return client; |
56 | } catch (Exception e) { | 60 | } |
61 | catch (Exception e) | ||
62 | { | ||
57 | Console.WriteLine(e.ToString()); | 63 | Console.WriteLine(e.ToString()); |
58 | throw new Exception("socket error !!"); | 64 | throw new Exception("socket error !!"); |
59 | } | 65 | } |
60 | } | 66 | } |
61 | 67 | ||
62 | private static void ConnectCallback(IAsyncResult ar) { | 68 | private static void ConnectCallback(IAsyncResult ar) |
63 | try { | 69 | { |
70 | try | ||
71 | { | ||
64 | Socket client = (Socket) ar.AsyncState; | 72 | Socket client = (Socket) ar.AsyncState; |
65 | client.EndConnect(ar); | 73 | client.EndConnect(ar); |
66 | Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); | 74 | Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); |
67 | connectDone.Set(); | 75 | connectDone.Set(); |
68 | } catch (Exception e) { | 76 | } |
77 | catch (Exception e) | ||
78 | { | ||
69 | Console.WriteLine(e.ToString()); | 79 | Console.WriteLine(e.ToString()); |
70 | } | 80 | } |
71 | } | 81 | } |
@@ -101,20 +111,26 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { | |||
101 | } | 111 | } |
102 | } | 112 | } |
103 | */ | 113 | */ |
104 | public static void Send(Socket client, byte[] byteData) { | 114 | |
115 | public static void Send(Socket client, byte[] byteData) | ||
116 | { | ||
105 | client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); | 117 | client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client); |
106 | } | 118 | } |
107 | 119 | ||
108 | private static void SendCallback(IAsyncResult ar) { | 120 | private static void SendCallback(IAsyncResult ar) |
109 | try { | 121 | { |
122 | try | ||
123 | { | ||
110 | Socket client = (Socket) ar.AsyncState; | 124 | Socket client = (Socket) ar.AsyncState; |
111 | int bytesSent = client.EndSend(ar); | 125 | int bytesSent = client.EndSend(ar); |
112 | if(bytesSent > 0) | 126 | if (bytesSent > 0) |
113 | { | 127 | { |
114 | //Console.WriteLine("Sent {0} bytes to server.", bytesSent); | 128 | //Console.WriteLine("Sent {0} bytes to server.", bytesSent); |
115 | } | 129 | } |
116 | sendDone.Set(); | 130 | sendDone.Set(); |
117 | } catch (Exception e) { | 131 | } |
132 | catch (Exception e) | ||
133 | { | ||
118 | Console.WriteLine(e.ToString()); | 134 | Console.WriteLine(e.ToString()); |
119 | } | 135 | } |
120 | } | 136 | } |
@@ -122,28 +138,28 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { | |||
122 | 138 | ||
123 | public class InternalPacketHeader | 139 | public class InternalPacketHeader |
124 | { | 140 | { |
141 | public Guid agent_id; | ||
125 | private byte[] buffer = new byte[32]; | 142 | private byte[] buffer = new byte[32]; |
126 | public int type; | ||
127 | public int throttlePacketType; | ||
128 | public int numbytes; | 143 | public int numbytes; |
129 | public Guid agent_id; | ||
130 | public int region_port; | 144 | public int region_port; |
145 | public int throttlePacketType; | ||
146 | public int type; | ||
131 | 147 | ||
132 | public void FromBytes(byte[] bytes) | 148 | public void FromBytes(byte[] bytes) |
133 | { | 149 | { |
134 | int i = 0; // offset | 150 | int i = 0; // offset |
135 | try | 151 | try |
136 | { | 152 | { |
137 | this.type = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); | 153 | type = (int) (bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); |
138 | this.throttlePacketType = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); | 154 | throttlePacketType = (int) (bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); |
139 | this.numbytes = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); | 155 | numbytes = (int) (bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); |
140 | this.agent_id = new Guid( | 156 | agent_id = new Guid( |
141 | bytes[i++] | (bytes[i++] << 8) | (bytes[i++] << 16) | bytes[i++] << 24, | 157 | bytes[i++] | (bytes[i++] << 8) | (bytes[i++] << 16) | bytes[i++] << 24, |
142 | (short)(bytes[i++] | (bytes[i++] << 8)), | 158 | (short) (bytes[i++] | (bytes[i++] << 8)), |
143 | (short)(bytes[i++] | (bytes[i++] << 8)), | 159 | (short) (bytes[i++] | (bytes[i++] << 8)), |
144 | bytes[i++], bytes[i++], bytes[i++], bytes[i++], | 160 | bytes[i++], bytes[i++], bytes[i++], bytes[i++], |
145 | bytes[i++], bytes[i++], bytes[i++], bytes[i++]); | 161 | bytes[i++], bytes[i++], bytes[i++], bytes[i++]); |
146 | this.region_port = (int)(bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); | 162 | region_port = (int) (bytes[i++] + (bytes[i++] << 8) + (bytes[i++] << 16) + (bytes[i++] << 24)); |
147 | } | 163 | } |
148 | catch (Exception) | 164 | catch (Exception) |
149 | { | 165 | { |
@@ -154,48 +170,54 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { | |||
154 | public byte[] ToBytes() | 170 | public byte[] ToBytes() |
155 | { | 171 | { |
156 | int i = 0; | 172 | int i = 0; |
157 | this.buffer[i++] = (byte)(this.type % 256); | 173 | buffer[i++] = (byte) (type % 256); |
158 | this.buffer[i++] = (byte)((this.type >> 8) % 256); | 174 | buffer[i++] = (byte) ((type >> 8) % 256); |
159 | this.buffer[i++] = (byte)((this.type >> 16) % 256); | 175 | buffer[i++] = (byte) ((type >> 16) % 256); |
160 | this.buffer[i++] = (byte)((this.type >> 24) % 256); | 176 | buffer[i++] = (byte) ((type >> 24) % 256); |
161 | 177 | ||
162 | this.buffer[i++] = (byte)(this.throttlePacketType % 256); | 178 | buffer[i++] = (byte) (throttlePacketType % 256); |
163 | this.buffer[i++] = (byte)((this.throttlePacketType >> 8) % 256); | 179 | buffer[i++] = (byte) ((throttlePacketType >> 8) % 256); |
164 | this.buffer[i++] = (byte)((this.throttlePacketType >> 16) % 256); | 180 | buffer[i++] = (byte) ((throttlePacketType >> 16) % 256); |
165 | this.buffer[i++] = (byte)((this.throttlePacketType >> 24) % 256); | 181 | buffer[i++] = (byte) ((throttlePacketType >> 24) % 256); |
166 | 182 | ||
167 | this.buffer[i++] = (byte)(this.numbytes % 256); | 183 | buffer[i++] = (byte) (numbytes % 256); |
168 | this.buffer[i++] = (byte)((this.numbytes >> 8) % 256); | 184 | buffer[i++] = (byte) ((numbytes >> 8) % 256); |
169 | this.buffer[i++] = (byte)((this.numbytes >> 16) % 256); | 185 | buffer[i++] = (byte) ((numbytes >> 16) % 256); |
170 | this.buffer[i++] = (byte)((this.numbytes >> 24) % 256); | 186 | buffer[i++] = (byte) ((numbytes >> 24) % 256); |
171 | 187 | ||
172 | // no endian care | 188 | // no endian care |
173 | Buffer.BlockCopy(agent_id.ToByteArray(), 0, this.buffer, i, 16); i += 16; | 189 | Buffer.BlockCopy(agent_id.ToByteArray(), 0, buffer, i, 16); |
190 | i += 16; | ||
174 | 191 | ||
175 | this.buffer[i++] = (byte)(this.region_port % 256); | 192 | buffer[i++] = (byte) (region_port % 256); |
176 | this.buffer[i++] = (byte)((this.region_port >> 8) % 256); | 193 | buffer[i++] = (byte) ((region_port >> 8) % 256); |
177 | this.buffer[i++] = (byte)((this.region_port >> 16) % 256); | 194 | buffer[i++] = (byte) ((region_port >> 16) % 256); |
178 | this.buffer[i++] = (byte)((this.region_port >> 24) % 256); | 195 | buffer[i++] = (byte) ((region_port >> 24) % 256); |
179 | 196 | ||
180 | return this.buffer; | 197 | return buffer; |
181 | } | 198 | } |
182 | } | 199 | } |
183 | 200 | ||
184 | public class TcpClient { | 201 | public class TcpClient |
185 | 202 | { | |
186 | public static int internalPacketHeaderSize = 4*4 + 16*1; | 203 | public static int internalPacketHeaderSize = 4 * 4 + 16 * 1; |
204 | private Socket mConnection; | ||
187 | 205 | ||
188 | private string mHostname; | 206 | private string mHostname; |
189 | private int mPort; | 207 | private int mPort; |
190 | private Socket mConnection; | 208 | |
191 | public TcpClient(string hostname, int port) { | 209 | public TcpClient(string hostname, int port) |
192 | this.mHostname = hostname; | 210 | { |
193 | this.mPort = port; | 211 | mHostname = hostname; |
194 | this.mConnection = null; | 212 | mPort = port; |
213 | mConnection = null; | ||
195 | } | 214 | } |
196 | public void connect() { | 215 | |
197 | this.mConnection = AsynchronousClient.StartClient(mHostname, mPort); | 216 | public void connect() |
217 | { | ||
218 | mConnection = AsynchronousClient.StartClient(mHostname, mPort); | ||
198 | } | 219 | } |
220 | |||
199 | /* | 221 | /* |
200 | public void recevie() { | 222 | public void recevie() { |
201 | if (mConnection == null) { | 223 | if (mConnection == null) { |
@@ -212,17 +234,18 @@ namespace OpenSim.ApplicationPlugins.LoadBalancer { | |||
212 | } | 234 | } |
213 | } | 235 | } |
214 | */ | 236 | */ |
215 | public void send(InternalPacketHeader header, byte[] packet) { | ||
216 | 237 | ||
238 | public void send(InternalPacketHeader header, byte[] packet) | ||
239 | { | ||
217 | lock (this) | 240 | lock (this) |
218 | { | 241 | { |
219 | 242 | if (mConnection == null) | |
220 | if (mConnection == null) { | 243 | { |
221 | // throw new Exception("client not initialized"); | 244 | // throw new Exception("client not initialized"); |
222 | connect(); | 245 | connect(); |
223 | } | 246 | } |
224 | 247 | ||
225 | AsynchronousClient.Send(this.mConnection, header.ToBytes()); | 248 | AsynchronousClient.Send(mConnection, header.ToBytes()); |
226 | 249 | ||
227 | /* | 250 | /* |
228 | for (int i = 0; i < 10; i++) | 251 | for (int i = 0; i < 10; i++) |
@@ -231,8 +254,8 @@ for (int i = 0; i < 10; i++) | |||
231 | } | 254 | } |
232 | Console.WriteLine(""); | 255 | Console.WriteLine(""); |
233 | */ | 256 | */ |
234 | AsynchronousClient.Send(this.mConnection, packet); | 257 | AsynchronousClient.Send(mConnection, packet); |
235 | } | 258 | } |
236 | } | 259 | } |
237 | } | 260 | } |
238 | } | 261 | } \ No newline at end of file |