console_client.vala 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE-GPLv2
  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. _connection.close();
  40. _connection = null;
  41. disconnected();
  42. }
  43. catch (Error e)
  44. {
  45. stderr.printf("%s\n", e.message);
  46. }
  47. }
  48. public bool is_connected()
  49. {
  50. return _connection != null && _connection.is_connected();
  51. }
  52. // Sends the JSON-encoded data to the target
  53. public void send(string json)
  54. {
  55. if (_connection == null)
  56. return;
  57. try
  58. {
  59. // FIXME: Add bit conversion utils
  60. uint32 len = json.length;
  61. uint8* ptr = (uint8*)(&len);
  62. var array = new uint8[4];
  63. for (var i = 0; i < 4; ++i)
  64. array[i] = ptr[i];
  65. _connection.output_stream.write(array);
  66. _connection.output_stream.write(json.data);
  67. }
  68. catch (Error e)
  69. {
  70. stderr.printf("%s\n", e.message);
  71. }
  72. }
  73. // Sends the lua script to the target
  74. public void send_script(string lua)
  75. {
  76. send("{\"type\":\"script\",\"script\":\"" + lua.replace("\\", "\\\\").replace("\"", "\\\"") + "\"}");
  77. }
  78. public void receive_async()
  79. {
  80. try
  81. {
  82. _connection.input_stream.read_bytes_async.begin(4, GLib.Priority.HIGH, null, on_read);
  83. }
  84. catch (Error e)
  85. {
  86. stderr.printf("%s\n", e.message);
  87. }
  88. }
  89. private void on_read(Object? obj, AsyncResult ar)
  90. {
  91. try
  92. {
  93. InputStream input_stream = (InputStream)obj;
  94. uint8[] header = input_stream.read_bytes_async.end(ar).get_data();
  95. // Connection closed
  96. if (header.length == 0)
  97. return;
  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. input_stream.read(data);
  106. message_received(this, data);
  107. }
  108. catch (Error e)
  109. {
  110. stderr.printf("%s\n", e.message);
  111. }
  112. }
  113. }
  114. } // namespace Crown