본문 바로가기

프로그램/유니티 네트워크

[유니티 네트워크] Multicast Sender, Multicast Receiver 구조체 전송 (UdpClient)

728x90
반응형

1. 내용

 (1) 권장 아이피 : 224.0.1.0 ~ 239.255.255.255

     PC의 Ip는 바꾸지 않아도 됩니다.

 

 (2) 권장 포트 번호 : 49152 ~ 65535

 

포트 번호

내용

0

사용하지 않음.

1 ~1023

잘 알려진 포트(Well-known port)

1024 ~ 49151

등록된 포트(Registered port)

49152 ~ 65535

동적 포트(Dynamic port)

 

 (3) 전송 패키지 사이즈

 Udp는 이론상으로는 65507 byte까지 보낼수 있지만, 내부 통신이 아닌 경우 1024 byte이내를 권장합니다.

 

 (4) 소스 내용

  - 유니티와 유니티, 또는 C#과의 통신, 또는 C++와의 통신

  - 클래스 : UdpClient

  - 개체 직렬화 : Marshal

  - 전송 패킷 : 구조체 ↔ 바이트 배열

 

2. 소스

2.1 Receiver.cs

using System;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

public class Receiver : MonoBehaviour
{
    private UdpClient m_Receiver;
    public string m_Ip = "224.0.1.0";
    public int m_Port = 50001;
    public Packet m_ReceivePacket = new Packet();
    private IPEndPoint m_IpEndPoint;

    void Start()
    {
        InitReceiver();
    }

    void Update()
    {
        Receive();
    }

    void OnApplicationQuit()
    {
        CloseReceiver();
    }

    void InitReceiver()
    {
        m_Receiver = new UdpClient();
        m_Receiver.ExclusiveAddressUse = false;
        IPEndPoint localIpEndPoint = new IPEndPoint(IPAddress.Any, m_Port);
        m_Receiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        m_Receiver.ExclusiveAddressUse = false;
        m_Receiver.Client.Bind(localIpEndPoint);

        IPAddress multicastIPAddress = IPAddress.Parse(m_Ip);
        m_Receiver.JoinMulticastGroup(multicastIPAddress);

        m_IpEndPoint = new IPEndPoint(IPAddress.Any, m_Port);
    }

    void Receive()
    {
        if (m_Receiver.Available != 0)
        {
            byte[] packet = new byte[1024];

            try
            {
                packet = m_Receiver.Receive(ref m_IpEndPoint);
            }

            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
                return;
            }

            m_ReceivePacket = ByteArrayToStruct<Packet>(packet);
            DoReceivePacket(); // 받은 값 처리
        }
    }

    void DoReceivePacket()
    {
        Debug.LogFormat($"m_IntArray[0] = {m_ReceivePacket.m_IntArray[0]} " +
            $"m_IntArray[1] = {m_ReceivePacket.m_IntArray[1] } " +
            $"FloatlVariable = {m_ReceivePacket.m_FloatlVariable} " +
            $"StringlVariable = {m_ReceivePacket.m_StringlVariable}" +
            $"BoolVariable = {m_ReceivePacket.m_BoolVariable} " +
            $"IntlVariable = {m_ReceivePacket.m_IntVariable} ");
        //출력: m_IntArray[0] = 7 m_IntArray[1] = 47 FloatlVariable = 2020 StringlVariable = Coder ZeroBoolVariable = True IntlVariable = 13 
    }

    void CloseReceiver()
    {
        if (m_Receiver != null)
        {
            m_Receiver.Close();
            m_Receiver = null;
        }
    }

    T ByteArrayToStruct<T>(byte[] buffer) where T : struct
    {
        int size = Marshal.SizeOf(typeof(T));
        if (size > buffer.Length)
        {
            throw new Exception();
        }

        IntPtr ptr = Marshal.AllocHGlobal(size);
        Marshal.Copy(buffer, 0, ptr, size);
        T obj = (T)Marshal.PtrToStructure(ptr, typeof(T));
        Marshal.FreeHGlobal(ptr);
        return obj;
    }
}

 

2.2 Sender.cs

using System;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

public class Sender : MonoBehaviour
{
    private UdpClient m_Sender;
    public string m_Ip = "224.0.1.0";
    public int m_Port = 50001;
    public Packet m_SendPacket = new Packet();
    private IPEndPoint m_IpEndPoint;

    void Start()
    {
        InitSender();
    }

    void Update()
    {
        Send();
    }

    void OnApplicationQuit()
    {
        CloseSender();
    }

    void InitSender()
    {
        m_Sender = new UdpClient();
        IPAddress ipAddress = IPAddress.Parse(m_Ip);
        m_Sender.JoinMulticastGroup(ipAddress);
        m_IpEndPoint = new IPEndPoint(ipAddress, m_Port);

        // SendPacket에 배열이 있으면 선언 해 주어야 함.
        m_SendPacket.m_IntArray = new int[2];
    }

    void Send()
    {
        try
        {
            SetSendPacket();

            byte[] sendPacket = StructToByteArray(m_SendPacket);
            m_Sender.Send(sendPacket, sendPacket.Length, m_IpEndPoint);
        }

        catch (Exception ex)
        {
            Debug.Log(ex.ToString());
            return;
        }
    }

    void SetSendPacket()
    {
        m_SendPacket.m_BoolVariable = true;
        m_SendPacket.m_IntVariable = 13;
        m_SendPacket.m_IntArray[0] = 7;
        m_SendPacket.m_IntArray[1] = 47;
        m_SendPacket.m_FloatlVariable = 2020;
        m_SendPacket.m_StringlVariable = "Coder Zero";
    }

    void CloseSender()
    {
        if (m_Sender != null)
        {
            m_Sender.Close();
            m_Sender = null;
        }
    }

    byte[] StructToByteArray(object obj)
    {
        int size = Marshal.SizeOf(obj);
        byte[] arr = new byte[size];
        IntPtr ptr = Marshal.AllocHGlobal(size);

        Marshal.StructureToPtr(obj, ptr, true);
        Marshal.Copy(ptr, arr, 0, size);
        Marshal.FreeHGlobal(ptr);
        return arr;
    }
}

 

2.3 Packets.cs

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
[Serializable]
public struct Packet
{
    [MarshalAs(UnmanagedType.Bool)]
    public bool m_BoolVariable;
    public int m_IntVariable;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
    public int[] m_IntArray;
    public float m_FloatlVariable;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string m_StringlVariable;
}
728x90
반응형