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 bts; using System.Threading; using System.Runtime.InteropServices; 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 { struct TcpKeepAlive { public uint OnOff; public uint KeepAliveTime; public uint KeepAliveInterval; } public static void set_server_socket_options(ref Socket server_socket) { // Enable KeepAlive with detailed settings TcpKeepAlive keepAliveSettings = new TcpKeepAlive { OnOff = 1, // Enable KeepAlive KeepAliveTime = 60000, // 1 minute in milliseconds KeepAliveInterval = 10000 // 10 seconds in milliseconds }; server_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); // Set keep-alive interval and time (Linux-specific method) if (OperatingSystem.IsLinux()) { ; const int keepAliveTime = 60; // Seconds const int keepAliveInterval = 10; // Seconds server_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, keepAliveTime); server_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, keepAliveInterval); } else { int size = Marshal.SizeOf(keepAliveSettings); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(keepAliveSettings, ptr, true); byte[] inOptionValues = new byte[size]; Marshal.Copy(ptr, inOptionValues, 0, size); Marshal.FreeHGlobal(ptr); server_socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); } // Set other socket options as needed server_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); server_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); server_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000); server_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000); server_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 8192); server_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 8192); // Optionally configure linger option LingerOption lingerOption = new LingerOption(true, 10); server_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption); } public static RemoteConnectionInfo get_peer_address(Socket sock) { IPEndPoint ip_ep = sock.RemoteEndPoint as IPEndPoint; int port = ip_ep.Port; string ip = ip_ep.Address.ToString(); return new RemoteConnectionInfo(ip, port); } public static RemoteConnectionInfo get_self_address(Socket socket) { IPEndPoint ip_ep = socket.LocalEndPoint as IPEndPoint; int port = ip_ep.Port; string ip = ip_ep.Address.ToString(); return new RemoteConnectionInfo(ip, port); } public static void set_client_socket_options(ref Socket client_socket) { TcpKeepAlive keep_alive_settings = new TcpKeepAlive { OnOff = 1, // Enable KeepAlive KeepAliveTime = 60000, // 1 minute in milliseconds KeepAliveInterval = 10000 // 10 seconds in milliseconds }; if (OperatingSystem.IsLinux()) { const int keepAliveTime = 60; // Seconds const int keepAliveInterval = 10; // Seconds client_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, keepAliveTime); client_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, keepAliveInterval); } else { int size = Marshal.SizeOf(keep_alive_settings); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(keep_alive_settings, ptr, true); byte[] inOptionValues = new byte[size]; Marshal.Copy(ptr, inOptionValues, 0, size); Marshal.FreeHGlobal(ptr); client_socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); } // Set other socket options as needed client_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); client_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); client_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000); client_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000); client_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 8192); client_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 8192); // Optionally configure linger option LingerOption lingerOption = new LingerOption(true, 10); client_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption); } #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(); ITCPConnection new_conn = new bts.AsyncTCPConnection_APM(client_socket, TimeSpan.FromSeconds(1), ITCPConnection.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 bts.AsyncTCPConnection_APM(client_socket, TimeSpan.FromSeconds(1), ITCPConnection.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) { Log.Gen.info($"send_bytes_async data_buff: {data_buff.Length}"); // var data_sz = data_buff.Length; byte[] sz_buff = new byte[4]; // // send the size first BinaryWriter bw = new BinaryWriter(new MemoryStream(sz_buff)); bw.Write(data_buff.Length); await SendMessageAsync(nw_stream.Socket, new Memory(sz_buff)); // await nw_stream.WriteAsync(sz_buff, 0, 4); // await nw_stream.WriteAsync(data_buff, 0, data_buff.Length); Memory mem = new Memory(data_buff); await SendMessageAsync(nw_stream.Socket, mem); } public static async Task SendMessageAsync(Socket sock, Memory mem_buff) { // use ManualResetEvent to make it non async Log.Gen.info($"SendMessageAsync mem: {mem_buff.Length}"); SocketAsyncEventArgs evargs = new SocketAsyncEventArgs(); evargs.SetBuffer(mem_buff); var tcs = new TaskCompletionSource(); evargs.Completed += (s, e) => { Log.Gen.info($"SendMessageAsync completed asynchronously e: {sock_event_args_txt(e)}"); tcs.SetResult(true); }; bool completed = !sock.SendAsync(evargs); if (completed) { Log.Gen.info($"SendMessageAsync completed synchronously e: {sock_event_args_txt(evargs)}"); tcs.SetResult(true); } await tcs.Task; } // 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[] data_buffer; // try { // 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(); // data_buffer = new byte[msg_sz]; // bytesRead = await nw_stream.ReadAsync(data_buffer, 0, data_buffer.Length); // Log.Gen.info($"read_bytes_async intended: {msg_sz} received: {bytesRead}"); // } catch (Exception ex) { // Log.Gen.error($"read_bytes_async exception: {ex}"); // return new byte[] { }; // } // //Console.WriteLine($"read_bytes sz: {data_buffer.Length}"); // return data_buffer; return ReceiveSizePrefixedMessageAsync(nw_stream.Socket); } static string sock_event_args_txt(SocketAsyncEventArgs seargs) { return $"err: {seargs.SocketError} bytes: {seargs.BytesTransferred}"; } // this is a limited sync wrapper ovr the async ReceiveAsync static byte[] ReceiveSizePrefixedMessageAsync(Socket socket) { const int buffer_size = 4; // Size of the buffer to read the message size var size_buffer = new byte[buffer_size]; var size_event_args = new SocketAsyncEventArgs(); size_event_args.SetBuffer(size_buffer, 0, buffer_size); var size_completed_event = new ManualResetEvent(false); size_event_args.Completed += (sender, e) => { Log.Gen.info($"ReceiveSizePrefixedMessageAsync async completed (msg len) e: {sock_event_args_txt(e)}"); size_completed_event.Set(); // Signal that the size receive operation is complete }; if (!socket.ReceiveAsync(size_event_args)) { Log.Gen.info($"ReceiveSizePrefixedMessageAsync msg size read completed synchronously e: {sock_event_args_txt(seargs: size_event_args)}"); // If ReceiveAsync completed synchronously, set the event manually size_completed_event.Set(); } size_completed_event.WaitOne(); // Wait for the size receive operation to complete int message_size = BitConverter.ToInt32(size_buffer, 0); //Log.assert_d(message_size > 0, $"message_size: {message_size}"); if (message_size <= 0) throw new ApplicationException($"msg_size: {message_size} not allowed"); Log.Gen.info($"ReceiveSizePrefixedMessageAsync message_size: {message_size}"); // at this ppint message sz has been read, now read the data var message_buffer = new byte[message_size]; var message_event_args = new SocketAsyncEventArgs(); message_event_args.SetBuffer(message_buffer, 0, message_size); var message_completed_event = new ManualResetEvent(false); message_event_args.Completed += (sender, e) => { Log.Gen.info($"ReceiveSizePrefixedMessageAsync async completed (msg) e: {sock_event_args_txt(e)}"); message_completed_event.Set(); // Signal that the message receive operation is complete }; if (!socket.ReceiveAsync(message_event_args)) { Log.Gen.info($"ReceiveSizePrefixedMessageAsync msg read completed synchronously e: {sock_event_args_txt(seargs: message_event_args)}"); // If ReceiveAsync completed synchronously, set the event manually message_completed_event.Set(); } message_completed_event.WaitOne(); // Wait for the message receive operation to complete return message_buffer; } } #endregion }