Skip to content

Commit

Permalink
Issue: #27
Browse files Browse the repository at this point in the history
Pings and Pongs now get resolved on the ReaderThread.

Should we add a test for this somehow????
  • Loading branch information
xforever1313 committed Nov 1, 2020
1 parent 2f27506 commit 71975cd
Showing 4 changed files with 34 additions and 8 deletions.
6 changes: 0 additions & 6 deletions Chaskis/Chaskis/DefaultHandlers.cs
Original file line number Diff line number Diff line change
@@ -98,12 +98,6 @@ public void Init( PluginInitor pluginInit )
this.AddAdminHandler();
this.AddDebugHandlers();
this.AddCtcpPingHandler();

// Must always check for pings.
this.handlers.Add( new PingHandler() );

// Must always handle pongs.
this.handlers.Add( new PongHandler() );
}

public void Init_Stage2( IDictionary<string, PluginConfig> plugins )
2 changes: 1 addition & 1 deletion Chaskis/ChaskisCore/Handlers/Ping/PingHandler.cs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ namespace Chaskis.Core
/// We need to send a PONG back, otherwise
/// the server will terminate our connection.
/// </summary>
public sealed class PingHandler : IIrcHandler
internal sealed class PingHandler : IIrcHandler
{
// -------- Fields --------

2 changes: 1 addition & 1 deletion Chaskis/ChaskisCore/Handlers/Pong/PongHandler.cs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ namespace Chaskis.Core
/// <summary>
/// Used to handle receiving pongs from a connection.
/// </summary>
public sealed class PongHandler : IIrcHandler
internal sealed class PongHandler : IIrcHandler
{
// -------- Fields --------

32 changes: 32 additions & 0 deletions Chaskis/ChaskisCore/IrcConnection.cs
Original file line number Diff line number Diff line change
@@ -108,6 +108,9 @@ public class IrcConnection : IDisposable, IConnection, IChaskisEventScheduler, I

private readonly IrcWatchdog watchDog;

private readonly PingHandler pingHandler;
private readonly PongHandler pongHandler;

// -------- Constructor --------

public IrcConnection( IIrcConfig config, INonDisposableStringParsingQueue parsingQueue ) :
@@ -148,6 +151,9 @@ public IrcConnection( IIrcConfig config, INonDisposableStringParsingQueue parsin
this.Watchdog_OnFailure,
60 * 1000
);

this.pingHandler = new PingHandler();
this.pongHandler = new PongHandler();
}

// ---------------- Properties ----------------
@@ -770,6 +776,31 @@ private void OnReadLine( string s )
this.ReadEvent?.Invoke( s );
}

/// <summary>
/// Issue #27
/// Handle pings and pongs on the reader thread, NOT the string parsing thread.
/// This is so if the string parsing thread hangs, we do not end up in a
/// connection loop if our watchdog fails.
/// </summary>
/// <remarks>
/// Not contained within <see cref="OnReadLine(string)"/> so there isn't
/// a performance cost when we call <see cref="OnReadLine(string)"/> outside
/// of the <see cref="ReaderThread"/>.
/// </remarks>
private void ResolvePingsAndPongs( string s )
{
HandlerArgs args = new HandlerArgs
{
BlackListedChannels = null,
IrcConfig = this.Config,
IrcWriter = this,
Line = s,
};

this.pingHandler.HandleEvent( args );
this.pongHandler.HandleEvent( args );
}

/// <summary>
/// The thread that does the reading.
/// </summary>
@@ -789,6 +820,7 @@ private void ReaderThread()
// Do nothing.
if( this.KeepReading )
{
this.ResolvePingsAndPongs( s );
this.OnReadLine( s );
}
}

0 comments on commit 71975cd

Please sign in to comment.