Untiy UDP局域网 异步发送图片

同步画面有问题,传图片吧

using System.Text;
using System.Net.Sockets;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System.Net;
using System;
using System.Threading.Tasks;
using System.Threading;public class UdpNet : MonoBehaviour
{public static UdpNet Instance { get; private set; }/// <summary>/// 默认发送数据最大长度,UDP一次性最大发送长度为65536,也就是64kb/// </summary>const int DEFAULT_SIZE = 60000;public UdpClient udpClient;/// <summary>/// 是否可以发送数据/// </summary>public bool isSend;/// <summary>/// 要发送的数据缓存队列/// </summary>private Queue<NetData> datasBuffer;/// <summary>/// 当前发送的数据/// </summary>private NetData curSendData;/// <summary>/// 数据接收注册方法/// </summary>public UnityAction<byte[]> ReceiveDataAction;/// <summary>/// UDP是否开启/// </summary>public bool IsConnect { get; private set; }/// <summary>/// 数据接收缓存队列/// </summary>public Queue<NetData> receiveBufferQueue;/// <summary>/// 当前接收的缓存数据/// </summary>private NetData curReceiveData;/// <summary>/// 当前发送的数据长度/// </summary> <summary>private int byteLen;/// <summary>/// 当前接收的数据长度/// </summary> <summary>private int receiveLen;public int Port;/// <summary>/// 开启UDP/// </summary>/// <param name="port"></param>public void Init(int port){if (udpClient != null){//Debug.LogWarning("已开启UDP控制端");return;}Port = port;udpClient = new UdpClient(port);datasBuffer = new Queue<NetData>();receiveBufferQueue = new Queue<NetData>();isSend = true;IsConnect = true;ReceiveFile();}/// <summary>/// 关闭UDP/// </summary>void Close(){if (udpClient != null){udpClient.Close();udpClient.Dispose();udpClient = null;IsConnect = false;isSend = false;datasBuffer.Clear();curSendData = null;curReceiveData = null;receiveBufferQueue.Clear();//Debug.Log("UdpNet 已关闭");}}#region Mono方法void Awake(){Instance = this;// Init(Port);}private void Update(){if (!IsConnect){return;}if (isSend && datasBuffer.Count > 0 && curSendData == null){isSend = false;curSendData = datasBuffer.Dequeue();byteLen = 0;int len = curSendData.byteArray.length;if (curSendData.byteArray.length > DEFAULT_SIZE){len = DEFAULT_SIZE;}byteLen += len;byte[] bytes = curSendData.byteArray.Read(len);udpClient.BeginSend(bytes, len, curSendData.iPEndPoint, SendFileAysncCallBack, curSendData);}if (receiveBufferQueue.Count > 0){ReceiveDataAction?.Invoke(receiveBufferQueue.Dequeue().byteArray.bytes);}}void OnDestroy(){Close();}#endregion#region 发送消息public void SendMsg(string msg, string ip, int port){byte[] data = Encoding.UTF8.GetBytes(msg);SendBytes(data, ip, port);}public void SendMsg(string msg, IPEndPoint iPEndPoint){byte[] data = Encoding.UTF8.GetBytes(msg);SendBytes(data, iPEndPoint);}public void SendBytes(byte[] data, string ip, int port){IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);SendBytes(data, iPEndPoint);}public void SendBytes(byte[] data, IPEndPoint iPEndPoint){byte[] bytes = Encode(data);//Debug.Log(bytes.Length);ByteArray byteArray = new ByteArray(bytes);datasBuffer.Enqueue(new NetData(byteArray, iPEndPoint));}private void SendFileAysncCallBack(IAsyncResult ar){try{int count = udpClient.EndSend(ar);if (ar.IsCompleted){curSendData.byteArray.readIdx += count;}else{Debug.Log("发送未成功,重新发送");}if (curSendData.byteArray.length == 0){isSend = true;//Debug.Log("发送完毕,共发送数据: " + byteLen);curSendData = null;return;}int len = curSendData.byteArray.length;if (curSendData.byteArray.length > DEFAULT_SIZE){len = DEFAULT_SIZE;}byte[] bytes;lock (curSendData){bytes = curSendData.byteArray.Read(len);}byteLen += len;//Debug.Log(len);RunThread(bytes, len);}catch (System.Exception e){Debug.LogError(e.Message);Close();return;}}//延迟1毫秒发送已缓解udp无法接收完全的问题async void RunThread(byte[] bytes, int len){await Task.Run(() =>{Thread.Sleep(1);udpClient.BeginSend(bytes, len, curSendData.iPEndPoint, SendFileAysncCallBack, curSendData);});}#endregion#region 接收消息private void ReceiveFile(){udpClient.BeginReceive(ReceiveFileAsyncBackCall, null);}private void ReceiveFileAsyncBackCall(IAsyncResult ar){IPEndPoint remoteIp = null;byte[] data = udpClient.EndReceive(ar, ref remoteIp);receiveLen += data.Length;if (curReceiveData == null){int len = Decode(data, out byte[] conData);curReceiveData = new NetData(new ByteArray(len), remoteIp);// curReceiveData.byteArray.Write(data, 4, data.Length - 4);curReceiveData.byteArray.Write(conData, 0, conData.Length);//Debug.Log($"当前接收数据长度: {receiveLen},总接收数据长度: {receiveLen}, 当前剩余容量: {curReceiveData.byteArray.remain}");}else{int dataLen = data.Length;if (data.Length > curReceiveData.byteArray.remain){dataLen = curReceiveData.byteArray.remain;}curReceiveData.byteArray.Write(data, 0, dataLen);//Debug.Log($"当前接收数据长度: {data.Length},总接收数据长度: {receiveLen}, 当前剩余容量: {curReceiveData.byteArray.remain}");}if (curReceiveData.byteArray.remain == 0){receiveBufferQueue.Enqueue(curReceiveData);//Debug.Log("接收完毕: " + curReceiveData.byteArray.writeIdx);curReceiveData = null;}ReceiveFile();}#endregionprivate static byte[] Encode(byte[] data){byte[] bytes = new byte[data.Length + 4];byte[] byteLen = BitConverter.GetBytes(data.Length);Array.Copy(byteLen, 0, bytes, 0, 4);Array.Copy(data, 0, bytes, 4, data.Length);//Debug.Log(bytes.Length);return bytes;}private static int Decode(byte[] data, out byte[] bytes){byte[] byteLen = new byte[4];Array.Copy(data, 0, byteLen, 0, 4);int len = BitConverter.ToInt32(byteLen);bytes = new byte[data.Length - 4];//Debug.Log("总数据长度: " + (len + 4));//Debug.Log("数据内容长度: " + len);Array.Copy(data, 4, bytes, 0, bytes.Length);return len;}public class NetData{public NetData(ByteArray byteArray, IPEndPoint iPEndPoint){this.byteArray = byteArray;this.iPEndPoint = iPEndPoint;}public ByteArray byteArray;public IPEndPoint iPEndPoint;}
}
using UnityEngine.UI;
using System;[Serializable]
public class ByteArray
{//默认大小const int DEFAULT_SIZE = 4096;//初始大小int initSize = 0;//缓冲区public byte[] bytes;//读写位置public int readIdx = 0;public int writeIdx = 0;//容量private int capacity = 0;//剩余空间public int remain { get { return capacity - writeIdx; } }//数据长度public int length { get { return writeIdx - readIdx; } }//构造函数public ByteArray(int size = DEFAULT_SIZE){bytes = new byte[size];capacity = size;initSize = size;readIdx = 0;writeIdx = 0;}//构造函数public ByteArray(byte[] defaultBytes){bytes = defaultBytes;capacity = defaultBytes.Length;initSize = defaultBytes.Length;readIdx = 0;writeIdx = defaultBytes.Length;}//重设尺寸public void ReSize(int size){if (size < length) return;if (size < initSize) return;int n = 1;while (n < size) n *= 2;capacity = n;byte[] newBytes = new byte[capacity];Array.Copy(bytes, readIdx, newBytes, 0, writeIdx - readIdx);bytes = newBytes;writeIdx = length;readIdx = 0;}//写入数据public int Write(byte[] bs, int offset, int count){// UnityEngine.Debug.Log($"remain: {remain} - bs.Length: {bs.Length} - offset: {offset} - count{count} - bytes.Length: {bytes.Length} - writeIdx: {writeIdx}");if (remain < count){ReSize(length + count);}Array.Copy(bs, offset, bytes, writeIdx, count);writeIdx += count;return count;}//读取数据public int Read(byte[] bs, int offset, int count){count = Math.Min(count, length);Array.Copy(bytes, 0, bs, offset, count);readIdx += count;CheckAndMoveBytes();return count;}//读取数据public byte[] Read(int count){// UnityEngine.Debug.Log($"当前数据长度为 {length},从 {readIdx} 开始读取长度为 {count} 的数据, 剩余数据长度为 {writeIdx - readIdx - count}");byte[] bs = new byte[count];Array.Copy(bytes, readIdx, bs, 0, count);// readIdx += count;return bs;}//检查并移动数据public void CheckAndMoveBytes(){if (length < 8){MoveBytes();}}//移动数据public void MoveBytes(){Array.Copy(bytes, readIdx, bytes, 0, length);writeIdx = length;readIdx = 0;}//读取Int16public Int16 ReadInt16(){if (length < 2) return 0;Int16 ret = BitConverter.ToInt16(bytes, readIdx);readIdx += 2;CheckAndMoveBytes();return ret;}//读取Int32public Int32 ReadInt32(){if (length < 4) return 0;Int32 ret = BitConverter.ToInt32(bytes, readIdx);readIdx += 4;CheckAndMoveBytes();return ret;}//打印缓冲区public override string ToString(){return BitConverter.ToString(bytes, readIdx, length);}//打印调试信息public string Debug(){return string.Format("readIdx({0}) writeIdx({1}) bytes({2})",readIdx,writeIdx,BitConverter.ToString(bytes, 0, capacity));}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
//接收视频画面
public class : MonoBehaviour
{public RawImage rawImage;public int port = 8889;// Start is called before the first frame updatevoid Start(){UdpNet.Instance.Init(port);UdpNet.Instance.ReceiveDataAction += ReceiveDataAction;}private void ReceiveDataAction(byte[] arg0){if (arg0.Length < 1000){Debug.Log(Encoding.UTF8.GetString(arg0));return;}Texture2D texture2D = new Texture2D(10, 10);texture2D.LoadImage(arg0);texture2D.Apply();rawImage.texture = texture2D;Resources.UnloadUnusedAssets();}// Update is called once per framevoid Update(){}
}
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.UI;
//发送视频画面
public class SendWebCameraVideo: MonoBehaviour
{public string deviceName;public WebCamTexture webCam;public RawImage rawImage;public int frames = 30;public string ToIP = "127.0.0.1";public int ToPort = 8889;public int Port = 7777;private IPEndPoint iPEndPoint;public float maxTime;public float timer;public bool send;// Start is called before the first frame updatevoid Start(){UdpNet.Instance.Init(Port);WebCamDevice[] devices = WebCamTexture.devices;deviceName = devices[0].name;RectTransform rawRect = rawImage.GetComponent<RectTransform>();webCam = new WebCamTexture(deviceName, (int)rawRect.sizeDelta.x, (int)rawRect.sizeDelta.y, frames);//设置宽、高和帧率   rawImage.texture = webCam;//渲染脚本所在有RawImage组件的物体maxTime = 1f / frames;iPEndPoint = new IPEndPoint(IPAddress.Parse(ToIP), ToPort);UdpNet.Instance.SendMsg("gogogo", iPEndPoint);}// Update is called once per framevoid Update(){timer += Time.deltaTime;if (timer >= maxTime && send){// send = false;timer = 0;byte[] data = TextureToTexture2D(rawImage.texture).EncodeToJPG();UdpNet.Instance.SendBytes(data, iPEndPoint);// Resources.UnloadUnusedAssets();}}public void Play(){webCam.Play();send = true;}public void Pause(){send = false;webCam.Pause();}/// 运行模式下Texture转换成Texture2Dprivate Texture2D TextureToTexture2D(Texture texture){Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);RenderTexture currentRT = RenderTexture.active;RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);Graphics.Blit(texture, renderTexture);RenderTexture.active = renderTexture;texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);texture2D.Apply();RenderTexture.active = currentRT;RenderTexture.ReleaseTemporary(renderTexture);return texture2D;}}

建两个工程,一个发送一个接收,UdpNet和ByteArray是通用的,每个工程都必须要有
1.发送工程界面
发送工程界面
2.接收界面工程
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/85385.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java内嵌浏览器CEF-JAVA、jcef、java chrome

java内嵌浏览器CEF-JAVA、jcef、java chrome jcef是老牌cef的chrome内嵌方案&#xff0c;可以进行java-chrome-h5-桌面开发&#xff0c;下面为最新版本&#xff08;2023年9月22日10:33:07&#xff09; JCEF&#xff08;Java Chromium Embedded Framework&#xff09;是一个基于…

Cesium 空间量算——生成点位坐标

文章目录 需求分析1. 点击坐标点实现2. 输入坐标实现 需求 用 Cesium 生成点位坐标&#xff0c;并明显标识 分析 以下是我的两种实现方式 第一种是坐标点击实现 第二种是输入坐标实现 1. 点击坐标点实现 //点位坐标getLocation() {this.hoverIndex 0;let that this;this.view…

板子接线图

1.ST-LINK V2接线 2.对抗板子刷蓝牙固件 接USB转TTL&#xff0c;用镊子短接两个孔 2.对抗板子用串口测试蓝牙AT命令 短接白色箭头&#xff0c;接TX&#xff0c;RX&#xff0c;电源

MongoDB【部署 04】Windows系统实现MongoDB多磁盘存储

Windows系统实现多磁盘存储 1.为什么2.多磁盘存储2.1 数据库配置2.2 文件夹磁盘映射2.3 创建新的数据集 3.总结 1.为什么 这里仅针对只有一台Windows系统服务器的情景&#xff1a; 当服务器存储不足时&#xff0c;或者要接入更多的数据&#xff0c;就会挂载新磁盘&#xff0c…

边缘计算AI智能安防监控视频平台车辆违停算法详解与应用

随着城市车辆保有量呈现高速增长趋势&#xff0c;交通拥堵、违章行为也日益泛滥。因为车辆未停放在指定区域导致的车位浪费、占用/堵塞交通要道、车辆剐蹭等问题层出不穷。通过人工进行违法停车的监控&#xff0c;不仅让监控人员工作负荷越来越大&#xff0c;而且存在发现不及时…

Lua学习笔记:词法分析

前言 本篇在讲什么 Lua的词法分析 本篇需要什么 对Lua语法有简单认知 对C语法有简单认知 依赖Visual Studio工具 本篇的特色 具有全流程的图文教学 重实践&#xff0c;轻理论&#xff0c;快速上手 提供全流程的源码内容 ★提高阅读体验★ &#x1f449; ♠ 一级标题…

SpringAOP补充-通知获取类型

JoinPoint 是 ProceedingJoinPoint 的父类。 getArgs()是JoinPoint获取原方法返回值的函数。 preceed()是ProceedingJoinPoint获取原方法返回值的函数。

【Java】Servlet API

Servlet API HttpServlet核心方法Servlet生命周期 HttpServletRequest核心方法 HttpServletResponse核心方法 HttpServlet 我们写 Servlet 代码的时候, 首先第一步就是先创建类, 继承自 HttpServlet, 并重写其中的某些方法. 核心方法 方法名称调用时机init在 HttpServlet 实…

近三年各领域数字孪生相关政策汇编(可下载)

自2021年国家“十四五”规划纲要提出“探索建设数字孪生城市”以来&#xff0c;国家发展和改革委员会、工业和信息化部、住房和城乡建设部、水利部、农业农村部等部门纷纷出台政策&#xff0c;大力推动数字孪生在千行百业的落地发展。这些政策不仅为数字孪生的应用提供了广阔的…

39 | selenium基础架构,UI测试架构

什么是测试基础架构&#xff1f; 测试基础架构指的是&#xff0c;执行测试的过程中用到的所有基础硬件设施以及相关的软件设施。因此&#xff0c;我们也把测试基础架构称之为广义的测试执行环境。通常来讲&#xff0c;测试基础架构主要包括以下内容&#xff1a; 执行测试的机器…

机器学习第十一课--K-Means聚类

一.聚类的概念 K-Means算法是最经典的聚类算法&#xff0c;几乎所有的聚类分析场景&#xff0c;你都可以使用K-Means&#xff0c;而且在营销场景上&#xff0c;它就是"King"&#xff0c;所以不管从事数据分析师甚至是AI工程师&#xff0c;不知道K-Means是”不可原谅…

MATLAB | R2023b更新了哪些好玩的东西?

R2023b来啦&#xff01;&#xff01;废话不多说看看新版本有啥有趣的玩意和好玩的特性叭&#xff01;&#xff01;依旧把绘图放最前面叭&#xff0c;有图的内容看的人多。。 1 调色板 MATLAB终于不只有一套配色了&#xff0c;诸君且看&#xff1a; y [3 5 7 9 11; 2 5 6 8 1…

uniapp确认提示框;uniapp判断输入框值是否符合正常手机号,身份证号

确认提示框 UniApp 中&#xff0c;你可以使用 uni.showModal 方法来创建确认提示框。以下是一个示例&#xff1a; <template><view class"container"><button click"showAuthModal">显示确认提示框</button></view> </…

【问题记录】解决“命令行终端”和“Git Bash”操作本地Git仓库时出现 中文乱码 的问题!

环境 Windows 11 家庭中文版git version 2.41.0.windows.1 问题情况 在使用 “命令行终端” 和 “Git Bash” 在本地Git仓库敲击命令时&#xff0c;对中文名称文件显示一连串的数字&#xff0c;如下所示&#xff1a;这种情况通常是由于字符编码设置不正确所引起的 解决办法 设置…

什么是分布式锁?他解决了什么样的问题?

相信对于朋友们来说&#xff0c;锁这个东西已经非常熟悉了&#xff0c;在说分布式锁之前&#xff0c;我们来聊聊单体应用时候的本地锁&#xff0c;这个锁很多小伙伴都会用 ✔本地锁 我们在开发单体应用的时候&#xff0c;为了保证多个线程并发访问公共资源的时候&#xff0c;…

网络编程day05(IO多路复用)

今日任务&#xff1a; TCP多路复用的客户端、服务端&#xff1a; 服务端代码&#xff1a; #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <unistd.h> …

uniapp 内容展开组件

uni-collapse折叠面板并不符合需求&#xff0c;需要自己写一个。 效果展示&#xff1a; 代码&#xff1a; &#xff08;vue3版本&#xff09; <template><view class"collapse-view"><view class"collapse-content"><swiper:autopl…

OpenHarmony应用核心技术理念与需求机遇简析

一、核心技术理念 图片来源&#xff1a;OpenHarmony官方网站 二、需求机遇简析 新的万物互联智能世界代表着新规则、新赛道、新切入点、新财富机会;各WEB网站、客户端( 苹果APP、安卓APK)、微信小程序等上的组织、企业、商户等;OpenHarmony既是一次机遇、同时又是一次大的挑战&…

好用的记笔记app选哪个?

当你在日常生活中突然获得了一个灵感&#xff0c;或者需要记录会议的重要内容&#xff0c;或者是学校课堂上的笔记&#xff0c;你通常会拿出手机&#xff0c;因为它总是在你身边&#xff0c;随时可用。这时候&#xff0c;一款好的记笔记App可以让你事半功倍。 敬业签是一款全面…

「UG/NX」Block UI 从列表选择部件SelectPartFromList

✨博客主页何曾参静谧的博客📌文章专栏「UG/NX」BlockUI集合📚全部专栏「UG/NX」NX二次开发「UG/NX」BlockUI集合「VS」Visual Studio「QT」QT5程序设计「C/C+&#