SafeNet/ConsoleApplication1/UdpMulticast.cs
2021-02-24 13:50:23 +02:00

215 lines
6.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace ConsoleApplication1
{
public class UdpMulticast
{
private Socket s;
private Thread listenThread;
private string mcastGroup;
private int port;
public UdpMulticast(string mcastGroup, int port)
{
this.mcastGroup = mcastGroup;
this.port = port;
}
private void Listen()
{
while (true)
{
try
{
byte[] b = new byte[1024];
Thread.Sleep(1);
int recv = s.Receive(b);
if (OnNewDataRecv != null)
{
byte[] tmp = new byte[recv];
for (int i = 0; i < recv; i++)
{
tmp[i] = b[i];
}
byte[] decByte = Encryption.Decrypt(tmp);
if (this.OnNewDataRecv != null)
this.OnNewDataRecv(decByte, decByte.Length);
}
if (s == null)
{
break;
}
}
catch (ThreadAbortException)
{
break;
}
catch (Exception ex)
{
SM.Debug("UdpMulticast.cs->Listen" + ex.ToString());
}
}
}
private void Listen_Decoded()
{
while (true)
{
try
{
byte[] b = new byte[1024];
Thread.Sleep(1);
int recv = s.Receive(b);
if (OnNewDataRecv != null)
{
byte[] tmp = new byte[recv];
for (int i = 0; i < recv; i++)
{
tmp[i] = b[i];
}
if (this.OnNewDataRecv != null)
this.OnNewDataRecv(tmp, tmp.Length);
}
if (s == null)
{
break;
}
}
catch (ThreadAbortException)
{
break;
}
catch (Exception ex)
{
SM.Debug("UdpMulticast.cs->Listen" + ex.ToString());
}
}
}
public delegate void newData4Send(byte[] data, int dataLen);
public event newData4Send OnNewDataRecv;
public bool StartListen()
{
bool ret = false;
try
{
if (s == null)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
if (s != null)
{
Console.WriteLine("PORT multi cast :" + port);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
s.Bind(ipep);
IPAddress ip = IPAddress.Parse(mcastGroup);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));
s.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, int.Parse("1"));
listenThread = new Thread(new ThreadStart(Listen));
listenThread.IsBackground = true;
}
if (listenThread != null)
listenThread.Start();
ret = true;
}
catch (Exception ex)
{
SM.Debug("Ex:" + ex.ToString());
}
return ret;
}
public bool StartListen_Decoded()
{
bool ret = false;
try
{
if (s == null)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
if (s != null)
{
Console.WriteLine("PORT multi cast :" + port);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
s.Bind(ipep);
IPAddress ip = IPAddress.Parse(mcastGroup);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));
s.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, int.Parse("1"));
listenThread = new Thread(new ThreadStart(Listen_Decoded));
listenThread.IsBackground = true;
}
if (listenThread != null)
listenThread.Start();
ret = true;
}
catch (Exception ex)
{
SM.Debug("Ex:" + ex.ToString());
}
return ret;
}
public void StopListen()
{
if (listenThread != null)
{
if (listenThread.IsAlive)
{
listenThread.Abort();
listenThread = null;
}
}
if (s != null)
{
s.Close();
s = null;
}
}
public void Send(byte[] data, int len)
{
if (s == null)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
if (s != null)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(mcastGroup), port);
byte[] encByte = Encryption.Encrypt(data);
s.SendTo(encByte, encByte.Length, SocketFlags.None, ipep);
}
else SM.Debug("s is NULL");
}
public void Send_decoded(byte[] data, int len)
{
if (s == null)
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
if (s != null)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(mcastGroup), port);
s.SendTo(data, data.Length, SocketFlags.None, ipep);
}
else SM.Debug("s is NULL");
}
}
}