-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.cc
349 lines (296 loc) · 7.91 KB
/
client.cc
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// CODYlib -*- mode:c++ -*-
// Copyright (C) 2020 Nathan Sidwell, [email protected]
// License: Apache v2.0
// Cody
#include "internal.hh"
// C
#include <cerrno>
#include <cstring>
// Client code
namespace Cody {
// These do not need to be members
static Packet ConnectResponse (std::vector<std::string> &words);
static Packet PathnameResponse (std::vector<std::string> &words);
static Packet OKResponse (std::vector<std::string> &words);
static Packet IncludeTranslateResponse (std::vector<std::string> &words);
// Must be consistently ordered with the RequestCode enum
static Packet (*const responseTable[Detail::RC_HWM])
(std::vector<std::string> &) =
{
&ConnectResponse,
&PathnameResponse,
&PathnameResponse,
&PathnameResponse,
&OKResponse,
&IncludeTranslateResponse,
&OKResponse,
};
Client::Client ()
{
fd.from = fd.to = -1;
}
Client::Client (Client &&src)
: write (std::move (src.write)),
read (std::move (src.read)),
corked (std::move (src.corked)),
is_direct (src.is_direct),
is_connected (src.is_connected)
{
if (is_direct)
server = src.server;
else
{
fd.from = src.fd.from;
fd.to = src.fd.to;
}
}
Client::~Client ()
{
}
Client &Client::operator= (Client &&src)
{
write = std::move (src.write);
read = std::move (src.read);
corked = std::move (src.corked);
is_direct = src.is_direct;
is_connected = src.is_connected;
if (is_direct)
server = src.server;
else
{
fd.from = src.fd.from;
fd.to = src.fd.to;
}
return *this;
}
int Client::CommunicateWithServer ()
{
write.PrepareToWrite ();
read.PrepareToRead ();
if (IsDirect ())
server->DirectProcess (write, read);
else
{
// Write the write buffer
while (int e = write.Write (fd.to))
if (e != EAGAIN && e != EINTR)
return e;
// Read the read buffer
while (int e = read.Read (fd.from))
if (e != EAGAIN && e != EINTR)
return e;
}
return 0;
}
static Packet CommunicationError (int err)
{
std::string e {u8"communication error: "};
e.append (strerror (err));
return Packet (Client::PC_ERROR, std::move (e));
}
Packet Client::ProcessResponse (std::vector<std::string> &words,
unsigned code, bool isLast)
{
if (int e = read.Lex (words))
{
if (e == EINVAL)
{
std::string msg (u8"malformed string '");
msg.append (words[0]);
msg.append (u8"'");
return Packet (Client::PC_ERROR, std::move (msg));
}
else
return Packet (Client::PC_ERROR, u8"missing response");
}
Assert (!words.empty ());
if (words[0] == u8"ERROR")
return Packet (Client::PC_ERROR,
words.size () == 2 ? words[1] : u8"malformed error response");
if (isLast && !read.IsAtEnd ())
return Packet (Client::PC_ERROR,
std::string (u8"unexpected extra response"));
Assert (code < Detail::RC_HWM);
Packet result (responseTable[code] (words));
result.SetRequest (code);
if (result.GetCode () == Client::PC_ERROR && result.GetString ().empty ())
{
std::string msg {u8"malformed response '"};
read.LexedLine (msg);
msg.append (u8"'");
result.GetString () = std::move (msg);
}
else if (result.GetCode () == Client::PC_CONNECT)
is_connected = true;
return result;
}
Packet Client::MaybeRequest (unsigned code)
{
if (IsCorked ())
{
corked.push_back (code);
return Packet (PC_CORKED);
}
if (int err = CommunicateWithServer ())
return CommunicationError (err);
std::vector<std::string> words;
return ProcessResponse(words, code, true);
}
void Client::Cork ()
{
if (corked.empty ())
corked.push_back (-1);
}
std::vector<Packet> Client::Uncork ()
{
std::vector<Packet> result;
if (corked.size () > 1)
{
if (int err = CommunicateWithServer ())
result.emplace_back (CommunicationError (err));
else
{
std::vector<std::string> words;
for (auto iter = corked.begin () + 1; iter != corked.end ();)
{
char code = *iter;
++iter;
result.emplace_back (ProcessResponse (words, code,
iter == corked.end ()));
}
}
}
corked.clear ();
return result;
}
// Now the individual message handlers
// HELLO $vernum $agent $ident
Packet Client::Connect (char const *agent, char const *ident,
size_t alen, size_t ilen)
{
write.BeginLine ();
write.AppendWord (u8"HELLO");
write.AppendInteger (Version);
write.AppendWord (agent, true, alen);
write.AppendWord (ident, true, ilen);
write.EndLine ();
return MaybeRequest (Detail::RC_CONNECT);
}
// HELLO $version $agent [$flags]
Packet ConnectResponse (std::vector<std::string> &words)
{
if (words[0] == u8"HELLO" && (words.size () == 3 || words.size () == 4))
{
char *eptr;
unsigned long val = strtoul (words[1].c_str (), &eptr, 10);
unsigned version = unsigned (val);
if (*eptr || version != val || version < Version)
return Packet (Client::PC_ERROR, u8"incompatible version");
else
{
unsigned flags = 0;
if (words.size () == 4)
{
val = strtoul (words[3].c_str (), &eptr, 10);
flags = unsigned (val);
}
return Packet (Client::PC_CONNECT, flags);
}
}
return Packet (Client::PC_ERROR, u8"");
}
// MODULE-REPO
Packet Client::ModuleRepo ()
{
write.BeginLine ();
write.AppendWord (u8"MODULE-REPO");
write.EndLine ();
return MaybeRequest (Detail::RC_MODULE_REPO);
}
// PATHNAME $dir | ERROR
Packet PathnameResponse (std::vector<std::string> &words)
{
if (words[0] == u8"PATHNAME" && words.size () == 2)
return Packet (Client::PC_PATHNAME, std::move (words[1]));
return Packet (Client::PC_ERROR, u8"");
}
// INVOKE $args
Packet Client::InvokeSubProcess (char const *const *argv, size_t argc)
{
write.BeginLine ();
write.AppendWord (u8"INVOKE");
for(size_t i = 0; i < argc; i++)
write.AppendWord (argv[i]);
write.EndLine ();
return MaybeRequest (Detail::RC_INVOKE);
}
// OK or ERROR
Packet OKResponse (std::vector<std::string> &words)
{
if (words[0] == u8"OK")
return Packet (Client::PC_OK);
else
return Packet (Client::PC_ERROR,
words.size () == 2 ? std::move (words[1]) : "");
}
// MODULE-EXPORT $modulename [$flags]
Packet Client::ModuleExport (char const *module, Flags flags, size_t mlen)
{
write.BeginLine ();
write.AppendWord (u8"MODULE-EXPORT");
write.AppendWord (module, true, mlen);
if (flags != Flags::None)
write.AppendInteger (unsigned (flags));
write.EndLine ();
return MaybeRequest (Detail::RC_MODULE_EXPORT);
}
// MODULE-IMPORT $modulename [$flags]
Packet Client::ModuleImport (char const *module, Flags flags, size_t mlen)
{
write.BeginLine ();
write.AppendWord (u8"MODULE-IMPORT");
write.AppendWord (module, true, mlen);
if (flags != Flags::None)
write.AppendInteger (unsigned (flags));
write.EndLine ();
return MaybeRequest (Detail::RC_MODULE_IMPORT);
}
// MODULE-COMPILED $modulename [$flags]
Packet Client::ModuleCompiled (char const *module, Flags flags, size_t mlen)
{
write.BeginLine ();
write.AppendWord (u8"MODULE-COMPILED");
write.AppendWord (module, true, mlen);
if (flags != Flags::None)
write.AppendInteger (unsigned (flags));
write.EndLine ();
return MaybeRequest (Detail::RC_MODULE_COMPILED);
}
// INCLUDE-TRANSLATE $includename [$flags]
Packet Client::IncludeTranslate (char const *include, Flags flags, size_t ilen)
{
write.BeginLine ();
write.AppendWord (u8"INCLUDE-TRANSLATE");
write.AppendWord (include, true, ilen);
if (flags != Flags::None)
write.AppendInteger (unsigned (flags));
write.EndLine ();
return MaybeRequest (Detail::RC_INCLUDE_TRANSLATE);
}
// BOOL $knowntextualness
// PATHNAME $cmifile
Packet IncludeTranslateResponse (std::vector<std::string> &words)
{
if (words[0] == u8"BOOL" && words.size () == 2)
{
if (words[1] == u8"FALSE")
return Packet (Client::PC_BOOL, 0);
else if (words[1] == u8"TRUE")
return Packet (Client::PC_BOOL, 1);
else
return Packet (Client::PC_ERROR, u8"");
}
else
return PathnameResponse (words);
}
}