console_client.vala 2.7 KB

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