-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPCsrv.pas
274 lines (247 loc) · 6.21 KB
/
RPCsrv.pas
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
unit RPCsrv;
(** Inter process procedure call server **)
INTERFACE
uses Classes,fgl,BaseUnix,Sockets,ObjectModel,ServerLoop,Crypto;
type
TRPCDelegate = class;
TRPCCon = class
socket:tSocket;
ArgsSize,ArgsRcvd: LongWord;
a, r: tMemoryStream;
iphash:tKey32;
delegate:TRPCDelegate;
procedure Create2;
destructor Destroy; override;
procedure Event(ev:word);
procedure SendTo(msg:tMemoryStream);
procedure Reset;
end;
TRPCDelegate = class
con: TRPCCon;
{procedure OnInput; virtual; abstract;
procedure OnOutput; virtual;
procedure WantOutput(enable:boolean);
constructor Create( icon: TRPCCon );
destructor Destroy; override;}
end;
TRemoteProc = procedure( con: tRPCCon );
TRemoteProcOpcode = Word;
procedure SetupRemoteProc( opcode: TRemoteProcOpcode; handler: tRemoteProc );
const
cRPCSuccess=0;
cRPCUnknown=2; {unknown opcode}
cRPCInvalid=3; {error in arguments}
cRPCBegan=4; {delegated process began}
type ERPCInvalid=class(Exception)
//constructor create;
end;
IMPLEMENTATION
type tRPCServer = object
listen:tSocket;
prev_on_terminate:procedure;
procedure Init;
procedure ListenEvent(ev:word);
procedure InitUnix;
end;
const cSocketName='ctrl';
var Server1: tRPCServer;
var Log: tEventLog;
var rpctable:TFPSMap;
procedure tRPCServer.InitUnix;
var addr:Sockets.sockaddr_un;
begin
addr.sun_family:=AF_UNIX;
addr.sun_path:=cSocketName;
fpUnlink(@addr.sun_path);
listen:=fpSocket(addr.sun_family,SOCK_STREAM,0);
SC(@fpSocket,listen);
SC(@fpBind,fpBind(listen,@addr,sizeof(addr)));
end;
procedure OnTerminate;
begin
fpUnlink(cSocketName);
if assigned(Server1.prev_on_terminate) then Server1.prev_on_terminate;
end;
procedure tRPCServer.Init;
begin
InitUnix;
SC(@fpListen,fpListen(listen,8));
ServerLoop.WatchFD(listen,@ListenEvent);
prev_on_terminate:=ServerLoop.OnTerminate;
ServerLoop.OnTerminate:=@OnTerminate;
end;
procedure tRPCServer.ListenEvent(ev:word);
var addr:Sockets.sockaddr_un;
var addrl:tSockLen;
var s:tSocket;
var cl:TRPCCon;
begin
addrl:=sizeof(addr);
s:=fpAccept(listen,@addr,@addrl);
if s<0 then begin
log.error(' accept failed',[]);
exit end;
cl:=TRPCCon.Create;
SHA256_Buffer(cl.iphash,32,addr,addrl);
cl.socket:=s;
//log.debug(' accepted from iphash=%S',[string(cl.iphash)]);
cl.Create2;
end;
type tMemoryStreamEX=class(tMemoryStream)
function Read(var Buffer; Count: LongInt): LongInt; override;
end;
function tMemoryStreamEX.Read(var Buffer; Count: LongInt): LongInt;
begin
result:=inherited Read(buffer,count);
if result<count then ERPCInvalid.Create('Not enough arguments');
end;
procedure tRPCCon.Create2;
begin
delegate:=nil;
a:=tMemoryStream.Create;
r:=tMemoryStream.Create;
Reset;
assert(socket>=0);
ServerLoop.WatchFD(socket,@Event);
end;
procedure tRPCCon.Reset;
begin
delegate:=nil;
a.Size:=4096;
ArgsSize:=2;
ArgsRcvd:=0;
r.Clear;
r.W2(0);
end;
destructor tRPCCon.Destroy;
begin
delegate.Free;
a.Free;
r.Free;
ServerLoop.WatchFD(socket,nil);
end;
procedure tRPCCon.Event(ev:word);
var rd:LongInt;
var ix:integer;
var opcode:TRemoteProcOpcode;
label error;
begin
//log.debug('.Client(%P).Event %D',[@self,ev]);
if assigned(delegate) then begin
{if (ev and POLLIN)>0 then delegate.OnInput;
if (ev and POLLOUT)>0 then delegate.OnOutput;}
assert(false);
end else if (ev and POLLIN)>0 then begin
rd:=fpRecv(socket,a.Memory+ArgsRcvd,ArgsSize-ArgsRcvd,0);
if rd<0 then begin
log.Error('.Client(%P).Event fpRecv failed %D',[@self,SocketError]);
goto error;
end else if rd=0 then goto error;
ArgsRcvd:=ArgsRcvd+rd;
if ArgsRcvd=2 then begin
a.Position:=0;
ArgsSize:=2+a.R2;
end;
if ArgsRcvd>=ArgsSize then begin
if ArgsSize<4 then begin
log.Error('.Client(%P).Event invalid rpc packet length',[@self]);
goto error;
end;
a.Size:=ArgsSize;
a.Position:=2;
opcode:=a.R2;
ix:=rpctable.IndexOf(@opcode);
if ix>-1 then try
TRemoteProc(rpctable.data[ix]^) ( self )
except
on ERPCInvalid do r.WriteByte(cRPCInvalid);
end else r.W2(cRPCUnknown);
r.Position:=0;
r.W2(r.Size-2);
self.SendTo(r);
self.Reset;
end;
end;
Exit;
error:
Self.Free;
//log.Debug('.Client(%P).Event destroy',[@self]);
Exit;
end;
procedure SetupRemoteProc( opcode: TRemoteProcOpcode; handler: tRemoteProc );
begin
//log.debug(' register RPC OpCode %D',[opcode]);
rpctable.Add(@opcode,@handler);
end;
procedure tRPCCon.SendTo(msg:tMemoryStream);
var re,tw:LongInt;
var pt:pointer;
begin
try
tw:=msg.Size;
pt:=msg.Memory;
while tw>0 do begin
re:=fpSend(socket,pt,tw,0);
if re<0 then SC(@fpSend,re-1);
tw:=tw-re;
pt:=pt+re;
end;
except {ignore exceptions}
end;
end;
BEGIN
ServerLoop.CreateLog(log,'RPCsrv');
Server1.Init;
rpctable:=TFPSMap.Create(sizeof(TRemoteProcOpcode),sizeof(TRemoteProc));
rpctable.sorted:=true;
rpctable.duplicates:=dupError;
END.
{*** Old Code ***}
{***Low-Level Part***}
if (ev and POLLOUT)>0 then begin
{$ifdef ctlStore}
if SndObjLeft=0 then begin
{$endif}
ServerLoop.WatchFD(s,nil);
ServerLoop.WatchFD(s,@Event);
{$ifdef ctlStore}
Dispose(SndObj,Done);
SndObj:=nil;
end else begin
arg:=GetMem(cBuf);
rc:=SndObjLeft;
if rc>cBuf then rc:=cBuf;
SndObj^.Read(arg^,rc);
sndc:=fpSend(s,arg,rc,0);
if sndc<=0 then error:=true
else if sndc<rc
then SndObj^.Skip(sndc-rc);
SndObjLeft:=SndObjLeft-sndc;
FreeMem(arg,cBuf);
end;
{$endif}
end;
if (ev and (POLLIN or POLLHUP or POLLERR))>0 then begin
{$ifdef ctlStore}
if RcvObjLeft>0 then begin
arg:=GetMem(cBuf);
rc:=cBuf; if rc>RcvObjLeft then rc:=RcvObjLeft;
rc:=fpRecv(s,arg,rc,0);
if rc>0 then begin
RcvObj^.Write(arg^,rc);
RcvObjHctx.Update(arg^,rc);
RcvObjLeft:=RcvObjLeft-rc;
if RcvObjLeft=0 then RcvObjComplete;
rc:=1;
end;
end else begin
{$endif}
{$ifdef ctlStore}
procedure tClient.SendObject(var o:tStoreObject; ilen:LongWord);
begin
SndObjLeft:=ilen;
SndObj:=@o;
ServerLoop.WatchFD(s,nil);
ServerLoop.WatchFDRW(s,@Event);
end;
{$endif}