-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathluarpc_rpc.h
178 lines (143 loc) · 5.09 KB
/
luarpc_rpc.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
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
#include "cexcept.h"
#include "type.h"
#include "serial.h"
#include "platform_conf.h"
/****************************************************************************/
// Parameters
#define NUM_FUNCNAME_CHARS 20 // Maximum function name length
#define MAX_LINK_ERRS ( 2 ) // Maximum number of framing errors before connection reset
#if defined( LUARPC_ENABLE_SERIAL )
#define LUARPC_MODE "serial"
#define tpt_handler ser_handler
#elif defined( LUARPC_ENABLE_SOCKET )
#define LUARPC_MODE "tcpip"
typedef int tpt_handler;
#define MAXCON ( 1 )
#else
#error "No RPC mode Selected.."
#endif
// a kind of silly way to get the maximum int, but oh well ...
#define MAXINT ((int)((((unsigned int)(-1)) << 1) >> 1))
/****************************************************************************/
// Debug Error Handling
// allow special handling for GCC compiler
#ifdef __GNUC__
#define DOGCC(x) x
#else
#define DOGCC(x) /* */
#endif
// Assertions for debug mode
#ifdef LUARPC_DEBUG
#ifdef __GNUC__
#define MYASSERT(a) if (!(a)) rpcdebug ( \
"assertion \"" #a "\" failed in %s() [%s]",__FUNCTION__,__FILE__);
#else
#define MYASSERT(a) if (!(a)) rpcdebug ( \
"assertion \"" #a "\" failed in %s:%d",__FILE__,__LINE__);
#endif
#else
#define MYASSERT(a) ;
#endif
//****************************************************************************
// Error Messages & Exceptions
#ifdef WIN32_BUILD
#include "windows.h"
#define transport_strerror strerror
#define transport_errno (GetLastError())
#else
#define transport_errno errno
#define transport_strerror strerror
#endif
// error numbers passed around are normal system "errno" error numbers
// (normally generated by transport operations), except when they have the
// following values:
enum {
ERR_EOF = MAXINT - 100, // reached end of file on transport
ERR_CLOSED = MAXINT - 101, // attempted operation on closed transport
ERR_PROTOCOL = MAXINT - 102, // some error in the received protocol
ERR_NODATA = MAXINT - 103,
ERR_COMMAND = MAXINT - 106,
ERR_HEADER = MAXINT - 107,
ERR_LONGFNAME = MAXINT - 108
};
enum exception_type { done, nonfatal, fatal };
struct exception {
enum exception_type type;
int errnum;
};
define_exception_type(struct exception);
extern struct exception_context the_exception_context[ 1 ];
//****************************************************************************
// LuaRPC Structures
// Transport Connection Structure
typedef struct _Transport Transport;
struct _Transport
{
tpt_handler fd;
unsigned tmr_id;
u32 loc_little: 1, // Local is little endian?
loc_armflt: 1, // local float representation is arm float?
loc_intnum: 1, // Local is integer only?
net_little: 1, // Network is little endian?
net_intnum: 1; // Network is integer only?
u8 lnum_bytes;
};
typedef struct _Handle Handle;
struct _Handle
{
Transport tpt; // the handle socket
int error_handler; // function reference
int async; // nonzero if async mode being used
int read_reply_count; // number of async call return values to read
};
typedef struct _Helper Helper;
struct _Helper {
Handle *handle; // pointer to handle object
Helper *parent; // parent helper
int pref; // Parent reference idx in registry
u8 nparents; // number of parents
char funcname[NUM_FUNCNAME_CHARS]; // name of the function
};
typedef struct _ServerHandle ServerHandle;
struct _ServerHandle {
Transport ltpt; // listening transport, always valid if no error
Transport atpt; // accepting transport, valid if connection established
int link_errs;
};
// Connection State Checking
#ifdef WIN32_BUILD
#define INVALID_TRANSPORT (INVALID_HANDLE_VALUE)
#else
#define INVALID_TRANSPORT (-1)
#endif
#define TRANSPORT_VERIFY_OPEN \
if (tpt->fd == INVALID_TRANSPORT) \
{ \
e.errnum = ERR_CLOSED; \
e.type = fatal; \
Throw( e ); \
}
// Arg & Error Checking Provided to Transport Mechanisms
int check_num_args (lua_State *L, int desired_n);
void deal_with_error (lua_State *L, Handle *h, const char *error_string);
void my_lua_error( lua_State *L, const char *errmsg );
// TRANSPORT API
// Setup Transport
void transport_init (Transport *tpt);
// Open Listener / Server
void transport_open_listener(lua_State *L, ServerHandle *handle);
// Open Connection / Client
int transport_open_connection(lua_State *L, Handle *handle);
// Accept Connection
void transport_accept (Transport *tpt, Transport *atpt);
// Read & Write to Transport
void transport_read_buffer (Transport *tpt, u8 *buffer, int length);
void transport_write_buffer (Transport *tpt, const u8 *buffer, int length);
// Check if data is available on connection without reading:
// - 1 = data available, 0 = no data available
int transport_readable (Transport *tpt);
// Check if transport is open:
// - 1 = connection open, 0 = connection closed
int transport_is_open (Transport *tpt);
// Shut down connection
void transport_close (Transport *tpt);