console_client.vala 2.6 KB

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