Esempio n. 1
0
        // initialize
        public void Init(AgentUtils <OBJECT> .IAgentsListener al, ActorUtils.IActorsListener pl)
        {
            _agent_manager.Init(al);
            _actor_manager.Init(pl);

            _gate = new PlayerGate(this);
            if (false == _gate.Connect
                    (Constant.ADRESS_LOCALHOST, Constant.GATE_PLAYER_PORT
                    , Constant.MAX_READ, Constant.MAX_WRITE, Constant.MAX_PACKAGE))
            {
                Debug.LogErrorFormat("Can not connect to gate.");
            }
        }
Esempio n. 2
0
        public void MessageSend(PlayerGate gate)
        {
            /* this lock is not necessary */            //lock( _message )
            {
                // build message: type
                _message.Type = PlayerToGate.Types.ETYPE.Message;

                // magic number
                _message.MagicNumber = Constant.PLAYER_TO_GATE_MAGIC_NUMBER;

                // entering and exiting region
                lock ( _region_actor )
                {
                    foreach (KeyValuePair <long, ActorUtils.RegionActorPair> kvp in _region_actor)
                    {
                        ActorUtils.RegionActorPair rap = kvp.Value;

                        // if exit and enter target are the same, skip this actor
                        if (null != rap.src && null != rap.dest)
                        {
                            if (rap.src.Equals(rap.dest))
                            {
                                continue;
                            }
                        }

                        // exiting
                        if (null != rap.src)
                        {
                            IdList ail = CollectionUtils.FindOrInsert(_message.ExitedRegions, rap.src, _message.ExitedActors, new IdList());
                            ail.Ids.Add(rap.actor.Id);

                            ConsoleOutput.Trace("Actor exits: " + rap.src.Index);
                        }

                        // entering
                        if (null != rap.dest)
                        {
                            ActorList al = CollectionUtils.FindOrInsert(_message.EnteredRegions, rap.dest, _message.EnteredActors, new ActorList());
                            al.Actors.Add(rap.actor);

                            ConsoleOutput.Trace("Actor enters: " + rap.dest.Index);
                        }
                    }

                    // clear buffer
                    _region_actor.Clear();
                }

                // write message
                byte[]            buffer = new byte[_message.CalculateSize()];
                CodedOutputStream os     = new CodedOutputStream(buffer);
                _message.WriteTo(os);

                gate.Write(buffer);

                // new message
                _message = new PlayerToGate();

                // log
                ConsoleOutput.Trace("SendMessage " + buffer.Length);
            }
        }