コード例 #1
0
        private static void Connect()
        {
            plugin.Verbose("Attempting Bot Connection...");
            plugin.Verbose("Your Bot IP: " + Config.GetString("bot.ip") + ". Your Bot Port: " + Config.GetInt("bot.port") + ".");

            while (!IsConnected())
            {
                try
                {
                    if (socket != null && socket.IsBound)
                    {
                        //socket.Shutdown(SocketShutdown.Both);
                        messageThread?.Abort();
                        socket.Close();
                    }

                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect(Config.GetString("bot.ip"), Config.GetInt("bot.port"));
                    messageThread = new Thread(() => new BotListener(plugin));
                    messageThread.Start();

                    networkStream = new NetworkStream(socket);

                    plugin.Info("Connected to Discord bot.");
                    plugin.SendMessage(Config.GetArray("channels.statusmessages"), "botmessages.connectedtobot");
                }
                catch (SocketException e)
                {
                    plugin.VerboseError("Error occured while connecting to discord bot server: " + e.Message.Trim());
                    plugin.DebugError(e.ToString());
                    Thread.Sleep(5000);
                }
                catch (ObjectDisposedException e)
                {
                    plugin.VerboseError("TCP client was unexpectedly closed.");
                    plugin.DebugError(e.ToString());
                    Thread.Sleep(5000);
                }
                catch (ArgumentOutOfRangeException e)
                {
                    plugin.VerboseError("Invalid port.");
                    plugin.DebugError(e.ToString());
                    Thread.Sleep(5000);
                }
                catch (ArgumentNullException e)
                {
                    plugin.VerboseError("IP address is null.");
                    plugin.DebugError(e.ToString());
                    Thread.Sleep(5000);
                }
            }
        }