public
Description: Lua-RPC for Lua 5.1.x
Homepage:
Clone URL: git://github.com/jsnyder/luarpc.git
luarpc / luarpc_rpc.h
100644 196 lines (146 sloc) 5.171 kb
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
#include "cexcept.h"
 
/****************************************************************************/
/* handle the differences between winsock and unix */
 
#ifdef WIN32 /* BEGIN WIN32 SOCKET SETUP */
 
#define close closesocket
#define read(fd,buf,len) recv ((fd),(buf),(len),0)
#define write(fd,buf,len) send ((fd),(buf),(len),0)
#define SOCKTYPE SOCKET
#define sock_errno (WSAGetLastError())
 
#else
 
#define SOCKTYPE int
#define net_startup() ;
#define sock_errno errno
#define transport_strerror strerror
 
#endif
 
/****************************************************************************/
/* parameters */
 
#define MAXCON 10 /* maximum number of waiting server connections */
 
/* a kind of silly way to get the maximum int, but oh well ... */
#define MAXINT ((int)((((unsigned int)(-1)) << 1) >> 1))
 
/****************************************************************************/
/* error handling */
 
/* allow special handling for GCC compiler */
#ifdef __GNUC__
#define DOGCC(x) x
#else
#define DOGCC(x) /* */
#endif
 
 
/* assertions */
 
#ifndef NDEBUG
#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
 
/****************************************************************************/
/* more error handling */
 
/* 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_BADFNAME = MAXINT - 104,
ERR_DATALINK = MAXINT - 105,
ERR_COMMAND = MAXINT - 106
};
 
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 ];
 
#define NUM_FUNCNAME_CHARS 20
 
/* Transport Connection Structure */
 
/* FIXME: should be cleaner */
typedef struct _Transport Transport;
struct _Transport
{
#ifdef LUARPC_ENABLE_SOCKET
  SOCKTYPE fd; /* INVALID_TRANSPORT if socket is closed */
#endif
 
#ifdef LUARPC_ENABLE_SERIAL
  int fd; /* INVALID_TRANSPORT if socket is closed */
#endif
 
#ifdef LUARPC_ENABLE_FIFO
int wrfd, rdfd;
#endif
  u8 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;
};
 
 
#ifdef LUARPC_ENABLE_SOCKET
#define LUARPC_MODE "tcpip"
#endif
 
#ifdef LUARPC_ENABLE_SERIAL
#define LUARPC_MODE "serial"
#endif
 
 
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 */
u8 nparents; /* number of parents */
  char funcname[NUM_FUNCNAME_CHARS]; /* name of the function */
};
 
 
typedef struct _ServerHandle ServerHandle;
struct _ServerHandle {
  Transport ltpt; /* listening socket, always valid if no error */
  Transport atpt; /* accepting socket, valid if connection established */
int link_errs;
};
 
/* Maximum number of framing errors before connection reset */
#define MAX_LINK_ERRS ( 2 )
 
#define INVALID_TRANSPORT (-1)
 
#define HEAD_BYTE (0x7e)
 
#define TAIL_BYTE (0x7f)
 
#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);
 
/* 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);