-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup heartbeat and connection monitoring (#3)
* Add heartbeat Fix disconnet message * Setup connection monitoring for proactive termination
- Loading branch information
1 parent
34da14a
commit 656d93f
Showing
6 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"ServerOptions": { | ||
"ApiKey": "[API_KEY_PLACEHOLDER]" | ||
"ApiKey": "[API_KEY_PLACEHOLDER]", | ||
"InactivityTimeout": "0:0:5", | ||
"HeartbeatFrequency": "0:0:1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using MTSC.ServerSide; | ||
using MTSC; | ||
using System.Net.Sockets; | ||
using MTSC.ServerSide.Handlers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using GuildWarsPartySearch.Server.Options; | ||
|
||
namespace GuildWarsPartySearch.Server.Tcp; | ||
|
||
public class ConnectionMonitorHandler : IHandler | ||
{ | ||
private bool initialized; | ||
private TimeSpan inactivityTimeout; | ||
|
||
void IHandler.ClientRemoved(MTSC.ServerSide.Server server, ClientData client) { } | ||
|
||
bool IHandler.HandleClient(MTSC.ServerSide.Server server, ClientData client) => false; | ||
|
||
bool IHandler.HandleReceivedMessage(MTSC.ServerSide.Server server, ClientData client, Message message) => false; | ||
|
||
bool IHandler.HandleSendMessage(MTSC.ServerSide.Server server, ClientData client, ref Message message) => false; | ||
|
||
bool IHandler.PreHandleReceivedMessage(MTSC.ServerSide.Server server, ClientData client, ref Message message) => false; | ||
|
||
void IHandler.Tick(MTSC.ServerSide.Server server) | ||
{ | ||
if (!this.initialized) | ||
{ | ||
this.initialized = true; | ||
this.inactivityTimeout = server.ServiceManager.GetRequiredService<IOptions<ServerOptions>>().Value.InactivityTimeout ?? TimeSpan.FromSeconds(15); | ||
} | ||
|
||
foreach (ClientData client in server.Clients) | ||
{ | ||
if ((DateTime.Now - client.LastActivityTime) > this.inactivityTimeout) | ||
{ | ||
if (!IsConnected(client.Socket)) | ||
{ | ||
server.Log("Disconnected: " + client.Socket.RemoteEndPoint?.ToString()); | ||
client.ToBeRemoved = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private bool IsConnected(Socket client) | ||
{ | ||
try | ||
{ | ||
if (client is not null && client.Connected) | ||
{ | ||
/* pear to the documentation on Poll: | ||
* When passing SelectMode.SelectRead as a parameter to the Poll method it will return | ||
* -either- true if Socket.Listen(Int32) has been called and a connection is pending; | ||
* -or- true if data is available for reading; | ||
* -or- true if the connection has been closed, reset, or terminated; | ||
* otherwise, returns false | ||
*/ | ||
|
||
// Detect if client disconnected | ||
if (client.Poll(0, SelectMode.SelectRead)) | ||
{ | ||
byte[] buff = new byte[1]; | ||
if (client.Receive(buff, SocketFlags.Peek) == 0) | ||
{ | ||
// Client disconnected | ||
return false; | ||
} | ||
else | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
} |