79 lines
2.7 KiB
C#
79 lines
2.7 KiB
C#
using BCG.Utilities.General;
|
|
|
|
using System;
|
|
|
|
namespace BCG {
|
|
public class TCPConnectionTestStream : ViewStream, IWorker, ITCPConnectionHandler, ITCPMessageHandler {
|
|
class State {
|
|
public int n_sends = 0;
|
|
public int n_recvs = 0;
|
|
}
|
|
private readonly string _ip;
|
|
private readonly int _port;
|
|
private readonly ViewStreamDatabase _vsd;
|
|
State _state = new State();
|
|
private readonly int _msg_size;
|
|
|
|
private IBlueTableSchema _schema;
|
|
private TimeSpan _send_period;
|
|
private ITCPConnection _connection = null;
|
|
private byte[] _buff;
|
|
|
|
public TCPConnectionTestStream(string ip, int port, int msg_size, int send_period_seconds) :
|
|
this(ip, port, msg_size, send_period_seconds, ViewStreamDatabase.instance) { }
|
|
public TCPConnectionTestStream(string ip, int port, int msg_size, int send_period_seconds, ViewStreamDatabase vsd) {
|
|
_ip = ip;
|
|
_port = port;
|
|
_msg_size = msg_size;
|
|
_vsd = vsd;
|
|
_schema = BlueDataUtility.form_schema("TCPClient",
|
|
Tuple.Create("id", TypeCode.Int32),
|
|
Tuple.Create("n_sends", TypeCode.UInt32),
|
|
Tuple.Create("n_recvs", TypeCode.UInt32));
|
|
_send_period = TimeSpan.FromSeconds(send_period_seconds);
|
|
_buff = new byte[msg_size];
|
|
for (int i = 0; i < _buff.Length; i++) {
|
|
_buff[i] = (byte)i;
|
|
}
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber() {
|
|
MasterWorker.getInstance(false).addWorker(this, _send_period);
|
|
var server_address = new BlutoSubAddress(_ip, _port);
|
|
bts.AsyncTCPConnector_APM.connect_to(server_address, this);
|
|
}
|
|
|
|
protected override void myOnLastSubscriber() {
|
|
MasterWorker.getInstance(false).removeWorker(this);
|
|
}
|
|
|
|
public void doWork() {
|
|
if (_connection != null) {
|
|
_connection.send(_buff);
|
|
_state.n_sends++;
|
|
}
|
|
IBlueRow r = new BlueRow(_schema, 0, _state.n_sends, _state.n_recvs);
|
|
BlueTable rows = new BlueTable(r.schema);
|
|
rows.addRow(r);
|
|
var dp = BlueDataUtility.build_dp_inc(rows, myLastValue as DataPacket, true);
|
|
myFireEvent(dp);
|
|
}
|
|
|
|
public void onConnection(ITCPConnection connection) {
|
|
_connection = connection;
|
|
_connection.register(this);
|
|
}
|
|
public bool isDisposed { get; }
|
|
|
|
public void onData(RemoteMessage msg) {
|
|
bts.Log.Gen.info($"TCPConnectionTestStream received msg: {msg}");
|
|
_state.n_recvs++;
|
|
}
|
|
|
|
public void Dispose() {
|
|
|
|
}
|
|
|
|
|
|
}
|
|
} |