console_client.vala 3.0 KB

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