-
Notifications
You must be signed in to change notification settings - Fork 0
/
sisclient.h
126 lines (109 loc) · 3.26 KB
/
sisclient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
#include <string>
#include <mutex>
#include <set>
#include <future>
#include <list>
#include "types.h"
#include "sissocket.h"
#include "variant.h"
#include "sisdevice.h"
#include "sisluafunc.h"
#include "luaalloc.h"
struct SISClientConfig
{
SISClientConfig();
std::string name;
std::string host;
unsigned port;
SISDevice device;
};
class SISClient
{
public:
SISClient(const char *name);
~SISClient();
bool configure(VarCRef mycfg, VarCRef devcfg);
SISSocket connect(); // return new socket id
void disconnect();
u64 updateTimer(u64 now, u64 dt);
void updateIncoming(); // copy incoming data into inbuf
void delayedConnected();
bool isConnected() const;
void wasDisconnected();
enum State
{
UNDEF = -1,
ERROR,
DISCONNECTED,
CONNECTING,
CONNECTED, // but not authed
AUTHING, // authing right now
AUTHED,
IDLE, // authed not doing anything
INPROCESS, // sending/receiving something right now
_STATE_MAX
};
State setState(State st); // returns prev state
State getState() const { return state; }
bool sendall(const char *buf, size_t size);
int sendsome(const char *buf, size_t size); // <0: error, >0: this many bytes sent, ==0: can't send right now
int readInput(char *dst, size_t bytes);
size_t availInput() const { return inbuf.size() - inbufOffs; }
char *getInputPtr() { return inbuf.data() + inbufOffs; }
void advanceInput(size_t n);
// client status
const char *getStateStr() const;
u64 getTimeInState() const { return timeInState; }
const SISClientConfig& getConfig() const { return cfg; }
struct ActionResult
{
inline ActionResult() : status(0), nret(0), error(false) {}
std::string text;
std::string contentType;
unsigned status;
unsigned nret;
bool error;
};
// scheduled or running job
struct Job
{
Job();
~Job();
void unref(lua_State *L); // call this before dtor
lua_State *Lco;
int Lcoref;
int Lparams;
State beginState;
State endState;
State failState;
bool started;
u64 expiryTime; // wallclock time
std::string actionName;
std::promise<ActionResult> result;
};
typedef std::future<ActionResult> FutureResult;
FutureResult queryAsync(const char *action, VarCRef vars, u64 expireIn);
void setTimeout(u64 timeout) { stateMaxTime = timeout ? timeInState + timeout : 0; }
private:
void _abortScheduled();
void _clearBuffer();
void _disconnect();
void heartbeat();
void authenticate();
FutureResult scheduleAction(const char *name, VarCRef vars, State activestate, State donestate, State failstate, u64 expireIn);
u64 updateCoro(); // must be called with mtx held
SISSocket socket;
u64 timeInState;
u64 stateMaxTime; // fail current
SISClientConfig cfg;
State state, nextState;
//CoroRunner tasks;
std::vector<char> inbuf;
size_t inbufOffs;
std::recursive_mutex mtx;
lua_State *L;
LuaAlloc *LA;
std::list<Job> jobs; // always work on the head
std::set<std::string> luafuncs; // available Lua global funcs exported by the script
};