console_client.vala 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. /// Manages communication with engine executable.
  8. public class ConsoleClient : GLib.Object
  9. {
  10. public 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. GLib.SocketClient client = new GLib.SocketClient();
  23. _connection = client.connect(new InetSocketAddress.from_string(address, port), null);
  24. if (_connection != null) {
  25. connected(address, port);
  26. receive_async();
  27. }
  28. } catch (Error e) {
  29. // Ignore
  30. }
  31. }
  32. public void close()
  33. {
  34. try {
  35. if (_connection != null) {
  36. _connection.close();
  37. _connection = null;
  38. disconnected();
  39. }
  40. } catch (Error e) {
  41. loge(e.message);
  42. }
  43. }
  44. public bool is_connected()
  45. {
  46. return _connection != null && _connection.is_connected();
  47. }
  48. // Sends the JSON-encoded data to the target
  49. public void send(string json)
  50. {
  51. if (!is_connected())
  52. return;
  53. try {
  54. // FIXME: Add bit conversion utils
  55. uint32 len = json.length;
  56. uint8* ptr = (uint8*)(&len);
  57. uint8 header[4];
  58. for (var i = 0; i < 4; ++i)
  59. header[i] = ptr[i];
  60. size_t bytes_read;
  61. _connection.output_stream.write_all(header, out bytes_read);
  62. _connection.output_stream.write_all(json.data, out bytes_read);
  63. } catch (Error e) {
  64. loge(e.message);
  65. }
  66. }
  67. // Sends the lua script to the target
  68. public void send_script(string lua)
  69. {
  70. send("{\"type\":\"repl\",\"repl\":\"\"\"%s\"\"\"}".printf(lua));
  71. }
  72. public void receive_async()
  73. {
  74. _connection.input_stream.read_bytes_async.begin(4, GLib.Priority.DEFAULT, null, on_read);
  75. }
  76. public void on_read(Object? obj, AsyncResult ar)
  77. {
  78. try {
  79. InputStream input_stream = (InputStream)obj;
  80. uint8[] header = input_stream.read_bytes_async.end(ar).get_data();
  81. // Connection closed gracefully
  82. if (header.length == 0) {
  83. close();
  84. return;
  85. }
  86. assert(header.length > 0);
  87. // FIXME: Add bit conversion utils
  88. uint32 size = 0;
  89. size |= header[3] << 24;
  90. size |= header[2] << 16;
  91. size |= header[1] << 8;
  92. size |= header[0] << 0;
  93. uint8[] data = new uint8[size];
  94. size_t bytes_read = 0;
  95. if (input_stream.read_all(data, out bytes_read)) {
  96. message_received(this, data);
  97. receive_async();
  98. }
  99. } catch (Error e) {
  100. if (e.code == 44) // An existing connection was forcibly closed by the remote host.
  101. close();
  102. }
  103. }
  104. }
  105. } // namespace Crown