Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
Fixed critical network issue and another config load issue
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Dec 22, 2018
1 parent bac5be7 commit e5c8438
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 49 deletions.
110 changes: 64 additions & 46 deletions SCPDiscordPlugin/NetworkSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public static void Run(SCPDiscord plugin)
}
Thread.Sleep(1000);
}
catch(Exception)
catch(Exception e)
{
plugin.Warn("Network error caught, if this happens a lot try using the 'scpd_rc' command.");
plugin.Error("Network error caught, if this happens a lot try using the 'scpd_rc' command." + e);
}
}
}
Expand Down Expand Up @@ -114,7 +114,20 @@ public static bool IsConnected()
{
return false;
}
return !((socket.Poll(1000, SelectMode.SelectRead) && (socket.Available == 0)) || !socket.Connected);

try
{
return !((socket.Poll(1000, SelectMode.SelectRead) && (socket.Available == 0)) || !socket.Connected);
}
catch (ObjectDisposedException e)
{
if (Config.GetBool("settings.verbose"))
{
plugin.Error("TCP client was unexpectedly closed.");
plugin.Error(e.ToString());
}
return false;
}
}

private static void Connect(string address, int port)
Expand All @@ -123,67 +136,72 @@ private static void Connect(string address, int port)
{
plugin.Info("Attempting Bot Connection...");
}
try

while(!IsConnected())
{
if(socket != null && socket.IsBound)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
else
try
{
if(socket != null && socket.IsBound)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
if (Config.GetBool("settings.verbose"))
{
plugin.Info("Your Bot IP: " + Config.GetString("bot.ip") + ". Your Bot Port: " + Config.GetInt("bot.port") + ".");
}
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(Config.GetString("bot.ip"), Config.GetInt("bot.port"));
plugin.Info("Connected to Discord bot.");
plugin.SendMessage(Config.GetArray("channels.statusmessages"), "botmessages.connectedtobot");
}
if (Config.GetBool("settings.verbose"))
catch (SocketException e)
{
plugin.Info("Your Bot IP: " + Config.GetString("bot.ip") + ". Your Bot Port: " + Config.GetInt("bot.port") + ".");
}
socket.Connect(Config.GetString("bot.ip"), Config.GetInt("bot.port"));
plugin.Info("Connected to Discord bot.");
plugin.SendMessage(Config.GetArray("channels.statusmessages"), "botmessages.connectedtobot");
}
catch (SocketException e)
{
if (Config.GetBool("settings.verbose"))
{
plugin.Error("Error occured while connecting to discord bot server.");
plugin.Error(e.ToString());
if (Config.GetBool("settings.verbose"))
{
plugin.Error("Error occured while connecting to discord bot server: " + e.Message);
if(Config.GetBool("settings.verbose"))
{
plugin.Error(e.ToString());
}

}
Thread.Sleep(5000);
}
Thread.Sleep(5000);
}
catch (ObjectDisposedException e)
{
if (Config.GetBool("settings.verbose"))
catch (ObjectDisposedException e)
{
plugin.Error("TCP client was unexpectedly closed.");
plugin.Error(e.ToString());
if (Config.GetBool("settings.verbose"))
{
plugin.Error("TCP client was unexpectedly closed.");
plugin.Error(e.ToString());
}
Thread.Sleep(5000);
}
Thread.Sleep(5000);
}
catch (ArgumentOutOfRangeException e)
{
if (Config.GetBool("settings.verbose"))
catch (ArgumentOutOfRangeException e)
{
plugin.Error("Invalid port.");
plugin.Error(e.ToString());
if (Config.GetBool("settings.verbose"))
{
plugin.Error("Invalid port.");
plugin.Error(e.ToString());
}
Thread.Sleep(5000);
}
Thread.Sleep(5000);
}
catch (ArgumentNullException e)
{
if (Config.GetBool("settings.verbose"))
catch (ArgumentNullException e)
{
plugin.Error("IP address is null.");
plugin.Error(e.ToString());
if (Config.GetBool("settings.verbose"))
{
plugin.Error("IP address is null.");
plugin.Error(e.ToString());
}
Thread.Sleep(5000);
}
Thread.Sleep(5000);
}
}

public static void Disconnect()
{
socket.Disconnect(false);
socket = null;
//socket = null;
}
/// ///////////////////////////////////////////////

Expand Down
6 changes: 3 additions & 3 deletions SCPDiscordPlugin/SCPDiscord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace SCPDiscord
name = "SCPDiscord",
description = "SCP:SL - Discord bridge.",
id = "karlofduty.scpdiscord",
version = "1.0.0-C",
version = "1.0.0-D",
SmodMajor = 3,
SmodMinor = 2,
SmodRevision = 0
Expand Down Expand Up @@ -69,9 +69,9 @@ public override void OnEnable()
this.AddCommand("scpd_debug", new DebugCommand(this));


Task.Run(() =>
Task.Run(async () =>
{
Thread.Sleep(2000);
await Task.Delay(4000);
SetUpFileSystem();
LoadConfig();
roleSync = new RoleSync(this);
Expand Down

0 comments on commit e5c8438

Please sign in to comment.