From 404698152e43460fdddd288296830e2e63d2fec0 Mon Sep 17 00:00:00 2001 From: Taha Date: Sat, 30 Sep 2023 12:01:05 -0400 Subject: [PATCH] Merging, and adding test project --- .../DataStreamTests/DataStreamTests.csproj | 19 ++ .../DataStreamTests/UnitTest1.cs | 15 ++ src/DataStreamSuite/DataStreamTests/Usings.cs | 1 + .../PubSub/Client/SimpleTCPClient.cs | 132 ++++++++++ .../PubSub/ManagedTCP/AsyncTCPConnection2.cs | 231 ++++++++++++++++++ .../ClientToServerManagedTCPConnector.cs | 48 ++-- .../PubSub/ManagedTCP/ITCPConnection.cs | 12 +- .../PubSub/ManagedTCP/RemoteMessage.cs | 26 +- ...verToClientManagedTCPConnectionAcceptor.cs | 42 ++-- ...verToClientManagedTCPConnectionAcceptor.cs | 64 +++-- .../PubSub/ManagedTCP/TCPSocketUtils.cs | 113 +++++++++ .../PubSub/Server/SimpleTCPServer.cs | 103 ++++++++ src/DataStreamSuite/PubSubServer/Program.cs | 21 +- 13 files changed, 717 insertions(+), 110 deletions(-) create mode 100644 src/DataStreamSuite/DataStreamTests/DataStreamTests.csproj create mode 100644 src/DataStreamSuite/DataStreamTests/UnitTest1.cs create mode 100644 src/DataStreamSuite/DataStreamTests/Usings.cs create mode 100644 src/DataStreamSuite/PubSub/Client/SimpleTCPClient.cs create mode 100644 src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection2.cs create mode 100644 src/DataStreamSuite/PubSub/ManagedTCP/TCPSocketUtils.cs create mode 100644 src/DataStreamSuite/PubSub/Server/SimpleTCPServer.cs diff --git a/src/DataStreamSuite/DataStreamTests/DataStreamTests.csproj b/src/DataStreamSuite/DataStreamTests/DataStreamTests.csproj new file mode 100644 index 000000000..f892af124 --- /dev/null +++ b/src/DataStreamSuite/DataStreamTests/DataStreamTests.csproj @@ -0,0 +1,19 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + diff --git a/src/DataStreamSuite/DataStreamTests/UnitTest1.cs b/src/DataStreamSuite/DataStreamTests/UnitTest1.cs new file mode 100644 index 000000000..bc5cb5f9f --- /dev/null +++ b/src/DataStreamSuite/DataStreamTests/UnitTest1.cs @@ -0,0 +1,15 @@ +namespace DataStreamTests; + +public class Tests +{ + [SetUp] + public void Setup() + { + } + + [Test] + public void Test1() + { + Assert.Pass(); + } +} \ No newline at end of file diff --git a/src/DataStreamSuite/DataStreamTests/Usings.cs b/src/DataStreamSuite/DataStreamTests/Usings.cs new file mode 100644 index 000000000..cefced496 --- /dev/null +++ b/src/DataStreamSuite/DataStreamTests/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/Client/SimpleTCPClient.cs b/src/DataStreamSuite/PubSub/Client/SimpleTCPClient.cs new file mode 100644 index 000000000..66fdd971b --- /dev/null +++ b/src/DataStreamSuite/PubSub/Client/SimpleTCPClient.cs @@ -0,0 +1,132 @@ +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; + +namespace BCG +{ + + // This uses AsyncTCPConnection2's stateful async functions + public class SimpleTCPClientAsync : ITCPConnectionHandler, ITCPMessageHandler + { + AsyncTCPConnection2 _conn = null; + int _m_msgs_received = 0; + public static async Task start(string ip, int port) + { + SimpleTCPClientAsync c = new SimpleTCPClientAsync(ip, port); + c.connect(); + } + + public void connect() + { + Task.Run(async () => await TCPSocketUtils.connect_to_server_async("127.0.0.1", 12345, this, TimeSpan.FromSeconds(2))); + } + + public void onConnection(ITCPConnection connection) + { + Console.WriteLine("SimpleTCPClientAsync.onConnection connected to server, starting task to send data in a loop"); + connection.register(this); + // start sending data + Task.Run(async () => + { + byte[] buff = new byte[1024]; + for (int i = 0; i < buff.Length; i++) + { + buff[i] = (byte)i; + } + _conn = connection as AsyncTCPConnection2; + while (!_conn.isDisposed) + { + await _conn.send_async(buff); + await Task.Delay(100); + } + }); + } + + public void onData(RemoteMessage msg) + { + _m_msgs_received++; + Console.WriteLine($"SimpleTCPClientAsync.onData n_msgs: {_m_msgs_received} msg: {msg}"); + if (msg.messageType == RemoteMessageType.disconnection) + { + connect(); + } + } + + public bool isDisposed { get { return false; } } + + public void Dispose() { } + + public SimpleTCPClientAsync(string ip, int port) { } + + } + public class SimpleTCPClient : ITCPConnectionHandler, ITCPMessageHandler + { + private int _n_msgs_received = 0; + AsyncTCPConnection2 _conn = null; + + public static void start(string ip, int port) + { + SimpleTCPClient c = new SimpleTCPClient(ip, port); + c.connect(); + } + + public void connect() + { + Console.WriteLine("SimpleTCPClient.onConnection connect() called"); + ClientToServerManagedTCPConnector.connect_to(new BlutoSubAddress("bluto://127.0.0.1:12345"), this, TimeSpan.FromSeconds(1)); + } + + public void onConnection(ITCPConnection connection) + { + _conn = connection as AsyncTCPConnection2; + Console.WriteLine("SimpleTCPClient.onConnection connected to server, starting task to send data in a loop"); + _conn.register(this); + byte[] buff = new byte[1024]; + for (int i = 0; i < buff.Length; i++) + { + buff[i] = (byte)i; + } + + + while (!_conn.isDisposed) + { + _conn.send(buff); + System.Threading.Thread.Sleep(100); + } + + // Task.Run(async () => + // { + // while (!_conn.isDisposed) + // { + // await _conn.send_async(buff); + // await Task.Delay(100); + // } + // }); + } + + public void onData(RemoteMessage msg) + { + _n_msgs_received++; + Console.WriteLine($"SimpleTCPClient.onData messages received: {_n_msgs_received} msg: {msg} messages received: {_n_msgs_received}"); + if (msg.messageType == RemoteMessageType.disconnection) + { + connect(); + } + } + + public bool isDisposed { get { return false; } } + + public void Dispose() { } + + public SimpleTCPClient(string ip, int port) { } + + } +} diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection2.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection2.cs new file mode 100644 index 000000000..3472c3cc4 --- /dev/null +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection2.cs @@ -0,0 +1,231 @@ +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 () => await _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); + //Console.WriteLine($"_keep_reading sz: {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 confId, SendConfirmationDelegate del) { + confId = String.Empty; + } + + public void send(byte[] data) { + Task.Run(async () => await send_async(data)); + //_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 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() { + + } + + public bool isDisposed { + get { + return _is_disposed; + } + } + + } +} \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/ClientToServerManagedTCPConnector.cs b/src/DataStreamSuite/PubSub/ManagedTCP/ClientToServerManagedTCPConnector.cs index c40017c27..73a2bd00f 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/ClientToServerManagedTCPConnector.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/ClientToServerManagedTCPConnector.cs @@ -4,21 +4,19 @@ using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; +using System.Threading.Tasks; //using BCG.Utilities; //using BCG.Utilities.General; -namespace BCG -{ - internal class SocketConnectionArgs - { +namespace BCG { + internal class SocketConnectionArgs { public EndPoint ep; public Socket sock; public ITCPConnectionHandler handler; public string serverURL; internal SocketConnectionArgs(EndPoint theEp, Socket theSock, ITCPConnectionHandler theHandler, - string theServerURL) - { + string theServerURL) { ep = theEp; sock = theSock; handler = theHandler; @@ -26,46 +24,46 @@ namespace BCG } } - public class ClientToServerManagedTCPConnector - { + public class ClientToServerManagedTCPConnector { private static Random myRnd = new Random(DateTime.Now.Millisecond); private static TimeSpan myHBTimeSpan = new TimeSpan(0, 0, 0, 0, PubSub3Config.instance.PS3_HeartBeatInterval); private static int myWaitTimeBeforeRetryingUpperLimit = 5000, myWaitTimeBeforeRetryingLowerLimit = 1000; - public static void connectTo(BlutoSubAddress serverAddress, ITCPConnectionHandler handler) - { - // TODOds - // BlueEventLog.logEvent(String.Format("Connection being attempted to server {0}", serverAddress.ToString()), - // PS3LogLevels.connection); + public static void connectTo(BlutoSubAddress serverAddress, ITCPConnectionHandler handler) { Console.WriteLine($"ClientToServerManagedTCPConnector.connectTo server: {serverAddress}"); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); EndPoint ep = new IPEndPoint(IPAddress.Parse(serverAddress.ip), serverAddress.port); sock.BeginConnect(ep, new AsyncCallback(myOnConnect), new SocketConnectionArgs(ep, sock, handler, serverAddress.ToString())); } - private static void myOnConnect(IAsyncResult res) - { + public static async Task connect_to_async(BlutoSubAddress serverAddress, ITCPConnectionHandler handler, TimeSpan wait_before_retrying, int max_n_attempts = 1024) { + bts.Log.Gen.info($"ClientToServerManagedTCPConnector.connectTo server: {serverAddress}"); + await TCPSocketUtils.connect_to_server_async(serverAddress.ip, serverAddress.port, handler, wait_before_retrying, max_n_attempts); + } + + public static void connect_to(BlutoSubAddress serverAddress, ITCPConnectionHandler handler, TimeSpan wait_before_retrying, int max_n_attempts = 1024) { + bts.Log.Gen.info($"ClientToServerManagedTCPConnector.connectTo server: {serverAddress}"); + Task.Run(async () => { + await TCPSocketUtils.connect_to_server_async(serverAddress.ip, serverAddress.port, handler, wait_before_retrying, max_n_attempts); + }); + } + + private static void myOnConnect(IAsyncResult res) { Console.WriteLine("ClientToServerManagedTCPConnector.myOnConnect " + res.ToString() + " " + res.AsyncState.ToString()); SocketConnectionArgs args = res.AsyncState as SocketConnectionArgs; - try - { + try { args.sock.EndConnect(res); - } - catch (SocketException sex) - { + } catch (SocketException sex) { string errMsg = String.Format("{0}: Connection to {1} failed.", DateTime.Now.ToString(), args.serverURL.ToString()); //BlueEventLog.logEvent(sex.Message + ";" + errMsg, PS3LogLevels.disconnection); //Console.WriteLine(errMsg); - if (args.handler.isDisposed) - { + if (args.handler.isDisposed) { string doneMsg = String.Format("{0}: Connection to {1} was disposed. Not Retrying.", DateTime.Now.ToString(), args.serverURL.ToString()); //Console.WriteLine(doneMsg); // BlueEventLog.logEvent(doneMsg, PS3LogLevels.disconnection); return; - } - else - { + } else { string retryMsg = String.Format("{0}: Retrying Connection to {1}.", DateTime.Now.ToString(), args.serverURL.ToString()); //Console.WriteLine(retryMsg); //BlueEventLog.logEvent(retryMsg, PS3LogLevels.disconnection); diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/ITCPConnection.cs b/src/DataStreamSuite/PubSub/ManagedTCP/ITCPConnection.cs index 5ab5d377a..a5f1d5635 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/ITCPConnection.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/ITCPConnection.cs @@ -2,15 +2,13 @@ using System; using System.Collections.Generic; using System.Text; -namespace BCG -{ +namespace BCG { public delegate void SendConfirmationDelegate(string messageId, bool confirmed); /// /// This is the very basic connection that sits right - /// on top of the sockets (unmanaged, managed whatever). + /// on top of the sockets (used on both server and client) /// - public interface ITCPConnection : IDisposable - { + public interface ITCPConnection : IDisposable { /// /// The delegate is fired signalling if the data send was /// successful or not @@ -18,7 +16,7 @@ namespace BCG /// /// /// - void sendWithConfirmation (byte[] data, ref string confId, SendConfirmationDelegate del); + void sendWithConfirmation(byte[] data, ref string confId, SendConfirmationDelegate del); /// /// sends data to the other side. theres no guaranatee that /// data is delievered. @@ -54,6 +52,6 @@ namespace BCG /// hence this is an indicator of whether the connection is /// alive or dead /// - bool isDisposed { get;} + bool isDisposed { get; } } } \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/RemoteMessage.cs b/src/DataStreamSuite/PubSub/ManagedTCP/RemoteMessage.cs index 4c3ce1fe7..f7b64eeda 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/RemoteMessage.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/RemoteMessage.cs @@ -2,13 +2,11 @@ using System; using System.Collections.Generic; using System.Text; -namespace BCG -{ +namespace BCG { /// /// Type of messages that can be sent over the transport. /// - public enum RemoteMessageType - { + public enum RemoteMessageType { heartbeat, data, connection, disconnection } @@ -17,33 +15,33 @@ namespace BCG /// This can be a transport generated message (heartbeat) or user generated /// message (data) /// - public class RemoteMessage - { + public class RemoteMessage { private byte[] myMsg; private RemoteMessageType myMsgType; private RemoteConnectionInfo myConnInfo; - public RemoteMessage(byte[] message, RemoteMessageType messageType, RemoteConnectionInfo connInfo) - { + public RemoteMessage(byte[] message, RemoteMessageType messageType, RemoteConnectionInfo connInfo) { myMsg = message; myMsgType = messageType; myConnInfo = connInfo; } - public byte[] message - { + public byte[] message { get { return myMsg; } } - public RemoteMessageType messageType - { + public RemoteMessageType messageType { get { return myMsgType; } } - public RemoteConnectionInfo remoteConnectionInfo - { + public RemoteConnectionInfo remoteConnectionInfo { get { return myConnInfo; } } + override public string ToString() { + Func len = (byte[] m) => { return m == null ? 0 : m.Length; }; + return $"type: {myMsgType} conn: {myConnInfo} msg: {len(myMsg)}"; + } + } } diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/ServerToClientManagedTCPConnectionAcceptor.cs b/src/DataStreamSuite/PubSub/ManagedTCP/ServerToClientManagedTCPConnectionAcceptor.cs index 319fa1df5..53dbe83ec 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/ServerToClientManagedTCPConnectionAcceptor.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/ServerToClientManagedTCPConnectionAcceptor.cs @@ -5,39 +5,45 @@ using System.Text; //using BCG.Utilities.General; //using BCG.Utilities; -namespace BCG -{ - public class ServerToClientManagedTCPConnectionAcceptor - { +namespace BCG { + public class ServerToClientManagedTCPConnectionAcceptor { private static Dictionary myPortToSingleServerAcceptorMap = new Dictionary(); private static object mySyncObject = new object(); - public static int activateWithRandomPort(ITCPConnectionHandler handler, TimeSpan clientHBTimeSpan) - { + public static int activateWithRandomPort(ITCPConnectionHandler handler, TimeSpan clientHBTimeSpan) { SingleServerToClientManagedTCPConnectionAcceptor theAcceptor = new SingleServerToClientManagedTCPConnectionAcceptor(); int portNo = theAcceptor.activateWithRandomPort(handler, clientHBTimeSpan); - lock (mySyncObject) - { + lock (mySyncObject) { myPortToSingleServerAcceptorMap[portNo] = theAcceptor; } - return portNo; + return portNo; } - public static int activate(ITCPConnectionHandler handler, int port, TimeSpan clientHBTimeSpan) - { - lock (mySyncObject) - { - if (myPortToSingleServerAcceptorMap.ContainsKey(port)) - { + public static int activate(ITCPConnectionHandler handler, int port, TimeSpan clientHBTimeSpan) { + lock (mySyncObject) { + if (myPortToSingleServerAcceptorMap.ContainsKey(port)) { string msg = string.Format("Error: Application is already accepting clients on port {0}", port); // BlueEventLog.logEvent(msg, BlueEventLog2Level.Error); return port; + } else { + SingleServerToClientManagedTCPConnectionAcceptor theAcceptor = new SingleServerToClientManagedTCPConnectionAcceptor(); + int assignedport = theAcceptor.activate(handler, port, clientHBTimeSpan); + myPortToSingleServerAcceptorMap[assignedport] = theAcceptor; + return assignedport; } - else - { + } //end lock + } //end activate code + + public static int accept_connections(ITCPConnectionHandler handler, int port, TimeSpan clientHBTimeSpan) { + lock (mySyncObject) { + if (myPortToSingleServerAcceptorMap.ContainsKey(port)) { + string msg = string.Format("Error: Application is already accepting clients on port {0}", port); + // BlueEventLog.logEvent(msg, BlueEventLog2Level.Error); + return port; + } else { SingleServerToClientManagedTCPConnectionAcceptor theAcceptor = new SingleServerToClientManagedTCPConnectionAcceptor(); int assignedport = theAcceptor.activate(handler, port, clientHBTimeSpan); myPortToSingleServerAcceptorMap[assignedport] = theAcceptor; @@ -47,5 +53,7 @@ namespace BCG } //end activate code + + } } diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/SingleServerToClientManagedTCPConnectionAcceptor.cs b/src/DataStreamSuite/PubSub/ManagedTCP/SingleServerToClientManagedTCPConnectionAcceptor.cs index 9623e9423..2ec8ebfca 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/SingleServerToClientManagedTCPConnectionAcceptor.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/SingleServerToClientManagedTCPConnectionAcceptor.cs @@ -4,13 +4,12 @@ using System.Text; using System.Threading; using System.Net; using System.Net.Sockets; +using System.Threading.Tasks; //using BCG.Utilities; -namespace BCG -{ - public class SingleServerToClientManagedTCPConnectionAcceptor - { +namespace BCG { + public class SingleServerToClientManagedTCPConnectionAcceptor { private TimeSpan myHBTimeSpan, myWaitTimeBeforeRetrying = new TimeSpan(0, 0, 0, 0, 250); private ITCPConnectionHandler myHandler; @@ -18,58 +17,50 @@ namespace BCG private int myPort; - public int activateWithRandomPort(ITCPConnectionHandler handler, TimeSpan clientHBTimeSpan) - { + public int activateWithRandomPort(ITCPConnectionHandler handler, TimeSpan clientHBTimeSpan) { myHBTimeSpan = clientHBTimeSpan; myHandler = handler; myAcceptSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); int port = 0; bool madeItThrough = false; -// BlueEventLog.logEvent("Looking for open port to run the server on.", PS3LogLevels.connection); + // BlueEventLog.logEvent("Looking for open port to run the server on.", PS3LogLevels.connection); Random randomizer = new Random(); PubSubPortRange theRange = PubSub3Config.instance.PS3_PortRange; - + //if the range of ports is 1000, then trying 5,000 times means //your odds of missing an open port are less than 1% - int maxAttempts = (theRange.maxPort - theRange.minPort) * 5; + int maxAttempts = (theRange.maxPort - theRange.minPort) * 5; int totalAttempts = 0; - while (!madeItThrough) - { - port = randomizer.Next(); + while (!madeItThrough) { + port = randomizer.Next(); port = theRange.minPort + (port % (theRange.maxPort - theRange.minPort)); - try - { + try { myAcceptSock.Bind(new IPEndPoint(IPAddress.Any, port)); madeItThrough = true; - } - catch (Exception) - { + } catch (Exception) { totalAttempts++; - if (totalAttempts < maxAttempts){ + if (totalAttempts < maxAttempts) { //BlueEventLog.logEvent("Couldn't bind to port " + port.ToString() + "; retrying...", PS3LogLevels.connection); - } - else - { + } else { // BlueEventLog2.instance().abort("Couldn't find an open port to advertise the server on. Consider modifying PS3_PortRange. Talk to Mark for details."); } } } myPort = port; myAcceptSock.Listen(250); - myAcceptSock.BeginAccept(new AsyncCallback(myOnAccept), new object[] { }); + myAcceptSock.BeginAccept(new AsyncCallback(myOnAccept), new object[] { }); return myPort; } - public int activate (ITCPConnectionHandler handler, int port, TimeSpan clientHBTimeSpan) - { + public int activate(ITCPConnectionHandler handler, int port, TimeSpan clientHBTimeSpan) { myHBTimeSpan = clientHBTimeSpan; myHandler = handler; myAcceptSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - IPEndPoint endpoint = new IPEndPoint (IPAddress.Any, port); + IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, port); myAcceptSock.Bind(endpoint); myAcceptSock.Listen(250); myAcceptSock.BeginAccept(new AsyncCallback(myOnAccept), new object[] { }); @@ -77,15 +68,18 @@ namespace BCG return myPort; // not sure why we need this class variable myPort, but I am just keeping it up with the existing code } - private void myOnAccept(IAsyncResult res) - { + public int accept_on_port(ITCPConnectionHandler handler, string ip, int port, TimeSpan clientHBTimeSpan) { + Task.Run(async () => { + await TCPSocketUtils.accept_connections_async(ip, port, handler, TimeSpan.FromSeconds(1)); + }); + return port; + } + + private void myOnAccept(IAsyncResult res) { Socket newSock; - try - { + try { newSock = myAcceptSock.EndAccept(res); - } - catch (Exception ex) - { + } catch (Exception ex) { // BlueEventLog.logEvent(ex); Thread.Sleep(myWaitTimeBeforeRetrying); myAcceptSock.BeginAccept(new AsyncCallback(myOnAccept), new object[] { }); @@ -98,10 +92,10 @@ namespace BCG ITCPConnection newConn = new AsyncTCPConnection(newSock, myHBTimeSpan); myHandler.onConnection(newConn); myAcceptSock.BeginAccept(new AsyncCallback(myOnAccept), new object[] { }); - string ip;int port; - newConn.getPeerAddress (out ip, out port); + string ip; int port; + newConn.getPeerAddress(out ip, out port); string connMsg = String.Format("Connection to port {2} made from {0}:{1}", ip, port, myPort); -// BlueEventLog.logEvent(connMsg, PS3LogLevels.connection); + // BlueEventLog.logEvent(connMsg, PS3LogLevels.connection); //Console.WriteLine(connMsg); } } diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/TCPSocketUtils.cs b/src/DataStreamSuite/PubSub/ManagedTCP/TCPSocketUtils.cs new file mode 100644 index 000000000..ed0943d40 --- /dev/null +++ b/src/DataStreamSuite/PubSub/ManagedTCP/TCPSocketUtils.cs @@ -0,0 +1,113 @@ +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 +{ + // utility functions for TCP socket async communications. Does the following + // client to server connect + // server to client connect accepting + // send data + // receive data + public static class TCPSocketUtils + { + #region socket opening and connecting + // keeps accepting connections forever + public static async Task accept_connections_async(string ip, int port, ITCPConnectionHandler handler, + TimeSpan wait_after_error) + { + IPAddress ipAddress = String.IsNullOrEmpty(ip) ? IPAddress.Parse(ip) : IPAddress.Any; + var ip_end_point = new IPEndPoint(ipAddress, port); + Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + listener.Bind(ip_end_point); + listener.Listen(10); + Socket client_socket = null; + while (true) + { + try + { + // keeps on accepting connections + client_socket = await listener.AcceptAsync(); + AsyncTCPConnection2 new_conn = new(client_socket, AsyncTCPConnection2.ConnectionSide.Server); + handler.onConnection(new_conn); + } + catch (Exception ex) + { + bts.Log.Gen.error($"TCPConnection.accept ex: {ex} trying again"); + Task.Delay(wait_after_error); + } + } + } + + // connects to the server and ona successful connection, calls con__handler.on_connection_async with an ITCPConnection. + // In case of failures to connect it waits for a given amount of time before retrying + public static async Task connect_to_server_async(string ip, int server_port, ITCPConnectionHandler conn_handler, + TimeSpan wait_after_failed_attempt, int n_max_attempts = 1024) + { + Socket client_socket = null; + for (int i = 0; i < n_max_attempts; i++) + { + try + { + string log_conn = $"{ip}:{server_port}"; + bts.Log.Gen.info($"AsyncTCPConnection2.connect_to_server conn: {log_conn} attempt no: {i}"); + IPAddress server_ip_address = IPAddress.Parse(ip); + client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + await client_socket.ConnectAsync(server_ip_address, server_port); + bts.Log.Gen.info($"AsyncTCPConnection2.connect_to_server conn: {log_conn} successfully connected after {i} attempts"); + ITCPConnection new_conn = new AsyncTCPConnection2(client_socket, AsyncTCPConnection2.ConnectionSide.Client); + conn_handler.onConnection(new_conn); + break; + } + catch (Exception ex) + { + bts.Log.Gen.error($"AsyncTCPConnection2.connect_to_server threw ex: {ex}, will reattempt now"); + Task.Delay(wait_after_failed_attempt); + } + } + return client_socket; + } + #endregion + + #region data sending/receiving + + // prefixes the size of the data and sends it as 2 sends, for size and data + // (can be done as one send perhaps) + public static async Task send_bytes_async(NetworkStream nw_stream, byte[] data_buff) + { + var data_sz = data_buff.Length; + byte[] sz_buff = new byte[4]; + // aend the size first + BinaryWriter bw = new BinaryWriter(new MemoryStream(sz_buff)); + bw.Write(data_sz); + await nw_stream.WriteAsync(sz_buff, 0, 4); + await nw_stream.WriteAsync(data_buff, 0, data_buff.Length); + } + + // reads the bytes on a connected socket, by doing the opposite of the above, + // reads the size first and then the data + public static async Task read_bytes_async(NetworkStream nw_stream) + { + byte[] sz_buffer = new byte[4]; + //Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. + int bytesRead = await nw_stream.ReadAsync(sz_buffer, 0, sz_buffer.Length); + BinaryReader br = new BinaryReader(new MemoryStream(sz_buffer)); + int msg_sz = br.ReadInt32(); + byte[] data_buffer = new byte[msg_sz]; + bytesRead = await nw_stream.ReadAsync(data_buffer, 0, data_buffer.Length); + //Console.WriteLine($"read_bytes sz: {data_buffer.Length}"); + return data_buffer; + } + #endregion + } +} \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/Server/SimpleTCPServer.cs b/src/DataStreamSuite/PubSub/Server/SimpleTCPServer.cs new file mode 100644 index 000000000..0e65e1444 --- /dev/null +++ b/src/DataStreamSuite/PubSub/Server/SimpleTCPServer.cs @@ -0,0 +1,103 @@ +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 System.Data; + +namespace BCG { + + public class SimpleTCPServerAsync : ITCPConnectionHandler, ITCPMessageHandler { + private AsyncTCPConnection2 _conn = null; + int _n_msgs_received = 0; + SingleServerToClientManagedTCPConnectionAcceptor _acceptor = new SingleServerToClientManagedTCPConnectionAcceptor(); + + public static async Task start() { + Console.WriteLine("SimpleTCPServer.start accepting socket connections"); + + SimpleTCPServerAsync s = new SimpleTCPServerAsync(); + //await AsyncTCPConnection2.accept_connections("127.0.0.1", 12345, s, TimeSpan.FromMilliseconds(100)); + s.accept(); + } + + // public static async Task init() + // { + // } + + public void accept() { + _acceptor.accept_on_port(this, "127.0.0.1", 12345, TimeSpan.Zero); + } + + public void onConnection(ITCPConnection connection) { + Console.WriteLine("SimpleTCPServerAsync.onConnection new client connected"); + _conn = connection as AsyncTCPConnection2; + connection.register(this); + } + + + public void onData(RemoteMessage msg) { + _n_msgs_received++; + Console.WriteLine($"SimpleTCPServerAsync.onData msgs_received: {_n_msgs_received} msg: {msg} , echoing it back"); + if (msg.messageType == RemoteMessageType.data && _conn != null) { + + //_conn.send(msg.message); + Task.Run(async () => { + Console.WriteLine($"SimpleTCPServerAsync.onData sending msg: {msg} to client"); + await _conn.send_async(msg.message); + }); + } + } + + public bool isDisposed { get { return false; } } + + public void Dispose() { } + } + + public class SimpleTCPServer : ITCPConnectionHandler, ITCPMessageHandler { + private AsyncTCPConnection2 _conn = null; + private int _n_msgs_received = 0; + SingleServerToClientManagedTCPConnectionAcceptor _acceptor = new SingleServerToClientManagedTCPConnectionAcceptor(); + + public static async Task start() { + Console.WriteLine("SimpleTCPServer.start accepting socket connections"); + SimpleTCPServer s = new SimpleTCPServer(); + s.accept(); + //await AsyncTCPConnection2.accept_connections("127.0.0.1", 12345, s, TimeSpan.FromMilliseconds(100)); + } + + public void accept() { + _acceptor.accept_on_port(this, "127.0.0.1", 12345, TimeSpan.Zero); + } + + public void onConnection(ITCPConnection connection) { + Console.WriteLine("SimpleTCPServerAsync.onConnection new client connected"); + _conn = connection as AsyncTCPConnection2; + connection.register(this); + } + + public async void onData(RemoteMessage msg) { + _n_msgs_received++; + Console.WriteLine($"SimpleTCPServerAsync.onData msg: {msg} , msgs received: {_n_msgs_received} echoing it back"); + if (msg.messageType == RemoteMessageType.data && _conn != null) { + Task.Run(async () => { + //Console.WriteLine($"SimpleTCPServerAsync.onData sending msg: {msg} to client"); + await _conn.send_async(msg.message); + }); + } + } + + public bool isDisposed { get { return false; } } + + public void Dispose() { } + } +} \ No newline at end of file diff --git a/src/DataStreamSuite/PubSubServer/Program.cs b/src/DataStreamSuite/PubSubServer/Program.cs index 311681e5c..62ff4b560 100644 --- a/src/DataStreamSuite/PubSubServer/Program.cs +++ b/src/DataStreamSuite/PubSubServer/Program.cs @@ -1,22 +1,22 @@ using System; using System.Threading; +using System.Threading.Tasks; using System.Linq; //using BCG.Utilities; //using BCG.Utilities.General; -namespace BCG -{ - class PubSub2Main - { +namespace BCG { + class PubSub2Main { /// /// The main entry point for the application. /// [STAThread] - public static void Main(string[] args) - { + public static async Task Main(string[] args) { + SimpleTCPServerAsync.start(); + Task.Delay(1000000000).Wait(); if (args.Length > 0 && args[0] == "daemon") args = args.Skip(1).ToArray(); @@ -26,14 +26,11 @@ namespace BCG long currentMemory = 0; int defaultWaitTime = 100; bool gced = false; - new Thread(() => - { - while (true) - { + new Thread(() => { + while (true) { gced = false; currentMemory = GC.GetTotalMemory(false); - if (config.PS3_GCActivationThreshold > 0 && currentMemory > config.PS3_GCActivationThreshold) - { + if (config.PS3_GCActivationThreshold > 0 && currentMemory > config.PS3_GCActivationThreshold) { string gcMsg = String.Format("GCing memory, mem before GC = {0}", currentMemory); // BlueEventLog.logEvent(gcMsg, PS3LogLevels.info); Console.WriteLine(gcMsg);