udp 和tcp结合视频监控系统.zip
using System; using System.Collections.Concurrent; using System.Net.Sockets; using System.Text; using System.Threading; namespace Gimela.Net.Sockets { /// <summary> /// UDP发送器 /// </summary> public class UdpSender : IDisposable { private bool disposed = false; private Thread senderThread; private UdpClient udpClient; private ConcurrentQueue<byte[]> queue; private readonly ManualResetEvent waiter; /// <summary> /// UDP发送器 /// </summary> /// <param name="sentToAddress">发送目的地址</param> /// <param name="sentToPort">发送目的端口</param> public UdpSender(string sentToAddress, int sentToPort) { Address = sentToAddress; Port = sentToPort; this.Encoding = Encoding.Default; queue = new ConcurrentQueue<byte[]>(); waiter = new ManualResetEvent(false); udpClient = new UdpClient(); udpClient.AllowNatTraversal(true); senderThread = new Thread(new ThreadStart(WorkThread)); } /// <summary> /// 发送目的地址 /// </summary> public string Address { get; private set; } /// <summary> /// 发送目的端口 /// </summary> public int Port { get; private set; } /// <summary> /// 发送器是否正在运行 /// </summary> public bool IsRunning { get; private set; } /// <summary> /// 发送器所使用的编码 /// </summary> public Encoding Encoding { get; set; } /// <summary> /// 启动发送器 /// </summary> /// <returns>UDP发送器</returns> public UdpSender Start() { if (!IsRunning) { IsRunning = true; senderThread.Start(); } return this; } /// <summary> /// 停止发送器 /// </summary> /// <returns>UDP发送器</returns> public UdpSender Stop() { IsRunning = false; return this; } /// <summary> /// 发送数据报文 /// </summary> /// <param name="datagram">数据报文</param> /// <returns>UDP发送器</returns> public UdpSender Send(byte[] datagram) { if (!IsRunning) throw new InvalidProgramException("This sender has not been started."); queue.Enqueue(datagram); waiter.Set(); return this; } /// <summary> /// 发送数据报文 /// </summary> /// <param name="datagram">数据报文</param> /// <returns>UDP发送器</returns> public UdpSender Send(string datagram) { if (!IsRunning) throw new InvalidProgramException("This sender has not been started."); queue.Enqueue(this.Encoding.GetBytes(datagram)); waiter.Set(); return this; } private void WorkThread() { while (IsRunning) { waiter.WaitOne(); waiter.Reset(); while (queue.Count > 0) { byte[] datagram = null; if (queue.TryDequeue(out datagram)) { udpClient.BeginSend(datagram, datagram.Length, Address, Port, SendCompleted, udpClient); } } } } private void SendCompleted(IAsyncResult ar) { UdpClient udp = ar.AsyncState as UdpClient; if (udp != null) { udp.EndSend(ar); } } #region IDisposable Members /// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { Stop(); waiter.Close(); if (senderThread != null) { try { senderThread.Abort(); } catch (ThreadStateException) { } finally { senderThread = null; } } if (udpClient != null) { udpClient.Close(); udpClient = null; } } disposed = true; } } #endregion } }
- 粉丝: 5
- 资源: 26
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 使用C++实现的常见算法
- travel-web-springboot【程序员VIP专用】.zip
- 基于Matlab, ConvergeCase中部分2D结果文件输出至EXCEL中 能力有限,代码和功能极其简陋.zip
- java桌面小程序,主要为游戏.zip学习资源
- Java桌面-坦克大战小游戏.zip程序资源
- java语言做的魔板小游戏.zip
- 初学JAVA制作的坦克大战小游戏,使用JAVA 的GUI模拟2,5D界面.zip
- 公开整理-2024年832个国家级贫困县摘帽情况分省分年统计.xlsx
- 纯js+Jquery实现2048游戏
- 叠罗汉游戏,安卓java实现,自定义Framlayout,属性动画.zip