Ejemplo n.º 1
0
        public Agent(AgentConfiguration agentConfiguration)
        {
            logger.Info("[Agent] Agent created");

            var teamId = agentConfiguration.TeamID.ToLower() == "red" ? TeamId.Red : TeamId.Blue;

            StartGameComponent         = new StartGameComponent(this, teamId);
            AgentInformationsComponent = new AgentInformationsComponent(this);
            AgentConfiguration         = agentConfiguration;
            NetworkComponent           = new ClientNetworkComponent(agentConfiguration.CsIP, agentConfiguration.CsPort);
            Piece          = null;
            WaitingPlayers = new List <int>();
            switch (agentConfiguration.Strategy)
            {
            case winningStrategy:
                strategy = new WinningStrategy();
                break;

            case doNothingStrategy:
                strategy = new DoNothingStrategy();
                break;

            default:
                strategy = new SimpleStrategy();
                break;
            }
            injectedMessages = new List <BaseMessage>();
            AgentState       = AgentState.Created;
            ProcessMessages  = new ProcessMessages(this);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new instance for the given component, with the given serializer.
        /// All methods with the <see cref="NetworkRpcAttribute"/> are registered.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="serializer"></param>
        public RpcHandler(INetworkComponent context, ISerializer serializer)
        {
            _context = context;

            if (!serializer.IsTypeRegistered(typeof(RpcCall)))
            {
                serializer.RegisterCustomType(DefaultTypes.Rpc, new RpcCallSerializer(serializer));
            }

            InitializeRpcs();
        }
Ejemplo n.º 3
0
        public void ConnectToCommunicationServer(INetworkComponent mockedNetworkComponent = null)
        {
            NetworkComponent = mockedNetworkComponent != null
                ? mockedNetworkComponent
                : new ClientNetworkComponent(Configuration.CsIP, Configuration.CsPort);

            if (!NetworkComponent.Connect(ClientType.GameMaster))
            {
                throw new ApplicationException("Unable to connect to CS");
            }

            Logger.Get().Info("[GM] Connected to Communication Server");
            state = GameMasterState.ConnectingAgents;
            currentMessageProcessor = ConnectionLogic;
        }
Ejemplo n.º 4
0
        internal virtual void RegisterNetworkComponent(INetworkComponent component)
        {
            component.Entity = this;
            if (_components.ContainsValue(component))
            {
                throw new InvalidOperationException("The component was already registered to this entity.");
            }

            if (_components.ContainsKey(component.Id))
            {
                throw new InvalidOperationException($"A component with ID {component.Id} was already registered to this entity.");
            }

            _components[component.Id]      = component;
            _componentCaches[component.Id] = new MemoryStream(ComponentCacheCapacity);
            (component as IRegistrationCallbacks)?.OnRegister();
        }
Ejemplo n.º 5
0
        public void RegisterComponent(int id, INetworkComponent instance)
        {
            if (m_NetworkComponents.ContainsKey(id))
            {
                var previousInstance = m_NetworkComponents[id];

                if (!previousInstance.TryGetTarget(out _))
                {
                    m_NetworkComponents.Remove(id);
                }
                else
                {
                    throw new Exception("Component already registered: " + id);
                }
            }

            m_NetworkComponents.Add(id, new WeakReference <INetworkComponent>(instance));
        }
Ejemplo n.º 6
0
        public NetworkContext RegisterComponent(INetworkComponent component)
        {
            INetworkObject networkObject = null;

            if (component is INetworkObject)
            {
                networkObject = component as INetworkObject;
            }
            else
            {
                foreach (var item in (component as MonoBehaviour).GetComponentsInParent <MonoBehaviour>()) // search up
                {
                    if (item is INetworkObject)
                    {
                        networkObject = item as INetworkObject;
                        break;
                    }
                }
            }

            actions.Add((Action)(() => // this may be called while iterating over objectproperties, so register it to execute when outside of the iterator
            {
                if (!objectProperties.ContainsKey(networkObject))
                {
                    objectProperties.Add(networkObject, new ObjectProperties()
                    {
                        identity = networkObject,
                        scene = this,
                    });
                }
                objectProperties[networkObject].components[GetComponentId(component)] = component;
            }));

            NetworkContext context = new NetworkContext();

            context.scene         = this;
            context.networkObject = networkObject;
            context.componentId   = GetComponentId(component);

            return(context);
        }
Ejemplo n.º 7
0
        public NetworkContext RegisterComponent(INetworkComponent component)
        {
            INetworkObject networkObject = null;

            if (component is INetworkObject)
            {
                networkObject = component as INetworkObject;
            }
            else
            {
                foreach (var item in (component as MonoBehaviour).GetComponentsInParent <MonoBehaviour>()) // search up
                {
                    if (item is INetworkObject)
                    {
                        networkObject = item as INetworkObject;
                        break;
                    }
                }
            }

            if (!objectProperties.ContainsKey(networkObject))
            {
                objectProperties.Add(networkObject, new ObjectProperties()
                {
                    identity = networkObject,
                    scene    = this,
                });
            }

            objectProperties[networkObject].components[GetComponentId(component)] = component;

            NetworkContext context = new NetworkContext();

            context.scene         = this;
            context.networkObject = networkObject;
            context.componentId   = GetComponentId(component);
            context.component     = component;

            return(context);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// This checks all connections for messages and fans them out into the individual recieve queues
        /// </summary>
        public void ReceiveConnectionMessages()
        {
            ReferenceCountedMessage m;

            foreach (var c in connections)
            {
                do
                {
                    m = c.Receive();
                    if (m != null)
                    {
                        try
                        {
                            var sgbmessage = new ReferenceCountedSceneGraphMessage(m);

                            matching.Clear();
                            foreach (var item in objectProperties)
                            {
                                if (item.Key.Id == sgbmessage.objectid)
                                {
                                    matching.Add(item.Value);
                                }
                            }

                            foreach (var item in matching)
                            {
                                INetworkComponent component = null;

                                try
                                {
                                    component = item.components[sgbmessage.componentid];
                                }
                                catch (KeyNotFoundException)
                                {
                                    continue;
                                }

                                try
                                {
                                    //     Profiler.BeginSample("Component Message Processing " + component.ToString());
                                    component.ProcessMessage(sgbmessage);
                                }
                                catch (MissingReferenceException e)
                                {
                                    if (component is UnityEngine.Object)
                                    {
                                        if (!(component as UnityEngine.Object))
                                        {
                                            item.components.Remove(sgbmessage.componentid);
                                            return;
                                        }
                                    }

                                    throw e;
                                }
                                // finally
                                // {
                                //     Profiler.EndSample();
                                // }
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.LogException(e); // because otherwise this will not be visible to the main thread
                        }
                        finally
                        {
                            m.Release();
                        }
                    }
                    else
                    {
                        break;
                    }
                } while (true);
            }
        }
Ejemplo n.º 9
0
 public static ushort GetComponentId(INetworkComponent component)
 {
     return(component.GetType().GetComponentId());
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Add a networked component to the network client. Once registered the object can be moved around.
 /// </summary>
 public static NetworkContext Register(INetworkComponent component)
 {
     return(FindNetworkScene(component as MonoBehaviour).RegisterComponent(component));
 }
Ejemplo n.º 11
0
 public virtual bool RemoveNetworkComponent(INetworkComponent component)
 {
     return(RemoveNetworkComponent(component.Id));
 }
Ejemplo n.º 12
0
 public virtual void AddNetworkComponent(INetworkComponent component)
 {
     RegisterNetworkComponent(component);
     _componentAddSubject.OnNext(component);
 }
Ejemplo n.º 13
0
        /// <summary>
        /// This checks all connections for messages and fans them out into the individual recieve queues
        /// </summary>
        public void ReceiveConnectionMessages()
        {
            ReferenceCountedMessage m;

            foreach (var c in connections)
            {
                do
                {
                    m = c.Receive();
                    if (m != null)
                    {
                        try
                        {
                            var sgbmessage = new ReferenceCountedSceneGraphMessage(m);
                            foreach (var item in objectProperties)
                            {
                                // Checks if the Unity object has been destroyed. When Unity objects are destroyed, the managed reference can remain around, but the object is invalid. Unity overrides the truth check to indicate this.

                                if (item.Key is UnityEngine.Object)
                                {
                                    if (!(item.Key as UnityEngine.Object))
                                    {
                                        actions.Add(() =>
                                        {
                                            try
                                            {
                                                objectProperties.Remove(item.Key);
                                            }
                                            catch (KeyNotFoundException)
                                            {
                                            }
                                        });
                                        continue;
                                    }
                                }

                                if (item.Key.Id == sgbmessage.objectid)
                                {
                                    INetworkComponent component = null;

                                    try
                                    {
                                        component = item.Value.components[sgbmessage.componentid];
                                    }
                                    catch (KeyNotFoundException)
                                    {
                                        continue;
                                    }

                                    try
                                    {
                                        Profiler.BeginSample("Component Message Processing " + component.ToString());
                                        component.ProcessMessage(sgbmessage);
                                    }
                                    catch (MissingReferenceException e)
                                    {
                                        if (component is UnityEngine.Object)
                                        {
                                            if (!(component as UnityEngine.Object))
                                            {
                                                item.Value.components.Remove(sgbmessage.componentid);
                                                return;
                                            }
                                        }

                                        throw e;
                                    }
                                    finally
                                    {
                                        Profiler.EndSample();
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.LogException(e); // because otherwise this will not be visible to the main thread
                        }
                        finally
                        {
                            m.Release();
                        }
                    }
                    else
                    {
                        break;
                    }
                } while (true);
            }
        }
Ejemplo n.º 14
0
 public bool RemoveNetworkComponent(INetworkComponent component)
 {
     return(_entity.RemoveNetworkComponent(component));
 }
Ejemplo n.º 15
0
 public void AddNetworkComponent(INetworkComponent component)
 {
     _entity.AddNetworkComponent(component);
 }