forked from jsnyder/luarpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
563 lines (458 loc) · 13.9 KB
/
client.c
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*****************************************************************************
* Lua-RPC library, Copyright (C) 2001 Russell L. Smith. All rights reserved. *
* Email: [email protected] Web: www.q12.org *
* For documentation, see http://www.q12.org/lua. For the license agreement, *
* see the file LICENSE that comes with this distribution. *
*****************************************************************************/
// Modifications by James Snyder - [email protected]
// - more generic backend interface to accomodate different link types
// - integration with eLua (including support for rotables)
// - extensions of remote global table as local table metaphor
// - methods to allow remote assignment, getting remote values
// - accessing and calling types nested multiple levels deep on tables now works
// - port from Lua 4.x to 5.x
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#ifdef __MINGW32__
void *alloca(size_t);
#else
#include <alloca.h>
#endif
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#if !defined( LUA_CROSS_COMPILER ) && !defined( LUARPC_STANDALONE )
#include "platform.h"
#endif
#include "platform_conf.h"
#ifdef LUA_OPTIMIZE_MEMORY
#include "lrotable.h"
#endif
#include "luarpc_rpc.h"
#ifdef BUILD_RPC
// Support for Compiling with & without rotables
#ifdef LUA_OPTIMIZE_MEMORY
#define LUA_ISCALLABLE( state, idx ) ( lua_isfunction( state, idx ) || lua_islightfunction( state, idx ) )
#else
#define LUA_ISCALLABLE( state, idx ) lua_isfunction( state, idx )
#endif
// RPC Commands
enum
{
RPC_CMD_CALL = 1,
RPC_CMD_GET,
RPC_CMD_CON,
RPC_CMD_NEWINDEX
};
// RPC Status Codes
enum
{
RPC_READY = 64,
RPC_UNSUPPORTED_CMD,
RPC_DONE
};
enum { RPC_PROTOCOL_VERSION = 3 };
// return a string representation of an error number
static const char * errorString( int n )
{
switch (n) {
case ERR_EOF: return "connection closed unexpectedly";
case ERR_CLOSED: return "operation requested on closed transport";
case ERR_PROTOCOL: return "error in the received protocol";
case ERR_COMMAND: return "undefined command";
case ERR_NODATA: return "no data received when attempting to read";
case ERR_HEADER: return "header exchanged failed";
case ERR_LONGFNAME: return "function name too long";
default: return transport_strerror( n );
}
}
static int generic_catch_handler(lua_State *L, Handle *handle, struct exception e )
{
deal_with_error( L, handle, errorString( e.errnum ) );
switch( e.type )
{
case nonfatal:
lua_pushnil( L );
return 1;
break;
case fatal:
transport_close( &handle->tpt );
break;
default: lua_assert( 0 );
}
return 0;
}
// **************************************************************************
// client side handle and handle helper userdata objects.
//
// a handle userdata (handle to a RPC server) is a pointer to a Handle object.
// a helper userdata is a pointer to a Helper object.
//
// helpers let us make expressions like:
// handle.funcname (a,b,c)
// "handle.funcname" returns the helper object, which calls the remote
// function.
// handle a client or server side error. NOTE: this function may or may not
// return. the handle `h' may be 0.
void deal_with_error(lua_State *L, Handle *h, const char *error_string)
{
if( global_error_handler != LUA_NOREF )
{
lua_getref( L, global_error_handler );
lua_pushstring( L, error_string );
lua_pcall( L, 1, 0, 0 );
}
else
luaL_error( L, error_string );
}
Handle *handle_create( lua_State *L )
{
Handle *h = ( Handle * )lua_newuserdata( L, sizeof( Handle ) );
luaL_getmetatable( L, "rpc.handle" );
lua_setmetatable( L, -2 );
h->error_handler = LUA_NOREF;
h->async = 0;
h->read_reply_count = 0;
return h;
}
static Helper *helper_create( lua_State *L, Handle *handle, const char *funcname )
{
Helper *h = ( Helper * )lua_newuserdata( L, sizeof( Helper ) );
luaL_getmetatable( L, "rpc.helper" );
lua_setmetatable( L, -2 );
lua_pushvalue( L, 1 ); // push parent handle
h->pref = luaL_ref( L, LUA_REGISTRYINDEX ); // put ref into struct
h->handle = handle;
h->parent = NULL;
h->nparents = 0;
strncpy( h->funcname, funcname, NUM_FUNCNAME_CHARS );
return h;
}
// indexing a handle returns a helper
static int handle_index (lua_State *L)
{
const char *s;
check_num_args( L, 2 );
MYASSERT( lua_isuserdata( L, 1 ) && ismetatable_type( L, 1, "rpc.handle" ) );
if( lua_type( L, 2 ) != LUA_TSTRING )
return luaL_error( L, "can't index a handle with a non-string" );
s = lua_tostring( L, 2 );
if ( strlen( s ) > NUM_FUNCNAME_CHARS - 1 )
return luaL_error( L, errorString( ERR_LONGFNAME ) );
helper_create( L, ( Handle * )lua_touserdata( L, 1 ), s );
// return the helper object
return 1;
}
static int helper_newindex( lua_State *L );
// indexing a handle returns a helper
static int handle_newindex( lua_State *L )
{
const char *s;
check_num_args( L, 3 );
MYASSERT( lua_isuserdata( L, 1 ) && ismetatable_type( L, 1, "rpc.handle" ) );
if( lua_type( L, 2 ) != LUA_TSTRING )
return luaL_error( L, "can't index handle with a non-string" );
s = lua_tostring( L, 2 );
if ( strlen( s ) > NUM_FUNCNAME_CHARS - 1 )
return luaL_error( L, errorString( ERR_LONGFNAME ) );
helper_create( L, ( Handle * )lua_touserdata( L, 1 ), "" );
lua_replace(L, 1);
helper_newindex( L );
return 0;
}
// replays series of indexes to remote side as a string
void helper_remote_index( Helper *helper )
{
int i, len;
Helper **hstack;
Transport *tpt = &helper->handle->tpt;
// get length of name & make stack of helpers
len = strlen( helper->funcname );
if( helper->nparents > 0 ) // If helper has parents, build string to remote index
{
hstack = ( Helper ** )alloca( sizeof( Helper * ) * helper->nparents );
hstack[ helper->nparents - 1 ] = helper->parent;
len += strlen( hstack[ helper->nparents - 1 ]->funcname ) + 1;
for( i = helper->nparents - 1 ; i > 0 ; i -- )
{
hstack[ i - 1 ] = hstack[ i ]->parent;
len += strlen( hstack[ i ]->funcname ) + 1;
}
transport_write_u32( tpt, len );
// replay helper key names
for( i = 0 ; i < helper->nparents ; i ++ )
{
transport_write_string( tpt, hstack[ i ]->funcname, strlen( hstack[ i ]->funcname ) );
transport_write_string( tpt, ".", 1 );
}
}
else // If helper has no parents, just use length of global
transport_write_u32( tpt, len );
transport_write_string( tpt, helper->funcname, strlen( helper->funcname ) );
}
#ifdef HELPER_WAIT
static void helper_wait_ready( Transport *tpt, u8 cmd )
{
struct exception e;
u8 cmdresp;
transport_write_u8( tpt, cmd );
cmdresp = transport_read_u8( tpt );
if( cmdresp != RPC_READY )
{
e.errnum = ERR_PROTOCOL;
e.type = nonfatal;
Throw( e );
}
}
#else
#define helper_wait_ready transport_write_u8
#endif
static int helper_get( lua_State *L, Helper *helper )
{
struct exception e;
int freturn = 0;
Transport *tpt = &helper->handle->tpt;
Try
{
TRANSPORT_START_WRITING(tpt);
helper_wait_ready( tpt, RPC_CMD_GET );
helper_remote_index( helper );
TRANSPORT_START_READING(tpt);
read_variable( tpt, L );
TRANSPORT_STOP(tpt);
freturn = 1;
}
Catch( e )
{
freturn = generic_catch_handler( L, helper->handle, e );
}
return freturn;
}
// static int helper_async( lua_State *L )
// {
// /* first read out any pending return values for old async calls */
// for (; h->handle->read_reply_count > 0; h->handle->read_reply_count--) {
// ret_code = transport_read_u8 (tpt); /* return code */
// if( ret_code == 0 )
// {
// /* read return arguments, ignore everything we read */
// nret = transport_read_u32( tpt );
//
// for (i=0; i < ( ( int ) nret ); i++)
// read_variable (tpt,L);
//
// lua_pop (L,nret);
// }
// else
// {
// /* read error and handle it */
// u32 code = transport_read_u32( tpt );
// u32 len = transport_read_u32( tpt );
// char *err_string = ( char * )alloca( len + 1 );
// transport_read_string( tpt, err_string, len );
// err_string[ len ] = 0;
//
// deal_with_error( L, h->handle, err_string );
// freturn = 0;
// }
// }
// }
static int helper_call (lua_State *L)
{
struct exception e;
int freturn = 0;
Helper *h;
Transport *tpt;
h = ( Helper * )luaL_checkudata(L, 1, "rpc.helper");
luaL_argcheck(L, h, 1, "helper expected");
tpt = &h->handle->tpt;
// capture special calls, otherwise execute normal remote call
if( strcmp("get", h->funcname ) == 0 )
{
helper_get( L, h->parent );
freturn = 1;
}
else
{
Try
{
int i,n;
u32 nret,ret_code;
TRANSPORT_START_WRITING(tpt);
// write function name
helper_wait_ready( tpt, RPC_CMD_CALL );
helper_remote_index( h );
// write number of arguments
n = lua_gettop( L );
transport_write_u32( tpt, n - 1 );
// write each argument
for( i = 2; i <= n; i ++ )
write_variable( tpt, L, i );
/* if we're in async mode, we're done */
/*if ( h->handle->async )
{
h->handle->read_reply_count++;
freturn = 0;
}*/
TRANSPORT_START_READING(tpt);
// read return code
ret_code = transport_read_u8( tpt );
if ( ret_code == 0 )
{
// read return arguments
nret = transport_read_u32( tpt );
for ( i = 0; i < ( ( int ) nret ); i ++ )
read_variable( tpt, L );
freturn = ( int )nret;
}
else
{
// read error and handle it
transport_read_u32( tpt ); // read code (not being used here)
u32 len = transport_read_u32( tpt );
char *err_string = ( char * )alloca( len + 1 );
transport_read_string( tpt, err_string, len );
err_string[ len ] = 0;
deal_with_error( L, h->handle, err_string );
freturn = 0;
}
TRANSPORT_STOP(tpt);
}
Catch( e )
{
freturn = generic_catch_handler( L, h->handle, e );
}
}
return freturn;
}
// __newindex even on helper,
static int helper_newindex( lua_State *L )
{
struct exception e;
int freturn = 0;
int ret_code;
Helper *h;
Transport *tpt;
h = ( Helper * )luaL_checkudata(L, -3, "rpc.helper");
luaL_argcheck(L, h, -3, "helper expected");
luaL_checktype(L, -2, LUA_TSTRING );
tpt = &h->handle->tpt;
Try
{
// index destination on remote side
TRANSPORT_START_WRITING(tpt);
helper_wait_ready( tpt, RPC_CMD_NEWINDEX );
helper_remote_index( h );
write_variable( tpt, L, lua_gettop( L ) - 1 );
write_variable( tpt, L, lua_gettop( L ) );
TRANSPORT_START_READING(tpt);
ret_code = transport_read_u8( tpt );
if( ret_code != 0 )
{
// read error and handle it
transport_read_u32( tpt ); // Read code (not using here)
u32 len = transport_read_u32( tpt );
char *err_string = ( char * )alloca( len + 1 );
transport_read_string( tpt, err_string, len );
err_string[ len ] = 0;
deal_with_error( L, h->handle, err_string );
}
TRANSPORT_STOP(tpt);
freturn = 0;
}
Catch( e )
{
freturn = generic_catch_handler( L, h->handle, e );
}
return freturn;
}
static Helper *helper_append( lua_State *L, Helper *helper, const char *funcname )
{
Helper *h = ( Helper * )lua_newuserdata( L, sizeof( Helper ) );
luaL_getmetatable( L, "rpc.helper" );
lua_setmetatable( L, -2 );
lua_pushvalue( L, 1 ); // push parent
h->pref = luaL_ref( L, LUA_REGISTRYINDEX ); // put ref into struct
h->handle = helper->handle;
h->parent = helper;
h->nparents = helper->nparents + 1;
strncpy ( h->funcname, funcname, NUM_FUNCNAME_CHARS );
return h;
}
// indexing a helper returns a helper
static int helper_index( lua_State *L )
{
const char *s;
check_num_args( L, 2 );
MYASSERT( lua_isuserdata( L, 1 ) && ismetatable_type( L, 1, "rpc.helper" ) );
if( lua_type( L, 2 ) != LUA_TSTRING )
return luaL_error( L, "can't index handle with non-string" );
s = lua_tostring( L, 2 );
if ( strlen( s ) > NUM_FUNCNAME_CHARS - 1 )
return luaL_error( L, errorString( ERR_LONGFNAME ) );
helper_append( L, ( Helper * )lua_touserdata( L, 1 ), s );
return 1;
}
static int helper_close (lua_State *L)
{
Helper *h = ( Helper * )luaL_checkudata(L, 1, "rpc.helper");
luaL_argcheck(L, h, 1, "helper expected");
luaL_unref(L, LUA_REGISTRYINDEX, h->pref);
h->pref = LUA_REFNIL;
return 0;
}
#ifndef LUARPC_STANDALONE
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE rpc_handle[] =
{
{ LSTRKEY( "__index" ), LFUNCVAL( handle_index ) },
{ LSTRKEY( "__newindex"), LFUNCVAL( handle_newindex )},
{ LNILKEY, LNILVAL }
};
const LUA_REG_TYPE rpc_helper[] =
{
{ LSTRKEY( "__call" ), LFUNCVAL( helper_call ) },
{ LSTRKEY( "__index" ), LFUNCVAL( helper_index ) },
{ LSTRKEY( "__newindex" ), LFUNCVAL( helper_newindex ) },
{ LSTRKEY( "__gc" ), LFUNCVAL( helper_close ) },
{ LNILKEY, LNILVAL }
};
void register_client(lua_State *L)
{
#if LUA_OPTIMIZE_MEMORY > 0
luaL_rometatable(L, "rpc.helper", (void*)rpc_helper);
luaL_rometatable(L, "rpc.handle", (void*)rpc_handle);
#else
luaL_newmetatable( L, "rpc.helper" );
luaL_register( L, NULL, rpc_helper );
luaL_newmetatable( L, "rpc.handle" );
luaL_register( L, NULL, rpc_handle );
#endif
}
#else
static const luaL_reg rpc_handle[] =
{
{ "__index", handle_index },
{ "__newindex", handle_newindex },
{ NULL, NULL }
};
static const luaL_reg rpc_helper[] =
{
{ "__call", helper_call },
{ "__index", helper_index },
{ "__newindex", helper_newindex },
{ "__gc", helper_close },
{ NULL, NULL }
};
void register_client(lua_State *L)
{
luaL_newmetatable( L, "rpc.helper" );
luaL_register( L, NULL, rpc_helper );
luaL_newmetatable( L, "rpc.handle" );
luaL_register( L, NULL, rpc_handle );
}
#endif
#endif