104 lines
3.4 KiB
C#
104 lines
3.4 KiB
C#
using System;
|
|
using System.Linq.Expressions;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
|
|
using Spark.Core.Reactive;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using BCG;
|
|
using bts;
|
|
using System.Transactions;
|
|
|
|
namespace BCG {
|
|
|
|
public class SimpleTCPClient : ITCPConnectionHandler, ITCPMessageHandler {
|
|
private readonly RemoteConnectionInfo _server_addr;
|
|
private int _m_msgs_received = 0;
|
|
private readonly UInt32 _msg_sz = 1024;// * 1024;
|
|
private readonly int _max_n_msgs = Int32.MaxValue;
|
|
private bool _send_flag = true;
|
|
int _send_wait_period_ms = 5000;
|
|
Action<string> _listener;
|
|
|
|
public SimpleTCPClient(string server_ip, int server_port, UInt32 msg_sz, int send_wait_period_ms, int max_n_msgs) {
|
|
_max_n_msgs = max_n_msgs;
|
|
_msg_sz = msg_sz;
|
|
_send_wait_period_ms = send_wait_period_ms;
|
|
_server_addr = new RemoteConnectionInfo(server_ip, server_port);
|
|
}
|
|
|
|
public void start() {
|
|
_connect();
|
|
}
|
|
|
|
private void _connect() {
|
|
Log.Gen.info($"SimpleTCPClient._connect to: {_server_addr}");
|
|
|
|
bts.AsyncTCPConnector_AsyncWrapper.connect_to(_server_addr, this);
|
|
//bts.AsyncTCPConnector_APM.connect_to(_server_addr, this);
|
|
}
|
|
|
|
public void register_listener(Action<string> cb) {
|
|
_listener += cb;
|
|
}
|
|
|
|
public void onConnection(ITCPConnection connection) {
|
|
string ip; int port;
|
|
var peer_rci = connection.get_peer_address();
|
|
bts.Log.Gen.info($"SimpleTCPClient.onConnection connected to server: {peer_rci}, starting task to send data in a loop");
|
|
if (_listener != null) {
|
|
_listener.Invoke($"Connected to server {peer_rci}");
|
|
}
|
|
connection.register(this);
|
|
// start sending data
|
|
_send_flag = true;
|
|
System.Threading.Tasks.Task.Run(() => _keep_sending_data(connection));
|
|
}
|
|
|
|
private void _keep_sending_data(ITCPConnection connection) {
|
|
byte[] buff = new byte[_msg_sz];
|
|
MemoryStream str = new MemoryStream(buff);
|
|
BinaryWriter bw = new BinaryWriter(str);
|
|
Action<int> fill_msg = (rep_i) => {
|
|
str.Position = 0;
|
|
int n_ints = (int)_msg_sz / 4;
|
|
for (int i = 0; i < n_ints; i++) {
|
|
bw.Write(rep_i);
|
|
}
|
|
};
|
|
|
|
|
|
for (int i = 0; i < _max_n_msgs; i++) {
|
|
if (!_send_flag) {
|
|
return;
|
|
}
|
|
fill_msg(1);
|
|
bts.Log.Gen.info($"Sending msg: {i} sz: {buff.Length}");
|
|
System.Threading.Thread.Sleep(_send_wait_period_ms);
|
|
connection.send(buff);
|
|
_listener.Invoke($"Data sent, sz: {buff.Length}");
|
|
}
|
|
}
|
|
|
|
public void onData(RemoteMessage msg) {
|
|
_m_msgs_received++;
|
|
if (msg.messageType == RemoteMessageType.Connection)
|
|
Log.Gen.error($"SimpleTCPClient.onData n_msgs: {_m_msgs_received} msg: {msg}");
|
|
_listener.Invoke($"Message received msg: {msg}");
|
|
if (msg.messageType == RemoteMessageType.Disconnection) {
|
|
Log.Gen.error($"SimpleTCPClient.onData disconnected, reconnecting");
|
|
_send_flag = false;
|
|
_connect();
|
|
}
|
|
}
|
|
|
|
public bool isDisposed { get { return false; } }
|
|
|
|
public void Dispose() { }
|
|
|
|
}
|
|
}
|