public Task StartAsync(EmbeddedServerOptions options)
        {
            if (options.ServerMode == EmbeddedServerMode.None)
            {
                throw new Exception();
            }

            m_serverMode  = options.ServerMode;
            m_gameOptions = options.NewGameOptions;

            if (!System.IO.Directory.Exists(options.SaveGamePath))
            {
                System.IO.Directory.CreateDirectory(options.SaveGamePath);
            }

            Guid save = Guid.Empty;

            m_saveManager = new SaveManager(options.SaveGamePath);

            if (options.CleanSaveDir)
            {
                m_saveManager.DeleteAll();
            }
            else
            {
                save = m_saveManager.GetLatestSaveFile();
            }

            CreateEmbeddedServer(options.SaveGamePath, save);

            var tcs = new TaskCompletionSource <object>();

            var serverStartWaitHandle = new AutoResetEvent(false);

            ThreadPool.RegisterWaitForSingleObject(serverStartWaitHandle,
                                                   (o, timeout) =>
            {
                serverStartWaitHandle.Dispose();

                if (timeout)
                {
                    m_serverThread.Abort();
                    tcs.SetException(new Exception("Timeout waiting for server"));
                }
                else
                {
                    tcs.SetResult(null);
                }
            },
                                                   null, TimeSpan.FromSeconds(60), true);

            m_serverThread = new Thread(ServerMain);
            m_serverThread.Start(serverStartWaitHandle);

            return(tcs.Task);
        }
Beispiel #2
0
        public Task StartAsync(EmbeddedServerOptions options)
        {
            if (options.ServerMode == EmbeddedServerMode.None)
                throw new Exception();

            m_serverMode = options.ServerMode;
            m_gameOptions = options.NewGameOptions;

            if (!System.IO.Directory.Exists(options.SaveGamePath))
                System.IO.Directory.CreateDirectory(options.SaveGamePath);

            Guid save = Guid.Empty;

            m_saveManager = new SaveManager(options.SaveGamePath);

            if (options.CleanSaveDir)
                m_saveManager.DeleteAll();
            else
                save = m_saveManager.GetLatestSaveFile();

            CreateEmbeddedServer(options.SaveGamePath, save);

            var tcs = new TaskCompletionSource<object>();

            var serverStartWaitHandle = new AutoResetEvent(false);

            ThreadPool.RegisterWaitForSingleObject(serverStartWaitHandle,
                (o, timeout) =>
                {
                    serverStartWaitHandle.Dispose();

                    if (timeout)
                    {
                        m_serverThread.Abort();
                        tcs.SetException(new Exception("Timeout waiting for server"));
                    }
                    else
                    {
                        tcs.SetResult(null);
                    }
                },
                null, TimeSpan.FromSeconds(60), true);

            m_serverThread = new Thread(ServerMain);
            m_serverThread.Start(serverStartWaitHandle);

            return tcs.Task;
        }