Beispiel #1
0
        public DcEngine(EngineSettings settings)
        {
            Settings          = settings;
            Settings.Changed += SettingsChanged;

            TagInfo = new TagInfo();

            Hubs             = new HubCollection();
            Hubs.HubAdded   += HubsHubAdded;
            Hubs.HubRemoved += HubsHubRemoved;

            SearchManager   = new SearchManager(this);
            DownloadManager = new DownloadManager(this);
            DownloadManager.DownloadAdding    += DownloadManagerDownloadAdding;
            DownloadManager.DownloadCompleted += DownloadManager_DownloadCompleted;

            TransferManager = new TransferManager(this);
            TransferManager.TransferAdded             += TransferManagerTransferAdded;
            TransferManager.TransferRemoved           += TransferManagerTransferRemoved;
            TransferManager.TransferUploadItemError   += TransferManagerTransferUploadItemError;
            TransferManager.TransferUploadItemRequest += TransferManager_TransferUploadItemRequest;

            SourceManager     = new SourceManager();
            FileSourceManager = new FileSourceManager();

            StatisticsManager = new StatisticsManager(this);

            UploadCacheManager = new UploadCacheManager(this);

            if (Settings.AutoSelectPort)
            {
                var tcpPort = Settings.TcpPort;
                var udpPort = Settings.UdpPort;

                while (!TcpConnectionListener.IsPortFree(tcpPort))
                {
                    tcpPort++;
                }
                if (Settings.TcpPort != tcpPort)
                {
                    Settings.TcpPort = tcpPort;
                }
                else
                {
                    InitTcp(Settings.TcpPort);
                }

                while (!UdpConnection.IsPortFree(udpPort))
                {
                    udpPort++;
                }
                if (Settings.UdpPort != udpPort)
                {
                    Settings.UdpPort = udpPort;
                }
                else
                {
                    InitUdp(Settings.UdpPort);
                }
            }
            else
            {
                InitUdp(Settings.UdpPort);
                InitTcp(Settings.TcpPort);
            }

            if (string.IsNullOrEmpty(Settings.LocalAddress))
            {
                // find local ip
                var host = Dns.GetHostEntry(Dns.GetHostName());

                //Array.Sort(host.AddressList, (one, two) => (one.ToString().StartsWith("192.168")?1:0)+ (two.ToString().StartsWith("192.168")?-1:0));

                foreach (var ip in host.AddressList)
                {
                    if (ip.AddressFamily.ToString() == "InterNetwork")
                    {
                        Settings.LocalAddress = ip.ToString();
                        break;
                    }
                }
            }

            if (Settings.HttpQueueLimit != 0)
            {
                HttpUploadItem.Manager.QueueLimit = Settings.HttpQueueLimit;
            }
            if (Settings.HttpConnectionsPerServer != 0)
            {
                HttpUploadItem.Manager.ConnectionsPerServer = Settings.HttpConnectionsPerServer;
            }
        }
Beispiel #2
0
        public void InitSinglePlayerServer(WorldParameters worldParam)
        {
            if (Server != null)
            {
                throw new InvalidOperationException("Already initialized");
            }

            _worldParam = worldParam;

            _serverFactory        = new EntityFactory();
            _serverFactory.Config = _worldParam.Configuration;
            var dbPath = Path.Combine(_vars.ApplicationDataPath, "Server", "Singleplayer", worldParam.WorldName, "ServerWorld.db");

            logger.Info("Local world db path is {0}", dbPath);

            _serverSqliteStorageSinglePlayer = new SqliteStorageManager(dbPath, _serverFactory, worldParam);
            _serverSqliteStorageSinglePlayer.Register("local", "qwe123".GetSHA1Hash(), UserRole.Administrator);

            var settings = new XmlSettingsManager <ServerSettings>(@"Server\localServer.config");

            settings.Load();
            settings.Save();

            //Utopia New Landscape Test

            IWorldProcessor          processor = null;
            IEntitySpawningControler entitySpawningControler = null;

            switch (worldParam.Configuration.WorldProcessor)
            {
            case WorldConfiguration.WorldProcessors.Flat:
                processor = new FlatWorldProcessor();
                break;

            case WorldConfiguration.WorldProcessors.Utopia:
                processor = new UtopiaProcessor(worldParam, _serverFactory, _landscapeEntityManager);
                entitySpawningControler = new UtopiaEntitySpawningControler((UtopiaWorldConfiguration)worldParam.Configuration);
                break;

            default:
                break;
            }

            var worldGenerator = new WorldGenerator(worldParam, processor);

            worldGenerator.EntitySpawningControler = entitySpawningControler;

            //Old s33m3 landscape
            //IWorldProcessor processor1 = new s33m3WorldProcessor(worldParam);
            //IWorldProcessor processor2 = new LandscapeLayersProcessor(worldParam, _serverFactory);
            //var worldGenerator = new WorldGenerator(worldParam, processor1, processor2);

            //Vlad Generator
            //var planProcessor = new PlanWorldProcessor(wp, _serverFactory);
            //var worldGenerator = new WorldGenerator(wp, planProcessor);
            settings.Settings.ChunksCountLimit = 1024 * 3; // better use viewRange * viewRange * 3

            var port = 4815;

            while (!TcpConnectionListener.IsPortFree(port))
            {
                port++;
            }
            settings.Settings.ServerPort = port;

            _server = new ServerCore(settings, worldGenerator, _serverSqliteStorageSinglePlayer, _serverSqliteStorageSinglePlayer, _serverSqliteStorageSinglePlayer, _serverSqliteStorageSinglePlayer, _serverFactory, worldParam);
            _serverFactory.LandscapeManager     = Server.LandscapeManager;
            _serverFactory.DynamicEntityManager = Server.AreaManager;
            _serverFactory.GlobalStateManager   = Server.GlobalStateManager;
            _serverFactory.ScheduleManager      = Server.Scheduler;
            _serverFactory.ServerSide           = true;

            _server.Initialize();

            Server.ConnectionManager.LocalMode = true;
            Server.ConnectionManager.Listen();
            Server.LoginManager.PlayerEntityNeeded  += LoginManagerPlayerEntityNeeded;
            Server.LoginManager.GenerationParameters = default(Utopia.Shared.World.PlanGenerator.GenerationParameters); // planProcessor.WorldPlan.Parameters;
            Server.Clock.SetCurrentTimeOfDay(UtopiaTimeSpan.FromHours(12));
        }
