1
0

TcpClient.cs 7.5 KB

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