| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <memory> |
| 4 |
#include <vector> |
| 5 |
#include <functional> |
| 6 |
#include <stdexcept> |
| 7 |
|
| 8 |
namespace gameconn |
| 9 |
{ |
| 10 |
|
| 11 |
class MessageTcp; |
| 12 |
|
| 13 |
// Thrown by some methods of AutomationEngine and GameConnection |
| 14 |
// if connection was lost or was not established at all. |
| 15 |
class DisconnectException : public std::runtime_error { |
| 16 |
public: |
| 17 |
DisconnectException() : std::runtime_error("Game connection lost") {} |
| 18 |
}; |
| 19 |
|
| 20 |
// Bitmask with all tag bits set. |
| 21 |
static const int TAGMASK_ALL = -1; |
| 22 |
|
| 23 |
// Putting this to wait-list of multistep procedure |
| 24 |
// results in waking up on next think (suitable for polling). |
| 25 |
static const int SEQNO_WAIT_POLL = -10000; |
| 26 |
|
| 27 |
struct MultistepProcReturn { |
| 28 |
// Which step will be executed next time. |
| 29 |
int nextStep; |
| 30 |
// Resume procedure when all of these requests are finished. |
| 31 |
// If it is empty, then procedure is continued immediately. |
| 32 |
// If only SEQNO_WAIT_POLL remains, then procedure is resumed on next think. |
| 33 |
std::vector<int> seqnoWaitList; |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* stgatilov: The engine for connection to TheDarkMod's automation via socket. |
| 39 |
*/ |
| 40 |
class AutomationEngine |
| 41 |
{ |
| 42 |
public: |
| 43 |
AutomationEngine(); |
| 44 |
~AutomationEngine(); |
| 45 |
|
| 46 |
|
| 47 |
// Connect to TDM instance if not connected yet. |
| 48 |
// Returns false if failed to connect, true on success. |
| 49 |
bool connect(); |
| 50 |
// Disconnect from TDM instance if connected. |
| 51 |
// If force = false, then it waits until all pending requests are finished. |
| 52 |
// If force = true, then all pending requests are dropped, no blocking for sure. |
| 53 |
void disconnect(bool force = false); |
| 54 |
|
| 55 |
// Returns false if connection is not yet established or has been closed for whatever reason. |
| 56 |
bool isAlive() const; |
| 57 |
// Returns true if connection was established, but was lost after that and disconnect was not called yet. |
| 58 |
bool hasLostConnection() const; |
| 59 |
|
| 60 |
|
| 61 |
// Check how socket is doing, accept responses, call callbacks. |
| 62 |
// This should be done regularly, e.g. in a timer. |
| 63 |
void think(); |
| 64 |
|
| 65 |
|
| 66 |
// Send given request synchronously, i.e. wait until its completition. |
| 67 |
// Returns response content. |
| 68 |
// Throws DisconnectException if connection is missing or lost during execution. |
| 69 |
std::string executeRequestBlocking(int tag, const std::string& request); |
| 70 |
|
| 71 |
// Send request !a!synchronously, i.e. send it to socket and return immediately. |
| 72 |
// Returns seqno of the request: it can be put to wait-list of multistep procedure or used in queries. |
| 73 |
// When request is finished, optional callback will be executed (from "think" method). |
| 74 |
// Note that if connection is lost, NO special callback is called. |
| 75 |
// May throw DisconnectException if connection is missing (but never throws if isAlive() is true before call). |
| 76 |
int executeRequestAsync(int tag, const std::string& request, const std::function<void(int)>& callback = {}); |
| 77 |
|
| 78 |
// Execute given multistep procedure, starting on the next think. |
| 79 |
// Returns ID of procedure for queries. |
| 80 |
// Multistep procedure is like a DFA of "steps", each step is executed till start to end. |
| 81 |
// When step is over, you can specify async requests to wait for, and which step to resume when they are finished. |
| 82 |
int executeMultistepProc(int tag, const std::function<MultistepProcReturn(int)>& function, int startStep = 0); |
| 83 |
|
| 84 |
|
| 85 |
// Return true iff any of the given requests or multistep procedures are not finished yet. |
| 86 |
bool areInProgress(const std::vector<int>& reqSeqnos, const std::vector<int>& procIds); |
| 87 |
// Wait for specified requests and multistep procedures to finish. |
| 88 |
// Throws DisconnectException if it cannot be done due to lost connection. |
| 89 |
void wait(const std::vector<int>& reqSeqnos, const std::vector<int>& procIds); |
| 90 |
|
| 91 |
// Return true iff any request or multistep procedure matching the given mask is not finished yet. |
| 92 |
// Note: tagMask is a bitmask, so pass (1<<tag) in order to wait for one tag only. |
| 93 |
bool areTagsInProgress(int tagMask = TAGMASK_ALL); |
| 94 |
// Wait for all active requests and multistep procedures matching the given mask to finish. |
| 95 |
// Throws DisconnectException if it cannot be done due to lost connection. |
| 96 |
void waitForTags(int tagMask = TAGMASK_ALL); |
| 97 |
|
| 98 |
|
| 99 |
// Returns response for just finished request with given seqno. |
| 100 |
// Can be called inside: |
| 101 |
// * executeRequestAsync callback |
| 102 |
// * multistep procedure |
| 103 |
std::string getResponse(int seqno) const; |
| 104 |
|
| 105 |
private: |
| 106 |
// Connection to TDM game (i.e. the socket with custom message framing). |
| 107 |
// It can be "dead" in two ways: |
| 108 |
// _connection is NULL --- no connection |
| 109 |
// *_connection is dead --- just lost connection |
| 110 |
std::unique_ptr<MessageTcp> _connection; |
| 111 |
// Sequence number of the last sent request (incremented sequentally). |
| 112 |
int _seqno = 0; |
| 113 |
// Number of multistep procedures ever seen (incremented sequentally). |
| 114 |
int _procIds = 0; |
| 115 |
// Used for counting recursive depth of think method. |
| 116 |
int _thinkDepth = 0; |
| 117 |
|
| 118 |
struct Request { |
| 119 |
int _seqno = 0; |
| 120 |
int _tag = 0; |
| 121 |
bool _finished = false; |
| 122 |
std::string _request; |
| 123 |
std::string _response; |
| 124 |
|
| 125 |
std::function<void(int)> _callback; |
| 126 |
}; |
| 127 |
std::vector<Request> _requests; |
| 128 |
|
| 129 |
struct MultistepProcedure { |
| 130 |
int _id = 0; |
| 131 |
int _tag = 0; |
| 132 |
std::vector<int> _waitForSeqnos; |
| 133 |
std::function<MultistepProcReturn(int)> _function; |
| 134 |
int _currentStep = -1; |
| 135 |
}; |
| 136 |
std::vector<MultistepProcedure> _multistepProcs; |
| 137 |
|
| 138 |
int generateNewSequenceNumber(); |
| 139 |
Request* sendRequest(int tag, const std::string& request); |
| 140 |
|
| 141 |
Request* findRequest(int seqno) const; |
| 142 |
MultistepProcedure* findMultistepProc(int id) const; |
| 143 |
|
| 144 |
bool isMultistepProcStillWaiting(const MultistepProcedure& proc, bool waitForPoll) const; |
| 145 |
void resumeMultistepProcedure(int id); |
| 146 |
}; |
| 147 |
|
| 148 |
} |