Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic ping handling and invocation #76

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libp2p/Libp2p.Protocols.Identify/IdentifyProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class IdentifyProtocol : IProtocol
private readonly ILogger? _logger;
private readonly IPeerFactoryBuilder _peerFactoryBuilder;

public IdentifyProtocol(IPeerFactoryBuilder peerFactoryBuilder, IdentifyProtocolSettings? settings, ILoggerFactory? loggerFactory = null)
public IdentifyProtocol(IPeerFactoryBuilder peerFactoryBuilder, IdentifyProtocolSettings? settings = null, ILoggerFactory? loggerFactory = null)
{
_logger = loggerFactory?.CreateLogger<IdentifyProtocol>();
_peerFactoryBuilder = peerFactoryBuilder;
Expand Down
13 changes: 13 additions & 0 deletions src/libp2p/Libp2p.Protocols.Yamux/YamuxProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ await WriteHeaderAsync(channel,

context.Connected(context.RemotePeer);

int pingCounter = 0;

using Timer timer = new((s) =>
{
_ = WriteHeaderAsync(channel, new YamuxHeader { Type = YamuxHeaderType.Ping, Flags = YamuxHeaderFlags.Syn, Length = ++pingCounter });
}, null, 30_000, 30_000);

_ = Task.Run(async () =>
{
foreach (IChannelRequest request in context.SubDialRequests.GetConsumingEnumerable())
Expand Down Expand Up @@ -127,6 +134,12 @@ await WriteHeaderAsync(channel,
await channels[header.StreamID].Channel!.WriteAsync(data);
}

if (header.Type == YamuxHeaderType.Ping && (header.Flags & YamuxHeaderFlags.Syn) == YamuxHeaderFlags.Syn)
{
_logger?.LogDebug("Pong");
await WriteHeaderAsync(channel, new YamuxHeader { Type = YamuxHeaderType.Ping, Flags = YamuxHeaderFlags.Ack, Length = header.Length });
}

if ((header.Flags & YamuxHeaderFlags.Fin) == YamuxHeaderFlags.Fin)
{
_ = channels[header.StreamID].Channel?.CloseAsync();
Expand Down
Loading