ConsoleClient.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Collections;
  6. namespace Crown
  7. {
  8. namespace Core
  9. {
  10. public delegate void MessageReceived(object o, MessageReceivedArgs args);
  11. public class MessageReceivedArgs : EventArgs
  12. {
  13. public readonly string Json;
  14. public MessageReceivedArgs(string json)
  15. {
  16. Json = json;
  17. }
  18. }
  19. public delegate void Connected(object o, ConnectedArgs args);
  20. public class ConnectedArgs : EventArgs
  21. {
  22. public readonly string Address;
  23. public readonly int Port;
  24. public ConnectedArgs(string address, int port)
  25. {
  26. Address = address;
  27. Port = port;
  28. }
  29. }
  30. public delegate void Disconnected(object o, DisconnectedArgs args);
  31. public class DisconnectedArgs : EventArgs
  32. {
  33. public DisconnectedArgs()
  34. {
  35. }
  36. }
  37. // Manages communication with engine executable.
  38. public class ConsoleClient
  39. {
  40. private Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  41. // Events
  42. public event Connected ConnectedEvent;
  43. public event Disconnected DisconnectedEvent;
  44. public event MessageReceived MessageReceivedEvent;
  45. public void Connect(string address, int port)
  46. {
  47. try {
  48. Client.BeginConnect(new IPEndPoint(IPAddress.Parse(address), port), new AsyncCallback(OnConnected), Client);
  49. }
  50. catch (Exception e) {
  51. Console.WriteLine(e.ToString());
  52. }
  53. }
  54. public void Close()
  55. {
  56. try {
  57. if (Client.Connected) {
  58. Client.Shutdown(SocketShutdown.Both);
  59. Client.Close();
  60. EmitDisconnected();
  61. }
  62. }
  63. catch (Exception e) {
  64. Console.WriteLine(e.ToString());
  65. }
  66. }
  67. private void OnConnected(IAsyncResult ar)
  68. {
  69. try {
  70. Socket client = (Socket)ar.AsyncState;
  71. client.EndConnect(ar);
  72. IPEndPoint ep = client.RemoteEndPoint as IPEndPoint;
  73. EmitConnected(ep.Address.ToString(), ep.Port);
  74. Receive(client);
  75. }
  76. catch (Exception e) {
  77. Console.WriteLine(e.ToString());
  78. }
  79. }
  80. // Sends the JSON-encoded data to the target
  81. public void Send(string json)
  82. {
  83. try {
  84. Client.Send(BitConverter.GetBytes(json.Length));
  85. Client.Send(Encoding.ASCII.GetBytes(json));
  86. } catch (Exception e) {
  87. Console.WriteLine(e.ToString());
  88. }
  89. }
  90. // Sends the lua script to the target
  91. public void SendScript(string lua)
  92. {
  93. lua = lua.Replace("\"", "\\\"");
  94. string json = "{\"type\":\"script\",\"script\":\"" + lua + "\"}";
  95. Send(json);
  96. }
  97. private class StateObject
  98. {
  99. public Socket Client;
  100. public byte[] msgHeader = new byte[4];
  101. public byte[] buf = new byte[4096];
  102. }
  103. private void Receive(Socket client)
  104. {
  105. try {
  106. StateObject state = new StateObject();
  107. state.Client = client;
  108. Client.BeginReceive(state.msgHeader, 0, 4, SocketFlags.None, new AsyncCallback(OnReceive), state);
  109. }
  110. catch (Exception e) {
  111. Console.WriteLine(e.ToString());
  112. }
  113. }
  114. private void OnReceive(IAsyncResult ar)
  115. {
  116. try {
  117. StateObject state = (StateObject)ar.AsyncState;
  118. Socket client = state.Client;
  119. int bytesRead = client.EndReceive(ar);
  120. if(bytesRead > 0)
  121. {
  122. Int32 size = BitConverter.ToInt32(state.msgHeader, 0);
  123. int rr = client.Receive(state.buf, size, SocketFlags.None);
  124. string json = Encoding.ASCII.GetString(state.buf, 0, rr);
  125. EmitMessageReceived(json);
  126. Receive(client);
  127. }
  128. else
  129. {
  130. Close();
  131. }
  132. }
  133. catch(Exception e) {
  134. Console.WriteLine(e.ToString());
  135. }
  136. }
  137. public void EmitDisconnected()
  138. {
  139. if (DisconnectedEvent != null)
  140. DisconnectedEvent(this, new DisconnectedArgs());
  141. }
  142. public void EmitMessageReceived(string json)
  143. {
  144. if (MessageReceivedEvent != null)
  145. MessageReceivedEvent(this, new MessageReceivedArgs(json));
  146. }
  147. public void EmitConnected(string address, int port)
  148. {
  149. if (ConnectedEvent != null)
  150. ConnectedEvent(this, new ConnectedArgs(address, port));
  151. }
  152. }
  153. } // namespace Core
  154. } // namespace Crown