1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| using System;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Data;
using System.Windows.Forms;
namespace test
{
/// <summary>
/// A la responsabilité de contenir le text de l'évenement et de le rendre accessible
/// </summary>
public class TableChangedEventArgs : EventArgs
{
private string myEventText = null;
public TableChangedEventArgs(string theEventText)
{
if (theEventText == null) throw new NullReferenceException();
myEventText = theEventText;
}
public string EventText
{
get { return this.myEventText; }
}
}
public class UDPidentite
{
private static UDPidentite _SingletonInstance = new UDPidentite();
public delegate void ChangedEventArgsHandler(object sender, TableChangedEventArgs e);
public event ChangedEventArgsHandler OnTableUDPChanged;
/// <summary>
/// Instance unique de la classe UDPidentité pour que tout le monde accède au même jeu de donnée
/// </summary>
public static UDPidentite SingletonInstance
{
get { return _SingletonInstance; }
}
public UDPidentite()
{
}
/// <summary>
/// Constructeur avec creation de la DataTable
/// </summary>
public void UDPLancerEcoute(int portEcoute)
{
// init de la table
TableUDP = new DataTable();
TableUDP.Columns.Add("Date dernière vue", typeof(DateTime));
TableUDP.Columns.Add("IP", typeof(string));
TableUDP.Columns.Add("Nom", typeof(string));
TableUDP.Columns.Add("Adresse MAC", typeof(string));
TableUDP.Columns.Add("Status", typeof(string));
TableUDP.Columns.Add("Hash", typeof(string));
Task.Run(async () =>
{
try
{
using (var udpClient = new UdpClient(portEcoute))
{
string loggingEvent = "";
while (true)
{
var receivedResults = await udpClient.ReceiveAsync();
loggingEvent += Encoding.ASCII.GetString(receivedResults.Buffer);
ReceptPeriphUDP(loggingEvent, receivedResults.RemoteEndPoint.Address.ToString());
TableChangedEventArgs e = new TableChangedEventArgs("Fait");
if (e != null) OnTableUDPChanged(this, e);
loggingEvent = "";
}
}
}
catch (Exception ex)
{
MessageBox.Show("Erreur dans suivi des périphériques \r\n" + ex.ToString(),
"Erreur dans suivi des périphériques",
MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
});
}
/// <summary>
/// DataTable contenant les identifications recues par UDP
/// </summary>
public DataTable TableUDP { get; set; }
/// <summary>
/// Ajoute la ligne spécifiée à la table
/// </summary>
private void ReceptPeriphUDP(string Trame, string IP)
{
String[] substrings = Trame.Split('#');
if (substrings[0] == "entete")
{
Boolean Exite = false;
// on regarde chaque ligne de la datatable pour voir si on vient de recevoir une IP qui existe déjà
for (int i = 0 ; i < TableUDP.Rows.Count ; i++)
{
if (TableUDP.Rows[i]["IP"].ToString() == IP) // si l'ip existe déjà dans la datatable
{
TableUDP.Rows[i].BeginEdit();
TableUDP.Rows[i]["IP"] = IP;
TableUDP.Rows[i]["Nom"] = substrings[1];
TableUDP.Rows[i]["Adresse MAC"] = substrings[3];
TableUDP.Rows[i]["Status"] = substrings[4];
TableUDP.Rows[i]["Hash"] = substrings[2];
TableUDP.Rows[i]["Date dernière vue"] = DateTime.Now;
TableUDP.Rows[i].EndEdit();
Exite = true;
}
}
if (Exite == false) // si on a pas trouvé l'élément on le crée
{
DataRow dr = TableUDP.NewRow();
dr["IP"] = IP;
dr["Nom"] = substrings[1];
dr["Adresse MAC"] = substrings[3];
dr["Status"] = substrings[4];
dr["Hash"] = substrings[2];
dr["Date dernière vue"] = DateTime.Now;
TableUDP.Rows.Add(dr);
}
}
}
}
} |
Partager