Ejemplo n.º 1
0
        static public RemoteAction Send(OverlayHost myHost, OverlayEndpoint remoteHost, Node.DisconnectProcessor dp,
                                        MessageType mt, params object[] args)
        {
            RemoteAction ra = new RemoteAction();

            ra.remoteHost = remoteHost;

            bool assigned = false;

            for (int i = 0; i < args.Length; ++i)
            {
                if (args[i].GetType() == typeof(RemoteActionIdInject))
                {
                    args[i]  = ra.Id;       // id is injected at first RemoteActionIdInject argument
                    assigned = true;
                    break;
                }
            }

            if (!assigned)
            {
                List <object> lst = new List <object>();
                lst.Add(ra.Id);             // or in front if there isn't one
                lst.AddRange(args);
                args = lst.ToArray();
            }

            if (dp != null)
            {
                myHost.TryConnectAsync(remoteHost, dp);
            }
            myHost.SendMessage(remoteHost, mt, args);

            return(ra);
        }
Ejemplo n.º 2
0
 public PlayerInfo(Guid id, OverlayEndpoint playerHost, OverlayEndpoint validatorHost, string name, int generation)
 {
     this.id            = id;
     this.playerHost    = playerHost;
     this.validatorHost = validatorHost;
     this.name          = name;
     this.generation    = generation;
 }
Ejemplo n.º 3
0
        public PlayerValidator(PlayerInfo info, GlobalHost globalHost, OverlayEndpoint serverHost, PlayerData playerData)
        {
            this.info       = info;
            this.serverHost = serverHost;
            this.playerData = playerData;

            Host = globalHost.NewHost(info.validatorHost.hostname, Game.Convert(AssignProcessor),
                                      BasicInfo.GenerateHandshake(NodeRole.PLAYER_VALIDATOR, info), Aggregator.longInactivityWait);

            SetConnectionHook(ProcessNewConnection);
        }
Ejemplo n.º 4
0
 protected override void ProcessClientMessage(MessageType mt, Stream stm, Node n)
 {
     if (mt == MessageType.SERVER_ADDRESS)
     {
         OverlayEndpoint host = Serializer.Deserialize <OverlayEndpoint>(stm);
         OnServerAddress(host);
     }
     else
     {
         throw new Exception("Client.ProcessClientMessage bad message type " + mt.ToString());
     }
 }
Ejemplo n.º 5
0
        public PlayerAgent(PlayerInfo info, GlobalHost globalHost, OverlayEndpoint serverHost, Client myClient)
        {
            this.Info       = info;
            this.serverHost = serverHost;
            this.myClient   = myClient;
            this.Data       = null;

            Host = globalHost.NewHost(info.playerHost.hostname, Game.Convert(AssignProcessor),
                                      BasicInfo.GenerateHandshake(NodeRole.PLAYER_AGENT, info), Aggregator.longInactivityWait);

            ConnectAsync(info.validatorHost, (di) => ProcessPlayerValidatorDisconnect(di, info));

            Log.Console("Player Agent {0}", info.GetShortInfo());
        }
Ejemplo n.º 6
0
        public void OnServerAddress(OverlayEndpoint serverHost_)
        {
            if (serverHost == null)
            {
                serverHost = serverHost_;

                Log.Console("Server at {0}", serverHost);

                ConnectAsync(serverHost, ProcessServerDisconnect);
                Host.BroadcastGroup(Client.hostName, MessageType.SERVER_ADDRESS, serverHost);

                onServerReadyHook();
            }
            else
            {
                MyAssert.Assert(serverHost == serverHost_);
            }
        }
Ejemplo n.º 7
0
        void NewPlayerProcess(IPEndPoint clientAddr, Guid id, int generation, PlayerData pd)
        {
            MyAssert.Assert(!playerLocks.Contains(id));
            ManualLock <Guid> lck = new ManualLock <Guid>(playerLocks, id);

            if (!validatorPool.Any())
            {
                throw new Exception("no validators!");
            }

            string playerName = PlayerNameMap(playerCounter++);
            string fullName   = "player " + playerName + (generation == 0 ? "" : " (" + generation + ")");

            OverlayEndpoint validatorHost = new OverlayEndpoint(validatorPool.Random(n => r.Next(n)),
                                                                new OverlayHostName("validator " + fullName));

            OverlayEndpoint playerNewHost = new OverlayEndpoint(clientAddr, new OverlayHostName("agent " + fullName));

            OverlayEndpoint playerClient    = new OverlayEndpoint(clientAddr, Client.hostName);
            OverlayEndpoint validatorClient = new OverlayEndpoint(validatorHost.addr, Client.hostName);

            PlayerInfo playerInfo = new PlayerInfo(id, playerNewHost, validatorHost, playerName, generation);

            RemoteAction
            .Send(Host, validatorClient, ProcessClientDisconnect, MessageType.PLAYER_VALIDATOR_ASSIGN, playerInfo, pd)
            .Respond(remoteActions, lck, (res, stm) =>
            {
                if (playerInfo.generation == 0)
                {
                    MyAssert.Assert(!players.ContainsKey(playerInfo.id));
                    players.Add(playerInfo.id, playerInfo);
                }
                else
                {
                    MyAssert.Assert(players.ContainsKey(playerInfo.id));
                    players[playerInfo.id] = playerInfo;
                }

                MessageClient(playerClient, MessageType.NEW_PLAYER_REQUEST_SUCCESS, playerInfo);

                Log.Console("New player " + playerInfo.name + " validated by " + playerInfo.validatorHost.addr);
            });
        }
