-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionManager.pas
201 lines (166 loc) · 6.98 KB
/
ConnectionManager.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
unit ConnectionManager;
{Обеспечиваем управление множественными поключениями без необходимости постоянных переподключений. При первом обращении нужное подключение создаётся,
при последующих - отдаются уже созданные.}
interface
uses CloudMailRu, CMLTypes, MRC_Helper, windows, Vcl.Controls, PLUGIN_Types, Settings, TCPasswordManagerHelper;
type
TNamedConnection = record
Name: WideString;
Connection: TCloudMailRu;
end;
TConnectionManager = class
private
Connections: array of TNamedConnection;
IniFileName: WideString;
Proxy: TProxySettings;
UploadBPS: integer;
DownloadBPS: integer;
Timeout: integer;
CloudMaxFileSize: integer;
PrecalculateHash: boolean;
CheckCRC: boolean;
ProgressHandleProc: TProgressHandler;
LogHandleProc: TLogHandler;
RequestHandleProc: TRequestHandler;
PasswordManager: TTCPasswordManager;
function ConnectionExists(connectionName: WideString): integer; //проверяет существование подключение
function new(connectionName: WideString): integer; //Добавляет подключение в пул
public
constructor Create(IniFileName: WideString; ProxySettings: TProxySettings; Timeout, CloudMaxFileSize, UploadBPS, DownloadBPS: integer; PrecalculateHash: boolean; CheckCRC: boolean; ProgressHandleProc: TProgressHandler; LogHandleProc: TLogHandler; RequestHandleProc: TRequestHandler; PasswordManager: TTCPasswordManager);
destructor Destroy(); override;
function get(connectionName: WideString; var OperationResult: integer; doInit: boolean = true): TCloudMailRu; //возвращает готовое подклчение по имени
function set_(connectionName: WideString; cloud: TCloudMailRu): boolean;
function init(connectionName: WideString; ProxySettings: TProxySettings; Timeout: integer): integer; //инициализирует подключение по его имени, возвращает код состояния
function free(connectionName: WideString): integer; //освобождает подключение по его имени, возвращает код состояния
function freeAll: integer; //освобождает все подключения
function initialized(connectionName: WideString): boolean; //Проверяет, инициализировано ли подключение
end;
implementation
{TConnectionManager}
constructor TConnectionManager.Create(IniFileName: WideString; ProxySettings: TProxySettings; Timeout, CloudMaxFileSize, UploadBPS, DownloadBPS: integer; PrecalculateHash: boolean; CheckCRC: boolean; ProgressHandleProc: TProgressHandler; LogHandleProc: TLogHandler; RequestHandleProc: TRequestHandler; PasswordManager: TTCPasswordManager);
begin
SetLength(Connections, 0);
self.IniFileName := IniFileName;
self.ProgressHandleProc := ProgressHandleProc;
self.LogHandleProc := LogHandleProc;
self.RequestHandleProc := RequestHandleProc;
self.Proxy := ProxySettings;
self.Timeout := Timeout;
self.CloudMaxFileSize := CloudMaxFileSize;
self.UploadBPS := UploadBPS;
self.DownloadBPS := DownloadBPS;
self.PrecalculateHash := PrecalculateHash;
self.CheckCRC := CheckCRC;
self.PasswordManager := PasswordManager;
end;
destructor TConnectionManager.Destroy;
begin
freeAll();
inherited;
end;
function TConnectionManager.get(connectionName: WideString; var OperationResult: integer; doInit: boolean = true): TCloudMailRu;
var
ConnectionIndex: integer;
begin
ConnectionIndex := ConnectionExists(connectionName);
if ConnectionIndex <> -1 then
begin
result := Connections[ConnectionIndex].Connection;
end else begin
result := Connections[new(connectionName)].Connection;
end;
if (doInit) then
begin
OperationResult := CLOUD_OPERATION_OK;
if not initialized(connectionName) then
OperationResult := init(connectionName, self.Proxy, self.Timeout);
if (OperationResult = CLOUD_OPERATION_OK) then
result := get(connectionName, OperationResult, false);
end;
{если подключиться не удалось, все функции облака будут возвращать негативный результат, но без AV}
end;
function TConnectionManager.set_(connectionName: WideString; cloud: TCloudMailRu): boolean;
var
ConnectionIndex: integer;
begin
ConnectionIndex := ConnectionExists(connectionName);
if ConnectionIndex = -1 then
exit(false);
Connections[ConnectionIndex].Connection := cloud;
result := true;
end;
function TConnectionManager.init(connectionName: WideString; ProxySettings: TProxySettings; Timeout: integer): integer;
var
cloud: TCloudMailRu;
AccountSettings: TAccountSettings;
LoginMethod: integer;
begin
result := CLOUD_OPERATION_OK;
AccountSettings := GetAccountSettingsFromIniFile(IniFileName, connectionName);
if not PasswordManager.GetAccountPassword(AccountSettings) then
exit(CLOUD_OPERATION_ERROR_STATUS_UNKNOWN); //INVALID_HANDLE_VALUE
if AccountSettings.encrypt_files_mode <> EncryptModeNone then
begin
if not PasswordManager.InitCloudCryptPasswords(AccountSettings) then
exit(CLOUD_OPERATION_FAILED);
end;
LogHandleProc(LogLevelConnect, MSGTYPE_CONNECT, PWideChar('CONNECT \' + connectionName));
cloud := TCloudMailRu.Create(AccountSettings, self.CloudMaxFileSize, self.Proxy, Timeout, self.UploadBPS, self.DownloadBPS, self.PrecalculateHash, self.CheckCRC, ProgressHandleProc, LogHandleProc, RequestHandleProc);
if not set_(connectionName, cloud) then
exit(CLOUD_OPERATION_ERROR_STATUS_UNKNOWN); //INVALID_HANDLE_VALUE
if (AccountSettings.twostep_auth) then
LoginMethod := CLOUD_AUTH_METHOD_TWO_STEP
else
LoginMethod := CLOUD_AUTH_METHOD_WEB;
if not(get(connectionName, result, false).login(LoginMethod)) then
begin
result := CLOUD_OPERATION_FAILED;
free(connectionName);
end;
//cloud.Destroy;
end;
function TConnectionManager.initialized(connectionName: WideString): boolean;
var
dump: integer;
begin
result := Assigned(get(connectionName, dump, false));
end;
function TConnectionManager.new(connectionName: WideString): integer;
begin
SetLength(Connections, Length(Connections) + 1);
Connections[Length(Connections) - 1].Name := connectionName;
result := Length(Connections) - 1;
end;
function TConnectionManager.ConnectionExists(connectionName: WideString): integer;
var
I: integer;
begin
result := -1;
for I := 0 to Length(Connections) - 1 do
begin
if Connections[I].Name = connectionName then
exit(I);
end;
end;
function TConnectionManager.free(connectionName: WideString): integer;
begin
result := CLOUD_OPERATION_OK;
get(connectionName, result, false).free;
set_(connectionName, nil);
end;
function TConnectionManager.freeAll: integer;
var
I: integer;
begin
result := CLOUD_OPERATION_OK;
for I := 0 to Length(Connections) - 1 do
begin
if initialized(Connections[I].Name) then
begin
Connections[I].Connection.free;
set_(Connections[I].Name, nil);
end;
end;
SetLength(Connections, 0);
end;
end.