console_client.vala 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. namespace Crown
  6. {
  7. /// Manages communication with engine executable.
  8. public class ConsoleClient : GLib.Object
  9. {
  10. private SocketConnection _connection;
  11. // Signals
  12. public signal void connected(string address, int port);
  13. public signal void disconnected();
  14. public signal void message_received(ConsoleClient client, uint8[] json);
  15. public ConsoleClient()
  16. {
  17. _connection = null;
  18. }
  19. public new void connect(string address, int port)
  20. {
  21. try
  22. {
  23. GLib.SocketClient client = new GLib.SocketClient();
  24. _connection = client.connect(new InetSocketAddress.from_string(address, port), null);
  25. if (_connection != null)
  26. connected(address, port);
  27. }
  28. catch (Error e)
  29. {
  30. stderr.printf("%s\n", e.message);
  31. }
  32. }
  33. public void close()
  34. {
  35. try
  36. {
  37. if (_connection != null)
  38. {
  39. _connection.close();
  40. _connection = null;
  41. disconnected();
  42. }
  43. }
  44. catch (Error e)
  45. {
  46. stderr.printf("%s\n", e.message);
  47. }
  48. }
  49. public bool is_connected()
  50. {
  51. return _connection != null && _connection.is_connected();
  52. }
  53. // Sends the JSON-encoded data to the target
  54. public void send(string json)
  55. {
  56. if (_connection == null)
  57. return;
  58. try
  59. {
  60. // FIXME: Add bit conversion utils
  61. uint32 len = json.length;
  62. uint8* ptr = (uint8*)(&len);
  63. var array = new uint8[4];
  64. for (var i = 0; i < 4; ++i)
  65. array[i] = ptr[i];
  66. _connection.output_stream.write(array);
  67. _connection.output_stream.write(json.data);
  68. }
  69. catch (Error e)
  70. {
  71. stderr.printf("%s\n", e.message);
  72. }
  73. }
  74. // Sends the lua script to the target
  75. public void send_script(string lua)
  76. {
  77. send("{\"type\":\"repl\",\"repl\":\"" + lua.replace("\\", "\\\\").replace("\"", "\\\"") + "\"}");
  78. }
  79. public void receive_async()
  80. {
  81. _connection.input_stream.read_bytes_async.begin(4, GLib.Priority.DEFAULT, null, on_read);
  82. }
  83. private void on_read(Object? obj, AsyncResult ar)
  84. {
  85. try
  86. {
  87. InputStream input_stream = (InputStream)obj;
  88. uint8[] header = input_stream.read_bytes_async.end(ar).get_data();
  89. // Connection closed gracefully
  90. if (header.length == 0)
  91. {
  92. close();
  93. return;
  94. }
  95. assert(header.length > 0);
  96. // FIXME: Add bit conversion utils
  97. uint32 size = 0;
  98. size |= header[3] << 24;
  99. size |= header[2] << 16;
  100. size |= header[1] << 8;
  101. size |= header[0] << 0;
  102. uint8[] data = new uint8[size];
  103. size_t bytes_read = 0;
  104. if (input_stream.read_all(data, out bytes_read))
  105. message_received(this, data);
  106. }
  107. catch (Error e)
  108. {
  109. stderr.printf("%s\n", e.message);
  110. if (e.code == 44) // An existing connection was forcibly closed by the remote host.
  111. disconnected();
  112. }
  113. }
  114. }
  115. } // namespace Crown