Skip to content

Commit be7fda8

Browse files
Update Listeners.cs
1 parent 2a7a894 commit be7fda8

1 file changed

Lines changed: 44 additions & 3 deletions

File tree

Flames/Network/Listeners.cs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public abstract class INetListen
3232
/// <summary> Begins listening for connections on the given IP and port </summary>
3333
/// <remarks> Client connections are asynchronously accepted </remarks>
3434
public abstract void Listen(IPAddress ip, int port);
35+
public virtual void Listen2(IPAddress ip, int port)
36+
{
37+
Listen(ip, port);
38+
}
3539

3640
/// <summary> Closes this network listener </summary>
3741
public abstract void Close();
@@ -81,6 +85,40 @@ public void EnableAddressReuse()
8185
// not really a critical issue if this fails to work
8286
}
8387
}
88+
public override void Listen2(IPAddress ip, int port)
89+
{
90+
if (IP == ip && Port == port) return;
91+
Close();
92+
IP = ip;
93+
Port = port;
94+
95+
try
96+
{
97+
socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
98+
DisableIPV6OnlyListener();
99+
EnableAddressReuse();
100+
101+
socket.Bind(new IPEndPoint(ip, port));
102+
socket.Listen((int)SocketOptionName.MaxConnections);
103+
AcceptNextAsync();
104+
}
105+
catch (Exception ex)
106+
{
107+
108+
Logger.LogError(ex);
109+
Logger.Log(LogType.Warning, "Failed to start listening on port {0} ({1})", port, ex.Message);
110+
111+
string msg = string.Format("Failed to start listening. Is another server or instance of {0} already running on port {1}?",
112+
Server.SoftwareName, port);
113+
Server.UpdateUrl(msg);
114+
socket = null;
115+
Logger.Log(LogType.Warning, "Retrying..");
116+
Listen(ip, port);
117+
//return;
118+
}
119+
Listening = true;
120+
Logger.Log(LogType.SystemActivity, "Started listening on port {0}... ", port);
121+
}
84122

85123
public override void Listen(IPAddress ip, int port)
86124
{
@@ -101,14 +139,17 @@ public override void Listen(IPAddress ip, int port)
101139
}
102140
catch (Exception ex)
103141
{
142+
104143
Logger.LogError(ex);
105144
Logger.Log(LogType.Warning, "Failed to start listening on port {0} ({1})", port, ex.Message);
106145

107146
string msg = string.Format("Failed to start listening. Is another server or instance of {0} already running on port {1}?",
108147
Server.SoftwareName, port);
109148
Server.UpdateUrl(msg);
110-
socket = null;
111-
return;
149+
socket = null;
150+
Logger.Log(LogType.Warning, "Retrying..");
151+
Listen2(ip, port);
152+
//return;
112153
}
113154
Listening = true;
114155
Logger.Log(LogType.SystemActivity, "Started listening on port {0}... ", port);
@@ -184,4 +225,4 @@ public override void Close()
184225
}
185226
}
186227
}
187-
}
228+
}

0 commit comments

Comments
 (0)