Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
devtekve committed Nov 19, 2022
2 parents 9bbfbd2 + 392b94f commit 6591dec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Services/Notification/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Works by receiving the same request than email method from the gatewayserver, bu
### Steps
1-Excute this [Update_ItemLock.sql Script](/Database/CommunityProvided/F3rreNotificationService/Scripts/Update_ItemLock.sql)

2-Excute this [Update_ItemLock.sql Script](/Database/CommunityProvided/F3rreNotificationService/Scripts/Update_SecPassWord.sql)
2-Excute this [Update_SecPassWord.sql Script](/Database/CommunityProvided/F3rreNotificationService/Scripts/Update_SecPassWord.sql)

3-Excute this [Update_TbUser.sql Script](/Database/CommunityProvided/F3rreNotificationService/Scripts/UpdateTbUser.sql)

Expand Down
60 changes: 32 additions & 28 deletions Services/Ping/NationPingService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Net;
using System.Net.Sockets;
using ISRORBilling.Models.Ping;

using Microsoft.Extensions.Options;

using System.Buffers;
using System.Net;
using System.Net.Sockets;

namespace ISRORBilling.Services.Ping
{
public class NationPingService : BackgroundService
Expand All @@ -29,45 +32,46 @@ protected override Task ExecuteAsync(CancellationToken cancellationToken)
_logger.LogInformation(
"Ping Service listening on [{ServiceOptionsListenAddress}:{ServiceOptionsListenPort}]",
_options.ListenAddress, _options.ListenPort);
return RequestHandler(cancellationToken);

return ProcessAccept(cancellationToken);
}

private async Task RequestHandler(CancellationToken cancellationToken)
private async Task ProcessAccept(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
var client = await _tcpListener.AcceptTcpClientAsync(cancellationToken).ConfigureAwait(false);
var innerCancellationToken = new CancellationTokenSource();
var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, innerCancellationToken.Token);
try
{
await DoRequest(client, linkedCancellationToken.Token);
}
catch
{
innerCancellationToken.Cancel();
}
var socket = await _tcpListener.AcceptSocketAsync(cancellationToken).ConfigureAwait(false);

if (_logger.IsEnabled(LogLevel.Debug))
_logger.LogDebug("[{clientEndPoint}]: connected.", socket.RemoteEndPoint);

_ = ProcessSocket(socket, cancellationToken);
}
}

private async Task DoRequest(TcpClient client, CancellationToken cancellationToken)
private static async Task ProcessSocket(Socket socket, CancellationToken cancellationToken)
{
_logger.LogDebug("[{@ClientRemoteEndPoint}]: connected", client.Client.RemoteEndPoint);
var stream = client.GetStream();
while (!cancellationToken.IsCancellationRequested && client.Connected)
await using var stream = new NetworkStream(socket, true);

var buffer = ArrayPool<byte>.Shared.Rent(14);
var memory = new Memory<byte>(buffer)[..14];
try
{
var received = new byte[14];
await stream.ReadExactlyAsync(received, cancellationToken);
await stream.ReadExactlyAsync(memory, cancellationToken).ConfigureAwait(false);
if (buffer[2] != (byte)'R' || buffer[3] != (byte)'E' || buffer[4] != (byte)'Q' || buffer[5] != (byte)'\0')
return;

received[2] = (byte)'A';
received[3] = (byte)'C';
received[4] = (byte)'K';
buffer[2] = (byte)'A';
buffer[3] = (byte)'C';
buffer[4] = (byte)'K';
buffer[5] = (byte)'\0';

await stream.WriteAsync(received, cancellationToken);
client.Close();
await stream.WriteAsync(memory, cancellationToken).ConfigureAwait(false);
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}

stream.Close(); //If the GW wants to keep the session alive this should be after the while loop
}
}
}

0 comments on commit 6591dec

Please sign in to comment.