console_client.vala 3.0 KB

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