当前位置: 首页 > news >正文

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.接收界面工程
在这里插入图片描述

相关文章:

Untiy UDP局域网 异步发送图片

同步画面有问题&#xff0c;传图片吧 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 Sy…...

移动端H5封装一个 ScrollList 横向滚动列表组件,实现向左滑动

效果&#xff1a; 1.封装组件&#xff1a; <template><div class"scroll-list"><divclass"scroll-list-content":style"{ background, color, fontSize: size }"ref"scrollListContent"><div class"scroll…...

Docker一键安装和基本配置

一键安装脚本 注&#xff1a;该脚本需要root权限 curl -sSL https://get.docker.com/ | sh非root组用户赋权 sudo groupadd docker # 若使用一键安装脚本会自动创建这个组&#xff0c;提示已存在 sudo gpasswd -a ${USER} docker # 将当前用户添加到docker组&#xff0c;也…...

MVC设计思想理解和ASP.NET MVC理解

三层模式 三层模式包括:UI层,业务逻辑层,数据访问层,模型层 MVC设计思想和ASP.NET MVC理解 MVC设计思想: MVC的思想就是把我们的程序分为三个核心的模块,这三个模块的详细介绍如下: 模型(Model) :负责封装与引用程序的业务逻辑相关的数据以及对数据的处理方法。模型层有对…...

大模型应用选择对比

大模型应用选择对比 1、知识库对比&#xff1a;dify、fastgpt、langchatchat 2、agent构建器选择&#xff1a;flowise、langflow、bisheng 3、召回率提升方案...

c++STL概述

目录 STL基本概念 STL六大组件 STL的优点 STL三大组件 容器 算法 迭代器 普通的迭代器访问vector容器元素 算法for_each实现循环 迭代器指向的元素类型是自定义数据类型 迭代器指向容器 常用容器 string容器 string的基本概念 string容器的操作 string的构造函…...

利用容器技术优化DevOps流程

利用容器技术优化DevOps流程 随着云计算的快速发展&#xff0c;容器技术也日益流行。容器技术可以打包和分发应用程序&#xff0c;并实现快速部署和扩展。在DevOps流程中&#xff0c;容器技术可以大大优化开发、测试、部署和运维各个环节。本文将介绍如何利用容器技术优化DevO…...

91 # 实现 express 的优化处理

上一节实现 express 的请求处理&#xff0c;这一节来进行实现 express 的优化处理 让 layer 提供 match 方法去匹配 pathname&#xff0c;方便拓展让 layer 提供 handle_request 方法&#xff0c;方便拓展利用第三方库 methods 批量生成方法性能优化问题 进行路由懒加载&#…...

arcgis拓扑检查实现多个矢量数据之间消除重叠区域

目录 环境介绍&#xff1a; 操作任务&#xff1a; 步骤&#xff1a; 1、数据库和文件结构准备 2、建立拓扑规则 3、一直下一页默认参数后&#xff0c;进行拓扑检查 4、打开TP_CK_Topology&#xff0c;会自动带出拓扑要素&#xff0c;红色区域为拓扑错误的地方&#xff1…...

基于Vue+ELement搭建登陆注册页面实现后端交互

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是Java方文山&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的专栏《ELement》。&#x1f3af;&#x1f3af; &#x1…...

JS获取经纬度, 并根据经纬度得到城市信息

在JavaScript中&#xff0c;获取经纬度通常需要使用定位服务&#xff0c;比如HTML5的Geolocation API。然而拿到坐标后&#xff0c;将经纬度转换为城市信息&#xff0c;则需要使用逆地理编码服务接口&#xff0c;比如百度或者高德的 API, 但是他们收费都很高, 我们可以使用一些…...

mac m1 docker安装nacos

文章目录 引言I m1安装docker1.1 Docker 下载1.2 终端Docker相关命令II docker安装nacos2.1 安装nacos2.2 镜像启动see alsoMac 查看进程端口引言 使用docker方式安装是最方便的 I m1安装docker 1.1 Docker 下载 https://docs.docker.com/docker-for-mac/apple-silicon/点击…...

位段 联合体 枚举

Hello好久不见&#xff0c;今天分享的是接上次结构体没有分享完的内容&#xff0c;这次我们讲讲位段 枚举和联合体的概念以及他们的用法。 2.1 什么是位段 位段的声明和结构是类似的&#xff0c;有两个不同&#xff1a; 1.位段的成员必须是 int、unsigned int 或signed int 。 …...

PHP循环获取Excel表头字母A-Z,当超过时输出AA,AB,AC,AD······

PHP循环获取Excel表头字母A-Z&#xff0c;当超过时输出AA,AB,AC,AD PHP循环生成Excel的列字母表 $count_num 26 * 27; $letter A; $arr []; while($count_num--){$arr[] $letter;$letter; }结果如下&#xff1a; 转为JSON更为直观&#xff1a; ["A","B&…...

识别准确率达 95%,华能东方电厂财务机器人实践探索

摘 要&#xff1a;基于华能集团公司大数据与人工智能构想理念&#xff0c;结合东方电厂实际工作需要&#xff0c;财务工作要向数字化、智能化纵深推进&#xff0c;随着财务数字化转型和升级加速&#xff0c;信息化水平不断提升&#xff0c;以及内部信息互联互通不断加深&#x…...

代码随想录算法训练营 单调栈part03

一、柱状图中最大的矩形 84. 柱状图中最大的矩形 - 力扣&#xff08;LeetCode&#xff09; 单调栈很重要的性质&#xff0c;就是单调栈里的顺序&#xff0c;是从小到大还是从大到小。 栈顶和栈顶的下一个元素以及要入栈的三个元素组成了我们要求最大面积的高度和宽度&#x…...

