Example #1
0
        // own actor.
        // update the actor
        // and watch operations on the actor
        private void ActorEnter(Region region, long id, Dictionary <int, ByteString> properties)
        {
            /* this lock is not necessary */            //lock( _region_actor )
            {
                // find actor
                // if actor is found, update its properties and replace the dest region.
                // because before sending the message, the same actor may change region several times,
                // the latest dest region will be the true that the actor truely enters.
                ActorUtils.RegionActorPair rap = null;
                if (_region_actor.TryGetValue(id, out rap))
                {
                    // copy properties
                    ActorUtils.CopyProperties(properties, rap.actor.Ptypes, rap.actor.Pdatas);

                    // set new dest
                    // replace the region was set
                    rap.dest = region;
                }
                // else insert the actor.
                else
                {
                    rap       = new ActorUtils.RegionActorPair();
                    rap.actor = ActorUtils.CreateActor(id, properties);
                    rap.dest  = region;

                    _region_actor.Add(id, rap);
                }
            }
        }
Example #2
0
        // actor exit
        private void ActorExit(Region region, long id)
        {
            /* this lock is not necessary */            //lock( _region_actor )
            {
                // find actor
                // if the actor is found, only set null src region.
                // because before sending the message, the same actor may change region several times,
                // the first src regin should be the truely region that the actor exits from.
                ActorUtils.RegionActorPair rap = null;
                if (_region_actor.TryGetValue(id, out rap))
                {
                    // set old region
                    if (null == rap.src)
                    {
                        rap.src = region;
                    }
                }
                else
                {
                    rap       = new ActorUtils.RegionActorPair();
                    rap.actor = ActorUtils.CreateActor(id, null);
                    rap.src   = region;

                    _region_actor.Add(id, rap);
                }
            }
        }
Example #3
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);
            }
        }