Skip to content
Open
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 VirtualMeetingMonitor/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
9 changes: 6 additions & 3 deletions VirtualMeetingMonitor/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VirtualMeetingMonitor
Expand All @@ -13,7 +14,7 @@ public partial class Form : System.Windows.Forms.Form
private readonly VirtualMeeting meeting = new VirtualMeeting();
readonly Timer timer = new Timer();
private const string LogFileName = "meetings.txt";

private Task networkListener;

public Form()
{
Expand All @@ -27,7 +28,7 @@ public Form()
timer.Tick += OnTimerEvent;

network.OutsideUDPTafficeReceived += Network_OutsideUDPTafficeReceived;
network.StartListening();
networkListener = network.StartListening();

meeting.OnMeetingStarted += Meeting_OnMeetingStarted;
meeting.OnMeetingEnded += Meeting_OnMeetingEnded;
Expand Down Expand Up @@ -141,9 +142,11 @@ private void OnTimerEvent(object sender, EventArgs e)
meeting.CheckMeetingStatus();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
private async void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
network.Stop();
//By awaiting the network listening task, we ensure that the task can be disposed, releasing its resources.
await networkListener;
}

private void openLogToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
100 changes: 62 additions & 38 deletions VirtualMeetingMonitor/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace VirtualMeetingMonitor
{
Expand All @@ -17,22 +18,53 @@ class Network
public event Notify OutsideUDPTafficeReceived;


public void StartListening()
public async Task StartListening()
{
SetupLocalIp();
ConfigureSocket();

IPHostEntry HosyEntry = Dns.GetHostEntry((Dns.GetHostName()));
if (HosyEntry.AddressList.Any())
//Start receiving the packets asynchronously
await ProcessPackets();
}

/// <summary>
/// Use the socket to receive data. Simply loop until the socket is disposed.
/// </summary>
/// <returns></returns>
private async Task ProcessPackets()
{
//Declare the buffer outside the loop so that do not recreate it every time we go around the loop.
ArraySegment<byte> buffer = new ArraySegment<byte>(byteData);

while (true)
{
foreach (IPAddress ip in HosyEntry.AddressList)
try
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIp = ip;
break;
}
// At this point the socket will wait for data to arrive. Awaiting here means
// control is returned to the main UI until there is data to process.
// This is *not* the same as blocking, but we can use the same mental model
// as if it was a blocking call.
int bytesReceived = await mainSocket.ReceiveAsync(buffer, SocketFlags.None);
ParseData(byteData, bytesReceived);
}
catch (ObjectDisposedException)
{
// This exception occurs when the application is stopping and the socket is closed.
// This signals that we should exit the loop.
return;
}
catch (Exception ex)
{
Console.WriteLine("OnReceive Exception: " + ex.Message);
}
}
}

/// <summary>
/// Configures the listening socket.
/// </summary>
private void ConfigureSocket()
{
//For sniffing the socket to capture the packets has to be a raw socket, with the
//address family being of type internetwork, and protocol being IP
mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
Expand All @@ -44,42 +76,37 @@ public void StartListening()
//Set the socket options
mainSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);

byte[] True = new byte[] { 1, 0, 0, 0 };
byte[] Out = new byte[] { 1, 0, 0, 0 }; //Capture outgoing packets
byte[] True = new byte[] {1, 0, 0, 0};
byte[] Out = new byte[] {1, 0, 0, 0}; //Capture outgoing packets

//Socket.IOControl is analogous to the WSAIoctl method of Winsock 2
// The current user must belong to the Administrators group on the local computer
mainSocket.IOControl(IOControlCode.ReceiveAll, True, Out);

//Start receiving the packets asynchronously
mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, OnReceive, null);
}

public void Stop()
/// <summary>
/// Set up a local IP address from the list of available addresses.
/// The first available IPv4 is chosen as the local IP address.
/// </summary>
private void SetupLocalIp()
{
mainSocket.Close();
}

private void OnReceive(IAsyncResult ar)
{
try
{
int nReceived = mainSocket.EndReceive(ar);
ParseData(byteData, nReceived);
}
catch (Exception ex)
IPHostEntry HosyEntry = Dns.GetHostEntry((Dns.GetHostName()));
if (HosyEntry.AddressList.Any())
{
Console.WriteLine("OnReceive Exception: " + ex.Message);
foreach (IPAddress ip in HosyEntry.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIp = ip;
break;
}
}
}
}

try
{
//Another call to BeginReceive so that we continue to receive the incoming packets
mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, OnReceive, null);
}
catch (ObjectDisposedException)
{
}
public void Stop()
{
mainSocket.Close();
}

private void ParseData(byte[] byteData, int nReceived)
Expand All @@ -91,7 +118,6 @@ private void ParseData(byte[] byteData, int nReceived)
}
}


private bool isOutsideUDPTaffice(IPHeader ipHeader)
{
bool retVal = false;
Expand All @@ -107,7 +133,5 @@ private bool isOutsideUDPTaffice(IPHeader ipHeader)
}
return retVal;
}


}
}
2 changes: 1 addition & 1 deletion VirtualMeetingMonitor/VirtualMeetingMonitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>VirtualMeetingMonitor</RootNamespace>
<AssemblyName>VirtualMeetingMonitor</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
Expand Down