使用 MyBatisPlus 的注解方式进行 SQL 查询,它结合了条件构造器(Wrapper)和自定义 SQL 片段来构建查询语句。

MyBatis-Plus 是一个基于 MyBatis 的增强工具&#xff0c;它提供了一套方便的注解方式来进行 SQL 查询。其中&#xff0c;它结合了条件构造器&#xff08;Wrapper&#xff09;和自定义 SQL 片段来构建查询语句。 官网&#xff1a;条件构造器 | MyBatis-Plus 1、使用 Wrapper …...

Python中统计单词出现的次数,包含(PySpark方法)

思路&#xff1a; 定义一个函数&#xff0c;使用open函数&#xff0c;将文本内容打开。 定义一个空字典和空列表&#xff0c;进行循环及条件判断操作def count_word(file_path):dict_data {} #定义一个空字典f open(file_path,"r",encoding"UTF-8")lis…...

探讨基于IEC61499 的分布式 ISA Batch 控制系统

ISA SP88 是批次过程控制的标准&#xff0c;对应的IEC标准是IEC 61512。该标准中一个重要的部分是配方管理&#xff08;Recipe Management&#xff09;。 所谓配方&#xff0c;是根据批量产品的要求&#xff0c;材料设定加工工艺&#xff0c;加工流程和参数。类似于传统制造业的…...

图论16(Leetcode863.二叉树中所有距离为K的结点)

答案&#xff1a; /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val x; }* }*/ class Solution {public List<Integer> distanceK(TreeNode root, TreeNode tar…...

eNSP-Cloud(实现本地电脑与eNSP内设备之间通信)

说明&#xff1a; 想象一下&#xff0c;你正在用eNSP搭建一个虚拟的网络世界&#xff0c;里面有虚拟的路由器、交换机、电脑&#xff08;PC&#xff09;等等。这些设备都在你的电脑里面“运行”&#xff0c;它们之间可以互相通信&#xff0c;就像一个封闭的小王国。 但是&#…...

springboot 百货中心供应链管理系统小程序

一、前言 随着我国经济迅速发展&#xff0c;人们对手机的需求越来越大&#xff0c;各种手机软件也都在被广泛应用&#xff0c;但是对于手机进行数据信息管理&#xff0c;对于手机的各种软件也是备受用户的喜爱&#xff0c;百货中心供应链管理系统被用户普遍使用&#xff0c;为方…...

以下是对华为 HarmonyOS NETX 5属性动画(ArkTS)文档的结构化整理,通过层级标题、表格和代码块提升可读性:

一、属性动画概述NETX 作用&#xff1a;实现组件通用属性的渐变过渡效果&#xff0c;提升用户体验。支持属性&#xff1a;width、height、backgroundColor、opacity、scale、rotate、translate等。注意事项&#xff1a; 布局类属性&#xff08;如宽高&#xff09;变化时&#…...

【Linux】C语言执行shell指令

在C语言中执行Shell指令 在C语言中&#xff0c;有几种方法可以执行Shell指令&#xff1a; 1. 使用system()函数 这是最简单的方法&#xff0c;包含在stdlib.h头文件中&#xff1a; #include <stdlib.h>int main() {system("ls -l"); // 执行ls -l命令retu…...

JVM垃圾回收机制全解析

Java虚拟机&#xff08;JVM&#xff09;中的垃圾收集器&#xff08;Garbage Collector&#xff0c;简称GC&#xff09;是用于自动管理内存的机制。它负责识别和清除不再被程序使用的对象&#xff0c;从而释放内存空间&#xff0c;避免内存泄漏和内存溢出等问题。垃圾收集器在Ja…...

在 Nginx Stream 层“改写”MQTT ngx_stream_mqtt_filter_module

1、为什么要修改 CONNECT 报文&#xff1f; 多租户隔离&#xff1a;自动为接入设备追加租户前缀&#xff0c;后端按 ClientID 拆分队列。零代码鉴权&#xff1a;将入站用户名替换为 OAuth Access-Token&#xff0c;后端 Broker 统一校验。灰度发布&#xff1a;根据 IP/地理位写…...

[10-3]软件I2C读写MPU6050 江协科技学习笔记(16个知识点)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...

如何在最短时间内提升打ctf(web)的水平?

刚刚刷完2遍 bugku 的 web 题&#xff0c;前来答题。 每个人对刷题理解是不同&#xff0c;有的人是看了writeup就等于刷了&#xff0c;有的人是收藏了writeup就等于刷了&#xff0c;有的人是跟着writeup做了一遍就等于刷了&#xff0c;还有的人是独立思考做了一遍就等于刷了。…...

什么是Ansible Jinja2

理解 Ansible Jinja2 模板 Ansible 是一款功能强大的开源自动化工具&#xff0c;可让您无缝地管理和配置系统。Ansible 的一大亮点是它使用 Jinja2 模板&#xff0c;允许您根据变量数据动态生成文件、配置设置和脚本。本文将向您介绍 Ansible 中的 Jinja2 模板&#xff0c;并通…...

重启Eureka集群中的节点,对已经注册的服务有什么影响

先看答案&#xff0c;如果正确地操作&#xff0c;重启Eureka集群中的节点&#xff0c;对已经注册的服务影响非常小&#xff0c;甚至可以做到无感知。 但如果操作不当&#xff0c;可能会引发短暂的服务发现问题。 下面我们从Eureka的核心工作原理来详细分析这个问题。 Eureka的…...