OpenGridProtocol.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using Nwc.XmlRpc;
  9. using System.Collections;
  10. namespace OpenSim.Servers
  11. {
  12. public class OpenGridProtocolServer
  13. {
  14. private Thread m_workerThread;
  15. private Socket m_listenerSocket;
  16. private IPEndPoint m_IPendpoint;
  17. private int m_port;
  18. private ArrayList m_clients;
  19. private class ClientHandler
  20. {
  21. private Thread m_clientThread;
  22. private Socket m_socketHandle;
  23. public ClientHandler(Socket clientSocketHandle)
  24. {
  25. m_socketHandle = clientSocketHandle;
  26. m_clientThread = new Thread(new ThreadStart(DoWork));
  27. m_clientThread.IsBackground = true;
  28. m_clientThread.Start();
  29. }
  30. private void DoWork()
  31. {
  32. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("OpenGridProtocol.cs: ClientHandler.DoWork() - Got new client");
  33. this.WriteLine("OpenSim 0.1, running OGS protocol 1.0");
  34. }
  35. private void WriteLine(string theline)
  36. {
  37. theline += "\n";
  38. byte[] thelinebuffer = System.Text.Encoding.ASCII.GetBytes(theline.ToCharArray());
  39. m_socketHandle.Send(thelinebuffer, theline.Length, 0);
  40. }
  41. }
  42. public OpenGridProtocolServer(int port)
  43. {
  44. m_port = port;
  45. }
  46. public void Start()
  47. {
  48. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("OpenGridProtocol.cs: Start() - Opening server socket");
  49. m_clients = new ArrayList();
  50. m_workerThread = new Thread(new ThreadStart(StartServerSocket));
  51. m_workerThread.IsBackground = true;
  52. m_workerThread.Start();
  53. }
  54. private void StartServerSocket()
  55. {
  56. try
  57. {
  58. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("OpenGridProtocol.cs: StartServerSocket() - Spawned main thread OK");
  59. m_IPendpoint = new IPEndPoint(IPAddress.Any, m_port);
  60. m_listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  61. m_listenerSocket.Bind(m_IPendpoint);
  62. m_listenerSocket.Listen(4);
  63. Socket sockethandle;
  64. while (true)
  65. {
  66. sockethandle = m_listenerSocket.Accept();
  67. lock (m_clients.SyncRoot)
  68. {
  69. m_clients.Add(new OpenGridProtocolServer.ClientHandler(sockethandle));
  70. }
  71. }
  72. }
  73. catch (Exception e)
  74. {
  75. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(e.Message);
  76. }
  77. }
  78. }
  79. }