// using BCG; // using Google.Protobuf; // using System; // using System.IO; // using System.Linq; // using System.Net; // using System.Net.Sockets; // using System.Runtime.CompilerServices; // using System.Text; // using System.Threading.Tasks; // using BCG; // using System.Xml.Serialization; // using System.Reflection; // using BCG.Utilities.General; // using System.Net.WebSockets; // namespace BCG { // public class AsyncTCPConnection2 : ITCPConnection, IWorker, IDisposable { // private Socket _socket; // // tied to the socket above // private NetworkStream _nw_stream; // private readonly ConnectionSide _side; // ITCPMessageHandler _message_handler; // private int _msg_id = 0; // RemoteConnectionInfo _peer_address; // RemoteConnectionInfo _self_address; // private TimeSpan _hb_period = TimeSpan.FromSeconds(1); // private DateTime _last_hb_sent = DateTime.MinValue; // private DateTime _last_hb_received = DateTime.MaxValue; // private static byte _msg_type_data = 1; // private static byte _msg_type_hb = 0; // private bool _is_disposed = false; // SyncQueue _data_send_queue = new SyncQueue(); // public class QueuedMessage { // public string id; // public SendConfirmationDelegate del; // public byte[] data; // public QueuedMessage(string id, SendConfirmationDelegate del, byte[] data) { // id = id; // del = del; // data = data; // } // public QueuedMessage(byte[] data) // : this(String.Empty, null, data) { } // } // public enum ConnectionSide { // Client, // Server // } // public AsyncTCPConnection2(Socket socket, ConnectionSide side) { // _socket = socket; // _nw_stream = new NetworkStream(socket); // _side = side; // IPEndPoint r_ep = socket.RemoteEndPoint as IPEndPoint; // _peer_address = new RemoteConnectionInfo(r_ep.Address.ToString(), r_ep.Port); // var loc_ep = socket.LocalEndPoint as IPEndPoint; // _self_address = new RemoteConnectionInfo(loc_ep.Address.ToString(), loc_ep.Port); // } // public void register(ITCPMessageHandler message_handler) { // _message_handler = message_handler; // Task.Run(async () => _keep_reading()); // Task.Run(async () => await _keep_sending()); // //_keep_reading(); // // if add a _keep_sending threadis added then we can maek send a non async // // methid making it much easier to integrate with the rest // } // // this thread pulls // private async Task _keep_sending() { // bts.Log.Gen.info($"TCPConnection._keep_sending starting"); // while (!_is_disposed) { // try { // QueuedMessage m = _data_send_queue.dequeue(100); // if (m == null) { // continue; // } // await send_async(m.data); // if (String.IsNullOrEmpty(m.id)) { // m.del(m.id, true); // } // } catch (Exception ex) { // bts.Log.Gen.error($"TCPConnection._keep_sending ex: {ex.Message}"); // await Task.Delay(100); // _handle_error(ex); // } // } // } // private async Task _keep_reading() { // bts.Log.Gen.info($"TCPConnection._keep_reading starting"); // while (!_is_disposed) { // try { // byte[] msg = await TCPSocketUtils.read_bytes_async(_nw_stream); // bts.Log.Gen.info($"AsyncTCPConnection2._keep_reading side: {_side} self: {get_self_address()} peer: {get_peer_address()} msg len: {msg.Length}"); // await _handle_msg(msg); // } catch (Exception ex) { // bts.Log.Gen.error($"TCPConnection._keep_reading ex: {ex.Message}"); // await Task.Delay(100); // _handle_error(ex); // } // } // } // private async Task _handle_msg(byte[] msg) { // // read the first byte to read the msg type and deal with it accordingly // BinaryReader br = new BinaryReader(new MemoryStream(msg)); // byte msg_type = br.ReadByte(); // if (msg_type == _msg_type_data) { // byte[] data = br.ReadBytes(msg.Length - 1); // RemoteMessage m = new RemoteMessage(data, RemoteMessageType.Data, _peer_address); // _message_handler.onData(m); // } else { // RemoteMessage m = new RemoteMessage(null, RemoteMessageType.Heartbeat, _peer_address); // _message_handler.onData(m); // } // } // // not very efficient // private static byte[] _prefix_msg_type(byte[] data) { // MemoryStream ms = new MemoryStream(); // BinaryWriter bw = new BinaryWriter(ms); // bw.Write(_msg_type_data); // bw.Write(data); // ms.Position = 0; // return ms.ToArray(); // } // private async void _handle_error(Exception ex) { // bts.Log.Gen.error($"TCPConnection._handle_error ex: {ex}"); // try { // var socket = _nw_stream.Socket; // socket.Close(); // socket.Dispose(); // } catch (Exception ex2) { // bts.Log.Gen.error($"TCPConnection._handle_error got exception when closing Socket ex: {ex}"); // } // _is_disposed = true; // RemoteMessage rm = new RemoteMessage(new byte[] { }, RemoteMessageType.Disconnection, _peer_address); // _message_handler.onData(rm); // } // public void send_with_confirmation(byte[] data, string confId, SendConfirmationDelegate del) { } // public async Task send_with_confirmation_async(byte[] data, string conf_id, SendConfirmationDelegate del) { // try { // conf_id = (++_msg_id).ToString(); // await TCPSocketUtils.send_bytes_async(_nw_stream, data); // del(conf_id, true); // } catch (Exception ex) { // _handle_error(ex); // } // } // public void sendWithConfirmation(byte[] data, ref string conf_id, SendConfirmationDelegate del) { // conf_id = (++_msg_id).ToString(); // send(data); // del(conf_id, true); // } // public void send(byte[] data) { // Task.Run(async () => await send_async(data)); // bts.Log.Gen.info($"AsyncTCPConnection2.send self: {get_self_address()} peer: {get_peer_address()} msg len: {data.Length} sent successfully"); // //_data_send_queue.enqueue(new QueuedMessage(data)); // } // public async Task send_async(byte[] data) { // // prefix 4 bytes for type (data vs hb) // MemoryStream str = new MemoryStream(); // BinaryWriter bw = new BinaryWriter(str); // bw.Write(_msg_type_data); // bw.Write(data); // try { // await TCPSocketUtils.send_bytes_async(_nw_stream, str.ToArray()); // } catch (Exception ex) { // bts.Log.Gen.error($"AsyncTCPConnection2.send_async ex: {ex}"); // _handle_error(ex); // } // } // public void getPeerAddress(out string ip, out int port) { // ip = _peer_address.ip; // port = _peer_address.port; // ///.Net has some problems getting the port, so using // ///native code for that // //sockaddr addr = new sockaddr(); // //int namelen = 16; // //getsockname(mySock.Handle, ref addr, ref namelen); // //port = addr.sin_port; // } // public RemoteConnectionInfo get_peer_address() { // string ip; int port; // getPeerAddress(out ip, out port); // return new RemoteConnectionInfo(ip, port); // } // public RemoteConnectionInfo get_self_address() { // string ip; int port; // getSelfAddress(out ip, out port); // return new RemoteConnectionInfo(ip, port); // } // public void getSelfAddress(out string ip, out int port) { // ip = _self_address.ip; // port = _self_address.port; // } // private async void _send_hb() { // if (_last_hb_sent - DateTime.Now > _hb_period) { // MemoryStream ms = new MemoryStream(); // BinaryWriter bw = new BinaryWriter(ms); // bw.Write(_msg_type_hb); // ms.Position = 0; // byte[] hb_msg = ms.ToArray(); // await TCPSocketUtils.send_bytes_async(_nw_stream, hb_msg); // } // } // public void doWork() { // _send_hb(); // } // public void Dispose() { // _nw_stream.Close(); // _nw_stream.Dispose(); // _is_disposed = true; // } // public bool isDisposed { // get { // return _is_disposed; // } // } // } // }