109 lines
2.6 KiB
C#
109 lines
2.6 KiB
C#
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Timers;
|
|
using System.Windows;
|
|
|
|
namespace LaOlaWelle
|
|
{
|
|
/// <summary>
|
|
/// Interaktionslogik für ServerGUI.xaml
|
|
/// </summary>
|
|
public partial class ServerGUI : Window
|
|
{
|
|
|
|
IPEndPoint remoteep;
|
|
UdpClient udpclient;
|
|
|
|
int client = 0;
|
|
double intervallTime = 0.5;
|
|
int clientsCnt = 0;
|
|
|
|
Timer timer;
|
|
|
|
bool initialized = false;
|
|
|
|
public ServerGUI()
|
|
{
|
|
InitializeComponent();
|
|
|
|
initialized = true;
|
|
udpclient = new UdpClient();
|
|
|
|
IPAddress multicastaddress = IPAddress.Parse("239.0.0.222");
|
|
udpclient.JoinMulticastGroup(multicastaddress);
|
|
remoteep = new IPEndPoint(multicastaddress, 2222);
|
|
}
|
|
|
|
private void clients_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
|
|
{
|
|
if (!initialized) return;
|
|
clientsCnt = (int)clients.Value;
|
|
clientsCNT.Content = clientsCnt;
|
|
}
|
|
|
|
private void intervall_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
|
|
{
|
|
if (!initialized) return;
|
|
intervallCNT.Content = intervall.Value;
|
|
intervallTime = intervall.Value;
|
|
}
|
|
|
|
private void start_CheckChanged(object sender, RoutedEventArgs e)
|
|
{
|
|
if (!initialized) return;
|
|
if ((bool)start.IsChecked)
|
|
{
|
|
timer = new Timer(intervallTime * 1000);
|
|
timer.Elapsed += Timer_Elapsed;
|
|
timer.Enabled = true;
|
|
timer.AutoReset = true;
|
|
} else
|
|
{
|
|
if (timer == null) return;
|
|
timer.Enabled = false;
|
|
}
|
|
}
|
|
|
|
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
|
|
{
|
|
if (!initialized) return;
|
|
var message = new Message();
|
|
message.ID = client;
|
|
message.State = state.close;
|
|
|
|
WriteMessage(message);
|
|
|
|
client++;
|
|
|
|
if (client > clientsCnt) client = 1;
|
|
|
|
message = new Message();
|
|
message.ID = client;
|
|
message.State = state.open;
|
|
WriteMessage(message);
|
|
}
|
|
|
|
private void WriteMessage(Message message)
|
|
{
|
|
using (var stream = new MemoryStream())
|
|
{
|
|
using (var writer = new BinaryWriter(stream))
|
|
{
|
|
writer.Write(message.Valid);
|
|
writer.Write(message.ID);
|
|
writer.Write((int)message.State);
|
|
|
|
writer.Flush();
|
|
|
|
stream.Position = 0;
|
|
|
|
byte[] data = stream.ToArray();
|
|
|
|
udpclient.Send(data, data.Length, remoteep);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|