Beispiel #3
0
        public void Initialize()
        {
            string downloadsPath = Client.Settings.StorageAutoSelect ? StorageHelper.GetBestSaveDirectory() : Client.Settings.StoragePath;

            if (Client.Settings.TorrentTcpPort == 0)
            {
                var r = new Random();
                int port;
                while (!TcpConnectionListener.IsPortFree(port = r.Next(6881, 7000)))
                {
                }
                logger.Info("Auto selected torrent port: {0}", port);
                Client.Settings.TorrentTcpPort = port;
            }

            var engineSettings = new EngineSettings(downloadsPath, Client.Settings.TorrentTcpPort)
            {
                PreferEncryption  = false,
                AllowedEncryption = EncryptionTypes.All
            };

            _torrentDefaults = new TorrentSettings(4, 50, 0, 0);

            _engine = new ClientEngine(engineSettings);
            _engine.ChangeListenEndpoint(new IPEndPoint(IPAddress.Any, Client.Settings.TorrentTcpPort));
            byte[] nodes = null;
            try
            {
                nodes = File.ReadAllBytes(TorrentDhtNodesPath);
            }
            catch
            {
                logger.Info("No existing dht nodes could be loaded");
            }

            var dhtListner = new DhtListener(new IPEndPoint(IPAddress.Any, Client.Settings.TorrentTcpPort));
            var dht        = new DhtEngine(dhtListner);

            _engine.RegisterDht(dht);
            dhtListner.Start();
            _engine.DhtEngine.Start(nodes);

            // If the SavePath does not exist, we want to create it.
            if (!Directory.Exists(_engine.Settings.SavePath))
            {
                Directory.CreateDirectory(_engine.Settings.SavePath);
            }

            // If the torrentsPath does not exist, we want to create it
            if (!Directory.Exists(TorrentsFolder))
            {
                Directory.CreateDirectory(TorrentsFolder);
            }

            BEncodedDictionary fastResume;

            try
            {
                if (File.Exists(TorrentFastResumePath))
                {
                    fastResume = BEncodedValue.Decode <BEncodedDictionary>(File.ReadAllBytes(TorrentFastResumePath));
                }
                else
                {
                    fastResume = new BEncodedDictionary();
                }
            }
            catch
            {
                fastResume = new BEncodedDictionary();
            }

            var knownTorrentsPaths = Directory.GetFiles(TorrentsFolder, "*.torrent");

            logger.Info("Loading {0} saved torrents", knownTorrentsPaths.Length);

            foreach (var file in knownTorrentsPaths)
            {
                Torrent torrent;
                try
                {
                    torrent = Torrent.Load(file);
                }
                catch (Exception e)
                {
                    logger.Error("Couldn't decode {0}: ", file);
                    logger.Error(e.Message);
                    continue;
                }

                var manager = new TorrentManager(torrent, downloadsPath, TorrentDefaults);
                if (fastResume.ContainsKey(torrent.InfoHash.ToHex()))
                {
                    manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.InfoHash.ToHex()]));
                }
                RegisterTorrent(manager);

                manager.Start();
            }

#if DEBUG
            FrmDebug       = new FrmDownloadDebug();
            FrmDebug.Width = Screen.PrimaryScreen.WorkingArea.Width;
            FrmDebug.Show();
#endif
        }