SC2API
An API for AI for StarCraft II
sc2_connection.h
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include <string>
8 #include <mutex>
9 #include <condition_variable>
10 #include <deque>
11 #include <functional>
12 #include <atomic>
13 
14 struct mg_connection;
15 
16 namespace SC2APIProtocol {
17  class Request;
18  class Response;
19 }
20 
21 namespace sc2 {
22 
23 
26 class Connection {
27 public:
28  Connection();
29  ~Connection();
30 
35  bool Connect(const std::string& address, int port, bool verbose = true);
36 
41  void Send(const SC2APIProtocol::Request* request);
42 
49  bool Receive(SC2APIProtocol::Response*& response, unsigned int timeout_ms);
50 
54  void PopResponse(SC2APIProtocol::Response*& response);
55 
58  void SetTimeoutCallback(std::function<void()> callback);
59 
60  void SetConnectionClosedCallback(std::function<void()> callback);
61 
64  bool HasConnection() const;
65 
67  void Disconnect();
68 
71  bool PollResponse();
72 
77  void PushResponse(SC2APIProtocol::Response*& response);
78 
79 
80  std::function<void()> timeout_callback_;
81  std::function<void()> connection_closed_callback_;
82 
83  mg_connection* connection_;
84 
85 private:
86  bool verbose_;
87 
88  std::deque<SC2APIProtocol::Response*> queue_;
89  std::mutex mutex_;
90  std::condition_variable condition_;
91 
92  std::atomic_bool has_response_;
93 };
94 
95 }
Definition: sc2_connection.h:26
Definition: sc2_action.h:9
std::function< void()> connection_closed_callback_
Timeout callback.
Definition: sc2_connection.h:81
std::function< void()> timeout_callback_
Timeout callback.
Definition: sc2_connection.h:80
mg_connection * connection_
A pointer to the civetweb connection object.
Definition: sc2_connection.h:83
Definition: sc2_connection.h:16