-
Notifications
You must be signed in to change notification settings - Fork 7
/
VK.LongPollServer.pas
410 lines (376 loc) · 11.2 KB
/
VK.LongPollServer.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
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
unit VK.LongPollServer;
interface
uses
System.SysUtils, System.Types, System.Classes, REST.Client, System.JSON,
System.Net.HttpClient, VK.Types, VK.Handler;
type
TOnLongPollServerUpdate = procedure(Sender: TObject; GroupID: string; Update: TJSONValue) of object;
TVkLongPollData = record
Key: string;
Wait: string;
TS: string;
Server: string;
Version: string;
Mode: string;
Action: string;
function Request: string;
end;
TVkLongPollServer = class
private
FThread: TThread;
FLongPollNeedStop: Boolean;
FLongPollStopped: Boolean;
FLongPollData: TVkLongPollData;
FParams: TParams;
FMethod: string;
FOnError: TOnVKError;
FInterval: Integer;
FGroupID: string;
FOnUpdate: TOnLongPollServerUpdate;
FHandler: TVkHandler;
FLogging: Boolean;
FDoSync: Boolean;
function QueryLongPollServer: Boolean;
procedure DoError(E: Exception);
procedure OnLongPollRecieve(Updates: TJSONArray);
procedure SetOnError(const Value: TOnVKError);
procedure SetInterval(const Value: Integer);
procedure SetGroupID(const Value: string);
procedure SetOnUpdate(const Value: TOnLongPollServerUpdate);
procedure SetMethod(const Value: string);
procedure SetParams(const Value: TParams);
function GetIsWork: Boolean;
procedure SetHandler(const Value: TVkHandler);
procedure SetLogging(const Value: Boolean);
procedure SetDoSync(const Value: Boolean);
procedure ParseResponse(Stream: TStringStream);
public
function Start: Boolean;
procedure Stop;
constructor Create; overload;
constructor Create(AClient: TRESTClient; AMethod: string; AParams: TParams); overload;
destructor Destroy; override;
property OnError: TOnVKError read FOnError write SetOnError;
property Interval: Integer read FInterval write SetInterval;
property GroupID: string read FGroupID write SetGroupID;
property OnUpdate: TOnLongPollServerUpdate read FOnUpdate write SetOnUpdate;
property Handler: TVkHandler read FHandler write SetHandler;
property Method: string read FMethod write SetMethod;
property Params: TParams read FParams write SetParams;
property IsWork: Boolean read GetIsWork;
property Logging: Boolean read FLogging write SetLogging;
property Thread: TThread read FThread;
property DoSync: Boolean read FDoSync write SetDoSync;
end;
const
DefaultLongPollServerInterval = 1000;
// Настройки лонгпул сервера
VK_LP_VERSION = '3';
VK_LP_WAIT = '25';
VK_LP_MODE = '10';
// Поля
VK_LP_FIELD_GROUP_ID = 'group_id';
VK_LP_FIELD_VERSION = 'lp_version';
VK_LP_FIELD_TS = 'ts';
VK_LP_FIELD_SERVER = 'server';
VK_LP_FIELD_KEY = 'key';
VK_LP_FIELD_ACTION_CHECK = 'a_check';
implementation
uses
System.Generics.Collections;
{ TVkLongPollServer }
constructor TVkLongPollServer.Create(AClient: TRESTClient; AMethod: string; AParams: TParams);
begin
inherited Create;
FDoSync := True;
Method := AMethod;
Params := AParams;
end;
constructor TVkLongPollServer.Create;
begin
inherited;
FDoSync := True;
FLogging := False;
FInterval := DefaultLongPollServerInterval;
end;
destructor TVkLongPollServer.Destroy;
begin
Stop;
inherited;
end;
procedure TVkLongPollServer.DoError(E: Exception);
begin
if Assigned(FOnError) then
begin
if FDoSync or (TThread.CurrentThread.ThreadID <> MainThreadID) then
TThread.ForceQueue(nil,
procedure
begin
FOnError(Self, E, ERROR_VK_LONGPOLL, E.Message);
end)
else
FOnError(Self, E, ERROR_VK_LONGPOLL, E.Message);
end;
end;
function TVkLongPollServer.GetIsWork: Boolean;
begin
Result := not FLongPollNeedStop;
end;
function TVkLongPollServer.QueryLongPollServer: Boolean;
var
JSText: string;
ResponseJSON: TJSONValue;
begin
Result := False;
ResponseJSON := nil;
//Выполняем запрос
try
with FHandler.Execute(FMethod, FParams) do
begin
if Success then
begin
ResponseJSON := GetJSONResponse;
JSText := ResponseJSON.ToJSON;
end;
Result := Success;
end;
except
on E: Exception do
begin
DoError(TVkLongPollServerException.Create(E.Message + #13#10 + JSText));
Exit;
end;
end;
//Если запрос выполнен успешно
if Result and Assigned(ResponseJSON) then
begin
try
FLongPollData.Action := VK_LP_FIELD_ACTION_CHECK;
FLongPollData.Mode := VK_LP_MODE;
FLongPollData.Key := ResponseJSON.GetValue(VK_LP_FIELD_KEY, '');
FLongPollData.Server := ResponseJSON.GetValue(VK_LP_FIELD_SERVER, '');
FLongPollData.TS := ResponseJSON.GetValue(VK_LP_FIELD_TS, '');
FLongPollData.Wait := VK_LP_WAIT;
FLongPollData.Version := VK_LP_VERSION;
Result := not FLongPollData.Server.IsEmpty;
finally
ResponseJSON.Free;
end;
end
else
Result := False;
if not Result then
DoError(TVkLongPollServerException.Create('QueryLongPollServer error '#13#10 + JSText));
end;
procedure TVkLongPollServer.OnLongPollRecieve(Updates: TJSONArray);
var
i: Integer;
begin
for i := 0 to Updates.Count - 1 do
begin
try
if FLogging then
try
FHandler.Log(Self, Updates.Items[i].ToString);
except
//
end;
FOnUpdate(Self, FGroupID, Updates.Items[i]);
except
on E: Exception do
DoError(TVkLongPollServerException.Create(E.Message));
end;
end;
end;
procedure TVkLongPollServer.SetDoSync(const Value: Boolean);
begin
FDoSync := Value;
end;
procedure TVkLongPollServer.SetLogging(const Value: Boolean);
begin
FLogging := Value;
end;
procedure TVkLongPollServer.SetGroupID(const Value: string);
begin
FGroupID := Value;
end;
procedure TVkLongPollServer.SetHandler(const Value: TVkHandler);
begin
FHandler := Value;
end;
procedure TVkLongPollServer.SetInterval(const Value: Integer);
begin
FInterval := Value;
end;
procedure TVkLongPollServer.SetMethod(const Value: string);
begin
FMethod := Value;
end;
procedure TVkLongPollServer.SetOnError(const Value: TOnVKError);
begin
FOnError := Value;
end;
procedure TVkLongPollServer.SetOnUpdate(const Value: TOnLongPollServerUpdate);
begin
FOnUpdate := Value;
end;
procedure TVkLongPollServer.SetParams(const Value: TParams);
var
Param: TParam;
begin
FParams := Value;
//Сохраним id группы, если его передали
for Param in FParams do
if Param[0] = VK_LP_FIELD_GROUP_ID then
FGroupID := Param[1];
end;
procedure TVkLongPollServer.ParseResponse(Stream: TStringStream);
var
JSON: TJSONValue;
Updates: TJSONArray;
begin
try
Stream.Position := 0;
JSON := TJSONObject.ParseJSONValue(Stream.DataString);
except
Exit;
end;
if Assigned(JSON) then
try
//Обновляем данные лонгпул сервера
FLongPollData.TS := JSON.GetValue(VK_LP_FIELD_TS, '');
//Пробуем получить список обновлений
if JSON.TryGetValue<TJSONArray>('updates', Updates) then
begin
//Отдаем обработку обновлений в основной поток
if not FLongPollNeedStop then
begin
if FDoSync then
TThread.Synchronize(nil,
procedure
begin
OnLongPollRecieve(Updates);
end)
else
OnLongPollRecieve(Updates);
end;
end
else //Ошибка при парсинге
begin
if not FLongPollNeedStop then
begin
//Если ошибка, то пробуем переподключиться к лонгпул серверу
if not QueryLongPollServer then
DoError(TVkLongPollServerParseException.Create('QueryLongPollServer error, result: ' + Stream.DataString));
end;
end;
finally
JSON.Free;
end;
end;
function TVkLongPollServer.Start: Boolean;
begin
Result := False;
FLongPollNeedStop := False;
//Проверяем, есть ли обработчик событий, иначе нафига нам лонгпул)
if not Assigned(FOnUpdate) then
raise Exception.Create('Необходимо обязательно указать обработчик входящих обновлений');
//Подключаемся к серверу, получаем данные для запросов
if not QueryLongPollServer then
Exit;
//Запускаем поток для выполнения запросов
FThread := TThread.CreateAnonymousThread(
procedure
var
HTTP: THTTPClient;
ReqCode: Integer;
Stream: TStringStream;
begin
FLongPollStopped := False;
HTTP := THTTPClient.Create;
Stream := TStringStream.Create('', TEncoding.UTF8);
try
while (not TThread.Current.CheckTerminated) and (not FLongPollNeedStop) do
begin
//Очистим результат запроса с прошлого раза
Stream.Clear;
//Выполняем запрос
ReqCode := 0;
try
var Res := HTTP.BeginGet(FLongPollData.Request, Stream);
while (not Res.IsCompleted) and (not Res.IsCancelled) do
begin
if FLongPollNeedStop then
Res.Cancel;
Sleep(1);
end;
if not FLongPollNeedStop then
ReqCode := (Res as IHTTPResponse).StatusCode;
except
on E: Exception do
DoError(TVkLongPollServerHTTPException.Create(E.Message));
end;
//Если пора останавливаться - выходим из цикла
if FLongPollNeedStop then
Break;
//Если запрос выполнен успешно
if ReqCode = 200 then
ParseResponse(Stream)
else
begin
//Если ошибка, то пробуем переподключиться к лонгпул серверу
if not QueryLongPollServer then
DoError(TVkLongPollServerParseException.Create('QueryLongPollServer error, result: ' + Stream.DataString));
end;
//Интервал между запросами
Sleep(FInterval);
//Если пора останавливаться - выходим из цикла
if FLongPollNeedStop then
Break;
end;
except
on E: Exception do
DoError(TVkLongPollServerParseException.Create(E.Message));
end;
FLongPollStopped := True;
HTTP.Free;
Stream.Free;
end);
FThread.FreeOnTerminate := False;
FThread.Start;
Result := True;
end;
procedure TVkLongPollServer.Stop;
begin
FLongPollNeedStop := True;
if Assigned(FThread) then
begin
FThread.Terminate;
while not FLongPollStopped do
begin
if not IsConsole then
CheckSynchronize(1000)
else
TThread.Yield;
Sleep(100);
end;
FThread.Free;
FThread := nil;
end
else
FLongPollNeedStop := False;
end;
{ TVkLongPollData }
function TVkLongPollData.Request: string;
begin
Result := Server +
'?act=' + Action +
'&key=' + Key +
'&mode=' + Mode +
'&ts=' + TS +
'&wait=' + Wait +
'&version=' + Version;
if Pos('http', Result) = 0 then
Result := 'http://' + Result;
end;
end.