Skip to content

Commit

Permalink
Fix typos and refactor code.
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed May 18, 2020
1 parent fd16f84 commit 1b6f117
Show file tree
Hide file tree
Showing 38 changed files with 175 additions and 158 deletions.
20 changes: 4 additions & 16 deletions Source/CoAPnet.Extensions.DTLS/DtlsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace CoAPnet.Extensions.DTLS
{
public partial class DtlsClient : DefaultTlsClient
public class DtlsClient : DefaultTlsClient
{
readonly ProtocolVersion _protocolVersion;
readonly PreSharedKey _preSharedKey;
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Source/CoAPnet.Extensions.DTLS/DtlsCoapTransportLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void Dispose()
_udpTransport?.Dispose();
}

ProtocolVersion ConvertProtocolVersion(DtlsVersion dtlsVersion)
static ProtocolVersion ConvertProtocolVersion(DtlsVersion dtlsVersion)
{
if (dtlsVersion == DtlsVersion.V1_0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion Source/CoAPnet.Extensions.DTLS/PreSharedKeyWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public byte[] GetPskIdentity()
return _preSharedKey.Identity;
}

public void NotifyIdentityHint(byte[] psk_identity_hint)
public void NotifyIdentityHint(byte[] pskIdentityHint)
{
}

Expand Down
4 changes: 2 additions & 2 deletions Source/CoAPnet.Extensions.DTLS/UdpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
16 changes: 7 additions & 9 deletions Source/CoAPnet/Client/CoapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public sealed class CoapClient : ICoapClient

readonly CoapNetLogger _logger;
readonly CoapClientObservationManager _observationManager;
readonly LowLevelCoapClient _lowLevelClient;

LowLevelCoapClient _lowLevelClient;
CoapClientConnectOptions _connectOptions;
CancellationTokenSource _cancellationToken;

Expand All @@ -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();
Expand Down Expand Up @@ -126,7 +121,10 @@ public async Task StopObservationAsync(CoapObserveResponse observeResponse, Canc

internal async Task<CoapMessage> 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();

Expand All @@ -137,7 +135,7 @@ internal async Task<CoapMessage> 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).
}
Expand Down
7 changes: 5 additions & 2 deletions Source/CoAPnet/Client/CoapClientBlockTransferReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -81,7 +84,7 @@ public async Task<ArraySegment<byte>> ReceiveFullPayload(CancellationToken cance
}
}

string FormatBlock2OptionValue(CoapBlockTransferOptionValue value)
static string FormatBlock2OptionValue(CoapBlockTransferOptionValue value)
{
return $"{value.Number}/{(value.HasFollowingBlocks ? 'M' : '_')}/{value.Size}";
}
Expand Down
2 changes: 1 addition & 1 deletion Source/CoAPnet/Client/CoapClientConnectOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Source/CoAPnet/Client/CoapClientConnectOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public CoapClientConnectOptionsBuilder WithEncryptedPort()

public CoapClientConnectOptionsBuilder WithUnencryptedPort()
{
return WithPort(CoapDefaultPort.Unencrpyted);
return WithPort(CoapDefaultPort.Unencrypted);
}

public CoapClientConnectOptionsBuilder WithTcpTransportLayer()
Expand Down
19 changes: 11 additions & 8 deletions Source/CoAPnet/Client/CoapClientObservationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoapMessageToken, ICoapResponseHandler> _observedResponseHandlers = new ConcurrentDictionary<CoapMessageToken, ICoapResponseHandler>();
readonly LowLevelCoapClient _client;
readonly CoapNetLogger _logger;
readonly CoapMessageToResponseConverter _messageToResponseConverter;
readonly ConcurrentDictionary<CoapMessageToken, ICoapResponseHandler> _observedResponseHandlers = new ConcurrentDictionary<CoapMessageToken, ICoapResponseHandler>();

public CoapClientObservationManager(CoapMessageToResponseConverter messageToResponseConverter, LowLevelCoapClient client, CoapNetLogger logger)
{
Expand All @@ -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)
Expand All @@ -36,7 +36,10 @@ public void Register(CoapMessageToken token, ICoapResponseHandler responseHandle

public async Task<bool> TryHandleReceivedMessage(CoapMessage message)
{
if (message is null) throw new ArgumentNullException(nameof(message));
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}

try
{
Expand Down Expand Up @@ -91,7 +94,7 @@ await responseHandler.HandleResponseAsync(new HandleResponseContext
}
}

async Task DeregisterObservation(CoapMessage message)
Task DeregisterObservation(CoapMessage message)
{
var emptyResponse = new CoapMessage
{
Expand All @@ -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);
}
}
}
13 changes: 8 additions & 5 deletions Source/CoAPnet/Client/CoapMessageToResponseConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public sealed class CoapMessageToResponseConverter
{
public CoapResponse Convert(CoapMessage message, ArraySegment<byte> payload)
{
if (message is null) throw new ArgumentNullException(nameof(message));
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}

return new CoapResponse
{
Expand All @@ -19,7 +22,7 @@ public CoapResponse Convert(CoapMessage message, ArraySegment<byte> payload)
};
}

CoapResponseOptions GetOptions(CoapMessage message)
static CoapResponseOptions GetOptions(CoapMessage message)
{
var options = new CoapResponseOptions();

Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 0 additions & 5 deletions Source/CoAPnet/Client/CoapMessageToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
9 changes: 9 additions & 0 deletions Source/CoAPnet/Client/CoapObserveOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CoAPnet.Client
{
public class CoapObserveOptions
{
public CoapObserveRequest Request { get; set; }

public ICoapResponseHandler ResponseHandler { get; set; }
}
}
7 changes: 0 additions & 7 deletions Source/CoAPnet/Client/CoapObserveRequest.cs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
5 changes: 1 addition & 4 deletions Source/CoAPnet/Client/CoapRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ public class CoapRequest

public CoapRequestOptions Options { get; set; } = new CoapRequestOptions();

public ArraySegment<byte> Payload
{
get; set;
}
public ArraySegment<byte> Payload { get; set; }
}
}

15 changes: 12 additions & 3 deletions Source/CoAPnet/Client/CoapRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -67,7 +73,10 @@ public CoapRequestBuilder WithPayload(ArraySegment<byte> 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));
}
Expand Down
7 changes: 5 additions & 2 deletions Source/CoAPnet/Client/CoapRequestToMessageConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
{
Expand Down
Loading

0 comments on commit 1b6f117

Please sign in to comment.