From 1b6f1177583ba64c6635c52f0878fc81256eb03d Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Mon, 18 May 2020 22:42:17 +0200 Subject: [PATCH] Fix typos and refactor code. --- Source/CoAPnet.Extensions.DTLS/DtlsClient.cs | 20 ++------ .../DtlsCoapTransportLayer.cs | 2 +- .../DtlsCoapTransportLayerOptionsBuilder.cs | 21 +++++++-- .../PreSharedKeyWrapper.cs | 2 +- .../CoAPnet.Extensions.DTLS/UdpTransport.cs | 4 +- Source/CoAPnet/Client/CoapClient.cs | 16 +++---- .../Client/CoapClientBlockTransferReceiver.cs | 7 ++- .../Client/CoapClientConnectOptions.cs | 2 +- .../Client/CoapClientConnectOptionsBuilder.cs | 2 +- .../Client/CoapClientObservationManager.cs | 19 ++++---- .../Client/CoapMessageToResponseConverter.cs | 13 +++-- Source/CoAPnet/Client/CoapMessageToken.cs | 5 -- Source/CoAPnet/Client/CoapObserveOptions.cs | 9 ++++ Source/CoAPnet/Client/CoapObserveRequest.cs | 7 --- Source/CoAPnet/Client/CoapRequest.cs | 5 +- Source/CoAPnet/Client/CoapRequestBuilder.cs | 15 ++++-- .../Client/CoapRequestToMessageConverter.cs | 7 ++- Source/CoAPnet/Client/CoapResponse.cs | 4 +- .../CoAPnet/Client/CoapResponseStatusCode.cs | 2 +- Source/CoAPnet/CoapFactory.cs | 10 +++- Source/CoAPnet/Internal/MemoryBuffer.cs | 17 +++---- Source/CoAPnet/Internal/ParallelTask.cs | 12 +++-- Source/CoAPnet/Logging/CoapNetLogger.cs | 10 +++- .../LowLevelClient/LowLevelCoapClient.cs | 5 +- .../CoapMessageDispatcher.cs | 10 +++- .../CoapBlockTransferOptionValueEncoder.cs | 5 +- Source/CoAPnet/Protocol/CoapDefaultPort.cs | 2 +- Source/CoAPnet/Protocol/CoapMessageCodes.cs | 2 +- .../Protocol/Encoding/CoapMessageDecoder.cs | 6 +-- .../Protocol/Encoding/CoapMessageEncoder.cs | 47 ++++++++++--------- .../Protocol/Encoding/CoapMessageReader.cs | 10 +--- .../Protocol/Encoding/CoapMessageWriter.cs | 2 +- .../Protocol/Options/CoapMessageOption.cs | 4 +- .../Options/CoapMessageOptionEmptyValue.cs | 4 -- .../Options/CoapMessageOptionOpaqueValue.cs | 7 +-- .../Options/CoapMessageOptionStringValue.cs | 7 +-- .../Transport/TcpCoapTransportLayer.cs | 4 +- .../Transport/UdpCoapTransportLayer.cs | 7 +-- 38 files changed, 175 insertions(+), 158 deletions(-) create mode 100644 Source/CoAPnet/Client/CoapObserveOptions.cs diff --git a/Source/CoAPnet.Extensions.DTLS/DtlsClient.cs b/Source/CoAPnet.Extensions.DTLS/DtlsClient.cs index 1971340..168babf 100644 --- a/Source/CoAPnet.Extensions.DTLS/DtlsClient.cs +++ b/Source/CoAPnet.Extensions.DTLS/DtlsClient.cs @@ -3,7 +3,7 @@ namespace CoAPnet.Extensions.DTLS { - public partial class DtlsClient : DefaultTlsClient + public class DtlsClient : DefaultTlsClient { readonly ProtocolVersion _protocolVersion; readonly PreSharedKey _preSharedKey; @@ -14,25 +14,13 @@ public DtlsClient(ProtocolVersion protocolVersion, PreSharedKey preSharedKey) _preSharedKey = preSharedKey ?? throw new ArgumentNullException(nameof(preSharedKey)); } - public override ProtocolVersion MinimumVersion - { - get - { - return _protocolVersion; - } - } + public override ProtocolVersion MinimumVersion => _protocolVersion; - public override ProtocolVersion ClientVersion - { - get - { - return _protocolVersion; - } - } + public override ProtocolVersion ClientVersion => _protocolVersion; public override int[] GetCipherSuites() { - return new int[] { + return new [] { CipherSuite.TLS_PSK_WITH_AES_128_CCM, CipherSuite.TLS_PSK_WITH_AES_128_CCM_8, CipherSuite.TLS_PSK_WITH_AES_256_CCM, diff --git a/Source/CoAPnet.Extensions.DTLS/DtlsCoapTransportLayer.cs b/Source/CoAPnet.Extensions.DTLS/DtlsCoapTransportLayer.cs index f055627..a976062 100644 --- a/Source/CoAPnet.Extensions.DTLS/DtlsCoapTransportLayer.cs +++ b/Source/CoAPnet.Extensions.DTLS/DtlsCoapTransportLayer.cs @@ -57,7 +57,7 @@ public void Dispose() _udpTransport?.Dispose(); } - ProtocolVersion ConvertProtocolVersion(DtlsVersion dtlsVersion) + static ProtocolVersion ConvertProtocolVersion(DtlsVersion dtlsVersion) { if (dtlsVersion == DtlsVersion.V1_0) { diff --git a/Source/CoAPnet.Extensions.DTLS/DtlsCoapTransportLayerOptionsBuilder.cs b/Source/CoAPnet.Extensions.DTLS/DtlsCoapTransportLayerOptionsBuilder.cs index cbb1bb1..5a4e935 100644 --- a/Source/CoAPnet.Extensions.DTLS/DtlsCoapTransportLayerOptionsBuilder.cs +++ b/Source/CoAPnet.Extensions.DTLS/DtlsCoapTransportLayerOptionsBuilder.cs @@ -9,8 +9,15 @@ public class DtlsCoapTransportLayerOptionsBuilder public DtlsCoapTransportLayerOptionsBuilder WithPreSharedKey(byte[] identity, byte[] key) { - if (identity is null) throw new ArgumentNullException(nameof(identity)); - if (key is null) throw new ArgumentNullException(nameof(key)); + if (identity is null) + { + throw new ArgumentNullException(nameof(identity)); + } + + if (key is null) + { + throw new ArgumentNullException(nameof(key)); + } _options.Credentials = new PreSharedKey { @@ -23,14 +30,20 @@ public DtlsCoapTransportLayerOptionsBuilder WithPreSharedKey(byte[] identity, by public DtlsCoapTransportLayerOptionsBuilder WithPreSharedKey(string identity, byte[] key) { - if (identity is null) throw new ArgumentNullException(nameof(identity)); + if (identity is null) + { + throw new ArgumentNullException(nameof(identity)); + } return WithPreSharedKey(Encoding.UTF8.GetBytes(identity), key); } public DtlsCoapTransportLayerOptionsBuilder WithPreSharedKey(string identity, string key) { - if (identity is null) throw new ArgumentNullException(nameof(identity)); + if (identity is null) + { + throw new ArgumentNullException(nameof(identity)); + } return WithPreSharedKey(Encoding.UTF8.GetBytes(identity), Encoding.UTF8.GetBytes(key)); } diff --git a/Source/CoAPnet.Extensions.DTLS/PreSharedKeyWrapper.cs b/Source/CoAPnet.Extensions.DTLS/PreSharedKeyWrapper.cs index 10006fd..4fddd1b 100644 --- a/Source/CoAPnet.Extensions.DTLS/PreSharedKeyWrapper.cs +++ b/Source/CoAPnet.Extensions.DTLS/PreSharedKeyWrapper.cs @@ -22,7 +22,7 @@ public byte[] GetPskIdentity() return _preSharedKey.Identity; } - public void NotifyIdentityHint(byte[] psk_identity_hint) + public void NotifyIdentityHint(byte[] pskIdentityHint) { } diff --git a/Source/CoAPnet.Extensions.DTLS/UdpTransport.cs b/Source/CoAPnet.Extensions.DTLS/UdpTransport.cs index 375a073..077660b 100644 --- a/Source/CoAPnet.Extensions.DTLS/UdpTransport.cs +++ b/Source/CoAPnet.Extensions.DTLS/UdpTransport.cs @@ -39,9 +39,9 @@ public int Receive(byte[] buf, int off, int len, int waitMillis) try { EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); - var datagramLength = _socket.ReceiveFrom(buf, off, len, SocketFlags.None, ref remoteEndPoint); + var length = _socket.ReceiveFrom(buf, off, len, SocketFlags.None, ref remoteEndPoint); - return datagramLength; + return length; } catch (SocketException) { diff --git a/Source/CoAPnet/Client/CoapClient.cs b/Source/CoAPnet/Client/CoapClient.cs index 83766a0..cd3306a 100644 --- a/Source/CoAPnet/Client/CoapClient.cs +++ b/Source/CoAPnet/Client/CoapClient.cs @@ -21,8 +21,8 @@ public sealed class CoapClient : ICoapClient readonly CoapNetLogger _logger; readonly CoapClientObservationManager _observationManager; + readonly LowLevelCoapClient _lowLevelClient; - LowLevelCoapClient _lowLevelClient; CoapClientConnectOptions _connectOptions; CancellationTokenSource _cancellationToken; @@ -36,12 +36,7 @@ public CoapClient(CoapNetLogger logger) public async Task ConnectAsync(CoapClientConnectOptions options, CancellationToken cancellationToken) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } - - _connectOptions = options; + _connectOptions = options ?? throw new ArgumentNullException(nameof(options)); await _lowLevelClient.ConnectAsync(options, cancellationToken).ConfigureAwait(false); _cancellationToken = new CancellationTokenSource(); @@ -126,7 +121,10 @@ public async Task StopObservationAsync(CoapObserveResponse observeResponse, Canc internal async Task RequestAsync(CoapMessage requestMessage, CancellationToken cancellationToken) { - if (requestMessage is null) throw new ArgumentNullException(nameof(requestMessage)); + if (requestMessage is null) + { + throw new ArgumentNullException(nameof(requestMessage)); + } requestMessage.Id = _messageIdProvider.Next(); @@ -137,7 +135,7 @@ internal async Task RequestAsync(CoapMessage requestMessage, Cancel var responseMessage = await responseAwaiter.WaitOneAsync(_connectOptions.CommunicationTimeout).ConfigureAwait(false); - if (responseMessage.Code == CoapMessageCodes.Empty) + if (responseMessage.Code.Equals(CoapMessageCodes.Empty)) { // TODO: Support message which are sent later (no piggybacking). } diff --git a/Source/CoAPnet/Client/CoapClientBlockTransferReceiver.cs b/Source/CoAPnet/Client/CoapClientBlockTransferReceiver.cs index 51de7a3..37ee605 100644 --- a/Source/CoAPnet/Client/CoapClientBlockTransferReceiver.cs +++ b/Source/CoAPnet/Client/CoapClientBlockTransferReceiver.cs @@ -31,7 +31,10 @@ public CoapClientBlockTransferReceiver(CoapMessage requestMessage, CoapMessage f public static bool IsBlockTransfer(CoapMessage responseMessage) { - if (responseMessage is null) throw new ArgumentNullException(nameof(responseMessage)); + if (responseMessage is null) + { + throw new ArgumentNullException(nameof(responseMessage)); + } return responseMessage.Options.Any(o => o.Number == CoapMessageOptionNumber.Block2); } @@ -81,7 +84,7 @@ public async Task> ReceiveFullPayload(CancellationToken cance } } - string FormatBlock2OptionValue(CoapBlockTransferOptionValue value) + static string FormatBlock2OptionValue(CoapBlockTransferOptionValue value) { return $"{value.Number}/{(value.HasFollowingBlocks ? 'M' : '_')}/{value.Size}"; } diff --git a/Source/CoAPnet/Client/CoapClientConnectOptions.cs b/Source/CoAPnet/Client/CoapClientConnectOptions.cs index b103f40..8103b12 100644 --- a/Source/CoAPnet/Client/CoapClientConnectOptions.cs +++ b/Source/CoAPnet/Client/CoapClientConnectOptions.cs @@ -11,7 +11,7 @@ public string Host get; set; } - public int Port { get; set; } = CoapDefaultPort.Unencrpyted; + public int Port { get; set; } = CoapDefaultPort.Unencrypted; public TimeSpan CommunicationTimeout { get; set; } = TimeSpan.FromSeconds(10); diff --git a/Source/CoAPnet/Client/CoapClientConnectOptionsBuilder.cs b/Source/CoAPnet/Client/CoapClientConnectOptionsBuilder.cs index 5a92025..07c29d6 100644 --- a/Source/CoAPnet/Client/CoapClientConnectOptionsBuilder.cs +++ b/Source/CoAPnet/Client/CoapClientConnectOptionsBuilder.cs @@ -40,7 +40,7 @@ public CoapClientConnectOptionsBuilder WithEncryptedPort() public CoapClientConnectOptionsBuilder WithUnencryptedPort() { - return WithPort(CoapDefaultPort.Unencrpyted); + return WithPort(CoapDefaultPort.Unencrypted); } public CoapClientConnectOptionsBuilder WithTcpTransportLayer() diff --git a/Source/CoAPnet/Client/CoapClientObservationManager.cs b/Source/CoAPnet/Client/CoapClientObservationManager.cs index a70cbb5..462c330 100644 --- a/Source/CoAPnet/Client/CoapClientObservationManager.cs +++ b/Source/CoAPnet/Client/CoapClientObservationManager.cs @@ -12,10 +12,10 @@ namespace CoAPnet.Client { public sealed class CoapClientObservationManager { - private readonly LowLevelCoapClient _client; - private readonly CoapNetLogger _logger; - private readonly CoapMessageToResponseConverter _messageToResponseConverter; - private readonly ConcurrentDictionary _observedResponseHandlers = new ConcurrentDictionary(); + readonly LowLevelCoapClient _client; + readonly CoapNetLogger _logger; + readonly CoapMessageToResponseConverter _messageToResponseConverter; + readonly ConcurrentDictionary _observedResponseHandlers = new ConcurrentDictionary(); public CoapClientObservationManager(CoapMessageToResponseConverter messageToResponseConverter, LowLevelCoapClient client, CoapNetLogger logger) { @@ -26,7 +26,7 @@ public CoapClientObservationManager(CoapMessageToResponseConverter messageToResp public void Deregister(CoapMessageToken token) { - _observedResponseHandlers.TryRemove(token, out var _); + _observedResponseHandlers.TryRemove(token, out _); } public void Register(CoapMessageToken token, ICoapResponseHandler responseHandler) @@ -36,7 +36,10 @@ public void Register(CoapMessageToken token, ICoapResponseHandler responseHandle public async Task TryHandleReceivedMessage(CoapMessage message) { - if (message is null) throw new ArgumentNullException(nameof(message)); + if (message is null) + { + throw new ArgumentNullException(nameof(message)); + } try { @@ -91,7 +94,7 @@ await responseHandler.HandleResponseAsync(new HandleResponseContext } } - async Task DeregisterObservation(CoapMessage message) + Task DeregisterObservation(CoapMessage message) { var emptyResponse = new CoapMessage { @@ -101,7 +104,7 @@ async Task DeregisterObservation(CoapMessage message) }; _logger.Information(nameof(CoapClient), "Received unobserved message. Sending empty response to deregister."); - await _client.SendAsync(emptyResponse, CancellationToken.None).ConfigureAwait(false); + return _client.SendAsync(emptyResponse, CancellationToken.None); } } } \ No newline at end of file diff --git a/Source/CoAPnet/Client/CoapMessageToResponseConverter.cs b/Source/CoAPnet/Client/CoapMessageToResponseConverter.cs index f369b39..a4366a3 100644 --- a/Source/CoAPnet/Client/CoapMessageToResponseConverter.cs +++ b/Source/CoAPnet/Client/CoapMessageToResponseConverter.cs @@ -9,7 +9,10 @@ public sealed class CoapMessageToResponseConverter { public CoapResponse Convert(CoapMessage message, ArraySegment payload) { - if (message is null) throw new ArgumentNullException(nameof(message)); + if (message is null) + { + throw new ArgumentNullException(nameof(message)); + } return new CoapResponse { @@ -19,7 +22,7 @@ public CoapResponse Convert(CoapMessage message, ArraySegment payload) }; } - CoapResponseOptions GetOptions(CoapMessage message) + static CoapResponseOptions GetOptions(CoapMessage message) { var options = new CoapResponseOptions(); @@ -49,7 +52,7 @@ CoapResponseOptions GetOptions(CoapMessage message) return options; } - CoapResponseStatusCode GetStatusCode(CoapMessage message) + static CoapResponseStatusCode GetStatusCode(CoapMessage message) { if (message.Code.Equals(CoapMessageCodes.Empty)) { @@ -141,9 +144,9 @@ CoapResponseStatusCode GetStatusCode(CoapMessage message) return CoapResponseStatusCode.NotImplemented; } - if (message.Code.Equals(CoapMessageCodes.BadBateway)) + if (message.Code.Equals(CoapMessageCodes.BadGateway)) { - return CoapResponseStatusCode.BadBateway; + return CoapResponseStatusCode.BadGateway; } if (message.Code.Equals(CoapMessageCodes.ServiceUnavailable)) diff --git a/Source/CoAPnet/Client/CoapMessageToken.cs b/Source/CoAPnet/Client/CoapMessageToken.cs index bd7601f..238c875 100644 --- a/Source/CoAPnet/Client/CoapMessageToken.cs +++ b/Source/CoAPnet/Client/CoapMessageToken.cs @@ -24,11 +24,6 @@ public override int GetHashCode() public override bool Equals(object obj) { - if (obj == null) - { - return false; - } - var other = obj as CoapMessageToken; if (other == null) { diff --git a/Source/CoAPnet/Client/CoapObserveOptions.cs b/Source/CoAPnet/Client/CoapObserveOptions.cs new file mode 100644 index 0000000..3515add --- /dev/null +++ b/Source/CoAPnet/Client/CoapObserveOptions.cs @@ -0,0 +1,9 @@ +namespace CoAPnet.Client +{ + public class CoapObserveOptions + { + public CoapObserveRequest Request { get; set; } + + public ICoapResponseHandler ResponseHandler { get; set; } + } +} \ No newline at end of file diff --git a/Source/CoAPnet/Client/CoapObserveRequest.cs b/Source/CoAPnet/Client/CoapObserveRequest.cs index 18035e3..c56a307 100644 --- a/Source/CoAPnet/Client/CoapObserveRequest.cs +++ b/Source/CoAPnet/Client/CoapObserveRequest.cs @@ -1,12 +1,5 @@ namespace CoAPnet.Client { - public class CoapObserveOptions - { - public CoapObserveRequest Request { get; set; } - - public ICoapResponseHandler ResponseHandler { get; set; } - } - public class CoapObserveRequest { public CoapRequestOptions Options { get; set; } = new CoapRequestOptions(); diff --git a/Source/CoAPnet/Client/CoapRequest.cs b/Source/CoAPnet/Client/CoapRequest.cs index 489bd5f..dc85350 100644 --- a/Source/CoAPnet/Client/CoapRequest.cs +++ b/Source/CoAPnet/Client/CoapRequest.cs @@ -8,10 +8,7 @@ public class CoapRequest public CoapRequestOptions Options { get; set; } = new CoapRequestOptions(); - public ArraySegment Payload - { - get; set; - } + public ArraySegment Payload { get; set; } } } diff --git a/Source/CoAPnet/Client/CoapRequestBuilder.cs b/Source/CoAPnet/Client/CoapRequestBuilder.cs index ea93579..b41f602 100644 --- a/Source/CoAPnet/Client/CoapRequestBuilder.cs +++ b/Source/CoAPnet/Client/CoapRequestBuilder.cs @@ -16,7 +16,10 @@ public CoapRequestBuilder WithMethod(CoapRequestMethod value) public CoapRequestBuilder WithPath(params string[] value) { - if (value is null) throw new ArgumentNullException(nameof(value)); + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } _request.Options.UriPath = string.Join("/", value); return this; @@ -30,7 +33,10 @@ public CoapRequestBuilder WithPath(string value) public CoapRequestBuilder WithQuery(string value) { - if (value is null) throw new ArgumentNullException(nameof(value)); + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } if (_request.Options.UriQuery == null) { @@ -67,7 +73,10 @@ public CoapRequestBuilder WithPayload(ArraySegment value) public CoapRequestBuilder WithPayload(string value) { - if (value is null) throw new ArgumentNullException(nameof(value)); + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } return WithPayload(Encoding.UTF8.GetBytes(value)); } diff --git a/Source/CoAPnet/Client/CoapRequestToMessageConverter.cs b/Source/CoAPnet/Client/CoapRequestToMessageConverter.cs index 49f0457..4b987e0 100644 --- a/Source/CoAPnet/Client/CoapRequestToMessageConverter.cs +++ b/Source/CoAPnet/Client/CoapRequestToMessageConverter.cs @@ -11,7 +11,10 @@ public sealed class CoapRequestToMessageConverter public CoapMessage Convert(CoapRequest request) { - if (request is null) throw new ArgumentNullException(nameof(request)); + if (request is null) + { + throw new ArgumentNullException(nameof(request)); + } var message = new CoapMessage { @@ -56,7 +59,7 @@ void ApplyUriPath(CoapRequest request, CoapMessage message) return; } - var paths = request.Options.UriPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); + var paths = request.Options.UriPath.Split(new [] { '/' }, StringSplitOptions.RemoveEmptyEntries); foreach (var path in paths) { diff --git a/Source/CoAPnet/Client/CoapResponse.cs b/Source/CoAPnet/Client/CoapResponse.cs index 38b26c5..0c1b7d1 100644 --- a/Source/CoAPnet/Client/CoapResponse.cs +++ b/Source/CoAPnet/Client/CoapResponse.cs @@ -1,6 +1,4 @@ -using System; - -namespace CoAPnet.Client +namespace CoAPnet.Client { public class CoapResponse { diff --git a/Source/CoAPnet/Client/CoapResponseStatusCode.cs b/Source/CoAPnet/Client/CoapResponseStatusCode.cs index cdf83c1..49874e0 100644 --- a/Source/CoAPnet/Client/CoapResponseStatusCode.cs +++ b/Source/CoAPnet/Client/CoapResponseStatusCode.cs @@ -23,7 +23,7 @@ public enum CoapResponseStatusCode InternalServerError = 500, NotImplemented = 501, - BadBateway = 502, + BadGateway = 502, ServiceUnavailable = 503, GatewayTimeout = 504, ProxyingNotSupported = 505 diff --git a/Source/CoAPnet/CoapFactory.cs b/Source/CoAPnet/CoapFactory.cs index 9ee1985..f3b80cf 100644 --- a/Source/CoAPnet/CoapFactory.cs +++ b/Source/CoAPnet/CoapFactory.cs @@ -16,7 +16,10 @@ public ILowLevelCoapClient CreateLowLevelClient() public ILowLevelCoapClient CreateLowLevelClient(CoapNetLogger logger) { - if (logger is null) new ArgumentNullException(nameof(logger)); + if (logger is null) + { + throw new ArgumentNullException(nameof(logger)); + } return new LowLevelCoapClient(logger); } @@ -28,7 +31,10 @@ public ICoapClient CreateClient() public ICoapClient CreateClient(CoapNetLogger logger) { - if (logger is null) new ArgumentNullException(nameof(logger)); + if (logger is null) + { + throw new ArgumentNullException(nameof(logger)); + } return new CoapClient(logger); } diff --git a/Source/CoAPnet/Internal/MemoryBuffer.cs b/Source/CoAPnet/Internal/MemoryBuffer.cs index 372ff37..1d7e6db 100644 --- a/Source/CoAPnet/Internal/MemoryBuffer.cs +++ b/Source/CoAPnet/Internal/MemoryBuffer.cs @@ -6,12 +6,7 @@ namespace CoAPnet.Internal public sealed class MemoryBuffer : IDisposable { readonly MemoryStream _memoryStream; - - public MemoryBuffer() - : this(128) - { - } - + public MemoryBuffer(int size) { _memoryStream = new MemoryStream(size); @@ -24,14 +19,20 @@ public void Write(byte buffer) public void Write(byte[] buffer) { - if (buffer is null) throw new ArgumentNullException(nameof(buffer)); + if (buffer is null) + { + throw new ArgumentNullException(nameof(buffer)); + } _memoryStream.Write(buffer, 0, buffer.Length); } public void Write(ArraySegment buffer) { - if (buffer.Array is null) throw new ArgumentNullException(nameof(buffer)); + if (buffer.Array is null) + { + throw new ArgumentNullException(nameof(buffer)); + } _memoryStream.Write(buffer.Array, buffer.Offset, buffer.Count); } diff --git a/Source/CoAPnet/Internal/ParallelTask.cs b/Source/CoAPnet/Internal/ParallelTask.cs index cf01989..1c7368f 100644 --- a/Source/CoAPnet/Internal/ParallelTask.cs +++ b/Source/CoAPnet/Internal/ParallelTask.cs @@ -8,16 +8,22 @@ public static class ParallelTask { public static void Run(Action action, CancellationToken cancellationToken) { - if (action is null) throw new ArgumentNullException(nameof(action)); + if (action is null) + { + throw new ArgumentNullException(nameof(action)); + } Task.Run(action, cancellationToken); } public static void Run(Func function, CancellationToken cancellationToken) { - if (function is null) throw new ArgumentNullException(nameof(function)); + if (function is null) + { + throw new ArgumentNullException(nameof(function)); + } - Task.Run(function); + Task.Run(function, cancellationToken); } } } diff --git a/Source/CoAPnet/Logging/CoapNetLogger.cs b/Source/CoAPnet/Logging/CoapNetLogger.cs index 5227eb2..706b8c8 100644 --- a/Source/CoAPnet/Logging/CoapNetLogger.cs +++ b/Source/CoAPnet/Logging/CoapNetLogger.cs @@ -42,7 +42,10 @@ public void Error(string source, Exception exception, string message, params obj public void RegisterSink(ICoapNetLoggerSink sink) { - if (sink is null) throw new ArgumentNullException(nameof(sink)); + if (sink is null) + { + throw new ArgumentNullException(nameof(sink)); + } lock (_sinks) { @@ -54,7 +57,10 @@ public void RegisterSink(ICoapNetLoggerSink sink) public void UnregisterSink(ICoapNetLoggerSink sink) { - if (sink is null) throw new ArgumentNullException(nameof(sink)); + if (sink is null) + { + throw new ArgumentNullException(nameof(sink)); + } lock (_sinks) { diff --git a/Source/CoAPnet/LowLevelClient/LowLevelCoapClient.cs b/Source/CoAPnet/LowLevelClient/LowLevelCoapClient.cs index 3f9fa87..3e8a1c6 100644 --- a/Source/CoAPnet/LowLevelClient/LowLevelCoapClient.cs +++ b/Source/CoAPnet/LowLevelClient/LowLevelCoapClient.cs @@ -46,7 +46,10 @@ public async Task ConnectAsync(CoapClientConnectOptions options, CancellationTok public Task SendAsync(CoapMessage message, CancellationToken cancellationToken) { - if (message is null) throw new ArgumentNullException(nameof(message)); + if (message is null) + { + throw new ArgumentNullException(nameof(message)); + } var requestMessageBuffer = _messageEncoder.Encode(message); return _transportLayerAdapter.SendAsync(requestMessageBuffer, cancellationToken); diff --git a/Source/CoAPnet/MessageDispatcher/CoapMessageDispatcher.cs b/Source/CoAPnet/MessageDispatcher/CoapMessageDispatcher.cs index 52a8b4b..e5efc36 100644 --- a/Source/CoAPnet/MessageDispatcher/CoapMessageDispatcher.cs +++ b/Source/CoAPnet/MessageDispatcher/CoapMessageDispatcher.cs @@ -10,7 +10,10 @@ public sealed class CoapMessageDispatcher public void Dispatch(Exception exception) { - if (exception is null) throw new ArgumentNullException(nameof(exception)); + if (exception is null) + { + throw new ArgumentNullException(nameof(exception)); + } foreach (var awaiter in _awaiters) { @@ -22,7 +25,10 @@ public void Dispatch(Exception exception) public bool TryHandleReceivedMessage(CoapMessage message) { - if (message == null) throw new ArgumentNullException(nameof(message)); + if (message == null) + { + throw new ArgumentNullException(nameof(message)); + } if (_awaiters.TryRemove(message.Id, out var awaiter)) { diff --git a/Source/CoAPnet/Protocol/BlockTransfer/CoapBlockTransferOptionValueEncoder.cs b/Source/CoAPnet/Protocol/BlockTransfer/CoapBlockTransferOptionValueEncoder.cs index 2842c68..afec2eb 100644 --- a/Source/CoAPnet/Protocol/BlockTransfer/CoapBlockTransferOptionValueEncoder.cs +++ b/Source/CoAPnet/Protocol/BlockTransfer/CoapBlockTransferOptionValueEncoder.cs @@ -7,7 +7,10 @@ public class CoapBlockTransferOptionValueEncoder { public uint Encode(CoapBlockTransferOptionValue value) { - if (value is null) throw new ArgumentNullException(nameof(value)); + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } if (value.Size > 1024) { diff --git a/Source/CoAPnet/Protocol/CoapDefaultPort.cs b/Source/CoAPnet/Protocol/CoapDefaultPort.cs index 4cf9ff7..376a1b6 100644 --- a/Source/CoAPnet/Protocol/CoapDefaultPort.cs +++ b/Source/CoAPnet/Protocol/CoapDefaultPort.cs @@ -3,7 +3,7 @@ public static class CoapDefaultPort { // Default IANA ports. - public static int Unencrpyted { get; } = 5683; + public static int Unencrypted { get; } = 5683; public static int Encrypted { get; } = 5684; } diff --git a/Source/CoAPnet/Protocol/CoapMessageCodes.cs b/Source/CoAPnet/Protocol/CoapMessageCodes.cs index 0ea6f2a..5aef014 100644 --- a/Source/CoAPnet/Protocol/CoapMessageCodes.cs +++ b/Source/CoAPnet/Protocol/CoapMessageCodes.cs @@ -28,7 +28,7 @@ public static class CoapMessageCodes public static CoapMessageCode InternalServerError { get; } = new CoapMessageCode(5, 0); public static CoapMessageCode NotImplemented { get; } = new CoapMessageCode(5, 1); - public static CoapMessageCode BadBateway { get; } = new CoapMessageCode(5, 2); + public static CoapMessageCode BadGateway { get; } = new CoapMessageCode(5, 2); public static CoapMessageCode ServiceUnavailable { get; } = new CoapMessageCode(5, 3); public static CoapMessageCode GatewayTimeout { get; } = new CoapMessageCode(5, 4); public static CoapMessageCode ProxyingNotSupported { get; } = new CoapMessageCode(5, 5); diff --git a/Source/CoAPnet/Protocol/Encoding/CoapMessageDecoder.cs b/Source/CoAPnet/Protocol/Encoding/CoapMessageDecoder.cs index 282b61d..9b5f5f5 100644 --- a/Source/CoAPnet/Protocol/Encoding/CoapMessageDecoder.cs +++ b/Source/CoAPnet/Protocol/Encoding/CoapMessageDecoder.cs @@ -8,8 +8,8 @@ namespace CoAPnet.Protocol.Encoding { public sealed class CoapMessageDecoder { - private readonly CoapNetLogger _logger; - private readonly CoapMessageOptionFactory _optionFactory = new CoapMessageOptionFactory(); + readonly CoapNetLogger _logger; + readonly CoapMessageOptionFactory _optionFactory = new CoapMessageOptionFactory(); public CoapMessageDecoder(CoapNetLogger logger) { @@ -219,7 +219,7 @@ List DecodeOptions(CoapMessageReader reader) return options; } - uint DecodeUintOptionValue(byte[] value) + static uint DecodeUintOptionValue(byte[] value) { if (value == null || value.Length == 0) { diff --git a/Source/CoAPnet/Protocol/Encoding/CoapMessageEncoder.cs b/Source/CoAPnet/Protocol/Encoding/CoapMessageEncoder.cs index 75347ae..47f82a7 100644 --- a/Source/CoAPnet/Protocol/Encoding/CoapMessageEncoder.cs +++ b/Source/CoAPnet/Protocol/Encoding/CoapMessageEncoder.cs @@ -8,11 +8,14 @@ namespace CoAPnet.Protocol.Encoding { public sealed class CoapMessageEncoder { - readonly byte[] EmptyArray = new byte[0]; + readonly byte[] _emptyArray = new byte[0]; public ArraySegment Encode(CoapMessage message) { - if (message == null) throw new ArgumentNullException(nameof(message)); + if (message == null) + { + throw new ArgumentNullException(nameof(message)); + } ThrowIfInvalid(message); @@ -63,7 +66,7 @@ void EncodeOptions(IEnumerable options, CoapMessageWriter wri if (option.Value is CoapMessageOptionEmptyValue) { - value = EmptyArray; + value = _emptyArray; } else if (option.Value is CoapMessageOptionUintValue uintValue) { @@ -115,47 +118,45 @@ void EncodeOptions(IEnumerable options, CoapMessageWriter wri } } - byte[] EncodeUintOptioNValue(uint value) + static byte[] EncodeUintOptioNValue(uint value) { if (value <= 255U) { - return new byte[] + return new [] { (byte)value }; } - else if (value <= 65535U) - { - return new byte[] - { - (byte)(value >> 8), - (byte)(value >> 0) - }; - } - else if (value <= 16777215U) + + if (value <= 65535U) { - return new byte[] + return new [] { - (byte)(value >> 16), (byte)(value >> 8), (byte)(value >> 0) }; } - else + + if (value <= 16777215U) { - return new byte[] + return new [] { - (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)(value >> 0) }; } - throw new CoapProtocolViolationException("The value for the uint option is too long."); + return new[] + { + (byte)(value >> 24), + (byte)(value >> 16), + (byte)(value >> 8), + (byte)(value >> 0) + }; } - void EncodeOptionValue(int value, out int nibble) + static void EncodeOptionValue(int value, out int nibble) { if (value <= 12) { @@ -178,7 +179,7 @@ void EncodeOptionValue(int value, out int nibble) throw new CoapProtocolViolationException("Option value is too long."); } - void ThrowIfInvalid(CoapMessage message) + static void ThrowIfInvalid(CoapMessage message) { if (message.Token?.Length > 8) { @@ -188,7 +189,7 @@ void ThrowIfInvalid(CoapMessage message) ThrowIfInvalid(message.Code); } - void ThrowIfInvalid(CoapMessageCode code) + static void ThrowIfInvalid(CoapMessageCode code) { if (code == null) { diff --git a/Source/CoAPnet/Protocol/Encoding/CoapMessageReader.cs b/Source/CoAPnet/Protocol/Encoding/CoapMessageReader.cs index 180ebbb..89fe2aa 100644 --- a/Source/CoAPnet/Protocol/Encoding/CoapMessageReader.cs +++ b/Source/CoAPnet/Protocol/Encoding/CoapMessageReader.cs @@ -10,7 +10,7 @@ public sealed class CoapMessageReader : IDisposable readonly ArraySegment _buffer; int _bitOffset = -1; - byte _byteCache = 0x0; + byte _byteCache; public CoapMessageReader(ArraySegment buffer) { @@ -18,13 +18,7 @@ public CoapMessageReader(ArraySegment buffer) _buffer = buffer; } - public bool EndOfStream - { - get - { - return _memoryStream.Position == _memoryStream.Length; - } - } + public bool EndOfStream => _memoryStream.Position == _memoryStream.Length; public int ReadBits(int count) { diff --git a/Source/CoAPnet/Protocol/Encoding/CoapMessageWriter.cs b/Source/CoAPnet/Protocol/Encoding/CoapMessageWriter.cs index c54f2fe..3b7f8c6 100644 --- a/Source/CoAPnet/Protocol/Encoding/CoapMessageWriter.cs +++ b/Source/CoAPnet/Protocol/Encoding/CoapMessageWriter.cs @@ -9,7 +9,7 @@ public sealed class CoapMessageWriter : IDisposable readonly MemoryBuffer _memoryBuffer = new MemoryBuffer(128); int _bitOffset = 7; - byte _byteCache = 0x0; + byte _byteCache; public void WriteBits(int data, int count) { diff --git a/Source/CoAPnet/Protocol/Options/CoapMessageOption.cs b/Source/CoAPnet/Protocol/Options/CoapMessageOption.cs index f7796b4..1c36872 100644 --- a/Source/CoAPnet/Protocol/Options/CoapMessageOption.cs +++ b/Source/CoAPnet/Protocol/Options/CoapMessageOption.cs @@ -6,10 +6,8 @@ public sealed class CoapMessageOption { public CoapMessageOption(CoapMessageOptionNumber number, CoapMessageOptionValue value) { - if (value is null) throw new ArgumentNullException(nameof(value)); - Number = number; - Value = value; + Value = value ?? throw new ArgumentNullException(nameof(value)); } public CoapMessageOptionNumber Number diff --git a/Source/CoAPnet/Protocol/Options/CoapMessageOptionEmptyValue.cs b/Source/CoAPnet/Protocol/Options/CoapMessageOptionEmptyValue.cs index bc11505..c54c0a4 100644 --- a/Source/CoAPnet/Protocol/Options/CoapMessageOptionEmptyValue.cs +++ b/Source/CoAPnet/Protocol/Options/CoapMessageOptionEmptyValue.cs @@ -2,10 +2,6 @@ { public class CoapMessageOptionEmptyValue : CoapMessageOptionValue { - public CoapMessageOptionEmptyValue() - { - } - public override bool Equals(object obj) { return obj is CoapMessageOptionEmptyValue; diff --git a/Source/CoAPnet/Protocol/Options/CoapMessageOptionOpaqueValue.cs b/Source/CoAPnet/Protocol/Options/CoapMessageOptionOpaqueValue.cs index 5f396a8..6e7e4e3 100644 --- a/Source/CoAPnet/Protocol/Options/CoapMessageOptionOpaqueValue.cs +++ b/Source/CoAPnet/Protocol/Options/CoapMessageOptionOpaqueValue.cs @@ -26,12 +26,7 @@ public override bool Equals(object obj) public override int GetHashCode() { - if (Value == null) - { - return 0; - } - - return Value.GetHashCode(); + return Value == null ? 0 : Value.GetHashCode(); } } } \ No newline at end of file diff --git a/Source/CoAPnet/Protocol/Options/CoapMessageOptionStringValue.cs b/Source/CoAPnet/Protocol/Options/CoapMessageOptionStringValue.cs index 323054a..6e170e6 100644 --- a/Source/CoAPnet/Protocol/Options/CoapMessageOptionStringValue.cs +++ b/Source/CoAPnet/Protocol/Options/CoapMessageOptionStringValue.cs @@ -24,12 +24,7 @@ public override bool Equals(object obj) public override int GetHashCode() { - if (Value == null) - { - return 0; - } - - return Value.GetHashCode(); + return Value == null ? 0 : Value.GetHashCode(); } } } \ No newline at end of file diff --git a/Source/CoAPnet/Transport/TcpCoapTransportLayer.cs b/Source/CoAPnet/Transport/TcpCoapTransportLayer.cs index 6eeecd6..f7d9a7a 100644 --- a/Source/CoAPnet/Transport/TcpCoapTransportLayer.cs +++ b/Source/CoAPnet/Transport/TcpCoapTransportLayer.cs @@ -20,7 +20,7 @@ public async Task ConnectAsync(CoapTransportLayerConnectOptions options, Cancell Dispose(); _tcpClient = new TcpClient(); - using (cancellationToken.Register(() => Dispose())) + using (cancellationToken.Register(Dispose)) { await _tcpClient.ConnectAsync(options.EndPoint.Address, options.EndPoint.Port).ConfigureAwait(false); _networkStream = _tcpClient.GetStream(); @@ -44,7 +44,7 @@ public Task ReceiveAsync(ArraySegment buffer, CancellationToken cance public Task SendAsync(ArraySegment buffer, CancellationToken cancellationToken) { - return _networkStream.WriteAsync(buffer.Array, buffer.Offset, buffer.Count); + return _networkStream.WriteAsync(buffer.Array, buffer.Offset, buffer.Count, cancellationToken); } } } diff --git a/Source/CoAPnet/Transport/UdpCoapTransportLayer.cs b/Source/CoAPnet/Transport/UdpCoapTransportLayer.cs index d7227eb..2ff4c73 100644 --- a/Source/CoAPnet/Transport/UdpCoapTransportLayer.cs +++ b/Source/CoAPnet/Transport/UdpCoapTransportLayer.cs @@ -12,12 +12,7 @@ public sealed class UdpCoapTransportLayer : ICoapTransportLayer public Task ConnectAsync(CoapTransportLayerConnectOptions options, CancellationToken cancellationToken) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } - - _connectOptions = options; + _connectOptions = options ?? throw new ArgumentNullException(nameof(options)); Dispose();