TCPClient.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.Collections.Generic;
  28. using System.Diagnostics;
  29. using System.Net;
  30. using System.Net.Sockets;
  31. namespace OpenSim.Region.ScriptEngine.Common.TRPC
  32. {
  33. public class TCPClient : TCPCommon.ClientInterface
  34. {
  35. public TCPClient()
  36. {
  37. }
  38. private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>();
  39. private int ClientCount = 0;
  40. public event TCPCommon.ClientConnectedDelegate ClientConnected;
  41. public event TCPCommon.DataReceivedDelegate DataReceived;
  42. public event TCPCommon.DataSentDelegate DataSent;
  43. public event TCPCommon.CloseDelegate Close;
  44. public event TCPCommon.ConnectErrorDelegate ConnectError;
  45. /// <summary>
  46. /// Creates client connection
  47. /// </summary>
  48. public void Connect(string RemoteHost, int RemotePort)
  49. {
  50. Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  51. IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort);
  52. //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock);
  53. newsock.Connect(ipe);
  54. }
  55. public int ConnectAndReturnID(string RemoteHost, int RemotePort)
  56. {
  57. Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  58. IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(RemoteHost), RemotePort);
  59. //newsock.BeginConnect(ipe, new AsyncCallback(asyncConnected), newsock);
  60. newsock.Connect(ipe);
  61. return ProcessConnection(newsock);
  62. }
  63. public void Disconnect(int ID)
  64. {
  65. Clients[ID].Disconnect();
  66. }
  67. // TODO: unused
  68. // void asyncConnected(IAsyncResult iar)
  69. // {
  70. // Socket client = (Socket)iar.AsyncState;
  71. // client.EndConnect(iar);
  72. // ProcessConnection(client);
  73. // }
  74. private int ProcessConnection(Socket client)
  75. {
  76. try
  77. {
  78. int id = ClientCount++;
  79. TCPSocket S = new TCPSocket(id, client);
  80. // Add to dictionary
  81. Clients.Add(id, S);
  82. // Add event handlers
  83. S.Close += new TCPSocket.CloseDelegate(S_Close);
  84. S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived);
  85. S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent);
  86. // Start it
  87. S.Start();
  88. Debug.WriteLine("Connection established: " + client.RemoteEndPoint.ToString());
  89. // Fire Connected-event
  90. if (ClientConnected != null)
  91. ClientConnected(id, client.RemoteEndPoint);
  92. return id;
  93. }
  94. catch (SocketException sex)
  95. {
  96. if (ConnectError != null)
  97. ConnectError(sex.Message);
  98. }
  99. return -1;
  100. }
  101. void S_DataSent(int ID, int length)
  102. {
  103. if (DataSent != null)
  104. DataSent(ID, length);
  105. }
  106. void S_DataReceived(int ID, byte[] data, int offset, int length)
  107. {
  108. if (DataReceived != null)
  109. DataReceived(ID, data, offset, length);
  110. }
  111. void S_Close(int ID)
  112. {
  113. if (Close != null)
  114. Close(ID);
  115. Clients.Remove(ID);
  116. }
  117. public void Send(int clientID, byte[] data, int offset, int len)
  118. {
  119. Clients[clientID].Send(clientID, data, offset, len);
  120. }
  121. }
  122. }