TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
client.h
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  */
9 
10 #pragma once
11 
12 #include <memory>
13 #include <set>
14 #include <vector>
15 
16 #include <torchcraft/constants.h>
17 
18 namespace torchcraft {
19 
20 void init();
21 
22 class State;
23 class Connection;
24 
25 class Client {
26  public:
27  struct Options {
28  std::string initial_map;
29  int window_size[2];
30  int window_pos[2];
32 
33  // A subset of unit types to consider when checking for end-of-game
34  // condition, for example.
35  std::set<BW::UnitType> only_consider_types;
36 
37  Options() : window_size{-1, -1}, window_pos{-1, -1}, micro_battles(false) {}
38  };
39 
40  struct Command {
41  int code = -1;
42  std::vector<int> args;
43  std::string str;
44 
45  Command() {}
46  Command(int code) : code(code) {}
47  Command(int code, std::string str) : code(code), str(std::move(str)) {}
48  Command(int code, std::initializer_list<int>&& args)
49  : code(code), args(args) {}
50  Command(int code, std::string str, std::initializer_list<int>&& args)
51  : code(code), args(args), str(std::move(str)) {}
52  Command(int code, std::string str, std::vector<int>& args)
53  : code(code), args(args), str(std::move(str)) {}
54  template <typename... Args>
55  Command(int code, int a, Args&&... args)
56  : Command(code, {a, std::forward<Args>(args)...}) {}
57  template <typename... Args>
58  Command(int code, std::string str, Args&&... args)
59  : Command(code, std::move(str), {std::forward<Args>(args)...}) {}
60  };
61 
62  // LIFECYCLE
63  Client();
64  ~Client();
65  Client(const Client&) = delete;
66  Client& operator=(const Client&) = delete;
67 
68  // OPERATIONS
69 
70  /// Create a socket connection and connect it to an endpoint specified by
71  /// a TCP address parametrized by a hostname and a port. The final endpoint
72  /// is defined as tcp://<hostname>:<port>
73  /// @param hostname [in] Hostname part of a TCP address for socket connection
74  /// @param port [in] Port part of a TCP address for socket connection
75  /// @param timeoutMs [in] Send / receive operation timeout in milliseconds,
76  /// the value is interpreted as follows:
77  /// -1 = blocking operation
78  /// 0 = non-blocking operation without retries
79  /// >0 = time (in milliseconds) after which the function returns an error,
80  /// if the operation was not accomplished
81  /// @return true if the connection was established; false otherwise
82  bool connect(const std::string& hostname, int port, int timeoutMs);
83 
84  /// Creates a new socket and connects it to an endpoint by a file socket.
85  /// ZMQ IPC is used for the connection; the full address is thus
86  /// ipc://<file_socket>
87  /// @param file_socket [in] file to use as the socket
88  /// @param timeoutMs [in] Send / receive operation timeout in milliseconds
89  /// (default = -1), the value is interpreted as follows:
90  /// -1 = blocking operation
91  /// 0 = non-blocking operation without retries
92  /// >0 = time (in milliseconds) after which the function returns an error,
93  /// if the operation was not accomplished
94  bool connect(const std::string& file_socket, int timeoutMs);
95 
96  /// Indicates whether the connection was successfully established
97  /// @return true if the connection was successfully established;
98  /// false otherwise
99  bool connected() const {
100  return conn_ != nullptr;
101  }
102 
103  /// Close the socket connection if it was previously successfully established
104  /// and destroy the associated socket
105  /// @return true if the connection was closed successfully; false if the
106  /// connection did not exist
107  bool close();
108 
109  /// Perform handshake over the established connection.
110  /// @param updates [out] State field names that were updated from
111  /// the handshake response
112  /// @param opts [in] Options to pass in the handshake message
113  /// (default = Options())
114  /// @return true if the handshake succeeded; false otherwise
115  bool init(std::vector<std::string>& updates, const Options& opts = Options());
116 
117  /// Send a message containing commands over the established socket connection
118  /// @param commands [in] Commands to send over the socket connection
119  /// @return true if the send operation succeeded, false otherwise
120  bool send(const std::vector<Command>& commands);
121 
122  /// Receive a message containing state updates over the established socket
123  /// connection
124  /// @param updates [out] State field names that were updated from
125  /// the received message
126  /// @return true if the receive operation succeeded, false otherwise
127  bool receive(std::vector<std::string>& updates);
128 
129  /// Blocks until a message is available for receive().
130  /// @return false on failure (timeout or lost connectivity), true otherwise
131  bool poll(long timeout = -1);
132 
133  std::string error() const {
134  return error_;
135  }
136 
137  std::vector<Command> lastCommands() const {
138  return lastCommands_;
139  }
140  std::vector<int8_t> lastCommandsStatus() const {
141  return lastCommandsStatus_;
142  }
143 
144  State* state() const {
145  return state_;
146  }
147 
148  private:
149  bool connect(std::unique_ptr<Connection>&&);
150  void clearError() {
151  error_.clear();
152  }
153 
154  // The connection is RAII and is created/reset in init().
155  std::unique_ptr<Connection> conn_;
156  State* state_;
157  bool sent_;
158  std::string error_;
159  std::string uid_;
160  std::vector<Command> lastCommands_;
161  std::vector<int8_t> lastCommandsStatus_;
162 };
163 
164 } // namespace torchcraft
std::vector< int > args
Definition: client.h:42
Copyright (c) 2015-present, Facebook, Inc.
Definition: openbwprocess.h:17
Command(int code, std::string str)
Definition: client.h:47
int window_pos[2]
Definition: client.h:30
bool receive(std::vector< std::string > &updates)
Receive a message containing state updates over the established socket connection.
bool connect(const std::string &hostname, int port, int timeoutMs)
Create a socket connection and connect it to an endpoint specified by a TCP address parametrized by a...
bool send(const std::vector< Command > &commands)
Send a message containing commands over the established socket connection.
bool micro_battles
Definition: client.h:31
STL namespace.
Definition: client.h:40
bool init(std::vector< std::string > &updates, const Options &opts=Options())
Perform handshake over the established connection.
std::string str
Definition: client.h:43
int window_size[2]
Definition: client.h:29
std::vector< Command > lastCommands() const
Definition: client.h:137
Command(int code, std::initializer_list< int > &&args)
Definition: client.h:48
std::string error() const
Definition: client.h:133
Command()
Definition: client.h:45
bool close()
Close the socket connection if it was previously successfully established and destroy the associated ...
Client & operator=(const Client &)=delete
Definition: state.h:43
Definition: client.h:25
Options()
Definition: client.h:37
Command(int code, std::string str, Args &&...args)
Definition: client.h:58
Command(int code, std::string str, std::initializer_list< int > &&args)
Definition: client.h:50
bool connected() const
Indicates whether the connection was successfully established.
Definition: client.h:99
std::vector< int8_t > lastCommandsStatus() const
Definition: client.h:140
Command(int code)
Definition: client.h:46
bool poll(long timeout=-1)
Blocks until a message is available for receive().
Definition: client.h:27
Command(int code, std::string str, std::vector< int > &args)
Definition: client.h:52
State * state() const
Definition: client.h:144
std::string initial_map
Definition: client.h:28
Command(int code, int a, Args &&...args)
Definition: client.h:55
void init()
Definition: cherrypi.cpp:317
std::set< BW::UnitType > only_consider_types
Definition: client.h:35