TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
openbwprocess.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include "forkserver.h"
11 #include <atomic>
12 #include <future>
13 #include <string>
14 #include <unordered_map>
15 #include <vector>
16 
17 namespace torchcraft {
18 class Client;
19 }
20 
21 namespace cherrypi {
22 
23 /**
24  * Launches and manages an OpenBW process.
25  */
27  public:
28  // Totally thread safe!
29  OpenBwProcess(std::vector<EnvVar> const& vars);
30  OpenBwProcess(std::string, std::vector<EnvVar> const& vars);
31  ~OpenBwProcess();
32 
33  /// Connect a TorchCraft client to this instance.
34  /// The timeout is passed to the client _and_ the future to wait on openbw,
35  /// and
36  /// it is executed in sequence, so the total timeout might be 2 * timeout
37  /// Returns whether the client connected successfully connected.
38  bool connect(torchcraft::Client* client, int timeoutMs = -1);
39 
40  /// No further forks will be started, and subsequent
41  /// OpenBwProcess constructors will throw
42  static void preventFurtherProcesses();
43 
44  private:
45  void redirectOutput();
46 
47  int pid_;
48  std::string socketPath_;
49  int fd_ = -1;
50  int wfd_ = -1;
51  std::future<void> goodf_;
52  std::promise<void> goodp_;
53  // Need to keep a variable alive for thread to continue running
54  std::future<void> outputThread_;
55  std::atomic_bool running_;
56 };
57 
58 } // namespace cherrypi
Copyright (c) 2015-present, Facebook, Inc.
Definition: openbwprocess.h:17
Launches and manages an OpenBW process.
Definition: openbwprocess.h:26
Definition: client.h:25
Main namespace for bot-related code.
Definition: areainfo.cpp:17