You can now choose port
where shard communication is happening. JSON explanations with instructions on how to ed it optional shard settings
This commit is contained in:
		@@ -9,13 +9,20 @@ namespace NadekoBot.DataStructures.ShardCom
 | 
			
		||||
{
 | 
			
		||||
    public class ShardComClient
 | 
			
		||||
    {
 | 
			
		||||
        private int port;
 | 
			
		||||
 | 
			
		||||
        public ShardComClient(int port)
 | 
			
		||||
        {
 | 
			
		||||
            this.port = port;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Send(ShardComMessage data)
 | 
			
		||||
        {
 | 
			
		||||
            var msg = JsonConvert.SerializeObject(data);
 | 
			
		||||
            using (var client = new UdpClient())
 | 
			
		||||
            {
 | 
			
		||||
                var bytes = Encoding.UTF8.GetBytes(msg);
 | 
			
		||||
                await client.SendAsync(bytes, bytes.Length, IPAddress.Loopback.ToString(), ShardComServer.Port).ConfigureAwait(false);
 | 
			
		||||
                await client.SendAsync(bytes, bytes.Length, IPAddress.Loopback.ToString(), port).ConfigureAwait(false);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -9,8 +9,12 @@ namespace NadekoBot.DataStructures.ShardCom
 | 
			
		||||
{
 | 
			
		||||
    public class ShardComServer : IDisposable
 | 
			
		||||
    {
 | 
			
		||||
        public const int Port = 5664;
 | 
			
		||||
        private readonly UdpClient _client = new UdpClient(Port);
 | 
			
		||||
        private readonly UdpClient _client;
 | 
			
		||||
 | 
			
		||||
        public ShardComServer(int port)
 | 
			
		||||
        {
 | 
			
		||||
            _client = new UdpClient(port);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Start()
 | 
			
		||||
        {
 | 
			
		||||
 
 | 
			
		||||
@@ -65,9 +65,9 @@ namespace NadekoBot
 | 
			
		||||
        public int ShardId { get; }
 | 
			
		||||
        public ShardsCoordinator ShardCoord { get; private set; }
 | 
			
		||||
 | 
			
		||||
        private readonly ShardComClient _comClient = new ShardComClient();
 | 
			
		||||
        private readonly ShardComClient _comClient;
 | 
			
		||||
 | 
			
		||||
        public NadekoBot(int shardId, int parentProcessId)
 | 
			
		||||
        public NadekoBot(int shardId, int parentProcessId, int? port = null)
 | 
			
		||||
        {
 | 
			
		||||
            if (shardId < 0)
 | 
			
		||||
                throw new ArgumentOutOfRangeException(nameof(shardId));
 | 
			
		||||
@@ -79,6 +79,10 @@ namespace NadekoBot
 | 
			
		||||
            TerribleElevatedPermissionCheck();
 | 
			
		||||
 | 
			
		||||
            Credentials = new BotCredentials();
 | 
			
		||||
 | 
			
		||||
            port = port ?? Credentials.ShardRunPort;
 | 
			
		||||
            _comClient = new ShardComClient(port.Value);
 | 
			
		||||
 | 
			
		||||
            Db = new DbService(Credentials);
 | 
			
		||||
 | 
			
		||||
            using (var uow = Db.UnitOfWork)
 | 
			
		||||
@@ -108,7 +112,7 @@ namespace NadekoBot
 | 
			
		||||
            Currency = new CurrencyService(BotConfig, Db);
 | 
			
		||||
            GoogleApi = new GoogleApiService(Credentials);
 | 
			
		||||
 | 
			
		||||
            SetupShard(shardId, parentProcessId);
 | 
			
		||||
            SetupShard(shardId, parentProcessId, port.Value);
 | 
			
		||||
 | 
			
		||||
#if GLOBAL_NADEKO
 | 
			
		||||
            Client.Log += Client_Log;
 | 
			
		||||
@@ -411,7 +415,7 @@ namespace NadekoBot
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void SetupShard(int shardId, int parentProcessId)
 | 
			
		||||
        private void SetupShard(int shardId, int parentProcessId, int port)
 | 
			
		||||
        {
 | 
			
		||||
            if (shardId != 0)
 | 
			
		||||
            {
 | 
			
		||||
@@ -432,7 +436,7 @@ namespace NadekoBot
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                ShardCoord = new ShardsCoordinator();
 | 
			
		||||
                ShardCoord = new ShardsCoordinator(port);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -4,8 +4,13 @@
 | 
			
		||||
    {
 | 
			
		||||
        public static void Main(string[] args)
 | 
			
		||||
        {
 | 
			
		||||
            if (args.Length == 2 && int.TryParse(args[0], out int shardId) && int.TryParse(args[1], out int parentProcessId))
 | 
			
		||||
                new NadekoBot(shardId, parentProcessId).RunAndBlockAsync(args).GetAwaiter().GetResult();
 | 
			
		||||
            if (args.Length == 3 && int.TryParse(args[0], out int shardId) && int.TryParse(args[1], out int parentProcessId))
 | 
			
		||||
            {
 | 
			
		||||
                int? port = null;
 | 
			
		||||
                if (int.TryParse(args[2], out var outPort))
 | 
			
		||||
                    port = outPort;
 | 
			
		||||
                new NadekoBot(shardId, parentProcessId, outPort).RunAndBlockAsync(args).GetAwaiter().GetResult();
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
                new NadekoBot(0, 0).RunAndBlockAsync(args).GetAwaiter().GetResult();
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -34,6 +34,7 @@ namespace NadekoBot.Services.Impl
 | 
			
		||||
        public string PatreonAccessToken { get; }
 | 
			
		||||
        public string ShardRunCommand { get; }
 | 
			
		||||
        public string ShardRunArguments { get; }
 | 
			
		||||
        public int ShardRunPort { get; }
 | 
			
		||||
 | 
			
		||||
        public BotCredentials()
 | 
			
		||||
        {
 | 
			
		||||
@@ -65,11 +66,16 @@ namespace NadekoBot.Services.Impl
 | 
			
		||||
                PatreonAccessToken = data[nameof(PatreonAccessToken)];
 | 
			
		||||
                ShardRunCommand = data[nameof(ShardRunCommand)];
 | 
			
		||||
                ShardRunArguments = data[nameof(ShardRunArguments)];
 | 
			
		||||
 | 
			
		||||
                if (string.IsNullOrWhiteSpace(ShardRunCommand))
 | 
			
		||||
                    ShardRunCommand = "dotnet";
 | 
			
		||||
                if (string.IsNullOrWhiteSpace(ShardRunArguments))
 | 
			
		||||
                    ShardRunArguments = "run -c Release -- {0} {1}";
 | 
			
		||||
                    ShardRunArguments = "run -c Release -- {0} {1} {2}";
 | 
			
		||||
                
 | 
			
		||||
                var portStr = data[nameof(ShardRunPort)];
 | 
			
		||||
                if (string.IsNullOrWhiteSpace(portStr))
 | 
			
		||||
                    ShardRunPort = new NadekoRandom().Next(5000, 6000);
 | 
			
		||||
                else
 | 
			
		||||
                    ShardRunPort = int.Parse(portStr);
 | 
			
		||||
 | 
			
		||||
                int ts = 1;
 | 
			
		||||
                int.TryParse(data[nameof(TotalShards)], out ts);
 | 
			
		||||
@@ -115,7 +121,10 @@ namespace NadekoBot.Services.Impl
 | 
			
		||||
            public DBConfig Db { get; set; } = new DBConfig("sqlite", "Filename=./data/NadekoBot.db");
 | 
			
		||||
            public int TotalShards { get; set; } = 1;
 | 
			
		||||
            public string PatreonAccessToken { get; set; } = "";
 | 
			
		||||
 | 
			
		||||
            public string ShardRunCommand { get; set; } = "";
 | 
			
		||||
            public string ShardRunArguments { get; set; } = "";
 | 
			
		||||
            public int? ShardRunPort { get; set; } = null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private class DbModel
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ namespace NadekoBot.Services.Impl
 | 
			
		||||
        private readonly IBotCredentials _creds;
 | 
			
		||||
        private readonly DateTime _started;
 | 
			
		||||
 | 
			
		||||
        public const string BotVersion = "1.43";
 | 
			
		||||
        public const string BotVersion = "1.5";
 | 
			
		||||
 | 
			
		||||
        public string Author => "Kwoth#2560";
 | 
			
		||||
        public string Library => "Discord.Net";
 | 
			
		||||
 
 | 
			
		||||
@@ -20,16 +20,18 @@ namespace NadekoBot
 | 
			
		||||
 | 
			
		||||
        private readonly Logger _log;
 | 
			
		||||
        private readonly ShardComServer _comServer;
 | 
			
		||||
        private readonly int _port;
 | 
			
		||||
 | 
			
		||||
        public ShardsCoordinator()
 | 
			
		||||
        public ShardsCoordinator(int port)
 | 
			
		||||
        {
 | 
			
		||||
            LogSetup.SetupLogger();
 | 
			
		||||
            Credentials = new BotCredentials();
 | 
			
		||||
            ShardProcesses = new Process[Credentials.TotalShards];
 | 
			
		||||
            Statuses = new ShardComMessage[Credentials.TotalShards];
 | 
			
		||||
            _log = LogManager.GetCurrentClassLogger();
 | 
			
		||||
            _port = port;
 | 
			
		||||
 | 
			
		||||
            _comServer = new ShardComServer();
 | 
			
		||||
            _comServer = new ShardComServer(port);
 | 
			
		||||
            _comServer.Start();
 | 
			
		||||
 | 
			
		||||
            _comServer.OnDataReceived += _comServer_OnDataReceived;
 | 
			
		||||
@@ -51,7 +53,7 @@ namespace NadekoBot
 | 
			
		||||
                var p = Process.Start(new ProcessStartInfo()
 | 
			
		||||
                {
 | 
			
		||||
                    FileName = Credentials.ShardRunCommand,
 | 
			
		||||
                    Arguments = string.Format(Credentials.ShardRunArguments, i, curProcessId)
 | 
			
		||||
                    Arguments = string.Format(Credentials.ShardRunArguments, i, curProcessId, _port)
 | 
			
		||||
                });
 | 
			
		||||
                await Task.Delay(5000);
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -16,5 +16,7 @@
 | 
			
		||||
  },
 | 
			
		||||
  "TotalShards": 1,
 | 
			
		||||
  "PatreonAccessToken": "",
 | 
			
		||||
  "ShardRunCommand": ""
 | 
			
		||||
  "ShardRunCommand": "",
 | 
			
		||||
  "ShardRunArguments": "",
 | 
			
		||||
  "ShardRunPort": null
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user