Skip to content

Commit 580e42d

Browse files
committed
Fix SonarCloud code smells in Socket and SslStream
- SslStream + remove unnecessary boolean literals in Length/DataAvailable getters. + rename Read/Write parameter size → count to match Stream. + fix garbled Length/DataAvailable summaries and a typo. - Socket + add XML doc to finalizer. + fix ambiguous Send/Receive cref references. - Socket.ReceiveFrom: remove unreachable endpoint snapshot/store block; clarify comments.
1 parent 2af90bd commit 580e42d

2 files changed

Lines changed: 23 additions & 38 deletions

File tree

‎nanoFramework.System.Net/Security/SslStream.cs‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -325,27 +325,27 @@ internal void Authenticate(bool isServer, string targetHost, X509Certificate cer
325325
public bool IsServer { get { return _isServer; } }
326326

327327
/// <summary>
328-
/// Gets the length of the stream. (Overrides NetworkStream. . :: . .Length.)
328+
/// Gets the number of bytes of decrypted data available to be read from the stream.
329329
/// </summary>
330330
public override long Length
331331
{
332332
get
333333
{
334-
if (_disposed == true) throw new ObjectDisposedException();
334+
if (_disposed) throw new ObjectDisposedException();
335335
if (_socket == null) throw new IOException();
336336

337337
return SslNative.DataAvailable(_socket);
338338
}
339339
}
340340

341341
/// <summary>
342-
/// Gets a value the indicates whether data is available in the stream. (Overrides NetworkStream. . :: . .DataAvailable.)
342+
/// Gets a value that indicates whether decrypted data is available on the stream to be read.
343343
/// </summary>
344344
public override bool DataAvailable
345345
{
346346
get
347347
{
348-
if (_disposed == true) throw new ObjectDisposedException();
348+
if (_disposed) throw new ObjectDisposedException();
349349
if (_socket == null) throw new IOException();
350350

351351
return (SslNative.DataAvailable(_socket) > 0);
@@ -393,9 +393,9 @@ protected override void Dispose(bool disposing)
393393
/// </summary>
394394
/// <param name="buffer">An array that receives the bytes read from this stream.</param>
395395
/// <param name="offset">An integer that contains the zero-based location in buffer at which to begin storing the data read from this stream.</param>
396-
/// <param name="size">The maximum number of bytes to read from this stream.</param>
396+
/// <param name="count">The maximum number of bytes to read from this stream.</param>
397397
/// <returns></returns>
398-
public override int Read(byte[] buffer, int offset, int size)
398+
public override int Read(byte[] buffer, int offset, int count)
399399
{
400400
if (buffer == null)
401401
{
@@ -412,24 +412,24 @@ public override int Read(byte[] buffer, int offset, int size)
412412
throw new ArgumentOutOfRangeException();
413413
}
414414

415-
if (size < 0 || size > buffer.Length - offset)
415+
if (count < 0 || count > buffer.Length - offset)
416416
{
417417
throw new ArgumentOutOfRangeException();
418418
}
419419

420-
return SslNative.SecureRead(_socket, buffer, offset, size, _socket.ReceiveTimeout);
420+
return SslNative.SecureRead(_socket, buffer, offset, count, _socket.ReceiveTimeout);
421421
}
422422

423423
/// <summary>
424424
/// Write the specified number of bytes to the underlying stream using the specified buffer and offset.
425425
/// </summary>
426426
/// <param name="buffer">An array that supplies the bytes written to the stream.</param>
427-
/// <param name="offset">he zero-based location in buffer at which to begin reading bytes to be written to the stream.</param>
428-
/// <param name="size">The number of bytes to read from buffer.</param>
427+
/// <param name="offset">The zero-based location in buffer at which to begin reading bytes to be written to the stream.</param>
428+
/// <param name="count">The number of bytes to read from buffer.</param>
429429
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
430430
/// <exception cref="ArgumentOutOfRangeException">
431431
/// <para>
432-
/// <paramref name="offset"/> or <paramref name="size"/> is less than zero
432+
/// <paramref name="offset"/> or <paramref name="count"/> is less than zero
433433
/// </para>
434434
/// <para>
435435
/// -or-
@@ -441,15 +441,15 @@ public override int Read(byte[] buffer, int offset, int size)
441441
/// -or-
442442
/// </para>
443443
/// <para>
444-
/// <paramref name="offset"/> + <paramref name="size"/> is greater than the length of <paramref name="buffer"/>.
444+
/// <paramref name="offset"/> + <paramref name="count"/> is greater than the length of <paramref name="buffer"/>.
445445
/// </para>
446446
/// </exception>
447447
/// <exception cref="ObjectDisposedException">The stream has been disposed.</exception>
448448
/// <exception cref="IOException">The write operation failed.</exception>
449449
public override void Write(
450450
byte[] buffer,
451451
int offset,
452-
int size)
452+
int count)
453453
{
454454
if (buffer == null)
455455
{
@@ -466,14 +466,14 @@ public override void Write(
466466
throw new ArgumentOutOfRangeException();
467467
}
468468

469-
if (size < 0 || size > buffer.Length - offset)
469+
if (count < 0 || count > buffer.Length - offset)
470470
{
471471
throw new ArgumentOutOfRangeException();
472472
}
473473

474-
int written = SslNative.SecureWrite(_socket, buffer, offset, size, _socket.SendTimeout);
474+
int written = SslNative.SecureWrite(_socket, buffer, offset, count, _socket.SendTimeout);
475475

476-
if (written <= 0 && size > 0)
476+
if (written <= 0 && count > 0)
477477
{
478478
throw new IOException();
479479
}

‎nanoFramework.System.Net/Sockets/Socket.cs‎

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public EndPoint RemoteEndPoint
181181
}
182182

183183
/// <summary>
184-
/// Gets a value that indicates whether a <see cref="Socket"/> is connected to a remote host as of the last <see cref="Send"/> or <see cref="Receive"/> operation.
184+
/// Gets a value that indicates whether a <see cref="Socket"/> is connected to a remote host as of the last <see cref="Send(byte[])"/> or <see cref="Receive(byte[])"/> operation.
185185
/// </summary>
186186
/// <value>
187187
/// <see langword="true"/> if the <see cref="Socket"/> was connected to a remote resource as of the most recent operation; otherwise, <see langword="false"/>.
@@ -766,30 +766,12 @@ public int ReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFl
766766

767767
if (_rightEndPoint == null)
768768
{
769-
// socket must have connection established or previously accepted a connection
769+
// socket must be bound (by Bind, Connect or SendTo)
770770
throw new SocketException(SocketError.NotConnected);
771771
}
772772

773-
EndPoint endPointSnapshot = remoteEP;
774-
Snapshot(ref endPointSnapshot);
775-
776-
int bytesTransferred = 0;
777-
778-
bytesTransferred = NativeSocket.recvfrom(this, buffer, offset, size, (int)socketFlags, m_recvTimeout, ref remoteEP);
779-
780-
if (!remoteEP.Equals(endPointSnapshot))
781-
{
782-
// no need to create a new EndPoint here if it's different from the orignal
783-
// because the interpreter has already created a new instance of an IPEndPoint
784-
785-
if (_rightEndPoint == null)
786-
{
787-
// save a copy of the EndPoint
788-
_rightEndPoint = remoteEP;
789-
}
790-
}
791-
792-
return bytesTransferred;
773+
// recvfrom always replaces remoteEP with a new IPEndPoint holding the sender's address; the caller's instance is never modified
774+
return NativeSocket.recvfrom(this, buffer, offset, size, (int)socketFlags, m_recvTimeout, ref remoteEP);
793775
}
794776

795777
/// <summary>
@@ -1091,6 +1073,9 @@ void IDisposable.Dispose()
10911073
GC.SuppressFinalize(this);
10921074
}
10931075

1076+
/// <summary>
1077+
/// Releases unmanaged resources used by the <see cref="Socket"/> before it is reclaimed by garbage collection.
1078+
/// </summary>
10941079
~Socket()
10951080
{
10961081
Dispose(false);

0 commit comments

Comments
 (0)