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); }
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; }
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); }
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()); } }
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()); }
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_); } }
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); }); }
protected void ConnectAsync(OverlayEndpoint addr, Node.DisconnectProcessor dp) { Host.ConnectAsync(addr, dp); }
protected void MessageClient(OverlayEndpoint addr, MessageType mt, params object[] arg) { Host.TryConnectAsync(addr, ProcessClientDisconnect); Host.SendMessage(addr, mt, arg); }
public static void SendMessage(this OverlayHost host, OverlayEndpoint remote, MessageType mt, params object[] objs) { host.SendMessage(remote, new GameMessage(mt, objs)); }
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); } }); }
void OnNewPlayerRequest(Guid playerId, OverlayEndpoint playerClient) { MyAssert.Assert(!players.ContainsKey(playerId)); NewPlayerProcess(playerClient.addr, playerId, 0, new PlayerData()); }