Invoking action on receiving data
This commit is contained in:
parent
0b09d473e4
commit
533ddb404f
|
@ -1,6 +1,6 @@
|
||||||
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
|
@ -10,10 +10,10 @@ namespace GameDev.UDP
|
||||||
{
|
{
|
||||||
[SerializeField] private int _port;
|
[SerializeField] private int _port;
|
||||||
[SerializeField] private string _ipAddress;
|
[SerializeField] private string _ipAddress;
|
||||||
[SerializeField] private bool listen;
|
|
||||||
|
|
||||||
[Header("State")]
|
[Header("State")]
|
||||||
[SerializeField] private byte _watchdog;
|
[SerializeField] private byte _watchdog;
|
||||||
|
public byte[] Data { get; private set; }
|
||||||
|
|
||||||
private UdpClient UdpClient { get; set; }
|
private UdpClient UdpClient { get; set; }
|
||||||
|
|
||||||
|
@ -21,6 +21,8 @@ namespace GameDev.UDP
|
||||||
|
|
||||||
private IPAddress _ipAddressParsed;
|
private IPAddress _ipAddressParsed;
|
||||||
|
|
||||||
|
public event Action ReceivedNewUdpData;
|
||||||
|
|
||||||
private void OnEnable()
|
private void OnEnable()
|
||||||
{
|
{
|
||||||
_ipAddressParsed = IPAddress.Parse(_ipAddress);
|
_ipAddressParsed = IPAddress.Parse(_ipAddress);
|
||||||
|
@ -28,7 +30,6 @@ namespace GameDev.UDP
|
||||||
UdpClient = new(_port);
|
UdpClient = new(_port);
|
||||||
UdpClient.JoinMulticastGroup(_ipAddressParsed);
|
UdpClient.JoinMulticastGroup(_ipAddressParsed);
|
||||||
_receiveThread = new(ReceiveData);
|
_receiveThread = new(ReceiveData);
|
||||||
_receiveThread.IsBackground = true;
|
|
||||||
_receiveThread.Start();
|
_receiveThread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,8 +42,9 @@ namespace GameDev.UDP
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var data = UdpClient.Receive(ref ipEndPoint);
|
var data = UdpClient.Receive(ref ipEndPoint);
|
||||||
|
|
||||||
_watchdog = data[0];
|
_watchdog = data[0];
|
||||||
|
Data = data;
|
||||||
|
ReceivedNewUdpData.Invoke();
|
||||||
}
|
}
|
||||||
catch (SocketException e)
|
catch (SocketException e)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue