Ventuz中的OSC协议(C#)

新转行Ventuz,有什么不对请指正,有些地方也不是很懂。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ventuz.OSC;

namespace OSCTest
{
    class OSCSeverScript
    {
        string m_nIP = "127.0.0.1";
        int m_nPort = 7001;

        #region   单例
        private static OSCSeverScript m_nOscServerScript;
        private OSCSeverScript() { }
        public static OSCSeverScript OnInstance()
        {
            if (m_nOscServerScript == null)
            {
                m_nOscServerScript = new OSCSeverScript();
            }
            return m_nOscServerScript;
        }
        #endregion

        #region Send
        public void OnSend()
        {
            Console.Write("请输入:");
            string m_nInput = Console.ReadLine();

            UdpWriter m_nUdpWrite;
            OscElement m_nOElement = new OscElement("/OSC_Test", m_nInput);//创建一个处理指定的地址(Address)和参数
            m_nUdpWrite = new UdpWriter(m_nIP, m_nPort);//创建一个将OscBundles发送到指定的UDP IP地址和端口的UdpWriter(IP可以是多播)
            OscBundle m_nOBundle = new OscBundle(0, m_nOElement);//创建一个OscBundle,添加一个OscElement
            m_nUdpWrite.Send(m_nOBundle);//发送

        }
        #endregion


        #region Recive
        UdpReader m_nUReader = new UdpReader(7001);//监听指定端口号
        public void OnReceive()
        {
            if (m_nUReader == null)
                return;
            OscMessage m_nOMessage = m_nUReader.Receive();//接收
            if (m_nOMessage == null)
                return;
            OscBundle m_nOBundle;

            if (m_nOMessage is OscBundle)
            {
                m_nOBundle = (OscBundle)m_nOMessage;
            }
            else
            {
                m_nOBundle = new OscBundle();
                m_nOBundle.AddElement(m_nOMessage);//添加一个OSCElement到OSCBundle
            }
            foreach (OscElement el in m_nOBundle.Elements)
            {
                if (el.Address.StartsWith("/OSC_Test"))//确定此字符串实例的开头是否与指定的字符串匹配(是我们的Address地址)
                {
                    for (int i = 0; i < el.Args.Length; i++)//值存在Args里,循环
                    {
                        if (el.Args[i] == null)//判断是否有值
                        {
                            el.Args[i] = "";
                        }
                        else
                        {
                            Console.WriteLine(el.Args[i]);//输出接收到的值
                        }
                    }
                }
            }
        }
        #endregion
        
    }
}


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页