using System; using System.Collections.Generic; 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 { private TimeSpan myHBTimeSpan, myWaitTimeBeforeRetrying = new TimeSpan(0, 0, 0, 0, 250); private ITCPConnectionHandler myHandler; private Socket myAcceptSock; private int myPort; 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); 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 totalAttempts = 0; while (!madeItThrough) { port = randomizer.Next(); port = theRange.minPort + (port % (theRange.maxPort - theRange.minPort)); try { myAcceptSock.Bind(new IPEndPoint(IPAddress.Any, port)); madeItThrough = true; } catch (Exception) { totalAttempts++; if (totalAttempts < maxAttempts) { //BlueEventLog.logEvent("Couldn't bind to port " + port.ToString() + "; retrying...", PS3LogLevels.connection); } 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[] { }); return myPort; } 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); myAcceptSock.Bind(endpoint); myAcceptSock.Listen(250); myAcceptSock.BeginAccept(new AsyncCallback(myOnAccept), new object[] { }); myPort = endpoint.Port; return myPort; // not sure why we need this class variable myPort, but I am just keeping it up with the existing code } public int accept_on_port(ITCPConnectionHandler handler, string ip, int port, TimeSpan client_HB_timespan) { Task.Run(async () => { await TCPSocketUtils.accept_connections_async(ip, port, handler, TimeSpan.FromSeconds(1)); }); return port; } private void myOnAccept(IAsyncResult res) { Socket newSock; try { newSock = myAcceptSock.EndAccept(res); } catch (Exception ex) { // BlueEventLog.logEvent(ex); Thread.Sleep(myWaitTimeBeforeRetrying); myAcceptSock.BeginAccept(new AsyncCallback(myOnAccept), new object[] { }); if (ex is OutOfMemoryException) throw ex; return; } newSock.LingerState = new LingerOption(false, 0); newSock.Blocking = false; 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 connMsg = String.Format("Connection to port {2} made from {0}:{1}", ip, port, myPort); // BlueEventLog.logEvent(connMsg, PS3LogLevels.connection); //Console.WriteLine(connMsg); } } }