Merge branch 'main2'
This commit is contained in:
		
						commit
						76f28e10b7
					
				| @ -6,6 +6,8 @@ using UnityEngine; | |||||||
| namespace RoboidControl.Unity { | namespace RoboidControl.Unity { | ||||||
| 
 | 
 | ||||||
|     public class SiteServer : MonoBehaviour { |     public class SiteServer : MonoBehaviour { | ||||||
|  |         public int port = 7681; | ||||||
|  | 
 | ||||||
|         public RoboidControl.SiteServer site; |         public RoboidControl.SiteServer site; | ||||||
| 
 | 
 | ||||||
|         public Queue<RoboidControl.Thing> thingQueue = new(); |         public Queue<RoboidControl.Thing> thingQueue = new(); | ||||||
| @ -13,7 +15,7 @@ namespace RoboidControl.Unity { | |||||||
|         protected virtual void Awake() { |         protected virtual void Awake() { | ||||||
|             Console.SetOut(new UnityLogWriter()); |             Console.SetOut(new UnityLogWriter()); | ||||||
| 
 | 
 | ||||||
|             site = new RoboidControl.SiteServer(7681); |             site = new RoboidControl.SiteServer(port); | ||||||
|             RoboidControl.Thing.OnNewThing += HandleNewThing; |             RoboidControl.Thing.OnNewThing += HandleNewThing; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| #if UNITY_5_3_OR_NEWER | #if UNITY_5_3_OR_NEWER | ||||||
| using System.Collections; | using System.Collections; | ||||||
| using Mono.Cecil.Cil; |  | ||||||
| using UnityEngine; | using UnityEngine; | ||||||
| using UnityEngine.Networking; | using UnityEngine.Networking; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -67,9 +67,9 @@ namespace RoboidControl.Unity { | |||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		private void OnTriggerEnter(Collider other) { | 		private void OnTriggerEnter(Collider other) { | ||||||
| 			Debug.Log("Touch?"); | 			// Debug.Log("Touch?"); | ||||||
| 			if (other.isTrigger) { | 			if (other.isTrigger) { | ||||||
| 				Debug.Log($"   was trigger {other.name}"); | 				// Debug.Log($"   was trigger {other.name}"); | ||||||
| 				return; | 				return; | ||||||
| 			} | 			} | ||||||
| 			if (this.transform.root == other.transform.root) { | 			if (this.transform.root == other.transform.root) { | ||||||
|  | |||||||
| @ -1,8 +1,8 @@ | |||||||
| using System; | using System; | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Collections.Concurrent; | using System.Collections.Concurrent; | ||||||
| using System.Net; | using System.Net; | ||||||
| using System.Net.Sockets; | using System.Net.Sockets; | ||||||
|  | using System.Net.NetworkInformation; | ||||||
| 
 | 
 | ||||||
| namespace RoboidControl { | namespace RoboidControl { | ||||||
| 
 | 
 | ||||||
| @ -46,8 +46,35 @@ namespace RoboidControl { | |||||||
|                 this.isIsolated = true; |                 this.isIsolated = true; | ||||||
|             else |             else | ||||||
|                 this.remoteSite = new Participant(ipAddress, port); |                 this.remoteSite = new Participant(ipAddress, port); | ||||||
|  | 
 | ||||||
|             Participant.AddParticipant(this); |             Participant.AddParticipant(this); | ||||||
| 
 | 
 | ||||||
|  |             // Determine local IP address | ||||||
|  |             IPAddress localIpAddress = null; | ||||||
|  |             IPAddress subnetMask = null; | ||||||
|  |             using (Socket socket = new(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { | ||||||
|  |                 // Connect to a remote endpoint — we won't actually send anything | ||||||
|  |                 socket.Connect("1.1.1.1", 65530); | ||||||
|  |                 if (socket.LocalEndPoint is IPEndPoint endPoint) { | ||||||
|  |                     localIpAddress = endPoint.Address; | ||||||
|  |                     this.ipAddress = localIpAddress.ToString(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             // Find subnet mask | ||||||
|  |             foreach (NetworkInterface nwInterface in NetworkInterface.GetAllNetworkInterfaces()) { | ||||||
|  |                 if (nwInterface.OperationalStatus != OperationalStatus.Up) | ||||||
|  |                     continue; | ||||||
|  | 
 | ||||||
|  |                 foreach (UnicastIPAddressInformation unicastAddress in nwInterface.GetIPProperties().UnicastAddresses) { | ||||||
|  |                     if (unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork && unicastAddress.Address.Equals(localIpAddress) | ||||||
|  |                     ) { | ||||||
|  |                         subnetMask = unicastAddress.IPv4Mask; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             if (localIpAddress != null && subnetMask != null) | ||||||
|  |                 GetBroadcastAddress(localIpAddress, subnetMask); | ||||||
|  | 
 | ||||||
|             this.endPoint = new IPEndPoint(IPAddress.Any, localPort); |             this.endPoint = new IPEndPoint(IPAddress.Any, localPort); | ||||||
|             this.udpClient = new UdpClient(localPort); |             this.udpClient = new UdpClient(localPort); | ||||||
|             this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); |             this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); | ||||||
| @ -64,34 +91,31 @@ namespace RoboidControl { | |||||||
|             this.port = port; |             this.port = port; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         protected void GetBroadcastAddress(IPAddress ip, IPAddress subnetMask) { | ||||||
|  |             byte[] ipBytes = ip.GetAddressBytes(); | ||||||
|  |             byte[] maskBytes = subnetMask.GetAddressBytes(); | ||||||
|  | 
 | ||||||
|  |             if (ipBytes.Length != maskBytes.Length) | ||||||
|  |                 throw new ArgumentException("IP address and subnet mask lengths do not match"); | ||||||
|  | 
 | ||||||
|  |             byte[] broadcastBytes = new byte[ipBytes.Length]; | ||||||
|  |             for (int i = 0; i < ipBytes.Length; i++) { | ||||||
|  |                 broadcastBytes[i] = (byte)(ipBytes[i] | (~maskBytes[i])); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             IPAddress broadcastAddress = new(broadcastBytes); | ||||||
|  |             this.broadcastIpAddress = broadcastAddress.ToString(); | ||||||
|  | 
 | ||||||
|  |             Console.WriteLine($"Subnet mask: {subnetMask.ToString()}"); | ||||||
|  |             Console.WriteLine($"Broadcast address: {this.broadcastIpAddress}"); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         private static ParticipantUDP isolatedParticipant = null; |         private static ParticipantUDP isolatedParticipant = null; | ||||||
|         public static ParticipantUDP Isolated() { |         public static ParticipantUDP Isolated() { | ||||||
|             if (isolatedParticipant == null) |             if (isolatedParticipant == null) | ||||||
|                 isolatedParticipant = new ParticipantUDP(0); |                 isolatedParticipant = new ParticipantUDP(0); | ||||||
|             return isolatedParticipant; |             return isolatedParticipant; | ||||||
|         } |         } | ||||||
| 
 |  | ||||||
|         // public List<Participant> owners = new List<Participant>(); |  | ||||||
| 
 |  | ||||||
|         // public Participant GetParticipant(string ipAddress, int port) { |  | ||||||
|         //     //Console.WriteLine($"Get Participant {ipAddress}:{port}"); |  | ||||||
|         //     foreach (Participant sender in owners) { |  | ||||||
|         //         if (sender.ipAddress == ipAddress && sender.port == port) |  | ||||||
|         //             return sender; |  | ||||||
|         //     } |  | ||||||
|         //     return null; |  | ||||||
|         // } |  | ||||||
|         // public Participant AddParticipant(string ipAddress, int port) { |  | ||||||
|         //     Console.WriteLine($"New Participant {ipAddress}:{port}"); |  | ||||||
|         //     Participant participant = new(ipAddress, port) { |  | ||||||
|         //         networkId = (byte)(this.owners.Count + 1) |  | ||||||
|         //     }; |  | ||||||
|         //     owners.Add(participant); |  | ||||||
|         //     return participant; |  | ||||||
|         // } |  | ||||||
| 
 |  | ||||||
|         // protected readonly Dictionary<byte, Func<Participant, byte, byte, Thing>> thingMsgProcessors = new(); |  | ||||||
| 
 |  | ||||||
|         #endregion Init |         #endregion Init | ||||||
| 
 | 
 | ||||||
|         #region Update |         #region Update | ||||||
| @ -161,7 +185,6 @@ namespace RoboidControl { | |||||||
|                 if (this.isIsolated) |                 if (this.isIsolated) | ||||||
|                     continue; |                     continue; | ||||||
| 
 | 
 | ||||||
|                 //foreach (Thing thing in participant.things) { |  | ||||||
|                 for (int thingIx = 0; thingIx < participant.things.Count; thingIx++) { |                 for (int thingIx = 0; thingIx < participant.things.Count; thingIx++) { | ||||||
|                     Thing thing = participant.things[thingIx]; |                     Thing thing = participant.things[thingIx]; | ||||||
|                     if (thing == null) |                     if (thing == null) | ||||||
| @ -212,7 +235,7 @@ namespace RoboidControl { | |||||||
|             if (bufferSize <= 0) |             if (bufferSize <= 0) | ||||||
|                 return true; |                 return true; | ||||||
| 
 | 
 | ||||||
|             Console.WriteLine($"publish to {broadcastIpAddress.ToString()} {this.port}"); |             // Console.WriteLine($"publish to {broadcastIpAddress.ToString()} {this.port}"); | ||||||
|             this.udpClient?.Send(this.buffer, bufferSize, this.broadcastIpAddress, this.port); |             this.udpClient?.Send(this.buffer, bufferSize, this.broadcastIpAddress, this.port); | ||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
| @ -252,10 +275,12 @@ namespace RoboidControl { | |||||||
|             // We can receive our own publish (broadcast) packages. How do we recognize them???? |             // We can receive our own publish (broadcast) packages. How do we recognize them???? | ||||||
|             // It is hard to determine our source port |             // It is hard to determine our source port | ||||||
|             string ipAddress = endPoint.Address.ToString(); |             string ipAddress = endPoint.Address.ToString(); | ||||||
|  |             if (ipAddress != this.ipAddress) { | ||||||
|                 Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port); |                 Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port); | ||||||
|                 remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port); |                 remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port); | ||||||
| 
 | 
 | ||||||
|                 ReceiveData(data, remoteParticipant); |                 ReceiveData(data, remoteParticipant); | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             udpClient.BeginReceive(new AsyncCallback(callbackResult => ReceiveUDP(callbackResult)), null); |             udpClient.BeginReceive(new AsyncCallback(callbackResult => ReceiveUDP(callbackResult)), null); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| using System; | using System; | ||||||
| using System.Net; | using System.Net; | ||||||
| using System.Net.Sockets; | using System.Net.Sockets; | ||||||
|  | using System.Net.NetworkInformation; | ||||||
| 
 | 
 | ||||||
| namespace RoboidControl { | namespace RoboidControl { | ||||||
| 
 | 
 | ||||||
| @ -17,6 +18,32 @@ namespace RoboidControl { | |||||||
|             this.name = "Site Server"; |             this.name = "Site Server"; | ||||||
|             Participant.AddParticipant(this); |             Participant.AddParticipant(this); | ||||||
| 
 | 
 | ||||||
|  |             // Determine local IP address | ||||||
|  |             IPAddress localIpAddress = null; | ||||||
|  |             IPAddress subnetMask = null; | ||||||
|  |             using (Socket socket = new(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { | ||||||
|  |                 // Connect to a remote endpoint — we won't actually send anything | ||||||
|  |                 socket.Connect("1.1.1.1", 65530); | ||||||
|  |                 if (socket.LocalEndPoint is IPEndPoint endPoint) { | ||||||
|  |                     localIpAddress = endPoint.Address; | ||||||
|  |                     this.ipAddress = localIpAddress.ToString(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             // Find subnet mask | ||||||
|  |             foreach (NetworkInterface nwInterface in NetworkInterface.GetAllNetworkInterfaces()) { | ||||||
|  |                 if (nwInterface.OperationalStatus != OperationalStatus.Up) | ||||||
|  |                     continue; | ||||||
|  | 
 | ||||||
|  |                 foreach (UnicastIPAddressInformation unicastAddress in nwInterface.GetIPProperties().UnicastAddresses) { | ||||||
|  |                     if (unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork && unicastAddress.Address.Equals(localIpAddress) | ||||||
|  |                     ) { | ||||||
|  |                         subnetMask = unicastAddress.IPv4Mask; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             if (localIpAddress != null && subnetMask != null) | ||||||
|  |                 GetBroadcastAddress(localIpAddress, subnetMask); | ||||||
|  | 
 | ||||||
|             Console.Write($"Prepare receive on port {port}"); |             Console.Write($"Prepare receive on port {port}"); | ||||||
|             this.endPoint = new IPEndPoint(IPAddress.Any, port); |             this.endPoint = new IPEndPoint(IPAddress.Any, port); | ||||||
|             this.udpClient = new UdpClient(port); |             this.udpClient = new UdpClient(port); | ||||||
| @ -77,7 +104,16 @@ namespace RoboidControl { | |||||||
| 
 | 
 | ||||||
|         protected override void Process(Participant sender, ThingMsg msg) { |         protected override void Process(Participant sender, ThingMsg msg) { | ||||||
|             Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} "); |             Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} "); | ||||||
|  | 
 | ||||||
|             Thing thing = sender.Get(msg.networkId, msg.thingId); |             Thing thing = sender.Get(msg.networkId, msg.thingId); | ||||||
|  |             if (thing == null) { | ||||||
|  |                 switch (msg.thingType) { | ||||||
|  |                     case (byte)Thing.Type.TouchSensor: | ||||||
|  |                         new TouchSensor(sender, msg.networkId, msg.thingId); | ||||||
|  |                         break; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|             if (thing == null) |             if (thing == null) | ||||||
|                 thing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); |                 thing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); | ||||||
| 
 | 
 | ||||||
| @ -92,6 +128,7 @@ namespace RoboidControl { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|         #endregion Receive |         #endregion Receive | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user