|
|
@@ -1,4 +1,4 @@
|
|
|
-using System.Collections;
|
|
|
+锘縰sing System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
using System;
|
|
|
@@ -9,16 +9,16 @@ using System.Threading;
|
|
|
|
|
|
public class BleUDP : MonoBehaviour
|
|
|
{
|
|
|
- public static BleUDP ins; //单例记录
|
|
|
+ public static BleUDP ins; //鍗曚緥璁板綍
|
|
|
|
|
|
- readonly string localIP = "127.0.0.1"; //本地IP
|
|
|
- readonly int myPort = 8001; //自身的注册端口
|
|
|
- readonly int targetPort = 8000; //发消息的目标端口
|
|
|
- Socket client; //UDP对象
|
|
|
+ readonly string localIP = "127.0.0.1"; //鏈�湴IP
|
|
|
+ readonly int myPort = 8001; //鑷�韩鐨勬敞鍐岀�鍙�
|
|
|
+ readonly int targetPort = 8000; //鍙戞秷鎭�殑鐩�爣绔�彛
|
|
|
+ Socket client; //UDP瀵硅薄
|
|
|
|
|
|
- float noneMsgTime = 0; //多久没收到字节消息了
|
|
|
- Queue<byte[]> byteQueue = new Queue<byte[]>(); //接收信息的线程把字节信息放这里
|
|
|
- Queue<byte[]> byteMainQueue = new Queue<byte[]>(); //主线程从byteQueue中取出数据后放在这里,供主线程update使用
|
|
|
+ float noneMsgTime = 0; //澶氫箙娌℃敹鍒板瓧鑺傛秷鎭�簡
|
|
|
+ Queue<byte[]> byteQueue = new Queue<byte[]>(); //鎺ユ敹淇℃伅鐨勭嚎绋嬫妸瀛楄妭淇℃伅鏀捐繖閲�
|
|
|
+ Queue<byte[]> byteMainQueue = new Queue<byte[]>(); //涓荤嚎绋嬩粠byteQueue涓�彇鍑烘暟鎹�悗鏀惧湪杩欓噷锛屼緵涓荤嚎绋媢pdate浣跨敤
|
|
|
|
|
|
public Action OnConnected;
|
|
|
public Action OnConnectionFailed;
|
|
|
@@ -39,6 +39,19 @@ public class BleUDP : MonoBehaviour
|
|
|
|
|
|
client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
|
client.Bind(new IPEndPoint(IPAddress.Parse(localIP), myPort));
|
|
|
+
|
|
|
+ /*
|
|
|
+ C# UDP Socket ReceiveFrom 杩滅▼涓绘満寮鸿揩鍏抽棴浜嗕竴涓�幇鏈夌殑杩炴帴銆�
|
|
|
+ 锛堢粡杩囧弽澶嶇爺绌讹紝涓嬭堪鎯呭喌鐨勫師鍥犵粓浜庢悶娓呮�浜嗭紝鏄�"ICMP port unreachable"鐨勯棶棰橈紝鍗筹細鑻ュ悜涓€涓�病鏈夌浉搴擴DP鐩戝惉绔�彛鐨勬湰鏈哄湴鍧€锛堟瘮濡�127.0.0.1锛夊彂閫乁DP鏁版嵁鍖咃紝浼氬洖澶岻CMP port unreachable鍖咃紝鑰岃繖涓�寘浼氳�C#鐨刄DP Socket ReceiveFrom鍑芥暟寰楀埌锛屽苟鎶ラ敊涓衡€滆繙绋嬩富鏈哄己杩�叧闂�簡涓€涓�幇鏈夎繛鎺モ€濓紒锛�
|
|
|
+ 锛堣€屼负浠€涔堝悜瀛樺湪鐨勫彟涓€涓猧p锛堝彟涓€鍙颁富鏈猴級鍙戦€佷笉浼氭姤閿欙紵鍥犱负闃茬伀澧欙紒win10鐨勯槻鐏��鎶婂�閮ㄥ洖澶嶇殑ICMP port unreachable缁欏睆钄芥帀浜嗐€傜粡瀹為獙锛屾妸闃茬伀澧欏叧鎺夊悗锛屽氨浼氬嚭鐜颁笂杩版姤閿欎簡銆傦級
|
|
|
+ 锛堣€屼负浠€涔堝悜涓€涓�笉瀛樺湪鐨刬p鍦板潃鍙戦€佷笉浼氭姤閿欙紵鍥犱负涓绘満涓嶅瓨鍦�紝鎵€浠ヤ笉浼氬洖澶岻CMP port unreachable锛�
|
|
|
+ */
|
|
|
+ //閰嶇疆socket鍙傛暟锛岄伩鍏岻CMP鍖呭�鑷寸殑寮傚父
|
|
|
+ uint IOC_IN = 0x80000000;
|
|
|
+ uint IOC_VENDOR = 0x18000000;
|
|
|
+ uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
|
|
|
+ client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
|
|
|
+
|
|
|
Thread threadReciveMsg = new Thread(ReciveMsg);
|
|
|
threadReciveMsg.Start();
|
|
|
}
|
|
|
@@ -111,7 +124,7 @@ public class BleUDP : MonoBehaviour
|
|
|
EndPoint point = new IPEndPoint(IPAddress.Any, 0);
|
|
|
byte[] buffer = new byte[1024];
|
|
|
int length = client.ReceiveFrom(buffer, ref point);
|
|
|
- if (!point.ToString().EndsWith(targetPort.ToString())) return;
|
|
|
+ if (!point.ToString().EndsWith(targetPort.ToString())) continue;
|
|
|
if (length <= 0) continue;
|
|
|
byte[] bytes = new byte[length];
|
|
|
Array.Copy(buffer, bytes, length);
|