Ejemplo n.º 8
0
 protected void ConnectAsync(OverlayEndpoint addr, Node.DisconnectProcessor dp)
 {
     Host.ConnectAsync(addr, dp);
 }
Ejemplo n.º 9
0
 protected void MessageClient(OverlayEndpoint addr, MessageType mt, params object[] arg)
 {
     Host.TryConnectAsync(addr, ProcessClientDisconnect);
     Host.SendMessage(addr, mt, arg);
 }
Ejemplo n.º 10
0
 public static void SendMessage(this OverlayHost host, OverlayEndpoint remote, MessageType mt, params object[] objs)
 {
     host.SendMessage(remote, new GameMessage(mt, objs));
 }
Ejemplo n.º 11
0
        void OnNewWorldRequest(Point worldPos, WorldSerialized ser, int generation)
        {
            if (worlds.ContainsKey(worldPos))
            {
                Log.Dump(worldPos, "world alrady present");
                return;
            }

            if (!validatorPool.Any())
            {
                throw new Exception("no validators!");
            }

            ManualLock <Point> lck = new ManualLock <Point>(worldLocks, worldPos);

            if (!lck.Locked)
            {
                Log.Dump(worldPos, "can't work, locked");
                return;
            }

            string hostName = "host world " + worldPos;

            if (generation != 0)
            {
                hostName = hostName + " (" + generation + ")";
            }
            OverlayEndpoint validatorHost = new OverlayEndpoint(validatorPool.Random(n => r.Next(n)), new OverlayHostName(hostName));

            WorldInitializer init;
            WorldInfo        newWorld;
            bool             hasSpawn = false;

            if (ser == null)
            {
                WorldSeed seed = new WorldSeed(r.Next(), RandomColor(worldPos));

                if (serverSpawnDensity == 0)
                {
                    if (worldPos == Point.Zero)
                    {
                        hasSpawn = true;
                    }
                }
                else if ((worldPos.x % serverSpawnDensity == 0) && (worldPos.y % serverSpawnDensity == 0))
                {
                    hasSpawn = true;
                }

                newWorld = new WorldInfo(worldPos, validatorHost, generation, hasSpawn);
                init     = new WorldInitializer(newWorld, seed);
            }
            else
            {
                hasSpawn = ser.spawnPos.HasValue;
                newWorld = new WorldInfo(worldPos, validatorHost, generation, hasSpawn);
                init     = new WorldInitializer(newWorld, ser);
            }


            OverlayEndpoint validatorClient = new OverlayEndpoint(validatorHost.addr, Client.hostName);

            RemoteAction
            .Send(Host, validatorClient, ProcessClientDisconnect, MessageType.WORLD_VALIDATOR_ASSIGN, init)
            .Respond(remoteActions, lck, (res, stm) =>
            {
                if (res != Response.SUCCESS)
                {
                    throw new Exception(Log.StDump("unexpected", res));
                }

                //if (hasSpawn == true)
                //    spawnWorlds.Add(worldPos);

                worlds.Add(newWorld.position, newWorld);

                //Log.LogWriteLine("New world " + worldPos + " validated by " + validatorHost.addr);

                foreach (Point p in Point.SymmetricRange(Point.One))
                {
                    if (p == Point.Zero)
                    {
                        continue;
                    }

                    Point neighborPos = p + newWorld.position;

                    if (!worlds.ContainsKey(neighborPos))
                    {
                        continue;
                    }

                    WorldInfo neighborWorld = worlds[neighborPos];

                    MessageWorld(neighborWorld, MessageType.NEW_NEIGHBOR, newWorld);
                    MessageWorld(newWorld, MessageType.NEW_NEIGHBOR, neighborWorld);
                }
            });
        }
Ejemplo n.º 12
0
 void OnNewPlayerRequest(Guid playerId, OverlayEndpoint playerClient)
 {
     MyAssert.Assert(!players.ContainsKey(playerId));
     NewPlayerProcess(playerClient.addr, playerId, 0, new PlayerData());
 }