1
0

TCPServer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. namespace OpenSim.Region.ScriptEngine.Common.TRPC
  33. {
  34. public class TCPServer: TCPCommon.ServerInterface
  35. {
  36. public readonly int LocalPort;
  37. public TCPServer(int localPort)
  38. {
  39. LocalPort = localPort;
  40. }
  41. private Socket server;
  42. /// <summary>
  43. /// Starts listening for new connections
  44. /// </summary>
  45. public void StartListen()
  46. {
  47. server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  48. IPEndPoint ipe = new IPEndPoint(IPAddress.Any, LocalPort);
  49. server.Bind(ipe);
  50. server.Listen(10);
  51. server.BeginAccept(new AsyncCallback(AsyncAcceptConnections), server);
  52. }
  53. /// <summary>
  54. /// Stops listening for new connections
  55. /// </summary>
  56. public void StopListen()
  57. {
  58. server.Close();
  59. server = null;
  60. }
  61. private readonly Dictionary<int, TCPSocket> Clients = new Dictionary<int, TCPSocket>();
  62. private int ClientCount = 0;
  63. public event TCPCommon.ClientConnectedDelegate ClientConnected;
  64. public event TCPCommon.DataReceivedDelegate DataReceived;
  65. public event TCPCommon.DataSentDelegate DataSent;
  66. public event TCPCommon.CloseDelegate Close;
  67. /// <summary>
  68. /// Async callback for new connections
  69. /// </summary>
  70. /// <param name="ar"></param>
  71. private void AsyncAcceptConnections(IAsyncResult ar)
  72. {
  73. int id = ClientCount++;
  74. Socket oldserver = (Socket)ar.AsyncState;
  75. Socket client = oldserver.EndAccept(ar);
  76. TCPSocket S = new TCPSocket(id, client);
  77. // Add to dictionary
  78. Clients.Add(id, S);
  79. // Add event handlers
  80. S.Close += new TCPSocket.CloseDelegate(S_Close);
  81. S.DataReceived += new TCPSocket.DataReceivedDelegate(S_DataReceived);
  82. S.DataSent += new TCPSocket.DataSentDelegate(S_DataSent);
  83. // Start it
  84. S.Start();
  85. Debug.WriteLine("Connection received: " + client.RemoteEndPoint.ToString());
  86. // Fire Connected-event
  87. if (ClientConnected != null)
  88. ClientConnected(id, client.RemoteEndPoint);
  89. }
  90. void S_DataSent(int ID, int length)
  91. {
  92. if (DataSent != null)
  93. DataSent(ID, length);
  94. }
  95. void S_DataReceived(int ID, byte[] data, int offset, int length)
  96. {
  97. if (DataReceived != null)
  98. DataReceived(ID, data, offset, length);
  99. }
  100. void S_Close(int ID)
  101. {
  102. if (Close != null)
  103. Close(ID);
  104. Clients.Remove(ID);
  105. }
  106. public void Send(int clientID, byte[] data, int offset, int len)
  107. {
  108. Clients[clientID].Send(clientID, data, offset, len);
  109. }
  110. }
  111. }