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 | ||||
|         /// </summary> | ||||
|         /// <param name="port">The port number on which to communicate</param> | ||||
|         public Participant(int port) : this() { | ||||
|             this.port = port; | ||||
|         } | ||||
|         // public Participant(int port) : this() { | ||||
|         //     this.port = port; | ||||
|         // } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// Create a new participant for a site at the given address and port | ||||
| @ -85,11 +85,11 @@ namespace Passer.RoboidControl { | ||||
|             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) { | ||||
|             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 { | ||||
| @ -97,8 +97,8 @@ namespace Passer.RoboidControl { | ||||
|         } | ||||
| 
 | ||||
|         public void Register<ThingClass>(byte thingType) where ThingClass : Thing { | ||||
|             thingMsgProcessors[thingType] = (byte networkId, byte thingId) => | ||||
|                 Activator.CreateInstance(typeof(ThingClass), networkId, thingId) as ThingClass; | ||||
|             thingMsgProcessors[thingType] = (RemoteParticipant sender, byte networkId, byte thingId) => | ||||
|                 Activator.CreateInstance(typeof(ThingClass), sender, networkId, thingId) as ThingClass; | ||||
|             Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); | ||||
|         } | ||||
| 
 | ||||
|  | ||||
| @ -1,4 +1,3 @@ | ||||
| #nullable enable | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| 
 | ||||
|  | ||||
| @ -9,23 +9,25 @@ namespace Passer.RoboidControl { | ||||
|     /// </summary> | ||||
|     public class SiteServer : Participant { | ||||
| 
 | ||||
|         public SiteServer(int port = 7681) : this("0.0.0.0", port) {} | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// Create a new site server | ||||
|         /// </summary> | ||||
|         /// <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.ipAddress = "0.0.0.0"; | ||||
|             this.ipAddress = ipAddress; | ||||
|             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}"); | ||||
|             this.udpClient = new UdpClient(port); // for receiving | ||||
|             this.udpClient.BeginReceive( | ||||
|                 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> | ||||
| @ -48,16 +50,19 @@ namespace Passer.RoboidControl { | ||||
|         protected override void Process(RemoteParticipant sender, NetworkIdMsg 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); | ||||
|             if (thing == 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) | ||||
|                         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); | ||||
|                     Console.WriteLine("Created generic new core thing"); | ||||
|                 } | ||||
| 
 | ||||
|                 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. | ||||
|         /// </summary> | ||||
|         /// 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 | ||||
| 
 | ||||
|  | ||||
| @ -3,7 +3,7 @@ using System.IO; | ||||
| using System.Text; | ||||
| using UnityEngine; | ||||
| 
 | ||||
| namespace Passer.Control.Unity { | ||||
| namespace Passer.RoboidControl.Unity { | ||||
| 
 | ||||
|     public class UnityLogWriter : TextWriter { | ||||
|         public override void Write(char value) { | ||||
|  | ||||
| @ -11,7 +11,7 @@ namespace Passer.RoboidControl.Unity { | ||||
|         public Queue<RoboidControl.Thing> thingQueue = new(); | ||||
| 
 | ||||
|         protected virtual void Awake() { | ||||
|             //Console.SetOut(new UnityLogWriter()); | ||||
|             Console.SetOut(new UnityLogWriter()); | ||||
| 
 | ||||
|             site = new(7681); | ||||
|             RoboidControl.Thing.OnNewThing += HandleNewThing; | ||||
| @ -22,6 +22,7 @@ namespace Passer.RoboidControl.Unity { | ||||
|         } | ||||
| 
 | ||||
|         public void HandleNewThing(RoboidControl.Thing thing) { | ||||
|             Debug.Log("Handle New thing event"); | ||||
|             site.Add(thing, false); | ||||
|             thingQueue.Enqueue(thing); | ||||
|         } | ||||
|  | ||||
| @ -12,7 +12,7 @@ namespace Passer.RoboidControl.Unity { | ||||
|         /// The core C# thing | ||||
|         /// </summary> | ||||
|         [field: SerializeField] | ||||
|         public RoboidControl.Thing core {get; set; } | ||||
|         public RoboidControl.Thing core { get; set; } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// Set the core C# thing | ||||
| @ -29,6 +29,23 @@ namespace Passer.RoboidControl.Unity { | ||||
|             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> | ||||
|         /// Update the Unity representation | ||||
|         /// </summary> | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Pascal Serrarens
						Pascal Serrarens