Skip to content

Commit

Permalink
Merge branch 'main' into migrate-dotnet8
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 authored Dec 4, 2023
2 parents f225f6a + 4591e32 commit ce61555
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 12 deletions.
26 changes: 26 additions & 0 deletions src/libp2p/Libp2p.Core/LogMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,38 @@ namespace Nethermind.Libp2p.Core;

internal static partial class LogMessages
{
private const int EventId = 101_000;

[LoggerMessage(
EventId = EventId + 1,
EventName = nameof(ReadChunk),
Message = "Read chunk {bytes} bytes",
Level = LogLevel.Trace)]
internal static partial void ReadChunk(
this ILogger logger,
long bytes);

[LoggerMessage(
EventId = EventId + 2,
EventName = nameof(ReadEnough),
Message = "Read enough {bytes} bytes",
Level = LogLevel.Trace)]
internal static partial void ReadEnough(
this ILogger logger,
long bytes);

[LoggerMessage(
EventId = EventId + 3,
EventName = nameof(WriteBytes),
Message = "Write {bytes} bytes",
Level = LogLevel.Trace)]
internal static partial void WriteBytes(
this ILogger logger,
long bytes);

[LoggerMessage(
EventId = EventId + 4,
EventName = nameof(DialStarted),
Message = "Dial {channel} on protocol {protocol} with sub-protocols {subProtocols}",
Level = LogLevel.Debug)]
internal static partial void DialStarted(
Expand All @@ -38,6 +48,8 @@ internal static partial void DialStarted(
IEnumerable<string> subProtocols);

[LoggerMessage(
EventId = EventId + 5,
EventName = nameof(ListenStarted),
Message = "Listen {channel} on protocol {protocol} with sub-protocols {subProtocols}",
Level = LogLevel.Debug)]
internal static partial void ListenStarted(
Expand All @@ -47,6 +59,8 @@ internal static partial void ListenStarted(
IEnumerable<string> subProtocols);

[LoggerMessage(
EventId = EventId + 6,
EventName = nameof(DialAndBindStarted),
Message = "Dial and bind {channel} on protocol {protocol} with sub-protocols {subProtocols}",
Level = LogLevel.Debug)]
internal static partial void DialAndBindStarted(
Expand All @@ -56,6 +70,8 @@ internal static partial void DialAndBindStarted(
IEnumerable<string> subProtocols);

[LoggerMessage(
EventId = EventId + 7,
EventName = nameof(ListenAndBindStarted),
Message = "Listen and bind {channel} on protocol {protocol} with sub-protocols {subProtocols}",
Level = LogLevel.Debug)]
internal static partial void ListenAndBindStarted(
Expand All @@ -65,13 +81,17 @@ internal static partial void ListenAndBindStarted(
IEnumerable<string> subProtocols);

[LoggerMessage(
EventId = EventId + 8,
EventName = nameof(ChannelCreated),
Message = "Create channel {chan}",
Level = LogLevel.Debug)]
internal static partial void ChannelCreated(
this ILogger logger,
string chan);

[LoggerMessage(
EventId = EventId + 9,
EventName = nameof(DialFailed),
Message = "Dial error {protocol} via {channel}: {errorMessage}",
Level = LogLevel.Error,
SkipEnabledCheck = true)]
Expand All @@ -83,6 +103,8 @@ internal static partial void DialFailed(
string errorMessage);

[LoggerMessage(
EventId = EventId + 10,
EventName = nameof(ListenFailed),
Message = "Listen error {protocol} via {channel}: {errorMessage}",
Level = LogLevel.Error,
SkipEnabledCheck = true)]
Expand All @@ -94,6 +116,8 @@ internal static partial void ListenFailed(
string errorMessage);

[LoggerMessage(
EventId = EventId + 11,
EventName = nameof(DialAndBindFailed),
Message = "Dial and bind error {protocol} via {channel}: {errorMessage}",
Level = LogLevel.Error,
SkipEnabledCheck = true)]
Expand All @@ -105,6 +129,8 @@ internal static partial void DialAndBindFailed(
string errorMessage);

[LoggerMessage(
EventId = EventId + 12,
EventName = nameof(ListenAndBindFailed),
Message = "Listen and bind error {protocol} via {channel}: {errorMessage}",
Level = LogLevel.Error,
SkipEnabledCheck = true)]
Expand Down
41 changes: 41 additions & 0 deletions src/libp2p/Libp2p.Protocols.Quic/LogMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT

using System.Net;
using Microsoft.Extensions.Logging;
using Nethermind.Libp2p.Core;

namespace Nethermind.Libp2p.Protocols.Quic;

internal static partial class LogMessages
{
private const int EventId = 200_001;

[LoggerMessage(
EventId = EventId + 1,
EventName = nameof(ReadyToHandleConnections),
Message = "Ready to handle connections",
Level = LogLevel.Debug)]
internal static partial void ReadyToHandleConnections(
this ILogger logger);

[LoggerMessage(
EventId = EventId + 2,
EventName = nameof(Connected),
Message = "Connected {localEndPoint} --> {remoteEndPoint}",
Level = LogLevel.Debug)]
internal static partial void Connected(
this ILogger logger,
IPEndPoint localEndPoint,
IPEndPoint remoteEndPoint);

[LoggerMessage(
EventId = EventId + 3,
EventName = nameof(SocketException),
Message = "Disconnected due to a socket exception: {errorMessage}",
Level = LogLevel.Information)]
internal static partial void SocketException(
this ILogger logger,
Exception exception,
string errorMessage);
}
26 changes: 14 additions & 12 deletions src/libp2p/Libp2p.Protocols.Quic/QuicProtocol.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT

using Nethermind.Libp2p.Core;
using Microsoft.Extensions.Logging;
using System.Buffers;
using System.Net.Sockets;
using MultiaddrEnum = Nethermind.Libp2p.Core.Enums.Multiaddr;
using System.Net;
using System.Net.Quic;
using System.Net.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Logging;
using Nethermind.Libp2p.Core;
using Nethermind.Libp2p.Protocols.Quic;
using System.Security.Cryptography;
using MultiaddrEnum = Nethermind.Libp2p.Core.Enums.Multiaddr;

namespace Nethermind.Libp2p.Protocols;

#pragma warning disable CA1416 // Do not inform about platform compatibility
#pragma warning disable CA2252 // EnablePreviewFeatures is set in the project, but build still fails
/// <summary>
/// https://github.com/libp2p/specs/blob/master/quic/README.md
/// </summary>
public class QuicProtocol : IProtocol
{
private readonly ILogger? _logger;
private readonly ILogger<QuicProtocol>? _logger;
private readonly ECDsa _sessionKey;

public QuicProtocol(ILoggerFactory? loggerFactory = null)
Expand Down Expand Up @@ -90,7 +93,7 @@ public async Task ListenAsync(IChannel channel, IChannelFactory? channelFactory,
await listener.DisposeAsync();
});

_logger?.LogDebug("Ready to handle connections");
_logger?.ReadyToHandleConnections();
context.ListenerReady();

while (!channel.IsClosed)
Expand Down Expand Up @@ -119,7 +122,6 @@ public async Task DialAsync(IChannel channel, IChannelFactory? channelFactory, I

IPEndPoint localEndpoint = new(ipAddress, udpPort);


addr = context.RemotePeer.Address;
ipProtocol = addr.Has(MultiaddrEnum.Ip4) ? MultiaddrEnum.Ip4 : MultiaddrEnum.Ip6;
ipAddress = IPAddress.Parse(addr.At(ipProtocol)!);
Expand Down Expand Up @@ -152,7 +154,7 @@ public async Task DialAsync(IChannel channel, IChannelFactory? channelFactory, I
await connection.DisposeAsync();
});

_logger?.LogDebug($"Connected {connection.LocalEndPoint} --> {connection.RemoteEndPoint}");
_logger?.Connected(connection.LocalEndPoint, connection.RemoteEndPoint);

await ProcessStreams(connection, context, channelFactory, channel.Token);
}
Expand Down Expand Up @@ -229,9 +231,9 @@ private void ExchangeData(QuicStream stream, IChannel upChannel, TaskCompletionS
await stream.WriteAsync(data.ToArray(), upChannel.Token);
}
}
catch (SocketException)
catch (SocketException ex)
{
_logger?.LogInformation("Disconnected due to a socket exception");
_logger?.SocketException(ex, ex.Message);
await upChannel.CloseAsync(false);
}
}, upChannel.Token);
Expand All @@ -250,9 +252,9 @@ private void ExchangeData(QuicStream stream, IChannel upChannel, TaskCompletionS
}
}
}
catch (SocketException)
catch (SocketException ex)
{
_logger?.LogInformation("Disconnected due to a socket exception");
_logger?.SocketException(ex, ex.Message);
await upChannel.CloseAsync(false);
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/libp2p/Libp2p/LogMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ namespace Nethermind.Libp2p.Stack;

internal static partial class LogMessages
{
private const int EventId = 100_000;

[LoggerMessage(
EventId = EventId + 1,
EventName = nameof(LogPickedProtocol),
Message = "{protocol} has been picked to {action}",
Level = LogLevel.Debug)]
internal static partial void LogPickedProtocol(
Expand Down

0 comments on commit ce61555

Please sign in to comment.