Skip to content

Commit adebcb2

Browse files
committed
Finalizers and cleanup around (DNET-698)
1 parent 011e555 commit adebcb2

59 files changed

Lines changed: 1816 additions & 3197 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/FetchResponse.cs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,13 @@ namespace FirebirdSql.Data.Client.Managed
2323
{
2424
internal class FetchResponse : IResponse
2525
{
26-
#region Fields
27-
28-
private int _status;
29-
private int _count;
30-
31-
#endregion
32-
33-
#region Properties
34-
35-
public int Status
36-
{
37-
get { return _status; }
38-
}
39-
40-
public int Count
41-
{
42-
get { return _count; }
43-
}
44-
45-
#endregion
46-
47-
#region Constructors
26+
public int Status { get; }
27+
public int Count { get; }
4828

4929
public FetchResponse(int status, int count)
5030
{
51-
_status = status;
52-
_count = count;
31+
Status = status;
32+
Count = count;
5333
}
54-
55-
#endregion
5634
}
5735
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/GdsConnection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ namespace FirebirdSql.Data.Client.Managed
3030
{
3131
internal class GdsConnection
3232
{
33-
const ulong KeepAliveTime = 1800000; //30min
34-
const ulong KeepAliveInterval = 1800000; //30min
33+
const ulong KeepAliveTime = 1800000; // 30min
34+
const ulong KeepAliveInterval = 1800000; // 30min
3535

3636
#region Fields
3737

@@ -271,7 +271,7 @@ private IPAddress GetIPAddress(string dataSource, AddressFamily addressFamily)
271271
IPAddress[] addresses = Dns.GetHostEntry(dataSource).AddressList;
272272
#endif
273273

274-
// Try to avoid problems with IPV6 addresses
274+
// try to avoid problems with IPv6 addresses
275275
foreach (IPAddress address in addresses)
276276
{
277277
if (address.AddressFamily == addressFamily)

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/GenericResponse.cs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,49 +23,17 @@ namespace FirebirdSql.Data.Client.Managed
2323
{
2424
internal sealed class GenericResponse : IResponse
2525
{
26-
#region Fields
27-
28-
private int _objectHandle;
29-
private long _blobId;
30-
private byte[] _data;
31-
private IscException _exception;
32-
33-
#endregion
34-
35-
#region Properties
36-
37-
public int ObjectHandle
38-
{
39-
get { return _objectHandle; }
40-
}
41-
42-
public long BlobId
43-
{
44-
get { return _blobId; }
45-
}
46-
47-
public byte[] Data
48-
{
49-
get { return _data; }
50-
}
51-
52-
public IscException Exception
53-
{
54-
get { return _exception; }
55-
}
56-
57-
#endregion
58-
59-
#region Constructors
26+
public int ObjectHandle { get; }
27+
public long BlobId { get; }
28+
public byte[] Data { get; }
29+
public IscException Exception { get; }
6030

6131
public GenericResponse(int objectHandle, long blobId, byte[] data, IscException exception)
6232
{
63-
_objectHandle = objectHandle;
64-
_blobId = blobId;
65-
_data = data;
66-
_exception = exception;
33+
ObjectHandle = objectHandle;
34+
BlobId = blobId;
35+
Data = data;
36+
Exception = exception;
6737
}
68-
69-
#endregion
7038
}
7139
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/SqlResponse.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,11 @@ namespace FirebirdSql.Data.Client.Managed
2323
{
2424
internal class SqlResponse : IResponse
2525
{
26-
#region Fields
27-
28-
private int _count;
29-
30-
#endregion
31-
32-
#region Properties
33-
34-
public int Count
35-
{
36-
get { return _count; }
37-
}
38-
39-
#endregion
40-
41-
#region Constructors
26+
public int Count { get; }
4227

4328
public SqlResponse(int count)
4429
{
45-
_count = count;
30+
Count = count;
4631
}
47-
48-
#endregion
4932
}
5033
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/SspiHelper.cs

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,15 @@ public SecBuffer(int bufferSize)
8888
}
8989

9090
public SecBuffer(byte[] secBufferBytes)
91+
: this(secBufferBytes.Length)
9192
{
92-
cbBuffer = secBufferBytes.Length;
93-
bufferType = (int)SecBufferType.SECBUFFER_TOKEN;
94-
pvBuffer = Marshal.AllocHGlobal(cbBuffer);
9593
Marshal.Copy(secBufferBytes, 0, pvBuffer, cbBuffer);
9694
}
9795

9896
public SecBuffer(byte[] secBufferBytes, SecBufferType bufferType)
97+
: this(secBufferBytes)
9998
{
100-
cbBuffer = secBufferBytes.Length;
10199
this.bufferType = (int)bufferType;
102-
pvBuffer = Marshal.AllocHGlobal(cbBuffer);
103-
Marshal.Copy(secBufferBytes, 0, pvBuffer, cbBuffer);
104100
}
105101

106102
public void Dispose()
@@ -129,7 +125,7 @@ private struct SecBufferDesc : IDisposable
129125
{
130126
public int ulVersion;
131127
public int cBuffers;
132-
public IntPtr pBuffers; //Point to SecBuffer
128+
public IntPtr pBuffers;
133129

134130
public SecBufferDesc(int bufferSize)
135131
{
@@ -272,9 +268,9 @@ out SecInteger ptsExpiry //PTimeStamp
272268

273269
#region Private members
274270

275-
private SecHandle _clientCredentials = new SecHandle();
276-
private SecHandle _clientContext = new SecHandle();
277-
private bool _disposed = false;
271+
private SecHandle _clientCredentials;
272+
private SecHandle _clientContext;
273+
private bool _disposed;
278274

279275
private string _securPackage;
280276
private string _remotePrincipal;
@@ -309,10 +305,12 @@ public SspiHelper(string securityPackage, string remotePrincipal)
309305
{
310306
_securPackage = securityPackage;
311307
_remotePrincipal = remotePrincipal;
308+
_clientCredentials = new SecHandle();
312309
SecInteger expiry = new SecInteger();
313-
if (AcquireCredentialsHandle(null, securityPackage, SECPKG_CRED_OUTBOUND,
314-
IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero,
315-
out _clientCredentials, out expiry) != SEC_E_OK)
310+
int resCode = AcquireCredentialsHandle(null, securityPackage, SECPKG_CRED_OUTBOUND,
311+
IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero,
312+
out _clientCredentials, out expiry);
313+
if (resCode != SEC_E_OK)
316314
throw new Exception($"{nameof(AcquireCredentialsHandle)} failed");
317315
}
318316

@@ -326,27 +324,27 @@ public SspiHelper(string securityPackage, string remotePrincipal)
326324
/// <returns>Client authentication data to be sent to server</returns>
327325
public byte[] InitializeClientSecurity()
328326
{
329-
if (_disposed)
330-
throw new ObjectDisposedException(nameof(SspiHelper));
327+
EnsureDisposed();
331328
CloseClientContext();
332-
SecInteger expiry = new SecInteger(0);
329+
_clientContext = new SecHandle();
330+
SecInteger expiry = new SecInteger();
333331
uint contextAttributes;
334332
SecBufferDesc clientTokenBuf = new SecBufferDesc(MAX_TOKEN_SIZE);
335333
try
336334
{
337335
int resCode = InitializeSecurityContext(
338336
ref _clientCredentials,
339337
IntPtr.Zero,
340-
_remotePrincipal,// null string pszTargetName,
338+
_remotePrincipal,
341339
STANDARD_CONTEXT_ATTRIBUTES,
342-
0,//int Reserved1,
343-
SECURITY_NATIVE_DREP,//int TargetDataRep
344-
IntPtr.Zero, //Always zero first time around...
345-
0, //int Reserved2,
346-
out _clientContext, //pHandle CtxtHandle = SecHandle
347-
ref clientTokenBuf,//ref SecBufferDesc pOutput, //PSecBufferDesc
348-
out contextAttributes,//ref int pfContextAttr,
349-
out expiry); //ref IntPtr ptsExpiry ); //PTimeStamp
340+
0,
341+
SECURITY_NATIVE_DREP,
342+
IntPtr.Zero,
343+
0,
344+
out _clientContext,
345+
ref clientTokenBuf,
346+
out contextAttributes,
347+
out expiry);
350348
if (resCode != SEC_E_OK && resCode != SEC_I_CONTINUE_NEEDED)
351349
throw new Exception($"{nameof(InitializeSecurityContext)} failed");
352350
return clientTokenBuf.GetSecBufferBytes();
@@ -366,8 +364,7 @@ public byte[] InitializeClientSecurity()
366364
/// <returns>Client authentication data to be sent to server</returns>
367365
public byte[] GetClientSecurity(byte[] serverToken)
368366
{
369-
if (_disposed)
370-
throw new ObjectDisposedException(nameof(SspiHelper));
367+
EnsureDisposed();
371368
if (_clientContext.IsInvalid)
372369
throw new InvalidOperationException($"{nameof(InitializeClientSecurity)} not called");
373370
SecInteger expiry = new SecInteger();
@@ -381,16 +378,16 @@ public byte[] GetClientSecurity(byte[] serverToken)
381378
int resCode = InitializeSecurityContext(
382379
ref _clientCredentials,
383380
ref _clientContext,
384-
_remotePrincipal,// null string pszTargetName,
381+
_remotePrincipal,
385382
STANDARD_CONTEXT_ATTRIBUTES,
386-
0,//int Reserved1,
387-
SECURITY_NATIVE_DREP,//int TargetDataRep
388-
ref serverTokenBuf, // server token must be ref because it is struct
389-
0, //int Reserved2,
390-
out _clientContext, //pHandle CtxtHandle = SecHandle
391-
ref clientTokenBuf,//ref SecBufferDesc pOutput, //PSecBufferDesc
392-
out contextAttributes,//ref int pfContextAttr,
393-
out expiry); //ref IntPtr ptsExpiry ); //PTimeStamp
383+
0,
384+
SECURITY_NATIVE_DREP,
385+
ref serverTokenBuf,
386+
0,
387+
out _clientContext,
388+
ref clientTokenBuf,
389+
out contextAttributes,
390+
out expiry);
394391
if (resCode != SEC_E_OK && resCode != SEC_I_CONTINUE_NEEDED)
395392
throw new Exception($"{nameof(InitializeSecurityContext)} failed");
396393
return clientTokenBuf.GetSecBufferBytes();
@@ -431,37 +428,30 @@ public void Dispose()
431428

432429
private void Dispose(bool disposing)
433430
{
434-
lock (this)
431+
if (!_disposed)
435432
{
436-
if (!_disposed)
437-
{
438-
CloseClientContext();
439-
CloseClientCredentials();
440-
441-
if (disposing)
442-
{ }
443-
444-
_disposed = true;
445-
}
433+
_disposed = true;
434+
CloseClientContext();
435+
CloseClientCredentials();
446436
}
447437
}
448438

449439
private void CloseClientContext()
450440
{
451441
if (!_clientContext.IsInvalid)
452-
{
453442
DeleteSecurityContext(ref _clientContext);
454-
_clientContext = new SecHandle();
455-
}
456443
}
457444

458445
private void CloseClientCredentials()
459446
{
460447
if (!_clientCredentials.IsInvalid)
461-
{
462448
FreeCredentialsHandle(ref _clientCredentials);
463-
_clientCredentials = new SecHandle();
464-
}
449+
}
450+
451+
private void EnsureDisposed()
452+
{
453+
if (_disposed)
454+
throw new ObjectDisposedException(nameof(SspiHelper));
465455
}
466456

467457
#endregion

0 commit comments

Comments
 (0)