Client.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.Net.Sockets;
  30. using System.Text;
  31. using System.Text.RegularExpressions;
  32. namespace OpenSim.GridLaunch.GUI.Network
  33. {
  34. internal class Client
  35. {
  36. public TcpClient tcpClient;
  37. private byte[] readBuffer = new byte[4096];
  38. private byte[] writeBuffer;
  39. private TCPD tcp;
  40. private string inputData = "";
  41. private object inputDataLock = new object();
  42. public Client(TCPD _tcp, TcpClient Client)
  43. {
  44. tcp = _tcp;
  45. tcpClient = Client;
  46. asyncReadStart();
  47. Write("OpenSim TCP Console GUI");
  48. Write("Use commands /0, /1, /2, etc to switch between applications.");
  49. Write("Type /list for list of applications.");
  50. Write("Anything that doesn't start with a / will be sent to selected application");
  51. Write("type /quit to exit");
  52. }
  53. private void asyncReadStart()
  54. {
  55. tcpClient.GetStream().BeginRead(readBuffer, 0, readBuffer.Length, asyncReadCallBack, null);
  56. }
  57. //private Regex LineExtractor = new Regex("^(.*)$")
  58. private void asyncReadCallBack(IAsyncResult ar)
  59. {
  60. try
  61. {
  62. // Read data
  63. int len = tcpClient.GetStream().EndRead(ar);
  64. // Send it to app
  65. string newData = System.Text.Encoding.ASCII.GetString(readBuffer, 0, len);
  66. //lock (inputDataLock)
  67. //{
  68. inputData += newData;
  69. if (newData.Contains("\n"))
  70. SendInputLines();
  71. //}
  72. // Start it again
  73. asyncReadStart();
  74. }
  75. catch
  76. {
  77. // TODO: Remove client when we get exception
  78. // Temp patch: if exception we don't call asyncReadStart()
  79. }
  80. }
  81. private void SendInputLines()
  82. {
  83. StringBuilder line = new StringBuilder();
  84. foreach (char c in inputData)
  85. {
  86. if (c == 13)
  87. continue;
  88. if (c == 10)
  89. {
  90. Program.WriteLine(tcp.currentApp, line.ToString());
  91. line = new StringBuilder();
  92. continue;
  93. }
  94. line.Append(c);
  95. }
  96. // We'll keep whatever is left over
  97. inputData = line.ToString();
  98. }
  99. public void Write(string Text)
  100. {
  101. writeBuffer = Encoding.ASCII.GetBytes(Text);
  102. tcpClient.GetStream().Write(writeBuffer, 0, writeBuffer.Length);
  103. }
  104. }
  105. }