Skip to content

Commit

Permalink
Add some other stuff for work
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWandererProductions committed Jan 25, 2025
1 parent 8e3aa7a commit 021ddb5
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 80 deletions.
56 changes: 50 additions & 6 deletions CommonLibraryTests/Communication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*/

using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Communication;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -36,12 +40,52 @@ public async Task CommunicationsAsync()
File.Delete(path);
}

//[TestMethod]
//public async Task Untested()
//{
// //var str = await HttpClientManager.CallSoapServiceAsync();
/// <summary>
/// Listeners the should respond with pong when pinged.
/// </summary>
[TestMethod]
public async Task ListenerShouldRespondWithPongWhenPinged()
{
// Arrange
const int port = 12345; // Example port
var listener = new Listener(port);
var cancellationTokenSource = new CancellationTokenSource();

// Start the listener in a separate task to simulate real-world usage
var listenerTask = Task.Run(() => listener.StartListening(cancellationTokenSource.Token));

// Wait for a brief moment to ensure the server is up and listening
await Task.Delay(1000);

// Act - Connect to the server and send a message
using (var client = new TcpClient("localhost", port))
{
var stream = client.GetStream();
byte[] requestMessage = Encoding.ASCII.GetBytes("PING");
stream.Write(requestMessage, 0, requestMessage.Length);

// //Trace.WriteLine(str);
//}
// Read the response
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);

// Assert - Verify the response is "PONG"
Assert.AreEqual("PONG", response, "Expected response was not received.");
}

// Stop the listener after the test
cancellationTokenSource.Cancel();
await listenerTask; // Ensure listener has finished

listener.StopListening();
}
}

//[TestMethod]
//public async Task Untested()
//{
// //var str = await HttpClientManager.CallSoapServiceAsync();

// //Trace.WriteLine(str);
//}
}
61 changes: 0 additions & 61 deletions Communication/FileObserver.cs

This file was deleted.

7 changes: 7 additions & 0 deletions Communication/INetCom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,12 @@ Task SaveFile(string filePath, IEnumerable<string> urls, IProgress<int> progress
/// <returns>An HttpResponseMessage containing the response details.</returns>
Task<string> SendMessageAsync(string url, string method, string body = null,
string contentType = "application/json");

/// <summary>
/// Listeners the specified port.
/// </summary>
/// <param name="port">The port.</param>
/// <returns>Reference to the listener</returns>
Listener Listener(int port);
}
}
72 changes: 72 additions & 0 deletions Communication/Listener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Communication
{
public class Listener
{
private System.Net.Sockets.TcpListener tcpListener;
private bool isRunning;
private readonly int _port = 12345;

public Listener(int port)
{
_port = port;
tcpListener = new System.Net.Sockets.TcpListener(IPAddress.Any, port);
}

public void StartListening(CancellationToken cancellationToken)
{
isRunning = true;
tcpListener.Start();
Console.WriteLine($"Listening on port {_port}...");

while (isRunning)
{
if (cancellationToken.IsCancellationRequested)
{
StopListening();
return;
}

try
{
if (tcpListener.Pending()) // Non-blocking accept
{
var client = tcpListener.AcceptTcpClient();
ThreadPool.QueueUserWorkItem(HandleClient, client);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error accepting connection: {ex.Message}");
}
}
}

public void StopListening()
{
isRunning = false;
tcpListener.Stop();
Console.WriteLine("Server stopped.");
}

private static void HandleClient(object obj)
{
var client = (TcpClient)obj;
var stream = client.GetStream();

// Respond with a simple message (acting as the "ping response")
string response = "PONG";
byte[] buffer = Encoding.ASCII.GetBytes(response);
stream.Write(buffer, 0, buffer.Length);

// Close the connection
client.Close();
Console.WriteLine("Responded to a ping.");
}
}
}
13 changes: 13 additions & 0 deletions Communication/NetCom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,18 @@ public async Task<string> SendMessageAsync(string url, string method, string bod
throw new Exception("An error occurred while sending the HTTP request.", ex);
}
}

/// <inheritdoc />
/// <summary>
/// Listeners the specified port.
/// </summary>
/// <param name="port">The port.</param>
/// <returns>
/// Reference to the listener
/// </returns>
public Listener Listener(int port)
{
return new(port);
}
}
}
13 changes: 0 additions & 13 deletions Communication/SoapClient.cs

This file was deleted.

0 comments on commit 021ddb5

Please sign in to comment.