From 47fdff299336ba34454439443542c49100f2a45a Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sun, 3 Jan 2016 22:23:30 +0100 Subject: [PATCH] WCF 1.5/2 --- ArchiSteamFarm/Bot.cs | 28 +++++++++--------------- ArchiSteamFarm/Program.cs | 7 +++--- ArchiSteamFarm/WCF.cs | 46 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index e34003736..539e67ccb 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -31,6 +31,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; +using System.Text; using System.Threading.Tasks; using System.Xml; @@ -39,9 +40,9 @@ namespace ArchiSteamFarm { private const ulong ArchiSCFarmGroup = 103582791440160998; private const ushort CallbackSleep = 500; // In miliseconds - private static readonly ConcurrentDictionary Bots = new ConcurrentDictionary(); private static readonly uint LoginID = MsgClientLogon.ObfuscationMask; // This must be the same for all ASF bots and all ASF processes + internal static readonly ConcurrentDictionary Bots = new ConcurrentDictionary(); internal static readonly HashSet GlobalBlacklist = new HashSet { 303700, 335590, 368020, 425280 }; private readonly string ConfigFile, LoginKeyFile, MobileAuthenticatorFile, SentryFile; @@ -424,26 +425,17 @@ namespace ArchiSteamFarm { } } - private void ResponseStatus(ulong steamID, string botName = null) { - if (steamID == 0) { - return; + internal static string ResponseStatus(string botName) { + if (string.IsNullOrEmpty(botName)) { + return null; } Bot bot; - - if (string.IsNullOrEmpty(botName)) { - bot = this; - } else { - if (!Bots.TryGetValue(botName, out bot)) { - SendMessage(steamID, "Couldn't find any bot named " + botName + "!"); - return; - } + if (!Bots.TryGetValue(botName, out bot)) { + return "Couldn't find any bot named " + botName + "!"; } - if (bot.CardsFarmer.CurrentGamesFarming.Count > 0) { - SendMessage(steamID, "Bot " + bot.BotName + " is currently farming appIDs: " + string.Join(", ", bot.CardsFarmer.CurrentGamesFarming) + " and has a total of " + bot.CardsFarmer.GamesToFarm.Count + " games left to farm"); - } - SendMessage(steamID, "Currently " + Bots.Count + " bots are running"); + return "Bot " + bot.BotName + " is currently farming appIDs: " + string.Join(", ", bot.CardsFarmer.CurrentGamesFarming) + " and has a total of " + bot.CardsFarmer.GamesToFarm.Count + " games left to farm"; } private void Response2FA(ulong steamID, string botName = null) { @@ -582,7 +574,7 @@ namespace ArchiSteamFarm { await Program.Restart().ConfigureAwait(false); break; case "!status": - ResponseStatus(steamID); + SendMessage(steamID, ResponseStatus(BotName)); break; case "!stop": await Shutdown().ConfigureAwait(false); @@ -607,7 +599,7 @@ namespace ArchiSteamFarm { await ResponseStop(steamID, args[1]).ConfigureAwait(false); break; case "!status": - ResponseStatus(steamID, args[1]); + SendMessage(steamID, ResponseStatus(args[1])); break; } } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 88ee49571..176bae1eb 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -100,7 +100,7 @@ namespace ArchiSteamFarm { internal static async Task Restart() { await Bot.ShutdownAllBots().ConfigureAwait(false); - System.Diagnostics.Process.Start(ExecutableFile); + System.Diagnostics.Process.Start(ExecutableFile, string.Join(" ", Environment.GetCommandLineArgs())); Environment.Exit(0); } @@ -151,7 +151,7 @@ namespace ArchiSteamFarm { } internal static void OnBotShutdown() { - if (Bot.GetRunningBotsCount() == 0) { + if (Mode != EMode.Server && Bot.GetRunningBotsCount() == 0) { Logging.LogGenericInfo("Main", "No bots are running, exiting"); ShutdownResetEvent.Set(); } @@ -190,7 +190,8 @@ namespace ArchiSteamFarm { continue; } - WCF.SendCommand(arg); + Logging.LogGenericNotice("WCF", "Command sent: " + arg); + Logging.LogGenericNotice("WCF", "Response received: " + WCF.SendCommand(arg)); break; } } diff --git a/ArchiSteamFarm/WCF.cs b/ArchiSteamFarm/WCF.cs index 3afdddc7b..6c5aec834 100644 --- a/ArchiSteamFarm/WCF.cs +++ b/ArchiSteamFarm/WCF.cs @@ -1,17 +1,20 @@ using System; using System.ServiceModel; +using System.ServiceModel.Channels; namespace ArchiSteamFarm { [ServiceContract] internal interface IWCF { [OperationContract] - string SendCommand(string command); + string HandleCommand(string input); } internal class WCF : IWCF { + private const string URL = "http://localhost:1242/ASF"; // 1242 = 1024 + A(65) + S(83) + F(70) private ServiceHost ServiceHost; + private Client Client; internal void StartServer() { if (ServiceHost != null) { @@ -45,8 +48,45 @@ namespace ArchiSteamFarm { ServiceHost = null; } - public string SendCommand(string command) { - throw new NotImplementedException(); + internal string SendCommand(string input) { + if (Client == null) { + Client = new Client(new BasicHttpBinding(), new EndpointAddress(URL)); + } + + return Client.HandleCommand(input); + } + + public string HandleCommand(string input) { + if (string.IsNullOrEmpty(input)) { + return null; + } + + string[] args = input.Split(' '); + if (args.Length < 2) { + return "Too few arguments, expected: "; + } + + string command = args[0]; + string botName = args[1]; + string argument; + if (args.Length > 2) { + argument = args[3]; + } + + switch (command) { + case "status": + return Bot.ResponseStatus(botName); + default: + return "Unrecognized command: " + command; + } + } + } + + internal class Client : ClientBase, IWCF { + internal Client(Binding binding, EndpointAddress address) : base(binding, address) { } + + public string HandleCommand(string input) { + return Channel.HandleCommand(input); } } }