console_client.vala 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  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 SocketClient _socket;
  11. private SocketConnection _connection;
  12. // Signals
  13. public signal void connected(string address, int port);
  14. public signal void disconnected();
  15. public signal void message_received(ConsoleClient client, uint8[] json);
  16. public ConsoleClient()
  17. {
  18. _socket = new SocketClient();
  19. _connection = null;
  20. }
  21. public new void connect(string address, int port)
  22. {
  23. try
  24. {
  25. _connection = _socket.connect(new InetSocketAddress.from_string(address, port), null);
  26. if (_connection != null)
  27. connected(address, port);
  28. }
  29. catch (Error e)
  30. {
  31. stderr.printf("%s\n", e.message);
  32. }
  33. }
  34. public void close()
  35. {
  36. try
  37. {
  38. if (_connection != null)
  39. {
  40. _connection.close();
  41. _connection = null;
  42. disconnected();
  43. }
  44. }
  45. catch (Error e)
  46. {
  47. stderr.printf("%s\n", e.message);
  48. }
  49. }
  50. public bool is_connected()
  51. {
  52. return _connection != null && _connection.is_connected();
  53. }
  54. // Sends the JSON-encoded data to the target
  55. public void send(string json)
  56. {
  57. if (_connection == null)
  58. return;
  59. try
  60. {
  61. // FIXME: Add bit conversion utils
  62. uint32 len = json.length;
  63. uint8* ptr = (uint8*)(&len);
  64. var array = new uint8[4];
  65. for (var i = 0; i < 4; ++i)
  66. array[i] = ptr[i];
  67. _connection.output_stream.write(array);
  68. _connection.output_stream.write(json.data);
  69. }
  70. catch (Error e)
  71. {
  72. stderr.printf("%s\n", e.message);
  73. }
  74. }
  75. // Sends the lua script to the target
  76. public void send_script(string lua)
  77. {
  78. send("{\"type\":\"repl\",\"repl\":\"" + lua.replace("\\", "\\\\").replace("\"", "\\\"") + "\"}");
  79. }
  80. public void receive_async()
  81. {
  82. _connection.input_stream.read_bytes_async.begin(4, GLib.Priority.DEFAULT, null, on_read);
  83. }
  84. private void on_read(Object? obj, AsyncResult ar)
  85. {
  86. try
  87. {
  88. InputStream input_stream = (InputStream)obj;
  89. uint8[] header = input_stream.read_bytes_async.end(ar).get_data();
  90. // Connection closed
  91. if (header.length == 0)
  92. {
  93. close();
  94. return;
  95. }
  96. // FIXME: Add bit conversion utils
  97. uint32 size = 0;
  98. size |= header[3] << 24;
  99. size |= header[2] << 16;
  100. size |= header[1] << 8;
  101. size |= header[0] << 0;
  102. uint8[] data = new uint8[size];
  103. size_t bytes_read = 0;
  104. if (input_stream.read_all(data, out bytes_read))
  105. message_received(this, data);
  106. }
  107. catch (Error e)
  108. {
  109. stderr.printf("%s\n", e.message);
  110. }
  111. }
  112. }
  113. } // namespace Crown