using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; //using BCG.Utilities; //using BCG.Utilities.General; using System.Threading; using mbts; using bts; using System.Linq; using System.Reflection.Metadata.Ecma335; using Bts.PubSub; using Microsoft.Win32; using System.ComponentModel; namespace BCG { /// /// Keeps an ITCPConnection for talking to the server. It also monitors heartbeats /// from the server, and in case of missing hbs, informs its clients, and tries to /// restablish the connection. /// Threads : /// its assumed that IPubSubServerConnection functions (including doWork) will /// be called from the same thread. ITCPMessageHandler.onData and /// ITCPConnectionHandler.onConnection are called on an arbitrary thread. /// _conn_callback is called from within doWork, and _data_callback is called grom // an arbitrary data thread /// public class PubSubServerManagedTCPConnection : IPubSubServerConnection,// external facing interface to communicate msgs to Pubsubserver ITCPMessageHandler, // to get messages from ITCPCOnnection ITCPConnectionHandler { // to get connections from AsyncTCPConnector_APM private ITCPConnection? _connection; private readonly RemoteConnectionInfo _server_rc_info; private readonly bool _is_sub; // if _data_callback is not subscribed to , the user of this class can poll updates, // in which case this queue will be used private SyncQueue _in_data_queue = new SyncQueue(); private DateTime _last_hb = DateTime.Now; private PubSubServerConnectionStatusChange? _conn_callback; private PubSubServerDataDelegate? _data_callback; private readonly TimeSpan _connection_drop_threshold = PubSubConfig.Instance.ConnectionDropThreshold(); private ThreadSafeQueue _conn_cb_queue = new ThreadSafeQueue(); // the callbacks for connection, comes from the ITCPConnection, when a connection is broken, // and from AsyncTCPConnector whne a connection is established. ALl that info is assembled here and // enqueued, deuqued oin the doWork and acted on private class ConnectionStatusChange { public enum Status { CONNECTED, DISCONNECTED } public readonly Status connection_satus; public readonly ITCPConnection? new_connection; public ConnectionStatusChange(Status connection_satus, ITCPConnection? new_connection) { this.connection_satus = connection_satus; this.new_connection = new_connection; } public override string ToString() { return connection_satus.ToString(); } } public PubSubServerManagedTCPConnection(RemoteConnectionInfo server_address, bool is_sub) { Log.Gen.info("PubSubServerManagedTCPConnection.cons server_address: {server_address} is_sub: {is_sub}"); _server_rc_info = server_address; _is_sub = is_sub; // this is where it tries to connect to the pubsub server, and gets a callback on onConnection _connect_to_ps_server(); } private void _connect_to_ps_server() { bts.Log.Gen.info($"PubSubServerManagedTCPConnection._connect_to_ps_server server _server_rc_info: {_server_rc_info} is_sub: {_is_sub}"); // onConnection will be called on an arbitrary thread AsyncTCPConnector_AsyncWrapper.connect_to(_server_rc_info, this); } #region IPubSubServerConnection Members private void _send(byte[] data) { bts.Log.Gen.info($"PubSubServerManagedTCPConnection._send client: {client_url} server: {_server_rc_info} data: {data.Length} is_sub: {_is_sub} _connection : {_connection != null}"); if (_connection != null) { _connection.send(data); bts.Log.Gen.info("PubSubServerManagedTCPConnection.send, data is sent, server: " + _server_rc_info + "data: " + data.Length); } else { bts.Log.Gen.warn($"PubSubServerManagedTCPConnection._send, could not send data, server: {_server_rc_info} data: {data.Length} is_sub: {_is_sub} _connection : {_connection != null} "); } } public void send(bts.PubSubClientMessage ps_msg) { bts.Log.Gen.info($"PubSubServerManagedTCPConnection.send server: {_server_rc_info} ps_msg: {ps_msg}"); // whys it constructed again? var msg_bytes = ps_msg.serialize_into_bytes(); //bts.Log.Gen.info($"PubSubServerManagedTCPConnection.send server: {_server_rc_info} msg_retrv: {msg_retrv}"); _send(msg_bytes); } public IEnumerable get_updates(TimeSpan timeout, int maxElements) { List rv_raw = _in_data_queue.dequeueMultipleElements(Convert.ToInt32(timeout.TotalMilliseconds), maxElements); List rv = new List(); var i = 0; foreach (var b in rv_raw) { PubSubServerMessage ps_msg = new PubSubServerMessage(); ps_msg.construct_deserialize(b); bts.Log.Gen.info($"PubSubServerManagedTCPConnection.get_updates downloaded: {ps_msg} msg_no: {i} / {rv_raw.Count}"); rv.Add(ps_msg); i++; } return rv; } public string server_url { get { return _server_rc_info.ToString(); } } public string client_url { get { if (_connection == null) return RemoteConnectionInfo.null_instance.ToString(); return _connection.get_self_address().ToString(); } } public void register_connection_callback(PubSubServerConnectionStatusChange del) { _conn_callback = del; } public void register_data_callback(PubSubServerDataDelegate del) { _data_callback = del; } #endregion #region IDisposable Members private bool _is_disposed = false; public void Dispose() { if (isDisposed) return; bts.Log.Gen.info("PubSubServerManagedTCPConnection.Dispose"); if (_connection != null) { _connection.Dispose(); } _is_disposed = true; } public bool isDisposed { get { return _is_disposed; } } #endregion #region ITCPMessageHandler Members /// /// This method is executed on an arbitrary CLR thread. /// /// public void onData(RemoteMessage message) { bts.Log.Gen.info($"PubSubServerManagedTCPConnection.onData connection: {_connection} message: {message}"); // TODO check for disconenction switch (message.messageType) { case RemoteMessageType.Data: ArgumentNullException.ThrowIfNull(message.message); if (_data_callback != null) { _data_callback(message.remoteConnectionInfo.ToString(), message.message); } else { _in_data_queue.enqueue(message.message); } break; case RemoteMessageType.Heartbeat: _last_hb = DateTime.Now; break; case RemoteMessageType.Connection: // since this is called from the connection, this condition will never hit, ssince //new connections are detected only by AsyncTCPConnector Log.assert(false); break; case RemoteMessageType.Disconnection: bts.Log.Gen.info($"PubSubServerManagedTCPConnection.onData case RemoteMessageType.Disconnection: message: {message}"); _conn_cb_queue.enqueue(new ConnectionStatusChange(ConnectionStatusChange.Status.DISCONNECTED, null)); break; } } #endregion #region ITCPConnectionHandler Members /// will be called on on an arbitrary .Net thread public void onConnection(ITCPConnection connection) { var server_rci = connection.get_peer_address(); bts.Log.Gen.info($"PubSubServerManagedTCPConnection.onConnection connection: {_connection_txt(connection)}"); if (_server_rc_info.Equals(server_rci)) { _conn_cb_queue.enqueue(new ConnectionStatusChange(ConnectionStatusChange.Status.CONNECTED, connection)); // _last_hb = DateTime.Now; // _new_connection = connection; } } #endregion private static string _connection_txt(ITCPConnection conn) { if (conn != null) { return $"conn_type: {conn.GetType()} server: {conn.get_peer_address()} self: {conn.get_self_address()}"; } return "NULL"; } #region IWorker Members /// /// should be called on the same thread as the other user functions. /// the design only allows for onConnection to be on a different /// thread. /// public void doWork() { //_dispose_and_reconnect_if_missing_hb(); if (_conn_cb_queue.getCount() > 0) { var css = _conn_cb_queue.dequeueAll(); foreach (var c in css) { Log.notnull(c); Log.Gen.info($"PubSubServerManagedTCPConnection.doWork c: {c}"); switch (c.connection_satus) { case ConnectionStatusChange.Status.CONNECTED: { Log.notnull(c.new_connection); c.new_connection.register(this); _connection = c.new_connection; _last_hb = DateTime.Now; _conn_callback?.Invoke(_server_rc_info.ToString(), true); break; } case ConnectionStatusChange.Status.DISCONNECTED: { Log.notnull(_connection); _connection.Dispose(); _connect_to_ps_server(); _conn_callback?.Invoke(_server_rc_info.ToString(), false); break; } } } } } #endregion } }