SC2API
An API for AI for StarCraft II
sc2_proto_interface.h
1 #pragma once
2 
3 #include "sc2_connection.h"
4 
5 #include "s2clientprotocol/sc2api.pb.h"
6 
7 #include <functional>
8 
9 namespace sc2 {
10 
11 class ControlInterface;
12 
13 const unsigned int kDefaultProtoInterfaceTimeout = 120000; // A generous 120 seconds.
14 
15 typedef std::shared_ptr<SC2APIProtocol::Request> GameRequestPtr;
16 typedef std::shared_ptr<const SC2APIProtocol::Response> GameResponsePtr;
17 
18 template<class MessageType>
20 public:
22  message_(nullptr) {
23  }
24 
25  void Set(GameResponsePtr response, const MessageType* message) {
26  response_ = response;
27  message_ = message;
28  }
29 
30  bool HasErrors() const {
31  if (!HasResponse())
32  return true;
33  if (response_->error_size() > 0)
34  return true;
35  if (!HasMessage())
36  return true;
37  return false;
38  }
39 
40  void Clear() {
41  message_ = nullptr;
42  response_ = nullptr;
43  }
44 
45  const MessageType* operator->() const {
46  assert(message_);
47  return message_;
48  }
49 
50  const MessageType* get() const {
51  assert(message_);
52  return message_;
53  }
54 
55  GameResponsePtr GetResponse() const {
56  return response_;
57  }
58 
59  bool HasResponse () const {
60  return response_.get() != nullptr;
61  }
62 
63  bool HasMessage () const {
64  return message_ != nullptr;
65  }
66 
67 private:
68  const MessageType* message_;
69  GameResponsePtr response_;
70 };
71 
72 #define SET_MESSAGE_RESPONSE(VAR, RESPONSE, MESSAGE_NAME) \
73  if (RESPONSE.get() && RESPONSE->has_##MESSAGE_NAME()) \
74  VAR.Set(RESPONSE, &RESPONSE->MESSAGE_NAME()); \
75  else \
76  VAR.Set(RESPONSE, nullptr);
77 
78 #define SET_SUBMESSAGE_RESPONSE(VAR, MESSAGE, SUBMESSAGE_NAME) \
79  if (!MESSAGE.HasErrors() && MESSAGE->has_##SUBMESSAGE_NAME()) \
80  VAR.Set(MESSAGE.GetResponse(), &MESSAGE->SUBMESSAGE_NAME()); \
81  else \
82  VAR.Set(MESSAGE.GetResponse(), nullptr);
83 
84 // Wraps proto and connections.
86 public:
88  bool ConnectToGame(const std::string& address, int port, int timeout_ms);
89  GameRequestPtr MakeRequest();
90  bool SendRequest(GameRequestPtr& request, bool ignore_pending_requests = false);
91  GameResponsePtr WaitForResponseInternal();
92  bool PingGame();
93  void Quit();
94  void SetErrorCallback(std::function<void(const std::string& error_str)> error_callback);
95  bool PollResponse();
96  SC2APIProtocol::Status GetLastStatus() const { return latest_status_; }
97  bool HasResponsePending() const;
98  SC2APIProtocol::Response::ResponseCase GetResponsePending() const { return response_pending_; }
99  int GetAssignedPort() const { return port_; }
100 
101  const std::vector<uint32_t>& GetStats() const { return count_uses_; }
102  void SetControl(ControlInterface* control) { control_ = control; }
103 
104  uint32_t GetBaseBuild() const { return base_build_; }
105  const std::string& GetDataVersion() const { return data_version_; }
106 
107 protected:
108  Connection connection_;
109  std::string address_;
110  int port_;
111  unsigned int default_timeout_ms_;
112  std::function<void(const std::string& error_str)> error_callback_;
113  SC2APIProtocol::Status latest_status_;
114  SC2APIProtocol::Response::ResponseCase response_pending_;
115  std::vector<uint32_t> count_uses_;
116  ControlInterface* control_;
117 
118  uint32_t base_build_;
119  std::string data_version_;
120 };
121 
122 // Helper to produce a string for the proto type.
123 const char* RequestResponseIDToName(int type);
124 
125 }
Definition: sc2_connection.h:26
A basic websocket connection and queue wrapper.
Definition: sc2_action.h:9
Definition: sc2_proto_interface.h:19
Definition: sc2_control_interfaces.h:16
Definition: sc2_proto_interface.h:85