Nearly all warnings were due to nullability issues, since the concept of nullable reference types did not exist when the DataStream code was originally written. * PubSubClientMessage - Normalize indentation/whitespace - Fix some typos - Address nullability issues to fix build warnings - Modernize code * Compression related files - ZipEntry - ZipFile - ZipInputStream - TarHeader * RemoteMessage - make 'message' nullable to resolve warnings elsewhere (in calling code) * PubSubSingleTagStateEvent.cs * PubSubMessageReactor * PubSubState * SystemHeartbeatPublisher * SystemStatsPublisher * SingleTagData * ManagedTCPMessageHub * PubsubDeserializationController * PublisherCore * InflaterHuffmanTree * AsyncTCPConnection_APM * ThreadSafeQueue * ManagedSubscriber * SubscriberCore * PubSubServerManagedTCPConnection * AsyncTCPConnection_APM * SuperMapStream * MapStream * TradeBarsStream
290 lines
13 KiB
C#
290 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using BCG.Utilities.General;
|
|
using BCG.Utilities;
|
|
|
|
using System.Threading;
|
|
using System.IO;
|
|
using System.Diagnostics;
|
|
using mbts;
|
|
using System.Linq;
|
|
using System.Collections;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using bts;
|
|
|
|
namespace BCG {
|
|
|
|
// this is the main TCP message exchange in the pubsub server. It accepts connections, data, as well as send data
|
|
public class ManagedTCPMessageHub : IPubsubMessageHub, // external facing interface to be used by PubSubMesage Reactor
|
|
ITCPConnectionHandler, // interface to receive connections when calling ServerToClientManagedTCPConnectionAcceptor.accept_connections
|
|
ITCPMessageHandler { // interface to receive data from ITCPConnection
|
|
private readonly int _port;
|
|
private bool _is_disposed = false;
|
|
// class used internally to queue messages from one trhead ot another
|
|
private class ConnectionStatusInfo {
|
|
public readonly string client_url;
|
|
public readonly PubSubCallbacks.ConnectionStatus status;
|
|
public ConnectionStatusInfo(string client_url_, PubSubCallbacks.ConnectionStatus status_) {
|
|
client_url = client_url_;
|
|
status = status_;
|
|
}
|
|
}
|
|
private bool _activated = false;
|
|
private PubSubCallbacks.PubSubServerConnectionStatusChange? _connection_cb;
|
|
private PubSubServerDataDelegate? _data_cb;
|
|
|
|
/// <summary>
|
|
/// client to connection map
|
|
/// </summary>
|
|
private Dictionary<string, ITCPConnection> _connection_map = new Dictionary<string, ITCPConnection>();
|
|
/// <summary>
|
|
/// data that is received from the client (on the onData thread) is queued in this
|
|
/// to be fetched by the client of this class in the get_updates.
|
|
/// </summary>
|
|
private SyncQueue<Pair<string, byte[]>> _received_data_queue;
|
|
/// <summary>
|
|
/// data to be sent to the client is enqueued here and is fetched by the
|
|
/// myDoItAll thread to be sent to the clients. the send is not directly sent out
|
|
/// since there is some state management needed, so we do it on the main message
|
|
/// pump thread
|
|
/// </summary>
|
|
private SyncQueue<Pair<string, byte[]>> _send_data_queue = new SyncQueue<Pair<string, byte[]>>();
|
|
/// <summary>
|
|
/// connections that are found to be dead or are just connected
|
|
/// are queued in the following queues respectively, and are
|
|
/// picked up by the doWork thread.
|
|
/// </summary>
|
|
// _connection_cb arguments are queued here
|
|
private ThreadSafeQueue<ConnectionStatusInfo> _conn_callback_queue = new ThreadSafeQueue<ConnectionStatusInfo>();
|
|
/// <summary>
|
|
/// new connections received (on the onConnection thread) are queued
|
|
/// in this and will be fetched by the message_pump thread
|
|
/// </summary>
|
|
private ThreadSafeQueue<ITCPConnection> _new_connections = new ThreadSafeQueue<ITCPConnection>();
|
|
|
|
private readonly int _read_send_queue_timeout = 100;// just used to pass it on
|
|
private readonly TimeSpan _hb_send_interval;
|
|
private TimeSpan _subscriber_drop_threshold;
|
|
private TimeSpan _collect_disposed_connections_period = TimeSpan.FromMilliseconds(500);
|
|
private DateTime _last_HB_collection_time = DateTime.Now;
|
|
private DateTime _last_disposed_dead_collection_time = DateTime.Now;
|
|
// this si always active, accepting new client connections
|
|
private AsyncTCPConnectionAcceptor_AsyncWrapper _connection_acceptor;
|
|
|
|
// TODO, get
|
|
public ManagedTCPMessageHub(int port, TimeSpan hb_send_interval, int missing_HBs_for_gc, TimeSpan collect_disposed_connections_period) {
|
|
_port = port;
|
|
// what kind of connection_acceptor is controlled here
|
|
_connection_acceptor = new AsyncTCPConnectionAcceptor_AsyncWrapper(_port, this);
|
|
_hb_send_interval = hb_send_interval;
|
|
_subscriber_drop_threshold = new TimeSpan(0, 0, 0, 0, Convert.ToInt32(hb_send_interval.TotalMilliseconds * missing_HBs_for_gc));
|
|
_collect_disposed_connections_period = collect_disposed_connections_period;
|
|
_received_data_queue = new SyncQueue<Pair<string, byte[]>>();
|
|
}
|
|
|
|
#region IPubsubMessageHub Members
|
|
|
|
// this is unused right now, since the get_updates on a polling thread is called to by the client of this class to get messages
|
|
public void register_data_callback(PubSubServerDataDelegate del) {
|
|
bts.Log.assert_d(!_activated, "connection callbacks should be registered before the call to activate");
|
|
bts.Log.Gen.info("ManagedTCPMessageHub.register_data_callback called");
|
|
_data_cb = del;
|
|
}
|
|
|
|
public void register_connection_callback(PubSubCallbacks.PubSubServerConnectionStatusChange del) {
|
|
bts.Log.assert_d(!_activated, "data callbacks should be registered before the call to activate");
|
|
bts.Log.Gen.info("ManagedTCPMessageHub.register_connection_callback called");
|
|
_connection_cb = del;
|
|
}
|
|
|
|
public void activate() {
|
|
_connection_acceptor.start();
|
|
bts.Log.Gen.info($"ManagedTCPMessageHub.activate TCP Component is ready, listening on port {_port} for incoming connections");
|
|
_activated = true;
|
|
// as the name says, all messages hand-off happen here
|
|
Thread do_it_all_thread = new Thread(new ThreadStart(_main_message_pump));
|
|
do_it_all_thread.IsBackground = true;
|
|
do_it_all_thread.Priority = ThreadPriority.Highest;
|
|
do_it_all_thread.Start();
|
|
}
|
|
|
|
// retutns the incoming either when timeout expires or n_elems are dequeued
|
|
public List<Pair<string, byte[]>> get_updates(TimeSpan timeout, int max_n_elems) {
|
|
return _received_data_queue.dequeueMultipleElements(timeout, max_n_elems);
|
|
}
|
|
|
|
// similiar to above, but returns all the elements that have been received since the last time this function was called
|
|
public List<Pair<string, byte[]>> get_all_updates() {
|
|
return _received_data_queue.dequeue_all();
|
|
}
|
|
|
|
// private bool _enqueue_send(string receiver, byte[] data) {
|
|
// bts.Log.Gen.info($"ManagedTCPMessageHub._enqueue_send receiver: {receiver} data len: {data.Length}");
|
|
// _send_data_queue.enqueue(new Pair<string, byte[]>(receiver, data));
|
|
// return true;
|
|
// }
|
|
|
|
public bool send(string receiver, bts.PubSubServerMessage ps_msg) {
|
|
var ps_msg_bytes = ps_msg.serialize_into_bytes();
|
|
bts.Log.Gen.info($"ManagedTCPMessageHub.send receiver: {receiver} ps_msg: {ps_msg} serialized msg sz: {ps_msg_bytes.Length}");
|
|
_send_data_queue.enqueue(new Pair<string, byte[]>(receiver, ps_msg_bytes));
|
|
return true;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IWorker Members
|
|
|
|
/// <summary>
|
|
/// Reports back connected and disconnected connections
|
|
/// via the ConnectionStatusChange callback
|
|
/// </summary>
|
|
public void doWork() {
|
|
if (_connection_cb != null) {
|
|
var conns = _conn_callback_queue.dequeueAll();
|
|
foreach (var ci in conns) {
|
|
_connection_cb(ci.client_url, ci.status);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ITCPConnectionHandler Members
|
|
|
|
/// <summary>
|
|
/// This is the only place where
|
|
/// clients connect to the pubsubserver.
|
|
/// The connection in qneueued, so we can report it back in the same thread
|
|
/// </summary>
|
|
/// <param name="connection"></param>
|
|
public void onConnection(ITCPConnection connection) {
|
|
bts.Log.Gen.info($"ManagedTCPMessageHub.onConnection connection: {connection}");
|
|
_new_connections.enqueue(connection);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ITCPMessageHandler Members
|
|
// called by ITCPConnection on an arbitrary thread
|
|
public void onData(RemoteMessage message) {
|
|
bts.Log.Gen.info($"ManagedTCPMessgageHub.onData message: {message}");
|
|
var sender_rci = message.remoteConnectionInfo;
|
|
switch (message.messageType) {
|
|
case RemoteMessageType.Data:
|
|
// if callback is registered fire it right away, else enqueue it
|
|
if (_data_cb != null)
|
|
_data_cb(sender_rci.ToString(), message.message);
|
|
else
|
|
_received_data_queue.enqueue(new Pair<string, byte[]>(sender_rci.ToString(), message.message));
|
|
break;
|
|
case RemoteMessageType.Connection:
|
|
// this conditional will never hit, because onData is ITCPConnection's callback, and ITCPConnection
|
|
// will not report a connection, connections are reported through ConnectionAcceptor
|
|
break;
|
|
case RemoteMessageType.Disconnection:
|
|
// we ignore this here, since in the pump thread function, we poll the connection and GC it if its disconnected
|
|
break;
|
|
case RemoteMessageType.Heartbeat:
|
|
// we dont need this either, since the HB time can be queried from the conneciton and GC'ed there and then
|
|
break;
|
|
default:
|
|
bts.Log.assert_d(false, "Unknown message type");
|
|
break;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void _main_message_pump() {
|
|
while (true) {
|
|
try {
|
|
while (true) {
|
|
// these are the connections that were received on onConnection, written to _conn_callback_queue
|
|
_handle_new_connections();
|
|
///dead connections that are disposed, writtent to _conn_callback_queue
|
|
_collect_disposed_connections();
|
|
|
|
Pair<string, byte[]> p = _send_data_queue.dequeue(_read_send_queue_timeout);
|
|
|
|
// times["send"].start();
|
|
if (p != null) {
|
|
ArgumentNullException.ThrowIfNull(p.first);
|
|
ArgumentNullException.ThrowIfNull(p.second);
|
|
_connection_map.TryGetValue(p.first, out var conn);
|
|
if (conn != null) {
|
|
conn.send(p.second);
|
|
Log.Gen.info($"ManagedTCPMessageHub._main_message_pump sending data client: {p.first} size: {p.second.Length}");
|
|
} else {
|
|
string error_msg = $"sending data of sz: {p.second.Length} to {p.first} failed, this client might have " +
|
|
"disconnected, in which case it will be garbage collected within 60 secs";
|
|
Log.Gen.error(error_msg);
|
|
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception ex) {
|
|
//BlueEventLog.logEvent(ex);
|
|
if (ex is OutOfMemoryException)
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void _collect_disposed_connections() {
|
|
// since we are not listening to onData, connection status, this is used to collect connections
|
|
// that might be disposed
|
|
if (DateTime.Now - _last_disposed_dead_collection_time > _collect_disposed_connections_period) {
|
|
_last_disposed_dead_collection_time = DateTime.Now;
|
|
//var disposed_conns = _connection_map.Values.Where(c => c.isDisposed);
|
|
List<string> disposed_urls = new List<string>();
|
|
foreach (KeyValuePair<string, ITCPConnection> p in _connection_map) {
|
|
var url = p.Key;
|
|
var conn = p.Value;
|
|
if (conn.isDisposed)
|
|
disposed_urls.Add(url);
|
|
}
|
|
foreach (string url in disposed_urls) {
|
|
string dispose_msg = $"Collecting disposed connection: {url} connection count before deleting this: {_connection_map.Count}";
|
|
bts.Log.Gen.warn(dispose_msg);
|
|
_connection_map.Remove(url);
|
|
_conn_callback_queue.enqueue(new ConnectionStatusInfo(url, PubSubCallbacks.ConnectionStatus.DISCONNECTED));
|
|
}
|
|
}
|
|
}
|
|
|
|
// these are the connections that were added to _new_connections on onConnection
|
|
// (which gets called in another thread)
|
|
private void _handle_new_connections() {
|
|
var new_conns = _new_connections.dequeueAll();
|
|
foreach (ITCPConnection conn in new_conns) {
|
|
var peer_rci = conn.get_peer_address();
|
|
bts.Log.Gen.info($"ManagedTCPMessageHub._handle_new_connections conn: {conn}");
|
|
///to stop it from being garbabge collected immediatly
|
|
// ot get data
|
|
conn.register(this);
|
|
_connection_map.Add(peer_rci.ToString(), conn);
|
|
// enqueued here to call the user sub in doWork
|
|
// why not fire the handler here ?
|
|
_conn_callback_queue.enqueue(new ConnectionStatusInfo(peer_rci.ToString(), PubSubCallbacks.ConnectionStatus.CONNECTED));
|
|
}
|
|
}
|
|
|
|
#region IDisposable Memebers
|
|
public void Dispose() {
|
|
_is_disposed = true;
|
|
|
|
}
|
|
public bool isDisposed {
|
|
get { return _is_disposed; }
|
|
}
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|