SC2API
An API for AI for StarCraft II
sc2_server.h
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include <utility>
8 #include <queue>
9 #include <vector>
10 #include <mutex>
11 
12 struct mg_connection;
13 struct mg_context;
14 
15 namespace SC2APIProtocol {
16  class Request;
17  class Response;
18 }
19 
20 namespace sc2 {
21 
22 typedef std::pair<struct mg_connection*, SC2APIProtocol::Request*> RequestData;
23 typedef std::pair<struct mg_connection*, SC2APIProtocol::Response*> ResponseData;
24 
25 class Server {
26 public:
27  Server();
28  ~Server();
29 
30  bool Listen(
31  const char* listeningPorts,
32  const char* requestTimeoutMs,
33  const char* websocketTimeoutMs,
34  const char* numThreads);
35 
36  void QueueRequest(struct mg_connection* conn, SC2APIProtocol::Request*& request);
37  void QueueResponse(struct mg_connection* conn, SC2APIProtocol::Response*& response);
38 
39  // If no connection is provided send it to the first connection attained.
40  void SendRequest(struct mg_connection* conn = nullptr);
41  void SendResponse(struct mg_connection* conn = nullptr);
42 
43  bool HasRequest();
44  bool HasResponse();
45 
46  const RequestData& PeekRequest();
47  const ResponseData& PeekResponse();
48 
49  std::vector<const mg_connection*> connections_;
50 private:
51  mg_context* mg_context_ = nullptr;
52 
53  std::queue<RequestData> requests_;
54  std::queue<ResponseData> responses_;
55 
56  std::mutex request_mutex_;
57  std::mutex response_mutex_;
58 };
59 
60 }
Definition: sc2_action.h:9
Definition: sc2_server.h:25
Definition: sc2_connection.h:16