Code style updates.
This commit is contained in:
parent
c9cc5ff23a
commit
4104e40843
42 changed files with 292 additions and 317 deletions
SharpChat
|
@ -21,7 +21,7 @@ namespace SharpChat {
|
|||
private Action<IWebSocketConnection> _config;
|
||||
|
||||
public SharpChatWebSocketServer(string location, bool supportDualStack = true) {
|
||||
Uri uri = new Uri(location);
|
||||
Uri uri = new(location);
|
||||
|
||||
Port = uri.Port;
|
||||
Location = location;
|
||||
|
@ -29,15 +29,15 @@ namespace SharpChat {
|
|||
|
||||
_locationIP = ParseIPAddress(uri);
|
||||
_scheme = uri.Scheme;
|
||||
Socket socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
|
||||
Socket socket = new(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
|
||||
|
||||
if (SupportDualStack && Type.GetType(@"Mono.Runtime") == null && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
||||
if(SupportDualStack && Type.GetType(@"Mono.Runtime") == null && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
||||
socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
|
||||
}
|
||||
|
||||
ListenerSocket = new SocketWrapper(socket);
|
||||
SupportedSubProtocols = new string[0];
|
||||
SupportedSubProtocols = Array.Empty<string>();
|
||||
}
|
||||
|
||||
public ISocket ListenerSocket { get; set; }
|
||||
|
@ -55,37 +55,38 @@ namespace SharpChat {
|
|||
|
||||
public void Dispose() {
|
||||
ListenerSocket.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private IPAddress ParseIPAddress(Uri uri) {
|
||||
private static IPAddress ParseIPAddress(Uri uri) {
|
||||
string ipStr = uri.Host;
|
||||
|
||||
if (ipStr == "0.0.0.0") {
|
||||
if(ipStr == "0.0.0.0") {
|
||||
return IPAddress.Any;
|
||||
} else if (ipStr == "[0000:0000:0000:0000:0000:0000:0000:0000]") {
|
||||
} else if(ipStr == "[0000:0000:0000:0000:0000:0000:0000:0000]") {
|
||||
return IPAddress.IPv6Any;
|
||||
} else {
|
||||
try {
|
||||
return IPAddress.Parse(ipStr);
|
||||
} catch (Exception ex) {
|
||||
} catch(Exception ex) {
|
||||
throw new FormatException("Failed to parse the IP address part of the location. Please make sure you specify a valid IP address. Use 0.0.0.0 or [::] to listen on all interfaces.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Start(Action<IWebSocketConnection> config) {
|
||||
IPEndPoint ipLocal = new IPEndPoint(_locationIP, Port);
|
||||
IPEndPoint ipLocal = new(_locationIP, Port);
|
||||
ListenerSocket.Bind(ipLocal);
|
||||
ListenerSocket.Listen(100);
|
||||
Port = ((IPEndPoint)ListenerSocket.LocalEndPoint).Port;
|
||||
FleckLog.Info(string.Format("Server started at {0} (actual port {1})", Location, Port));
|
||||
if (_scheme == "wss") {
|
||||
if (Certificate == null) {
|
||||
if(_scheme == "wss") {
|
||||
if(Certificate == null) {
|
||||
FleckLog.Error("Scheme cannot be 'wss' without a Certificate");
|
||||
return;
|
||||
}
|
||||
|
||||
if (EnabledSslProtocols == SslProtocols.None) {
|
||||
if(EnabledSslProtocols == SslProtocols.None) {
|
||||
EnabledSslProtocols = SslProtocols.Tls;
|
||||
FleckLog.Debug("Using default TLS 1.0 security protocol.");
|
||||
}
|
||||
|
@ -97,16 +98,16 @@ namespace SharpChat {
|
|||
private void ListenForClients() {
|
||||
ListenerSocket.Accept(OnClientConnect, e => {
|
||||
FleckLog.Error("Listener socket is closed", e);
|
||||
if (RestartAfterListenError) {
|
||||
if(RestartAfterListenError) {
|
||||
FleckLog.Info("Listener socket restarting");
|
||||
try {
|
||||
ListenerSocket.Dispose();
|
||||
Socket socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
|
||||
Socket socket = new(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
|
||||
ListenerSocket = new SocketWrapper(socket);
|
||||
Start(_config);
|
||||
FleckLog.Info("Listener socket restarted");
|
||||
} catch (Exception ex) {
|
||||
} catch(Exception ex) {
|
||||
FleckLog.Error("Listener could not be restarted", ex);
|
||||
}
|
||||
}
|
||||
|
@ -114,7 +115,7 @@ namespace SharpChat {
|
|||
}
|
||||
|
||||
private void OnClientConnect(ISocket clientSocket) {
|
||||
if (clientSocket == null) return; // socket closed
|
||||
if(clientSocket == null) return; // socket closed
|
||||
|
||||
FleckLog.Debug(string.Format("Client connected from {0}:{1}", clientSocket.RemoteIpAddress, clientSocket.RemotePort.ToString()));
|
||||
ListenForClients();
|
||||
|
@ -151,7 +152,7 @@ namespace SharpChat {
|
|||
},
|
||||
s => SubProtocolNegotiator.Negotiate(SupportedSubProtocols, s));
|
||||
|
||||
if (IsSecure) {
|
||||
if(IsSecure) {
|
||||
FleckLog.Debug("Authenticating Secure Connection");
|
||||
clientSocket
|
||||
.Authenticate(Certificate,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue