Permalink
Browse files

* Windows-os leallitas javitva a botban.

  • Loading branch information...
1 parent bed8764 commit 12f2f78564d4f194de59229bc60e5014c2e9ab18 Megax committed May 26, 2012
@@ -0,0 +1,61 @@
+/*
+ * This file is part of Schumix.
+ *
+ * Copyright (C) 2010-2012 Megax <http://www.megaxx.info/>
+ *
+ * Schumix is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Schumix is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Schumix. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
+using System.Threading;
+using Mono.Unix;
+using Mono.Unix.Native;
+using Schumix.Framework;
+using Schumix.Framework.Client;
+
+namespace Schumix.Server
+{
+ class Linux
+ {
+ private readonly ServerPacketHandler sServerPacketHandler = Singleton<ServerPacketHandler>.Instance;
+ private readonly Utilities sUtilities = Singleton<Utilities>.Instance;
+ private Linux() {}
+
+ public void Init()
+ {
+ new Thread(TerminateHandler).Start();
+ }
+
+ private void TerminateHandler()
+ {
+ Log.Notice("Linux", "Initializing Handler for SIGINT");
+ var signal = new UnixSignal(Signum.SIGINT);
+ signal.WaitOne();
+
+ Log.Notice("Linux", "Handler Terminated.");
+ sUtilities.RemovePidFile();
+ MainClass.sListener.Exit = true;
+ System.Console.CursorVisible = true;
+ var packet = new SchumixPacket();
+ packet.Write<int>((int)Opcode.SMSG_CLOSE_CONNECTION);
+ packet.Write<int>((int)0);
+
+ foreach(var list in sServerPacketHandler.HostList)
+ sServerPacketHandler.SendPacketBack(packet, list.Value, list.Key.Split(SchumixBase.Colon)[0], Convert.ToInt32(list.Key.Split(SchumixBase.Colon)[1]));
+
+ Thread.Sleep(2000);
+ MainClass.KillAllSchumixProccess();
+ }
+ }
+}
@@ -43,6 +43,8 @@ class MainClass
private static readonly New.Schumix sSchumix = Singleton<New.Schumix>.Instance;
private static readonly Utilities sUtilities = Singleton<Utilities>.Instance;
private static readonly Runtime sRuntime = Singleton<Runtime>.Instance;
+ private static readonly Windows sWindows = Singleton<Windows>.Instance;
+ private static readonly Linux sLinux = Singleton<Linux>.Instance;
public static ServerListener sListener { get; private set; }
/// <summary>
@@ -130,21 +132,10 @@ private static void Main(string[] args)
if(File.Exists("Installer.exe"))
File.Delete("Installer.exe");
- System.Console.CancelKeyPress += (sender, e) =>
- {
- sUtilities.RemovePidFile();
- sListener.Exit = true;
- System.Console.CursorVisible = true;
- var packet = new SchumixPacket();
- packet.Write<int>((int)Opcode.SMSG_CLOSE_CONNECTION);
- packet.Write<int>((int)0);
-
- foreach(var list in sServerPacketHandler.HostList)
- sServerPacketHandler.SendPacketBack(packet, list.Value, list.Key.Split(SchumixBase.Colon)[0], Convert.ToInt32(list.Key.Split(SchumixBase.Colon)[1]));
-
- Thread.Sleep(2000);
- KillAllSchumixProccess();
- };
+ if(sUtilities.GetPlatformType() == PlatformType.Windows)
+ sWindows.Init();
+ else if(sUtilities.GetPlatformType() == PlatformType.Linux)
+ sLinux.Init();
AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) =>
{
@@ -182,7 +173,7 @@ private static void Help()
System.Console.WriteLine("\t--console-localization=Value\tSet up the program's console language settings");
}
- private static void KillAllSchumixProccess()
+ public static void KillAllSchumixProccess()
{
foreach(var list in sSchumix._processlist)
{
@@ -58,7 +58,7 @@ private void Run()
{
try
{
- if(MainClass.sListener.Exit)
+ if(!MainClass.sListener.IsNull() && MainClass.sListener.Exit)
return;
foreach(var list in _processlist)
@@ -53,6 +53,9 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
+ <Reference Include="Mono.Posix">
+ <HintPath>..\..\Dependencies\Mono.Posix.dll</HintPath>
+ </Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
@@ -61,6 +64,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Config\Config.cs" />
<Compile Include="New\Schumix.cs" />
+ <Compile Include="Linux\Linux.cs" />
+ <Compile Include="Windows\Windows.cs" />
+ <Compile Include="Windows\CtrlType.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@@ -0,0 +1,32 @@
+/*
+ * This file is part of Schumix.
+ *
+ * Copyright (C) 2010-2012 Megax <http://www.megaxx.info/>
+ *
+ * Schumix is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Schumix is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Schumix. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
+
+namespace Schumix.Server
+{
+ enum CtrlType
+ {
+ CTRL_C_EVENT = 0,
+ CTRL_BREAK_EVENT = 1,
+ CTRL_CLOSE_EVENT = 2,
+ CTRL_LOGOFF_EVENT = 5,
+ CTRL_SHUTDOWN_EVENT = 6
+ }
+}
@@ -0,0 +1,86 @@
+/*
+ * This file is part of Schumix.
+ *
+ * Copyright (C) 2010-2012 Megax <http://www.megaxx.info/>
+ *
+ * Schumix is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Schumix is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Schumix. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
+using System.Threading;
+using System.Runtime.InteropServices;
+using Schumix.Framework;
+using Schumix.Framework.Client;
+
+namespace Schumix.Server
+{
+ class Windows
+ {
+ [DllImport("Kernel32")]
+ private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
+ private readonly ServerPacketHandler sServerPacketHandler = Singleton<ServerPacketHandler>.Instance;
+ private readonly Utilities sUtilities = Singleton<Utilities>.Instance;
+ private delegate bool EventHandler(CtrlType sig);
+ private EventHandler _handler;
+ private Windows() {}
+
+ public void Init()
+ {
+ _handler += new EventHandler(Handler);
+ SetConsoleCtrlHandler(_handler, true);
+ }
+
+ private bool Handler(CtrlType sig)
+ {
+ var packet = new SchumixPacket();
+ packet.Write<int>((int)Opcode.SMSG_CLOSE_CONNECTION);
+ packet.Write<int>((int)0);
+
+ switch(sig)
+ {
+ case CtrlType.CTRL_C_EVENT:
+ case CtrlType.CTRL_BREAK_EVENT:
+ case CtrlType.CTRL_CLOSE_EVENT:
+ Log.Notice("Windows", "Daemon killed.");
+ sUtilities.RemovePidFile();
+ MainClass.sListener.Exit = true;
+ System.Console.CursorVisible = true;
+
+ foreach(var list in sServerPacketHandler.HostList)
+ sServerPacketHandler.SendPacketBack(packet, list.Value, list.Key.Split(SchumixBase.Colon)[0], Convert.ToInt32(list.Key.Split(SchumixBase.Colon)[1]));
+
+ Thread.Sleep(2000);
+ MainClass.KillAllSchumixProccess();
+ break;
+ case CtrlType.CTRL_LOGOFF_EVENT:
+ case CtrlType.CTRL_SHUTDOWN_EVENT:
+ Log.Notice("Windows", "User is logging off.");
+ sUtilities.RemovePidFile();
+ MainClass.sListener.Exit = true;
+ System.Console.CursorVisible = true;
+
+ foreach(var list in sServerPacketHandler.HostList)
+ sServerPacketHandler.SendPacketBack(packet, list.Value, list.Key.Split(SchumixBase.Colon)[0], Convert.ToInt32(list.Key.Split(SchumixBase.Colon)[1]));
+
+ Thread.Sleep(2000);
+ MainClass.KillAllSchumixProccess();
+ break;
+ default:
+ break;
+ }
+
+ return true;
+ }
+ }
+}
@@ -0,0 +1,51 @@
+/*
+ * This file is part of Schumix.
+ *
+ * Copyright (C) 2010-2012 Megax <http://www.megaxx.info/>
+ *
+ * Schumix is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Schumix is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Schumix. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
+using System.Threading;
+using Mono.Unix;
+using Mono.Unix.Native;
+using Schumix.Irc;
+using Schumix.Framework;
+
+namespace Schumix
+{
+ class Linux
+ {
+ private readonly Sender sSender = Singleton<Sender>.Instance;
+ private Linux() {}
+
+ public void Init()
+ {
+ new Thread(TerminateHandler).Start();
+ }
+
+ private void TerminateHandler()
+ {
+ Log.Notice("Linux", "Initializing Handler for SIGINT");
+ var signal = new UnixSignal(Signum.SIGINT);
+ signal.WaitOne();
+
+ Log.Notice("Linux", "Handler Terminated.");
+ SchumixBase.Quit();
+ sSender.Quit("Daemon killed.");
+ Thread.Sleep(5*1000);
+ }
+ }
+}
@@ -22,8 +22,6 @@
using System.Text;
using System.Threading;
using System.Globalization;
-using Mono.Unix;
-using Mono.Unix.Native;
using Schumix.Irc;
using Schumix.Updater;
using Schumix.Framework;
@@ -50,11 +48,13 @@ class MainClass
/// </summary>
private static readonly Utilities sUtilities = Singleton<Utilities>.Instance;
private static readonly Runtime sRuntime = Singleton<Runtime>.Instance;
+ private static readonly Windows sWindows = Singleton<Windows>.Instance;
/// <summary>
/// Hozzáférést biztosít singleton-on keresztül a megadott class-hoz.
/// Üzenet küldés az irc szerver felé.
/// </summary>
private static readonly Sender sSender = Singleton<Sender>.Instance;
+ private static readonly Linux sLinux = Singleton<Linux>.Instance;
/// <summary>
/// A Main függvény. Itt indul el a program.
@@ -204,17 +204,10 @@ private static void Main(string[] args)
new SchumixBot();
- if(sUtilities.GetPlatformType() == PlatformType.Linux)
- StartHandler();
- else
- {
- System.Console.CancelKeyPress += (sender, e) =>
- {
- SchumixBase.Quit();
- sSender.Quit("Daemon killed.");
- Thread.Sleep(5*1000);
- };
- }
+ if(sUtilities.GetPlatformType() == PlatformType.Windows)
+ sWindows.Init();
+ else if(sUtilities.GetPlatformType() == PlatformType.Linux)
+ sLinux.Init();
AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) =>
{
@@ -249,22 +242,5 @@ private static void Help()
System.Console.WriteLine("\t--server-identify=Value\t\tSet identify.");
System.Console.WriteLine("\t--server-configs=Value\t\tSend Schumix's parameters at all.");
}
-
- private static void StartHandler()
- {
- new Thread(TerminateHandler).Start();
- }
-
- private static void TerminateHandler()
- {
- Log.Notice("Main", "Initializing Handler for SIGINT");
- var signal = new UnixSignal(Signum.SIGINT);
- signal.WaitOne();
-
- Log.Notice("Main", "Handler Terminated");
- SchumixBase.Quit();
- sSender.Quit("Daemon killed.");
- Thread.Sleep(5*1000);
- }
}
}
@@ -68,6 +68,9 @@
<Compile Include="Console\Commands\CommandManager.cs" />
<Compile Include="Console\Commands\CommandHelp.cs" />
<Compile Include="ScriptManager.cs" />
+ <Compile Include="Windows\Windows.cs" />
+ <Compile Include="Linux\Linux.cs" />
+ <Compile Include="Windows\CtrlType.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@@ -95,5 +98,7 @@
<ItemGroup>
<Folder Include="Console\Commands\" />
<Folder Include="Console\" />
+ <Folder Include="Windows\" />
+ <Folder Include="Linux\" />
</ItemGroup>
</Project>
Oops, something went wrong.

0 comments on commit 12f2f78

Please sign in to comment.