-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathceosservermethods.pas
172 lines (131 loc) · 3.56 KB
/
ceosservermethods.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
{*****************************************************************}
{ }
{ by Jose Benedito - [email protected] }
{ www.jbsolucoes.net }
{*****************************************************************}
unit ceosservermethods;
{$mode objfpc}{$H+}
interface
{$M+}
uses
Classes, SysUtils, variants, ceostypes, fpjson, jsonparser, ceosserver;
type
TFuncType = function(Request: TCeosRequestContent): TJSONStringType of object;
{ TCeosServerMethods }
TCeosServerMethods = class
private
procedure Call(Request: TCeosRequestContent; var Response: TCeosResponseContent);
public
constructor Create;
destructor Destroy; override;
procedure ProcessRequest(ARequest: TCeosRequestContent; var Response: TCeosResponseContent);
published
end;
function IsSelect(ASQL: string): boolean;
implementation
uses
ceosjson, ceosconsts, ceosmessages;
{ TCeosServerMethods }
constructor TCeosServerMethods.Create;
begin
end;
destructor TCeosServerMethods.Destroy;
begin
inherited Destroy;
end;
procedure TCeosServerMethods.Call(Request: TCeosRequestContent;
var Response: TCeosResponseContent);
var
m: TMethod;
sResult: string;
parser: tjsonparser;
begin
try
m.Code := Self.MethodAddress(Request.Method); //find method code
if assigned(m.Code) then
begin
m.Data := pointer(Self); //store pointer to object instance
sResult := TFuncType(m)(Request);
//addlog(sResult);
if IsJSON(sResult) then
begin
parser := TJSONParser.Create(sResult);
try
Response := TCeosResponseContent.Create;
Response.ID := Request.ID;
Response.ResultContent := parser.Parse;
//JSONRPCResult(parser.parse,Request.ID);
finally
parser.free;
parser := nil;
end;
end
else
begin
//addlog('result '+ sResult);
//Response := JSONRPCResult(TJSONString.create(sResult),Request.ID);
Response := TCeosResponseContent.Create;
Response.ID := Request.ID;
Response.ResultContent := TJSONString.create(sResult);
end;
end
else
begin
if assigned(Response) then
begin
Response.free;
Response := nil;
end;
//addlog('call '+ sResult);
Response := JSONRPCError(ERR_UNKNOW_FUNCTION, ERROR_UNKNOW_FUNCTION);
end;
except
on e: Exception do
begin
if assigned(Response) then
begin
Response.free;
Response := nil;
end;
Response := JSONRPCError(ERR_INTERNAL_ERROR, e.message, Request.ID);
end;
end;
end;
procedure TCeosServerMethods.ProcessRequest(ARequest: TCeosRequestContent; var Response: TCeosResponseContent);
begin
try
if Response <> nil then
begin
Response.free;
response := nil;
end;
if ARequest <> nil then
Call(ARequest, Response)
else
begin
if assigned(Response) then
begin
Response.free;
Response := nil;
end;
Response := JSONRPCError(ERR_INVALID_CONTENT, ERROR_INVALID_CONTENT, -1);
end;
except
on e: Exception do
begin
if assigned(Response) then
begin
Response.free;
Response := nil;
end;
Response := JSONRPCError(ERR_INTERNAL_ERROR, ERROR_INTERNAL_ERROR, -1);
end;
end;
end;
function IsSelect(ASQL: string): boolean;
begin
result := (pos('SELECT',UpperCase(ASQL)) > 0);
end;
initialization
UsingServerMethods := true;
end.