-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathluarpc.c
1760 lines (1461 loc) · 43.8 KB
/
luarpc.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* 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
// Prototypes for Local Functions
LUALIB_API int luaopen_rpc( lua_State *L );
Handle *handle_create( lua_State *L );
struct exception_context the_exception_context[ 1 ];
static void errorMessage( const char *msg, va_list ap )
{
fflush( stdout );
fflush( stderr );
fprintf( stderr,"\nError: " );
vfprintf( stderr,msg,ap );
fprintf( stderr,"\n\n" );
fflush( stderr );
}
DOGCC(static void panic( const char *msg, ... )
__attribute__ ((noreturn,unused));)
static void panic (const char *msg, ...)
{
va_list ap;
va_start (ap,msg);
errorMessage (msg,ap);
exit (1);
}
DOGCC(static void rpcdebug( const char *msg, ... )
__attribute__ ((noreturn,unused));)
static void rpcdebug (const char *msg, ...)
{
va_list ap;
va_start (ap,msg);
errorMessage (msg,ap);
abort();
}
// Lua Types
enum {
RPC_NIL=0,
RPC_NUMBER,
RPC_BOOLEAN,
RPC_STRING,
RPC_TABLE,
RPC_TABLE_END,
RPC_FUNCTION,
RPC_FUNCTION_END,
RPC_REMOTE
};
// 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 );
}
}
// **************************************************************************
// transport layer generics
// read arbitrary length from the transport into a string buffer.
static void transport_read_string( Transport *tpt, const char *buffer, int length )
{
transport_read_buffer( tpt, ( u8 * )buffer, length );
}
// write arbitrary length string buffer to the transport
static void transport_write_string( Transport *tpt, const char *buffer, int length )
{
transport_write_buffer( tpt, ( u8 * )buffer, length );
}
// read a u8 from the transport
static u8 transport_read_u8( Transport *tpt )
{
u8 b;
struct exception e;
TRANSPORT_VERIFY_OPEN;
transport_read_buffer( tpt, &b, 1 );
return b;
}
// write a u8 to the transport
static void transport_write_u8( Transport *tpt, u8 x )
{
struct exception e;
TRANSPORT_VERIFY_OPEN;
transport_write_buffer( tpt, &x, 1 );
}
static void swap_bytes( uint8_t *number, size_t numbersize )
{
int i;
for ( i = 0 ; i < numbersize / 2 ; i ++ )
{
uint8_t temp = number[ i ];
number[ i ] = number[ numbersize - 1 - i ];
number[ numbersize - 1 - i ] = temp;
}
}
union u32_bytes {
uint32_t i;
uint8_t b[ 4 ];
};
// read a u32 from the transport
static u32 transport_read_u32( Transport *tpt )
{
union u32_bytes ub;
struct exception e;
TRANSPORT_VERIFY_OPEN;
transport_read_buffer ( tpt, ub.b, 4 );
if( tpt->net_little != tpt->loc_little )
swap_bytes( ( uint8_t * )ub.b, 4 );
return ub.i;
}
// write a u32 to the transport
static void transport_write_u32( Transport *tpt, u32 x )
{
union u32_bytes ub;
struct exception e;
TRANSPORT_VERIFY_OPEN;
ub.i = ( uint32_t )x;
if( tpt->net_little != tpt->loc_little )
swap_bytes( ( uint8_t * )ub.b, 4 );
transport_write_buffer( tpt, ub.b, 4 );
}
// read a lua number from the transport
static lua_Number transport_read_number( Transport *tpt )
{
lua_Number x;
u8 b[ tpt->lnum_bytes ];
struct exception e;
TRANSPORT_VERIFY_OPEN;
transport_read_buffer ( tpt, b, tpt->lnum_bytes );
if( tpt->net_little != tpt->loc_little )
swap_bytes( ( uint8_t * )b, tpt->lnum_bytes );
if( tpt->net_intnum != tpt->loc_intnum ) // if we differ on num types, use int
{
switch( tpt->lnum_bytes ) // read integer types
{
case 1: {
int8_t y = *( int8_t * )b;
x = ( lua_Number )y;
} break;
case 2: {
int16_t y = *( int16_t * )b;
x = ( lua_Number )y;
} break;
case 4: {
int32_t y = *( int32_t * )b;
x = ( lua_Number )y;
} break;
case 8: {
int64_t y = *( int64_t * )b;
x = ( lua_Number )y;
} break;
default: lua_assert( 0 );
}
}
else
x = ( lua_Number ) *( lua_Number * )b; // if types match, use native type
return x;
}
// write a lua number to the transport
static void transport_write_number( Transport *tpt, lua_Number x )
{
struct exception e;
TRANSPORT_VERIFY_OPEN;
if( tpt->net_intnum )
{
switch( tpt->lnum_bytes )
{
case 1: {
int8_t y = ( int8_t )x;
transport_write_buffer( tpt, ( u8 * )&y, 1 );
} break;
case 2: {
int16_t y = ( int16_t )x;
if( tpt->net_little != tpt->loc_little )
swap_bytes( ( uint8_t * )&y, 2 );
transport_write_buffer( tpt, ( u8 * )&y, 2 );
} break;
case 4: {
int32_t y = ( int32_t )x;
if( tpt->net_little != tpt->loc_little )
swap_bytes( ( uint8_t * )&y, 4 );
transport_write_buffer( tpt,( u8 * )&y, 4 );
} break;
case 8: {
int64_t y = ( int64_t )x;
if( tpt->net_little != tpt->loc_little )
swap_bytes( ( uint8_t * )&y, 8 );
transport_write_buffer( tpt, ( u8 * )&y, 8 );
} break;
default: lua_assert(0);
}
}
else
{
if( tpt->net_little != tpt->loc_little )
swap_bytes( ( uint8_t * )&x, 8 );
transport_write_buffer( tpt, ( u8 * )&x, 8 );
}
}
// **************************************************************************
// lua utilities
// replacement for lua_error
void my_lua_error( lua_State *L, const char *errmsg )
{
lua_pushstring( L, errmsg );
lua_error( L );
}
int check_num_args( lua_State *L, int desired_n )
{
int n = lua_gettop( L ); // number of arguments on stack
if ( n != desired_n )
{
return luaL_error( L, "must have %d arg%c", desired_n,
( desired_n == 1 ) ? '\0' : 's' );
}
return n;
}
static int ismetatable_type( lua_State *L, int ud, const char *tname )
{
if( lua_getmetatable( L, ud ) ) { // does it have a metatable?
lua_getfield( L, LUA_REGISTRYINDEX, tname ); // get correct metatable
if( lua_rawequal( L, -1, -2 ) ) { // does it have the correct mt?
lua_pop( L, 2 ); // remove both metatables
return 1;
}
}
return 0;
}
/****************************************************************************/
// read and write lua variables to a transport.
// these functions do little error handling of their own, but they call transport
// functions which may throw exceptions, so calls to these functions must be
// wrapped in a Try block.
static void write_variable( Transport *tpt, lua_State *L, int var_index );
static int read_variable( Transport *tpt, lua_State *L );
// write a table at the given index in the stack. the index must be absolute
// (i.e. positive).
// @@@ circular table references will cause stack overflow!
static void write_table( Transport *tpt, lua_State *L, int table_index )
{
lua_pushnil( L ); // push first key
while ( lua_next( L, table_index ) )
{
// next key and value were pushed on the stack
write_variable( tpt, L, lua_gettop( L ) - 1 );
write_variable( tpt, L, lua_gettop( L ) );
// remove value, keep key for next iteration
lua_pop( L, 1 );
}
}
static int writer( lua_State *L, const void* b, size_t size, void* B ) {
(void)L;
luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
return 0;
}
#if defined( LUA_CROSS_COMPILER ) && !defined( LUARPC_STANDALONE )
#include "lundump.h"
#include "ldo.h"
// Dump bytecode representation of function onto stack and send. This
// implementation uses eLua's crosscompile dump to match match the
// bytecode representation to the client/server negotiated format.
static void write_function( Transport *tpt, lua_State *L, int var_index )
{
TValue *o;
luaL_Buffer b;
DumpTargetInfo target;
target.little_endian=tpt->net_little;
target.sizeof_int=sizeof(int);
target.sizeof_strsize_t=sizeof(strsize_t);
target.sizeof_lua_Number=tpt->lnum_bytes;
target.lua_Number_integral=tpt->net_intnum;
target.is_arm_fpa=0;
// push function onto stack, serialize to string
lua_pushvalue( L, var_index );
luaL_buffinit( L, &b );
lua_lock(L);
o = L->top - 1;
luaU_dump_crosscompile(L,clvalue(o)->l.p,writer,&b,0,target);
lua_unlock(L);
// put string representation on stack and send it
luaL_pushresult( &b );
write_variable( tpt, L, lua_gettop( L ) );
// Remove function & dumped string from stack
lua_pop( L, 2 );
}
#else
static void write_function( Transport *tpt, lua_State *L, int var_index )
{
luaL_Buffer b;
// push function onto stack, serialize to string
lua_pushvalue( L, var_index );
luaL_buffinit( L, &b );
lua_dump(L, writer, &b);
// put string representation on stack and send it
luaL_pushresult( &b );
write_variable( tpt, L, lua_gettop( L ) );
// Remove function & dumped string from stack
lua_pop( L, 2 );
}
#endif
static void helper_remote_index( Helper *helper );
// write a variable at the given index in the stack. the index must be absolute
// (i.e. positive).
static void write_variable( Transport *tpt, lua_State *L, int var_index )
{
int stack_at_start = lua_gettop( L );
switch( lua_type( L, var_index ) )
{
case LUA_TNUMBER:
transport_write_u8( tpt, RPC_NUMBER );
transport_write_number( tpt, lua_tonumber( L, var_index ) );
break;
case LUA_TSTRING:
{
const char *s;
u32 len;
transport_write_u8( tpt, RPC_STRING );
s = lua_tostring( L, var_index );
len = lua_strlen( L, var_index );
transport_write_u32( tpt, len );
transport_write_string( tpt, s, len );
break;
}
case LUA_TTABLE:
transport_write_u8( tpt, RPC_TABLE );
write_table( tpt, L, var_index );
transport_write_u8( tpt, RPC_TABLE_END );
break;
case LUA_TNIL:
transport_write_u8( tpt, RPC_NIL );
break;
case LUA_TBOOLEAN:
transport_write_u8( tpt,RPC_BOOLEAN );
transport_write_u8( tpt, ( u8 )lua_toboolean( L, var_index ) );
break;
case LUA_TFUNCTION:
transport_write_u8( tpt, RPC_FUNCTION );
write_function( tpt, L, var_index );
transport_write_u8( tpt, RPC_FUNCTION_END );
break;
case LUA_TUSERDATA:
if( lua_isuserdata( L, var_index ) && ismetatable_type( L, var_index, "rpc.helper" ) )
{
transport_write_u8( tpt, RPC_REMOTE );
helper_remote_index( ( Helper * )lua_touserdata( L, var_index ) );
} else
luaL_error( L, "userdata transmission unsupported" );
break;
case LUA_TTHREAD:
luaL_error( L, "thread transmission unsupported" );
break;
case LUA_TLIGHTUSERDATA:
luaL_error( L, "light userdata transmission unsupported" );
break;
}
MYASSERT( lua_gettop( L ) == stack_at_start );
}
// read a table and push in onto the stack
static void read_table( Transport *tpt, lua_State *L )
{
int table_index;
lua_newtable( L );
table_index = lua_gettop( L );
for ( ;; )
{
if( !read_variable( tpt, L ) )
return;
read_variable( tpt, L );
lua_rawset( L, table_index );
}
}
// read function and load
static void read_function( Transport *tpt, lua_State *L )
{
const char *b;
size_t len;
for( ;; )
{
if( !read_variable( tpt, L ) )
return;
b = luaL_checklstring( L, -1, &len );
luaL_loadbuffer( L, b, len, b );
lua_insert( L, -2 );
lua_pop( L, 1 );
}
}
static void read_index( Transport *tpt, lua_State *L )
{
u32 len;
char *funcname;
char *token = NULL;
len = transport_read_u32( tpt ); // variable name length
funcname = ( char * )alloca( len + 1 );
transport_read_string( tpt, funcname, len );
funcname[ len ] = 0;
token = strtok( funcname, "." );
lua_getglobal( L, token );
token = strtok( NULL, "." );
while( token != NULL )
{
lua_getfield( L, -1, token );
lua_remove( L, -2 );
token = strtok( NULL, "." );
}
}
// read a variable and push in onto the stack. this returns 1 if a "normal"
// variable was read, or 0 if an end-table or end-function marker was read (in which case
// nothing is pushed onto the stack).
static int read_variable( Transport *tpt, lua_State *L )
{
struct exception e;
u8 type = transport_read_u8( tpt );
switch( type )
{
case RPC_NIL:
lua_pushnil( L );
break;
case RPC_BOOLEAN:
lua_pushboolean( L, transport_read_u8( tpt ) );
break;
case RPC_NUMBER:
lua_pushnumber( L, transport_read_number( tpt ) );
break;
case RPC_STRING:
{
u32 len = transport_read_u32( tpt );
char *s = ( char * )alloca( len + 1 );
transport_read_string( tpt, s, len );
s[ len ] = 0;
lua_pushlstring( L, s, len );
break;
}
case RPC_TABLE:
read_table( tpt, L );
break;
case RPC_TABLE_END:
return 0;
case RPC_FUNCTION:
read_function( tpt, L );
break;
case RPC_FUNCTION_END:
return 0;
case RPC_REMOTE:
read_index( tpt, L );
break;
default:
e.errnum = type;
e.type = fatal;
Throw( e );
}
return 1;
}
// **************************************************************************
// rpc utilities
// functions for sending and receving headers
static void client_negotiate( Transport *tpt )
{
struct exception e;
char header[ 8 ];
int x = 1;
// default client configuration
tpt->loc_little = ( char )*( char * )&x;
tpt->lnum_bytes = ( char )sizeof( lua_Number );
tpt->loc_intnum = ( char )( ( ( lua_Number )0.5 ) == 0 );
// write the protocol header
header[0] = 'L';
header[1] = 'R';
header[2] = 'P';
header[3] = 'C';
header[4] = RPC_PROTOCOL_VERSION;
header[5] = tpt->loc_little;
header[6] = tpt->lnum_bytes;
header[7] = tpt->loc_intnum;
transport_write_string( tpt, header, sizeof( header ) );
// read server's response
transport_read_string( tpt, header, sizeof( header ) );
if( header[0] != 'L' ||
header[1] != 'R' ||
header[2] != 'P' ||
header[3] != 'C' ||
header[4] != RPC_PROTOCOL_VERSION )
{
e.errnum = ERR_HEADER;
e.type = nonfatal;
Throw( e );
}
// write configuration from response
tpt->net_little = header[5];
tpt->lnum_bytes = header[6];
tpt->net_intnum = header[7];
}
static void server_negotiate( Transport *tpt )
{
struct exception e;
char header[ 8 ];
int x = 1;
// default sever configuration
tpt->net_little = tpt->loc_little = ( char )*( char * )&x;
tpt->lnum_bytes = ( char )sizeof( lua_Number );
tpt->net_intnum = tpt->loc_intnum = ( char )( ( ( lua_Number )0.5 ) == 0 );
// read and check header from client
transport_read_string( tpt, header, sizeof( header ) );
if( header[0] != 'L' ||
header[1] != 'R' ||
header[2] != 'P' ||
header[3] != 'C' ||
header[4] != RPC_PROTOCOL_VERSION )
{
e.errnum = ERR_HEADER;
e.type = nonfatal;
Throw( e );
}
// check if endianness differs, if so use big endian order
if( header[ 5 ] != tpt->loc_little )
header[ 5 ] = tpt->net_little = 0;
// set number precision to lowest common denominator
if( header[ 6 ] > tpt->lnum_bytes )
header[ 6 ] = tpt->lnum_bytes;
if( header[ 6 ] < tpt->lnum_bytes )
tpt->lnum_bytes = header[ 6 ];
// if lua_Number is integer on either side, use integer
if( header[ 7 ] != tpt->loc_intnum )
header[ 7 ] = tpt->net_intnum = 1;
// send reconciled configuration to client
transport_write_string( tpt, header, sizeof( header ) );
}
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.
// global error default (no handler)
static int global_error_handler = LUA_NOREF;
// 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
static 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 - 1 ]->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 ) );
}
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 );
}
}
static int helper_get( lua_State *L, Helper *helper )
{
struct exception e;
int freturn = 0;
Transport *tpt = &helper->handle->tpt;
Try
{
helper_wait_ready( tpt, RPC_CMD_GET );
helper_remote_index( helper );
read_variable( tpt, L );
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;
// 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;
}*/
// 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;
}
}
Catch( e )
{
freturn = generic_catch_handler( L, h->handle, e );
}
}
return freturn;
}