diff --git a/src/DataStreamSuite/DataStreamPublisher/Program.cs b/src/DataStreamSuite/DataStreamPublisher/Program.cs index a47d19827..9407a102f 100644 --- a/src/DataStreamSuite/DataStreamPublisher/Program.cs +++ b/src/DataStreamSuite/DataStreamPublisher/Program.cs @@ -12,10 +12,8 @@ using System.Threading; bts.Log.Gen.info($"Starting DSPublisher"); Console.WriteLine("Starting DSPublisher..."); bool run_echo_tcp_client_only = true; -static void init(string[] args) -{ - Func login = async (l, p) => - { +static void init(string[] args) { + Func login = async (l, p) => { var login = l; var passwd = p; var service_ctx = bts.api.core.ServiceContext.create(); @@ -27,36 +25,35 @@ static void init(string[] args) } init(args); if (run_echo_tcp_client_only) - await start_echo_client(); + start_echo_client(); else _publish_streams(); -static ViewStream build_stream(string stream_expression) -{ - try - { +static ViewStream build_stream(string stream_expression) { + try { var exp = DataStreamExpressionsInterpreter.Evaluate(stream_expression); var rv = exp.getView(new Dictionary(0)); return rv; - } - catch (Exception ex) - { + } catch (Exception ex) { bts.Log.assert(false, $"Could not build stream: {stream_expression} ex: {ex}"); return null; } } -async Task start_echo_client() -{ +static void start_echo_client() { Console.WriteLine("Starting DSPublisher in TCP client echo mode, Hit key to continue"); Console.ReadLine(); - await SimpleTCPClientAsync.start("127.0.0.1", 12345); - Task.Delay(1000000000).Wait(); + SimpleTCPClient client = new SimpleTCPClient("127.0.0.1", 12345); + client.start(); + while (true) { + System.Threading.Thread.Sleep(1000); + } + // await SimpleTCPClient.start("127.0.0.1", 12345); + // Task.Delay(1000000000).Wait(); } -void _publish_streams() -{ +void _publish_streams() { PubTableBroker pb_broker = new PubTableBroker(); CloudStreamManager csm = new CloudStreamManager(); // get it from config @@ -69,16 +66,14 @@ void _publish_streams() {"literal_420","literal(\"x\", \"420.0\")"}, // {"bbo_ts","timeSeries(bbo())"} }; - foreach (var kv in streams_to_publish) - { + foreach (var kv in streams_to_publish) { var name = kv.Key; var exp = kv.Value; BCG.ViewStream vs = build_stream(exp); ViewPublisher vpub = new ViewPublisher(name, pb_broker, vs); vs.onChange += on_change; } - while (true) - { + while (true) { System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); ViewStreamDatabase.instance.doWork(); csm.doWork(); @@ -87,7 +82,6 @@ void _publish_streams() -void on_change(object obj) -{ +void on_change(object obj) { bts.Log.Gen.info($"DSPublisher on_change dp: {obj}"); } \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection.cs index a5b201e2f..0d5fbf05f 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection.cs @@ -1,542 +1,542 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Net; -using System.Net.Sockets; -using System.IO; -//using BCG.Utilities; -using BCG.Utilities.General; -using System.Runtime.InteropServices; +// using System; +// using System.Collections.Generic; +// using System.Text; +// using System.Net; +// using System.Net.Sockets; +// using System.IO; +// //using BCG.Utilities; +// using BCG.Utilities.General; +// using System.Runtime.InteropServices; -namespace BCG -{ - /// - /// This class, mantains a connection to a peer socket, using - /// the .Net async TCP library. It sends heartbeats at regular - /// intervals (config). It is in active states when messages - /// are sent and received without an error state. It assumed - /// dead state when either of the following happens - /// BeginReceive returns a SocketError - /// BeginSend returns a SocketError - /// EndReceive returns a SocketError - /// EndSend returns a SocketError - /// As soon as it assumes dead state it stops sending or - /// receiving any messages (including heartbeats). - /// The user of this class can either detect that by - /// missing heartbeats or checking the isConnected property. - /// Missing should be the more general way, since if the - /// peer dies in a 'silent' way, where none of the above - /// methods indicate an error, missing heartbeats is guaranteed - /// to detect it. - /// - public class AsyncTCPConnection : ITCPConnection, IWorker, IDisposable - { - /// - /// This is used as a state for the Async send. Reason being - /// that if onSend indicates that it could not send the full - /// data (ie EndSend does not return the full size), the offset - /// is used to resend the data - /// - public class AsyncMessage - { - public byte[] buffer; - public int offset; - public string messageId; - public SendConfirmationDelegate onSendDel; +// namespace BCG +// { +// /// +// /// This class, mantains a connection to a peer socket, using +// /// the .Net async TCP library. It sends heartbeats at regular +// /// intervals (config). It is in active states when messages +// /// are sent and received without an error state. It assumed +// /// dead state when either of the following happens +// /// BeginReceive returns a SocketError +// /// BeginSend returns a SocketError +// /// EndReceive returns a SocketError +// /// EndSend returns a SocketError +// /// As soon as it assumes dead state it stops sending or +// /// receiving any messages (including heartbeats). +// /// The user of this class can either detect that by +// /// missing heartbeats or checking the isConnected property. +// /// Missing should be the more general way, since if the +// /// peer dies in a 'silent' way, where none of the above +// /// methods indicate an error, missing heartbeats is guaranteed +// /// to detect it. +// /// +// public class AsyncTCPConnection : ITCPConnection, IWorker, IDisposable +// { +// /// +// /// This is used as a state for the Async send. Reason being +// /// that if onSend indicates that it could not send the full +// /// data (ie EndSend does not return the full size), the offset +// /// is used to resend the data +// /// +// public class AsyncMessage +// { +// public byte[] buffer; +// public int offset; +// public string messageId; +// public SendConfirmationDelegate onSendDel; - public AsyncMessage(byte[] theBuffer, int theOffset) - : this(theBuffer, theOffset, String.Empty, null) { } +// public AsyncMessage(byte[] theBuffer, int theOffset) +// : this(theBuffer, theOffset, String.Empty, null) { } - public AsyncMessage(byte[] theBuffer, int theOffset, string theMessageId, SendConfirmationDelegate theOnSendDel) - { - buffer = theBuffer; - offset = theOffset; - messageId = theMessageId; - onSendDel = theOnSendDel; - } +// public AsyncMessage(byte[] theBuffer, int theOffset, string theMessageId, SendConfirmationDelegate theOnSendDel) +// { +// buffer = theBuffer; +// offset = theOffset; +// messageId = theMessageId; +// onSendDel = theOnSendDel; +// } - public int length - { - get - { - return buffer.Length - offset; - } - } - } +// public int length +// { +// get +// { +// return buffer.Length - offset; +// } +// } +// } - enum ReadState - { - readingSize, - readingData - }; +// enum ReadState +// { +// readingSize, +// readingData +// }; - #region work around for .Net socket's LocalEndPoint bug +// #region work around for .Net socket's LocalEndPoint bug - [StructLayout(LayoutKind.Sequential)] - private struct in_addr - { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] - public byte[] sin_addr; - } +// [StructLayout(LayoutKind.Sequential)] +// private struct in_addr +// { +// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] +// public byte[] sin_addr; +// } - [StructLayout(LayoutKind.Sequential)] - private struct sockaddr - { - public short sin_family; - public ushort sin_port; - public in_addr sin_addr; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] - public byte[] sin_zero; - } +// [StructLayout(LayoutKind.Sequential)] +// private struct sockaddr +// { +// public short sin_family; +// public ushort sin_port; +// public in_addr sin_addr; +// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] +// public byte[] sin_zero; +// } - [DllImport("wsock32.dll")] - private static extern int getsockname(IntPtr s, ref sockaddr name, ref int namelen); +// [DllImport("wsock32.dll")] +// private static extern int getsockname(IntPtr s, ref sockaddr name, ref int namelen); - [DllImport("wsock32.dll")] - private static extern int getpeername(IntPtr s, ref sockaddr name, ref int namelen); +// [DllImport("wsock32.dll")] +// private static extern int getpeername(IntPtr s, ref sockaddr name, ref int namelen); - #endregion +// #endregion - private readonly int myMaxMessageSize = PubSub3Config.instance.PS3_MaxPacketSize; - private RemoteConnectionInfo myPeer, mySelf; - private Socket mySock; - private ITCPMessageHandler myMessageHandler; +// private readonly int myMaxMessageSize = PubSub3Config.instance.PS3_MaxPacketSize; +// private RemoteConnectionInfo myPeer, mySelf; +// private Socket mySock; +// private ITCPMessageHandler myMessageHandler; - private byte[] myCurrentReadBuffer; - private int myCurrentReadBufferOffset; - private ReadState myCurrentReadState; +// private byte[] myCurrentReadBuffer; +// private int myCurrentReadBufferOffset; +// private ReadState myCurrentReadState; - private bool myAlive = true; +// private bool myAlive = true; - private byte[] myHBMessage; - private TimeSpan myHBTimeSpan; - private MasterWorker myMasterWorker = MasterWorker.getInstance(true); - private bool myHBReceived = false, myDataReceived = false, myHBSent = false; - private DateTime myLastHBReceivedTime = new DateTime(1900, 1, 1); +// private byte[] myHBMessage; +// private TimeSpan myHBTimeSpan; +// private MasterWorker myMasterWorker = MasterWorker.getInstance(true); +// private bool myHBReceived = false, myDataReceived = false, myHBSent = false; +// private DateTime myLastHBReceivedTime = new DateTime(1900, 1, 1); - public AsyncTCPConnection(Socket sock, TimeSpan heartBeatTimeSpan) - { - myHBTimeSpan = heartBeatTimeSpan; - mySock = sock; - mySock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, true); - string ip; - int port; - getPeerAddress(out ip, out port); - myPeer = new RemoteConnectionInfo(ip, port); - getSelfAddress(out ip, out port); - mySelf = new RemoteConnectionInfo(ip, port); - // BlueEventLog.logEvent(String.Format("AsyncTCPConnection created : self = {0} , peer = {1}", - // mySelf.ToString(), myPeer.ToString()), PS3LogLevels.connection); - } +// public AsyncTCPConnection(Socket sock, TimeSpan heartBeatTimeSpan) +// { +// myHBTimeSpan = heartBeatTimeSpan; +// mySock = sock; +// mySock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, true); +// string ip; +// int port; +// getPeerAddress(out ip, out port); +// myPeer = new RemoteConnectionInfo(ip, port); +// getSelfAddress(out ip, out port); +// mySelf = new RemoteConnectionInfo(ip, port); +// // BlueEventLog.logEvent(String.Format("AsyncTCPConnection created : self = {0} , peer = {1}", +// // mySelf.ToString(), myPeer.ToString()), PS3LogLevels.connection); +// } - public override string ToString() - { - return String.Format("peer = {0}, self = {1}", myPeer.ToString(), mySelf.ToString()); - } +// public override string ToString() +// { +// return String.Format("peer = {0}, self = {1}", myPeer.ToString(), mySelf.ToString()); +// } - #region ITCPConnection Members +// #region ITCPConnection Members - public void sendWithConfirmation(byte[] data, ref string confId, SendConfirmationDelegate del) - { - confId = Guid.NewGuid().ToString(); - byte[] msg = myBuildDataMessage(data); - myBeginOrResumeSending(new AsyncMessage(msg, 0, confId, del)); - } +// public void sendWithConfirmation(byte[] data, ref string confId, SendConfirmationDelegate del) +// { +// confId = Guid.NewGuid().ToString(); +// byte[] msg = myBuildDataMessage(data); +// myBeginOrResumeSending(new AsyncMessage(msg, 0, confId, del)); +// } - public void send(byte[] data) - { - byte[] msg = myBuildDataMessage(data); - myBeginOrResumeSending(new AsyncMessage(msg, 0)); - } +// public void send(byte[] data) +// { +// byte[] msg = myBuildDataMessage(data); +// myBeginOrResumeSending(new AsyncMessage(msg, 0)); +// } - public void register(ITCPMessageHandler messageHandler) - { - // BlueEventLog.logAssert(myMessageHandler == null, "Message Handler is already assigned"); - // BlueEventLog.logEvent(String.Format("AsyncTCPConnection for peer = {0} being registered", myPeer.ToString()), - // PS3LogLevels.connection); - myMessageHandler = messageHandler; - myMasterWorker.addWorker(this, myHBTimeSpan); - myReadSize(); - } +// public void register(ITCPMessageHandler messageHandler) +// { +// // BlueEventLog.logAssert(myMessageHandler == null, "Message Handler is already assigned"); +// // BlueEventLog.logEvent(String.Format("AsyncTCPConnection for peer = {0} being registered", myPeer.ToString()), +// // PS3LogLevels.connection); +// myMessageHandler = messageHandler; +// myMasterWorker.addWorker(this, myHBTimeSpan); +// myReadSize(); +// } - public void getPeerAddress(out string ip, out int port) - { - ip = ""; - port = 0; - if (myAlive) - { - IPEndPoint ipEp = mySock.RemoteEndPoint as IPEndPoint; - ip = ipEp.Address.ToString(); - port = ipEp.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 getPeerAddress(out string ip, out int port) +// { +// ip = ""; +// port = 0; +// if (myAlive) +// { +// IPEndPoint ipEp = mySock.RemoteEndPoint as IPEndPoint; +// ip = ipEp.Address.ToString(); +// port = ipEp.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 = ""; - port = 0; - if (myAlive) - { - IPEndPoint ipEp = mySock.LocalEndPoint as IPEndPoint; - ip = ipEp.Address.ToString(); - port = ipEp.Port; - } - ///due to this bug in .Net socket, we have to resort to - ///native code - //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 = ""; +// port = 0; +// if (myAlive) +// { +// IPEndPoint ipEp = mySock.LocalEndPoint as IPEndPoint; +// ip = ipEp.Address.ToString(); +// port = ipEp.Port; +// } +// ///due to this bug in .Net socket, we have to resort to +// ///native code +// //sockaddr addr = new sockaddr(); +// //int namelen = 16; +// //getsockname(mySock.Handle, ref addr, ref namelen); +// //port = addr.sin_port; +// } - public bool isDisposed - { - get { return !myAlive; } - } +// public bool isDisposed +// { +// get { return !myAlive; } +// } - #endregion +// #endregion - #region IWorker Members +// #region IWorker Members - bool _last_connected = false; - public void doWork() - { - if (mySock != null) - //if (mySock.Connected != _last_connected) - { - Console.WriteLine($"AsyncTCPConnection.doWork connected: {mySock.Connected}"); - _last_connected = mySock.Connected; - } - if (!myHBSent) - { - // BlueEventLog.logEvent(String.Format("Sending heartbeat to peer {0} for the first time", myPeer.ToString()), - // PS3LogLevels.connection); - myHBSent = true; - } - myBeginOrResumeSending(new AsyncMessage(myBuildHeartbeatMessage(), 0)); - } +// bool _last_connected = false; +// public void doWork() +// { +// if (mySock != null) +// //if (mySock.Connected != _last_connected) +// { +// Console.WriteLine($"AsyncTCPConnection.doWork connected: {mySock.Connected}"); +// _last_connected = mySock.Connected; +// } +// if (!myHBSent) +// { +// // BlueEventLog.logEvent(String.Format("Sending heartbeat to peer {0} for the first time", myPeer.ToString()), +// // PS3LogLevels.connection); +// myHBSent = true; +// } +// myBeginOrResumeSending(new AsyncMessage(myBuildHeartbeatMessage(), 0)); +// } - #endregion +// #endregion - #region sending data +// #region sending data - private void myBeginOrResumeSending(AsyncMessage message) - { - if (myAlive) - { - try - { - //Console.WriteLine($"AsyncTCPConnection.myBeginOrResumeSending len: {message.length} connected: {mySock.Connected}"); - SocketError beginSendErr; - mySock.BeginSend(message.buffer, message.offset, message.length, SocketFlags.None, - out beginSendErr, new AsyncCallback(myOnSend), message); - Console.WriteLine($"AsyncTCPConnection.myBeginOrResumeSending len: {message.length} socker_error: {beginSendErr} connected: {mySock.Connected}"); - if (beginSendErr != SocketError.Success) - myHandleError(beginSendErr); - } - catch (Exception ex) - { - myHandleError(ex); - } - } - } +// private void myBeginOrResumeSending(AsyncMessage message) +// { +// if (myAlive) +// { +// try +// { +// //Console.WriteLine($"AsyncTCPConnection.myBeginOrResumeSending len: {message.length} connected: {mySock.Connected}"); +// SocketError beginSendErr; +// mySock.BeginSend(message.buffer, message.offset, message.length, SocketFlags.None, +// out beginSendErr, new AsyncCallback(myOnSend), message); +// Console.WriteLine($"AsyncTCPConnection.myBeginOrResumeSending len: {message.length} socker_error: {beginSendErr} connected: {mySock.Connected}"); +// if (beginSendErr != SocketError.Success) +// myHandleError(beginSendErr); +// } +// catch (Exception ex) +// { +// myHandleError(ex); +// } +// } +// } - private void myOnSend(IAsyncResult res) - { - if (myAlive) - { - try - { - SocketError writeErr; - int bytesSent = mySock.EndSend(res, out writeErr); - Console.WriteLine($"AsyncTCPConnection.myOnSend bytes: {bytesSent} writeErr: {writeErr} connected: {mySock.Connected}"); - AsyncMessage message = res.AsyncState as AsyncMessage; - message.offset += bytesSent; - if (writeErr == SocketError.Success) - { - if (message.offset == message.buffer.Length) - { - // if confirmation needs to be sent - if (String.Empty != message.messageId) - { - message.onSendDel(message.messageId, true); - } - } - else - { - ///resume writing rest of the message - // BlueEventLog.logEvent("could not send the whole message all at once , messageSize = " + - // message.buffer.Length + " offset = " + message.offset, PS3LogLevels.info); - myBeginOrResumeSending(message); - } - } - else - { - myHandleError(writeErr); - } - } - catch (Exception ex) - { - myHandleError(ex); - } - } - } +// private void myOnSend(IAsyncResult res) +// { +// if (myAlive) +// { +// try +// { +// SocketError writeErr; +// int bytesSent = mySock.EndSend(res, out writeErr); +// Console.WriteLine($"AsyncTCPConnection.myOnSend bytes: {bytesSent} writeErr: {writeErr} connected: {mySock.Connected}"); +// AsyncMessage message = res.AsyncState as AsyncMessage; +// message.offset += bytesSent; +// if (writeErr == SocketError.Success) +// { +// if (message.offset == message.buffer.Length) +// { +// // if confirmation needs to be sent +// if (String.Empty != message.messageId) +// { +// message.onSendDel(message.messageId, true); +// } +// } +// else +// { +// ///resume writing rest of the message +// // BlueEventLog.logEvent("could not send the whole message all at once , messageSize = " + +// // message.buffer.Length + " offset = " + message.offset, PS3LogLevels.info); +// myBeginOrResumeSending(message); +// } +// } +// else +// { +// myHandleError(writeErr); +// } +// } +// catch (Exception ex) +// { +// myHandleError(ex); +// } +// } +// } - /// - /// Assembles the heartbeat message, and uses it for - /// future references - /// - /// - private byte[] myBuildHeartbeatMessage() - { - if (myHBMessage == null) - { - BinaryWriter bw = new BinaryWriter(new MemoryStream()); - int sz = 4; - int msgType = 1; - bw.Write(sz); - bw.Write(msgType); - myHBMessage = (bw.BaseStream as MemoryStream).ToArray(); - } - return myHBMessage; - } +// /// +// /// Assembles the heartbeat message, and uses it for +// /// future references +// /// +// /// +// private byte[] myBuildHeartbeatMessage() +// { +// if (myHBMessage == null) +// { +// BinaryWriter bw = new BinaryWriter(new MemoryStream()); +// int sz = 4; +// int msgType = 1; +// bw.Write(sz); +// bw.Write(msgType); +// myHBMessage = (bw.BaseStream as MemoryStream).ToArray(); +// } +// return myHBMessage; +// } - /// - /// Assembles the data message. This is necessary - /// for the other side to understand the message. This can also - /// be achieved by not forming the message and use the - /// BeginSend (List<>) function rather than BeginSend (byte[]) - /// - /// - /// - private byte[] myBuildDataMessage(byte[] data) - { - BinaryWriter bw = new BinaryWriter(new MemoryStream()); - bw.Write(data.Length + 8); - bw.Write(0); - bw.Write(0); ///mid for backward compatibility - bw.Write(data); - return (bw.BaseStream as MemoryStream).ToArray(); - } +// /// +// /// Assembles the data message. This is necessary +// /// for the other side to understand the message. This can also +// /// be achieved by not forming the message and use the +// /// BeginSend (List<>) function rather than BeginSend (byte[]) +// /// +// /// +// /// +// private byte[] myBuildDataMessage(byte[] data) +// { +// BinaryWriter bw = new BinaryWriter(new MemoryStream()); +// bw.Write(data.Length + 8); +// bw.Write(0); +// bw.Write(0); ///mid for backward compatibility +// bw.Write(data); +// return (bw.BaseStream as MemoryStream).ToArray(); +// } - #endregion +// #endregion - #region receiving data +// #region receiving data - private void myReadSize() - { - myCurrentReadBuffer = new byte[4]; - myCurrentReadBufferOffset = 0; - myCurrentReadState = ReadState.readingSize; - myBeginOrResumeReading(); - } +// private void myReadSize() +// { +// myCurrentReadBuffer = new byte[4]; +// myCurrentReadBufferOffset = 0; +// myCurrentReadState = ReadState.readingSize; +// myBeginOrResumeReading(); +// } - private void myReadData(int dataSize) - { - myCurrentReadBuffer = new byte[dataSize]; - myCurrentReadBufferOffset = 0; - myCurrentReadState = ReadState.readingData; - myBeginOrResumeReading(); - } +// private void myReadData(int dataSize) +// { +// myCurrentReadBuffer = new byte[dataSize]; +// myCurrentReadBufferOffset = 0; +// myCurrentReadState = ReadState.readingData; +// myBeginOrResumeReading(); +// } - private void myBeginOrResumeReading() - { - if (myAlive) - { - try - { - int beginReceiveAttempts = 0; - while (true) - { - beginReceiveAttempts++; - SocketError readSockErr; - mySock.BeginReceive(myCurrentReadBuffer, myCurrentReadBufferOffset, - myCurrentReadBuffer.Length - myCurrentReadBufferOffset, - SocketFlags.None, out readSockErr, - new AsyncCallback(myOnRead), - myCurrentReadBuffer); - Console.WriteLine($"AsyncTCPConnection.myBeginOrResumeReading socket_error: {readSockErr} connected: {mySock.Connected}"); - if (readSockErr == SocketError.NoBufferSpaceAvailable) - { - string msg = String.Format("Not enough buffer space to receive {0} bytes. Attempt: {1} of {2}.", - myCurrentReadBuffer.Length - myCurrentReadBufferOffset, - beginReceiveAttempts, PubSub3Config.instance.PS3_retriesOnNoBufferSpace + 1); - //BlueEventLog.logEvent(msg, PS3LogLevels.disconnection); +// private void myBeginOrResumeReading() +// { +// if (myAlive) +// { +// try +// { +// int beginReceiveAttempts = 0; +// while (true) +// { +// beginReceiveAttempts++; +// SocketError readSockErr; +// mySock.BeginReceive(myCurrentReadBuffer, myCurrentReadBufferOffset, +// myCurrentReadBuffer.Length - myCurrentReadBufferOffset, +// SocketFlags.None, out readSockErr, +// new AsyncCallback(myOnRead), +// myCurrentReadBuffer); +// Console.WriteLine($"AsyncTCPConnection.myBeginOrResumeReading socket_error: {readSockErr} connected: {mySock.Connected}"); +// if (readSockErr == SocketError.NoBufferSpaceAvailable) +// { +// string msg = String.Format("Not enough buffer space to receive {0} bytes. Attempt: {1} of {2}.", +// myCurrentReadBuffer.Length - myCurrentReadBufferOffset, +// beginReceiveAttempts, PubSub3Config.instance.PS3_retriesOnNoBufferSpace + 1); +// //BlueEventLog.logEvent(msg, PS3LogLevels.disconnection); - if (beginReceiveAttempts < (PubSub3Config.instance.PS3_retriesOnNoBufferSpace + 1)) - System.Threading.Thread.Sleep(PubSub3Config.instance.PS3_millisecondsBetweenNoBufferSpaceRetries); - else - { - //BlueEventLog.logEvent("Could not recover from NoBufferSpace condition. Breaking connection.", PS3LogLevels.disconnection); - myHandleError(readSockErr); - } - } - else if (readSockErr == SocketError.Success) - { - if (beginReceiveAttempts > 1) - // BlueEventLog.logEvent(String.Format("Recovered from NoBufferSpace after {0} tries",beginReceiveAttempts-1) , - // PS3LogLevels.disconnection); - break; - } - else - myHandleError(readSockErr); - } - } - catch (Exception ex) - { - myHandleError(ex); - } - } - } +// if (beginReceiveAttempts < (PubSub3Config.instance.PS3_retriesOnNoBufferSpace + 1)) +// System.Threading.Thread.Sleep(PubSub3Config.instance.PS3_millisecondsBetweenNoBufferSpaceRetries); +// else +// { +// //BlueEventLog.logEvent("Could not recover from NoBufferSpace condition. Breaking connection.", PS3LogLevels.disconnection); +// myHandleError(readSockErr); +// } +// } +// else if (readSockErr == SocketError.Success) +// { +// if (beginReceiveAttempts > 1) +// // BlueEventLog.logEvent(String.Format("Recovered from NoBufferSpace after {0} tries",beginReceiveAttempts-1) , +// // PS3LogLevels.disconnection); +// break; +// } +// else +// myHandleError(readSockErr); +// } +// } +// catch (Exception ex) +// { +// myHandleError(ex); +// } +// } +// } - private void myOnRead(IAsyncResult res) - { - if (myAlive) - { - try - { - SocketError readErr; - Console.WriteLine($"AsyncTCPConnection.myOnRead pre connected: {mySock.Connected}"); - int bytesRead = mySock.EndReceive(res, out readErr); - Console.WriteLine($"AsyncTCPConnection.myOnRead bytes: {bytesRead} readErr: {readErr} connected: {mySock.Connected}"); - myCurrentReadBufferOffset += bytesRead; - if (readErr == SocketError.Success) - { - if (myCurrentReadBufferOffset == myCurrentReadBuffer.Length) - { - ///read the whole of the buffer - if (myCurrentReadState == ReadState.readingSize) - { - ///the size has already been read, now convert - ///it into an int, and start reading the data - int dataSize = new BinaryReader(new MemoryStream(myCurrentReadBuffer)).ReadInt32(); - if (dataSize > myMaxMessageSize) - { - string errMessage = "Encountered malformed message, size indicated = " + dataSize; - //BlueEventLog.logEvent(errMessage, "Error"); - myHandleError(new ApplicationException(errMessage)); - } - else - myReadData(dataSize); - } - else - { - ///the whole data has been finally read, time to - ///tell the subscriber about it - BinaryReader br = new BinaryReader(new MemoryStream(myCurrentReadBuffer)); - int msgType = br.ReadInt32(); - if (msgType == 0) - { - ///skips the message ID - br.ReadInt32(); - byte[] data = br.ReadBytes(myCurrentReadBuffer.Length - 8); - ///callbacks can be made through thread pool to avoid this - ///thread from being blocked, by this callback - if (!myDataReceived) - { - myDataReceived = true; - Console.WriteLine($"AsyncTCPConnection Data received for the first time from peer {myPeer.ToString()}"); - } - myMessageHandler.onData(new RemoteMessage(data, RemoteMessageType.Data, myPeer)); +// private void myOnRead(IAsyncResult res) +// { +// if (myAlive) +// { +// try +// { +// SocketError readErr; +// Console.WriteLine($"AsyncTCPConnection.myOnRead pre connected: {mySock.Connected}"); +// int bytesRead = mySock.EndReceive(res, out readErr); +// Console.WriteLine($"AsyncTCPConnection.myOnRead bytes: {bytesRead} readErr: {readErr} connected: {mySock.Connected}"); +// myCurrentReadBufferOffset += bytesRead; +// if (readErr == SocketError.Success) +// { +// if (myCurrentReadBufferOffset == myCurrentReadBuffer.Length) +// { +// ///read the whole of the buffer +// if (myCurrentReadState == ReadState.readingSize) +// { +// ///the size has already been read, now convert +// ///it into an int, and start reading the data +// int dataSize = new BinaryReader(new MemoryStream(myCurrentReadBuffer)).ReadInt32(); +// if (dataSize > myMaxMessageSize) +// { +// string errMessage = "Encountered malformed message, size indicated = " + dataSize; +// //BlueEventLog.logEvent(errMessage, "Error"); +// myHandleError(new ApplicationException(errMessage)); +// } +// else +// myReadData(dataSize); +// } +// else +// { +// ///the whole data has been finally read, time to +// ///tell the subscriber about it +// BinaryReader br = new BinaryReader(new MemoryStream(myCurrentReadBuffer)); +// int msgType = br.ReadInt32(); +// if (msgType == 0) +// { +// ///skips the message ID +// br.ReadInt32(); +// byte[] data = br.ReadBytes(myCurrentReadBuffer.Length - 8); +// ///callbacks can be made through thread pool to avoid this +// ///thread from being blocked, by this callback +// if (!myDataReceived) +// { +// myDataReceived = true; +// Console.WriteLine($"AsyncTCPConnection Data received for the first time from peer {myPeer.ToString()}"); +// } +// myMessageHandler.onData(new RemoteMessage(data, RemoteMessageType.Data, myPeer)); - } - else if (msgType == 1) - { - if (!myHBReceived) - { - myHBReceived = true; - Console.WriteLine($"AsyncTCPConnection HB received for the first time from peer {myPeer.ToString()}"); - } - myLastHBReceivedTime = DateTime.Now; - myMessageHandler.onData(new RemoteMessage(null, RemoteMessageType.Heartbeat, myPeer)); - } - myReadSize(); - } - } - else - { - ///still part of the buffer left to read - ///continue reading - myBeginOrResumeReading(); - } - } - else - { - myHandleError(readErr); - } - } - catch (Exception ex) - { - myHandleError(ex); - } - } - } +// } +// else if (msgType == 1) +// { +// if (!myHBReceived) +// { +// myHBReceived = true; +// Console.WriteLine($"AsyncTCPConnection HB received for the first time from peer {myPeer.ToString()}"); +// } +// myLastHBReceivedTime = DateTime.Now; +// myMessageHandler.onData(new RemoteMessage(null, RemoteMessageType.Heartbeat, myPeer)); +// } +// myReadSize(); +// } +// } +// else +// { +// ///still part of the buffer left to read +// ///continue reading +// myBeginOrResumeReading(); +// } +// } +// else +// { +// myHandleError(readErr); +// } +// } +// catch (Exception ex) +// { +// myHandleError(ex); +// } +// } +// } - #endregion +// #endregion - #region IDisposable Members +// #region IDisposable Members - public void Dispose() - { - Console.WriteLine("AsyncTCPConnection.Dispose()"); - lock (this) - { - if (myAlive) - { - myMasterWorker.removeWorker(this); - myAlive = false; - // BlueEventLog.logEvent(String.Format("AsycnTCPConnection with peer {0} being disposed, last HB was received at {1}" , - // myPeer.ToString(), myLastHBReceivedTime.ToLongTimeString()), - // PS3LogLevels.disconnection); - try - { - mySock.Close(); - } - catch (Exception ex) - { - String.Format("Exception of type {0} thrown while trying to close socket connected to {1} , with" + - " the following exception msg {2}", ex.GetType().ToString(), myPeer.ToString(), ex.Message); - //BlueEventLog.logEvent(ex); - if (ex is OutOfMemoryException) - throw ex; - } - } - } - } +// public void Dispose() +// { +// Console.WriteLine("AsyncTCPConnection.Dispose()"); +// lock (this) +// { +// if (myAlive) +// { +// myMasterWorker.removeWorker(this); +// myAlive = false; +// // BlueEventLog.logEvent(String.Format("AsycnTCPConnection with peer {0} being disposed, last HB was received at {1}" , +// // myPeer.ToString(), myLastHBReceivedTime.ToLongTimeString()), +// // PS3LogLevels.disconnection); +// try +// { +// mySock.Close(); +// } +// catch (Exception ex) +// { +// String.Format("Exception of type {0} thrown while trying to close socket connected to {1} , with" + +// " the following exception msg {2}", ex.GetType().ToString(), myPeer.ToString(), ex.Message); +// //BlueEventLog.logEvent(ex); +// if (ex is OutOfMemoryException) +// throw ex; +// } +// } +// } +// } - #endregion +// #endregion - private void myHandleError(object err) - { - lock (this) - { - string errMsg = ""; - if (err is Exception) - { - errMsg = String.Format("AsyncTCPConnection.Dispose Encountered an exception of type '{0}' on the socket connected to ip = {1} , port = {2} ; " + - "Exception message = {3} , if the socket has not been disposed, it will be disposed now", err.GetType(), myPeer.ip, myPeer.port, (err as Exception).Message); - if (err is OutOfMemoryException) - throw (err as OutOfMemoryException); - } - else if (err is SocketError) - { - errMsg = String.Format("AsyncTCPConnection.Dispose Encountered a SocketError of type '{0}' on the socket connected to ip = {1} , port = {2} ; " + - "if the socket has not been disposed, it will be disposed now", err.ToString(), myPeer.ip, myPeer.port); - Console.WriteLine($"AsyncTCPConnection.myHandleError errMsg: {errMsg}"); - } - //BlueEventLog.logEvent(errMsg, PS3LogLevels.disconnection); - if (myAlive) - { - Dispose(); - } - } - } - } -} \ No newline at end of file +// private void myHandleError(object err) +// { +// lock (this) +// { +// string errMsg = ""; +// if (err is Exception) +// { +// errMsg = String.Format("AsyncTCPConnection.Dispose Encountered an exception of type '{0}' on the socket connected to ip = {1} , port = {2} ; " + +// "Exception message = {3} , if the socket has not been disposed, it will be disposed now", err.GetType(), myPeer.ip, myPeer.port, (err as Exception).Message); +// if (err is OutOfMemoryException) +// throw (err as OutOfMemoryException); +// } +// else if (err is SocketError) +// { +// errMsg = String.Format("AsyncTCPConnection.Dispose Encountered a SocketError of type '{0}' on the socket connected to ip = {1} , port = {2} ; " + +// "if the socket has not been disposed, it will be disposed now", err.ToString(), myPeer.ip, myPeer.port); +// Console.WriteLine($"AsyncTCPConnection.myHandleError errMsg: {errMsg}"); +// } +// //BlueEventLog.logEvent(errMsg, PS3LogLevels.disconnection); +// if (myAlive) +// { +// Dispose(); +// } +// } +// } +// } +// } \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnectionAcceptor_APM.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnectionAcceptor_APM.cs new file mode 100644 index 000000000..57ea31643 --- /dev/null +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnectionAcceptor_APM.cs @@ -0,0 +1,75 @@ +using System.Net.Sockets; +using System.Net; + +using BCG; +using System; +using System.Collections.Generic; + + +namespace bts { + + // opens a TCP port and starts listening to connections + public class AsyncTCPConnectionAcceptor_APM { + private readonly int _port; + private Socket _accept_socket; + private ITCPConnectionHandler _connection_handler; + private HashSet _key_map = new HashSet(); + public AsyncTCPConnectionAcceptor_APM(int port, ITCPConnectionHandler connection_handler) { + _port = port; + _connection_handler = connection_handler; + } + + public void init() { + _init(); + _begin_accepting(); + } + + private void _init() { + _accept_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, _port); + _accept_socket.Bind(endpoint); + _accept_socket.Listen(250); + } + + private void _begin_accepting() { + var accept_key = Guid.NewGuid().ToString(); + var res = _accept_socket.BeginAccept(new AsyncCallback(_on_accept), accept_key); + if (res != null && res.IsCompleted && res.CompletedSynchronously) + _on_accept(res); + } + + private void _on_accept(IAsyncResult res) { + bts.Log.Gen.info("AsyncTCPConnectionAcceptor_APM._on_accept state: {res.AsyncState}"); + string key = res.AsyncState.ToString(); + if (_key_map.Contains(key)) { + return; + } + _key_map.Add(key); + Socket new_sock; + try { + new_sock = _accept_socket.EndAccept(res); + } catch (Exception ex) { + bts.Log.Gen.info("AsyncTCPConnectionAcceptor_APM._on_accept exception thrown by EndAccept ex: {ex}"); + System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(250)); + _begin_accepting(); + return; + } + new_sock.LingerState = new LingerOption(false, 0); + new_sock.Blocking = false; + ITCPConnection newConn = new AsyncTCPConnection_APM(new_sock, TimeSpan.FromSeconds(1), ITCPConnection.ConnectionSide.Server); + _connection_handler.onConnection(newConn); + // go back to accepting more conns + _begin_accepting(); + string ip; int port; + newConn.getPeerAddress(out ip, out port); + string connMsg = String.Format("Connection to port {2} made from {0}:{1}", ip, port, _port); + // BlueEventLog.logEvent(connMsg, PS3LogLevels.connection); + //Console.WriteLine(connMsg); + } + + + + + } + +} \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection_APM.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection_APM.cs index 03e04f2eb..79df641e4 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection_APM.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection_APM.cs @@ -243,6 +243,7 @@ namespace bts { port = 0; if (_is_alive) { IPEndPoint ipEp = _sock.RemoteEndPoint as IPEndPoint; + port = ipEp.Port; ip = ipEp.Address.ToString(); } } diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_APM.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_APM.cs new file mode 100644 index 000000000..3f92756d3 --- /dev/null +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_APM.cs @@ -0,0 +1,65 @@ +using System.Net.Sockets; +using System.Net; + +using BCG; +using System; +using System.Collections.Generic; + + +namespace bts { + // Unlike AsyncTCPConnectionAcceptor_APM this class is static since the requests to connect are stand alone requests + // whereas AsyncTCPConnectionAcceptor_APM is always listening for incoming connections which lends itself to more + // statefullness and hence non static + public static class AsyncTCPConnector_APM { + + internal class SocketConnectionArgs { + public EndPoint ep; + public Socket sock; + public ITCPConnectionHandler handler; + public string server_url; + + internal SocketConnectionArgs(EndPoint theEp, Socket the_sock, ITCPConnectionHandler the_handler, + string theServerURL) { + ep = theEp; + sock = the_sock; + handler = the_handler; + server_url = theServerURL; + } + + public override string ToString() { + return $"SocketConnectionArgs: ep: {ep} server_url: {server_url}"; + } + } + + public static void connect_to(BlutoSubAddress server_address, ITCPConnectionHandler handler) { + bts.Log.Gen.info($"AsyncTCPConnector_APM.connectTo server: {server_address}"); + // when connection is established, this sock passed to an ITCPCOnnection and sent back + Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + EndPoint ep = new IPEndPoint(IPAddress.Parse(server_address.ip), server_address.port); + sock.BeginConnect(ep, new AsyncCallback(_on_connect), + new SocketConnectionArgs(ep, sock, handler, server_address.ToString())); + } + + private static void _on_connect(IAsyncResult res) { + bts.Log.Gen.info("AsyncTCPConnector_APM._on_connect " + res.ToString() + " " + res.AsyncState.ToString()); + SocketConnectionArgs sc_args = res.AsyncState as SocketConnectionArgs; + try { + sc_args.sock.EndConnect(res); + } catch (SocketException socket_ex) { + bts.Log.Gen.info($"AsyncTCPConnector_APM._on_connect EndConnect failed, args: {sc_args} ex: {socket_ex}"); + if (sc_args.handler.isDisposed) { + return; + } else { + System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(1000)); + bts.Log.Gen.info($"AsyncTCPConnector_APM retrying connectiont to server: {sc_args.server_url}"); + sc_args.sock.BeginConnect(sc_args.ep, new AsyncCallback(_on_connect), sc_args); + return; + } + } + bts.Log.Gen.info($"AsyncTCPConnector_APM._on_connect EndConnect successful args: {sc_args}"); + sc_args.handler.onConnection(new AsyncTCPConnection_APM(sc_args.sock, TimeSpan.FromSeconds(1), + ITCPConnection.ConnectionSide.Client)); + } + + } +} \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/ClientToServerManagedTCPConnector.cs b/src/DataStreamSuite/PubSub/ManagedTCP/ClientToServerManagedTCPConnector.cs index 73a2bd00f..3b987a8d9 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/ClientToServerManagedTCPConnector.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/ClientToServerManagedTCPConnector.cs @@ -75,7 +75,7 @@ namespace BCG { } } //BlueEventLog.logEvent(String.Format("Successful connection made to {0}.", args.serverURL.ToString()), PS3LogLevels.connection); - args.handler.onConnection(new AsyncTCPConnection(args.sock, myHBTimeSpan)); + args.handler.onConnection(new bts.AsyncTCPConnection_APM(args.sock, myHBTimeSpan, ITCPConnection.ConnectionSide.Client)); } } diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/SingleServerToClientManagedTCPConnectionAcceptor.cs b/src/DataStreamSuite/PubSub/ManagedTCP/SingleServerToClientManagedTCPConnectionAcceptor.cs index 291430b04..245fd5f51 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/SingleServerToClientManagedTCPConnectionAcceptor.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/SingleServerToClientManagedTCPConnectionAcceptor.cs @@ -89,7 +89,7 @@ namespace BCG { } newSock.LingerState = new LingerOption(false, 0); newSock.Blocking = false; - ITCPConnection newConn = new AsyncTCPConnection(newSock, myHBTimeSpan); + ITCPConnection newConn = new bts.AsyncTCPConnection_APM(newSock, myHBTimeSpan, ITCPConnection.ConnectionSide.Server); myHandler.onConnection(newConn); myAcceptSock.BeginAccept(new AsyncCallback(myOnAccept), new object[] { }); string ip; int port; diff --git a/src/DataStreamSuite/PubSub/Testing/SimpleTCPClient.cs b/src/DataStreamSuite/PubSub/Testing/SimpleTCPClient.cs index a466dd13f..449a5883e 100644 --- a/src/DataStreamSuite/PubSub/Testing/SimpleTCPClient.cs +++ b/src/DataStreamSuite/PubSub/Testing/SimpleTCPClient.cs @@ -9,55 +9,63 @@ using System.IO; using Spark.Core.Reactive; using System.Security.Cryptography.X509Certificates; using BCG; +using bts; +using System.Transactions; namespace BCG { - public class SimpleTCPClientAsync : ITCPConnectionHandler, ITCPMessageHandler { - //AsyncTCPConnection2 _conn = null; - int _m_msgs_received = 0; - UInt32 msg_sz = 1024 * 1024;// * 1024 * 1024; - readonly int max_n_msgs = Int32.MaxValue; - int n_msgs_sent = 0; + public class SimpleTCPClient : ITCPConnectionHandler, ITCPMessageHandler { + private readonly BlutoSubAddress _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 = 1000; - int wait_time_ms = 1000; - public static async Task start(string ip, int port) { - SimpleTCPClientAsync c = new SimpleTCPClientAsync(ip, port); - c.connect(); + public SimpleTCPClient(string server_ip, int server_port) { + _server_addr = new BlutoSubAddress(server_ip, server_port); } - public void connect() { - Task.Run(async () => await TCPSocketUtils.connect_to_server_async("127.0.0.1", 12345, this, TimeSpan.FromSeconds(2))); + public void start() { + _connect(); } + private void _connect() { + bts.AsyncTCPConnector_APM.connect_to(_server_addr, this); + } + + public void onConnection(ITCPConnection connection) { string ip; int port; connection.getPeerAddress(out ip, out port); - Console.WriteLine($"SimpleTCPClientAsync.onConnection connected to server ip: {ip} port: {port}, starting task to send data in a loop"); + bts.Log.Gen.info($"SimpleTCPClientAsync.onConnection connected to server ip: {ip} port: {port}, starting task to send data in a loop"); connection.register(this); // start sending data - Task.Run(async () => { - byte[] buff = new byte[msg_sz]; - for (int i = 0; i < buff.Length; i++) { - buff[i] = (byte)i; + _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]; + for (int i = 0; i < buff.Length; i++) { + buff[i] = (byte)i; + } + for (int i = 0; i < _max_n_msgs; i++) { + if (!_send_flag) { + return; } - //_conn = connection as AsyncTCPConnection2; - while (!connection.isDisposed) { - await Task.Delay(wait_time_ms); - if (n_msgs_sent < max_n_msgs) { - Console.WriteLine($"Sending msg_no: {n_msgs_sent}"); - // Console.ReadLine(); - connection.send(buff); - n_msgs_sent++; - } - } - }); + bts.Log.Gen.info("Sending msg: {i} sz: {buff.Length}"); + System.Threading.Thread.Sleep(_send_wait_period_ms); + connection.send(buff); + } } 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(); + _send_flag = false; + _connect(); } } @@ -65,7 +73,5 @@ namespace BCG { public void Dispose() { } - public SimpleTCPClientAsync(string ip, int port) { } - } } diff --git a/src/DataStreamSuite/PubSub/Testing/SimpleTCPServer.cs b/src/DataStreamSuite/PubSub/Testing/SimpleTCPServer.cs index c5add2c20..996ef9296 100644 --- a/src/DataStreamSuite/PubSub/Testing/SimpleTCPServer.cs +++ b/src/DataStreamSuite/PubSub/Testing/SimpleTCPServer.cs @@ -3,48 +3,39 @@ 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; using System.Collections.Generic; +using bts; namespace BCG { - public class SimpleTCPServerAsync : ITCPConnectionHandler, ITCPMessageHandler { + public class SimpleTCPServer : ITCPConnectionHandler, ITCPMessageHandler { int _n_msgs_received = 0; - SingleServerToClientManagedTCPConnectionAcceptor _acceptor = new SingleServerToClientManagedTCPConnectionAcceptor(); + AsyncTCPConnectionAcceptor_APM _acceptor; IDictionary _conn_map = new Dictionary(); - public static async Task start() { - Console.WriteLine("SimpleTCPServer.start accepting socket connections"); - SimpleTCPServerAsync s = new SimpleTCPServerAsync(); - s.accept(); + public SimpleTCPServer(int port) { + _acceptor = new AsyncTCPConnectionAcceptor_APM(port, this); } - public void accept() { - _acceptor.accept_on_port(this, "127.0.0.1", 12345, TimeSpan.Zero); + public void init() { + //_acceptor = new AsyncTCPConnectionAcceptor_APM(12345, this); + _acceptor.init(); } + public void onConnection(ITCPConnection connection) { string ip; int port; connection.getPeerAddress(out ip, out port); RemoteConnectionInfo rci = new RemoteConnectionInfo(ip, port); - _conn_map.Add(rci, connection); - Console.WriteLine($"SimpleTCPServerAsync.onConnection new client connected conn: {rci}"); + _conn_map[rci] = connection; + bts.Log.Gen.info($"SimpleTCPServer.onConnection new client connected conn: {rci}"); connection.register(this); } public void onData(RemoteMessage msg) { - Console.WriteLine($"SimpleTCPServerAsync.onData msg: {msg}"); + Console.WriteLine($"SimpleTCPServer.onData msg: {msg}"); RemoteConnectionInfo rci = msg.remoteConnectionInfo; ITCPConnection this_conn; bool found = _conn_map.TryGetValue(rci, out this_conn); @@ -56,6 +47,11 @@ namespace BCG { Console.WriteLine($"\n\nSimpleTCPServerAsync.onData msgs_received: {_n_msgs_received} msg: {msg} ci: {rci} NOW echoing it back\n\n"); if (msg.messageType == RemoteMessageType.Data) { this_conn.send(msg.message); + } else { + if (msg.messageType == RemoteMessageType.Disconnection) { + var ci = msg.remoteConnectionInfo; + _conn_map.Remove(ci); + } } } diff --git a/src/DataStreamSuite/PubSub/documentation/readme.txt b/src/DataStreamSuite/PubSub/documentation/readme.txt new file mode 100644 index 000000000..28363c318 --- /dev/null +++ b/src/DataStreamSuite/PubSub/documentation/readme.txt @@ -0,0 +1,5 @@ +ITCPConnection implemented by AsyncTCPConnection_APM is the innermost class resposinsble for TCP sends and received. Its used +on both client and server symmetrically. All the hard async send/receive logic is there. +AsyncTCPConnectionAcceptor_APM opens a port and listens and accepts to connections from the client. It creates the ITCPConnection +on the server. +AsyncTCPConnector_APM connects to the server (above) and creates an ITCPConnection on the client side. \ No newline at end of file diff --git a/src/DataStreamSuite/PubSubServer/Program.cs b/src/DataStreamSuite/PubSubServer/Program.cs index 9d6f7e413..d98cc68ac 100644 --- a/src/DataStreamSuite/PubSubServer/Program.cs +++ b/src/DataStreamSuite/PubSubServer/Program.cs @@ -14,14 +14,15 @@ namespace BCG { /// The main entry point for the application. /// [STAThread] - public static async Task Main(string[] args) { + public static void Main(string[] args) { bool run_echo_tcp_server_only = true; bts.utils.BTSNativeMethods.provide_command_line(new string[] { }, 0); mbts.config.Configuration.initialize(); if (run_echo_tcp_server_only) { - Console.WriteLine("Running as TCP Echo server, Hit key to continue"); + Console.WriteLine("Running as TCP Echo server, Hit enter to continue"); Console.ReadLine(); - await SimpleTCPServerAsync.start(); + SimpleTCPServer sts = new SimpleTCPServer(12345); + sts.init(); Task.Delay(1000000000).Wait(); } if (args.Length > 0 && args[0] == "daemon")