console_client.vala 3.3 KB

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