| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- /*
- * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
- * License: https://github.com/dbartolini/crown/blob/master/LICENSE
- */
- using GLib;
- namespace Crown
- {
- /// Manages communication with engine executable.
- public class ConsoleClient : GLib.Object
- {
- private SocketClient _socket;
- private SocketConnection _connection;
- // Signals
- public signal void connected();
- public signal void disconnected();
- public signal void message_received(ConsoleClient client, uint8[] json);
- public ConsoleClient()
- {
- _socket = new SocketClient();
- _connection = null;
- }
- public new void connect(string address, int port)
- {
- try
- {
- _connection = _socket.connect(new InetSocketAddress.from_string(address, port), null);
- if (_connection != null)
- connected();
- }
- catch (Error e)
- {
- stderr.printf("%s\n", e.message);
- }
- }
- public void close()
- {
- try
- {
- if (_connection != null)
- {
- _connection.close();
- _connection = null;
- disconnected();
- }
- }
- catch (Error e)
- {
- stderr.printf("%s\n", e.message);
- }
- }
- public bool is_connected()
- {
- return _connection != null && _connection.is_connected();
- }
- // Sends the JSON-encoded data to the target
- public void send(string json)
- {
- if (_connection == null)
- return;
- try
- {
- // FIXME: Add bit conversion utils
- uint32 len = json.length;
- uint8* ptr = (uint8*)(&len);
- var array = new uint8[4];
- for (var i = 0; i < 4; ++i)
- array[i] = ptr[i];
- _connection.output_stream.write(array);
- _connection.output_stream.write(json.data);
- }
- catch (Error e)
- {
- stderr.printf("%s\n", e.message);
- }
- }
- // Sends the lua script to the target
- public void send_script(string lua)
- {
- send("{\"type\":\"script\",\"script\":\"" + lua.replace("\\", "\\\\").replace("\"", "\\\"") + "\"}");
- }
- public void receive_async()
- {
- try
- {
- _connection.input_stream.read_bytes_async.begin(4, GLib.Priority.HIGH, null, on_read);
- }
- catch (Error e)
- {
- stderr.printf("%s\n", e.message);
- }
- }
- private void on_read(Object? obj, AsyncResult ar)
- {
- try
- {
- InputStream input_stream = (InputStream)obj;
- uint8[] header = input_stream.read_bytes_async.end(ar).get_data();
- // Connection closed
- if (header.length == 0)
- {
- close();
- return;
- }
- // FIXME: Add bit conversion utils
- uint32 size = 0;
- size |= header[3] << 24;
- size |= header[2] << 16;
- size |= header[1] << 8;
- size |= header[0] << 0;
- uint8[] data = new uint8[size];
- input_stream.read(data);
- message_received(this, data);
- }
- catch (Error e)
- {
- stderr.printf("%s\n", e.message);
- }
- }
- }
- } // namespace Crown
|