First UDP communication is working
This commit is contained in:
		
							parent
							
								
									cd9b4a1e9e
								
							
						
					
					
						commit
						eca5e698cd
					
				| @ -34,9 +34,9 @@ namespace Passer.RoboidControl { | |||||||
|         /// Create a participant with the give UDP port |         /// Create a participant with the give UDP port | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         /// <param name="port">The port number on which to communicate</param> |         /// <param name="port">The port number on which to communicate</param> | ||||||
|         public Participant(int port) : this() { |         // public Participant(int port) : this() { | ||||||
|             this.port = port; |         //     this.port = port; | ||||||
|         } |         // } | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Create a new participant for a site at the given address and port |         /// Create a new participant for a site at the given address and port | ||||||
| @ -85,11 +85,11 @@ namespace Passer.RoboidControl { | |||||||
|             return participant; |             return participant; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         protected readonly Dictionary<byte, Func<byte, byte, Thing>> thingMsgProcessors = new Dictionary<byte, Func<byte, byte, Thing>>(); |         protected readonly Dictionary<byte, Func<RemoteParticipant, byte, byte, Thing>> thingMsgProcessors = new Dictionary<byte, Func<RemoteParticipant, byte, byte, Thing>>(); | ||||||
| 
 | 
 | ||||||
|         public delegate Thing ThingConstructor(byte networkId, byte thingId); |         public delegate Thing ThingConstructor(RemoteParticipant sender, byte networkId, byte thingId); | ||||||
|         public void Register(byte thingType, ThingConstructor constr) { |         public void Register(byte thingType, ThingConstructor constr) { | ||||||
|             thingMsgProcessors[thingType] = new Func<byte, byte, Thing>(constr); |             thingMsgProcessors[thingType] = new Func<RemoteParticipant, byte, byte, Thing>(constr); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public void Register<ThingClass>(Thing.Type thingType) where ThingClass : Thing { |         public void Register<ThingClass>(Thing.Type thingType) where ThingClass : Thing { | ||||||
| @ -97,8 +97,8 @@ namespace Passer.RoboidControl { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public void Register<ThingClass>(byte thingType) where ThingClass : Thing { |         public void Register<ThingClass>(byte thingType) where ThingClass : Thing { | ||||||
|             thingMsgProcessors[thingType] = (byte networkId, byte thingId) => |             thingMsgProcessors[thingType] = (RemoteParticipant sender, byte networkId, byte thingId) => | ||||||
|                 Activator.CreateInstance(typeof(ThingClass), networkId, thingId) as ThingClass; |                 Activator.CreateInstance(typeof(ThingClass), sender, networkId, thingId) as ThingClass; | ||||||
|             Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); |             Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,4 +1,3 @@ | |||||||
| #nullable enable |  | ||||||
| using System; | using System; | ||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -9,23 +9,25 @@ namespace Passer.RoboidControl { | |||||||
|     /// </summary> |     /// </summary> | ||||||
|     public class SiteServer : Participant { |     public class SiteServer : Participant { | ||||||
| 
 | 
 | ||||||
|  |         public SiteServer(int port = 7681) : this("0.0.0.0", port) {} | ||||||
|  | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Create a new site server |         /// Create a new site server | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         /// <param name="port"></param> |         /// <param name="port"></param> | ||||||
|         public SiteServer(int port = 7681) { |         public SiteServer(string ipAddress = "0.0.0.0", int port = 7681) : base() { | ||||||
|             this.name = "Site Server"; |             this.name = "Site Server"; | ||||||
| 
 | 
 | ||||||
|             this.ipAddress = "0.0.0.0"; |             this.ipAddress = ipAddress; | ||||||
|             this.port = port; |             this.port = port; | ||||||
| 
 | 
 | ||||||
|             //this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending |             this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending | ||||||
| 
 | 
 | ||||||
|             Console.Write($"Prepare receive on port {port}"); |             Console.Write($"Prepare receive on port {port}"); | ||||||
|             this.udpClient = new UdpClient(port); // for receiving |             this.udpClient = new UdpClient(port); // for receiving | ||||||
|             this.udpClient.BeginReceive( |             this.udpClient.BeginReceive( | ||||||
|                 new AsyncCallback(result => ReceiveUDP(result)), |                 new AsyncCallback(result => ReceiveUDP(result)), | ||||||
|                 new Tuple<UdpClient, IPEndPoint>(this.udpClient, new IPEndPoint(IPAddress.Any, port))); |                 new Tuple<UdpClient, IPEndPoint>(this.udpClient, new(IPAddress.Any, port))); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
| @ -48,16 +50,19 @@ namespace Passer.RoboidControl { | |||||||
|         protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { } |         protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { } | ||||||
| 
 | 
 | ||||||
|         protected override void Process(RemoteParticipant sender, ThingMsg msg) { |         protected override void Process(RemoteParticipant sender, ThingMsg msg) { | ||||||
|             //Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}]"); |             Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}]"); | ||||||
|             Thing thing = sender.Get(msg.networkId, msg.thingId); |             Thing thing = sender.Get(msg.networkId, msg.thingId); | ||||||
|             if (thing == null) { |             if (thing == null) { | ||||||
|                 Thing newThing = null; |                 Thing newThing = null; | ||||||
|                 if (thingMsgProcessors.TryGetValue(msg.thingType, out Func<byte, byte, Thing> value)) { |                 if (thingMsgProcessors.TryGetValue(msg.thingType, out Func<RemoteParticipant, byte, byte, Thing> value)) { | ||||||
|  |                     Console.WriteLine("Found thing message processor"); | ||||||
|                     if (value != null) |                     if (value != null) | ||||||
|                         newThing = value(msg.networkId, msg.thingId); |                         newThing = value(sender, msg.networkId, msg.thingId); | ||||||
|                 } |                 } | ||||||
|                 if (newThing == null) |                 if (newThing == null) { | ||||||
|                     newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); |                     newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); | ||||||
|  |                     Console.WriteLine("Created generic new core thing"); | ||||||
|  |                 } | ||||||
| 
 | 
 | ||||||
|                 sender.Add(newThing); |                 sender.Add(newThing); | ||||||
|             } |             } | ||||||
|  | |||||||
							
								
								
									
										7
									
								
								Thing.cs
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								Thing.cs
									
									
									
									
									
								
							| @ -228,7 +228,12 @@ namespace Passer.RoboidControl { | |||||||
|         /// Function which can be used to create components in external engines. |         /// Function which can be used to create components in external engines. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         /// Currently this is used to create GameObjects in Unity |         /// Currently this is used to create GameObjects in Unity | ||||||
|         public virtual void CreateComponent() { } |         public virtual void CreateComponent() { | ||||||
|  | #if UNITY_5_3_OR_NEWER             | ||||||
|  |             this.component = Unity.Thing.Create(this); | ||||||
|  |             this.component.core = this; | ||||||
|  | #endif             | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         #endregion Init |         #endregion Init | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -3,7 +3,7 @@ using System.IO; | |||||||
| using System.Text; | using System.Text; | ||||||
| using UnityEngine; | using UnityEngine; | ||||||
| 
 | 
 | ||||||
| namespace Passer.Control.Unity { | namespace Passer.RoboidControl.Unity { | ||||||
| 
 | 
 | ||||||
|     public class UnityLogWriter : TextWriter { |     public class UnityLogWriter : TextWriter { | ||||||
|         public override void Write(char value) { |         public override void Write(char value) { | ||||||
|  | |||||||
| @ -11,7 +11,7 @@ namespace Passer.RoboidControl.Unity { | |||||||
|         public Queue<RoboidControl.Thing> thingQueue = new(); |         public Queue<RoboidControl.Thing> thingQueue = new(); | ||||||
| 
 | 
 | ||||||
|         protected virtual void Awake() { |         protected virtual void Awake() { | ||||||
|             //Console.SetOut(new UnityLogWriter()); |             Console.SetOut(new UnityLogWriter()); | ||||||
| 
 | 
 | ||||||
|             site = new(7681); |             site = new(7681); | ||||||
|             RoboidControl.Thing.OnNewThing += HandleNewThing; |             RoboidControl.Thing.OnNewThing += HandleNewThing; | ||||||
| @ -22,6 +22,7 @@ namespace Passer.RoboidControl.Unity { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public void HandleNewThing(RoboidControl.Thing thing) { |         public void HandleNewThing(RoboidControl.Thing thing) { | ||||||
|  |             Debug.Log("Handle New thing event"); | ||||||
|             site.Add(thing, false); |             site.Add(thing, false); | ||||||
|             thingQueue.Enqueue(thing); |             thingQueue.Enqueue(thing); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ namespace Passer.RoboidControl.Unity { | |||||||
|         /// The core C# thing |         /// The core C# thing | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         [field: SerializeField] |         [field: SerializeField] | ||||||
|         public RoboidControl.Thing core {get; set; } |         public RoboidControl.Thing core { get; set; } | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Set the core C# thing |         /// Set the core C# thing | ||||||
| @ -29,6 +29,23 @@ namespace Passer.RoboidControl.Unity { | |||||||
|             siteServer.site.Add(thing); |             siteServer.site.Add(thing); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         public static Thing Create(RoboidControl.Thing core) { | ||||||
|  |             Debug.Log("Creating new Unity thing"); | ||||||
|  |             GameObject gameObj = string.IsNullOrEmpty(core.name) ? | ||||||
|  |                 new("Thing") : | ||||||
|  |                 new(core.name); | ||||||
|  |             Thing component = gameObj.AddComponent<Thing>(); | ||||||
|  | 
 | ||||||
|  |             component.core = core; | ||||||
|  |             if (core.parent != null && core.parent.component != null) | ||||||
|  |                 gameObj.transform.SetParent(core.parent.component.transform, false); | ||||||
|  | 
 | ||||||
|  |             if (core.position != null) | ||||||
|  |                 gameObj.transform.localPosition = core.position.ToVector3(); | ||||||
|  | 
 | ||||||
|  |             return component; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Update the Unity representation |         /// Update the Unity representation | ||||||
|         /// </summary> |         /// </summary> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Pascal Serrarens
						Pascal Serrarens