public void GetPlayers(Guid clientId, Guid roomId, ServerEventHandler <Player[]> callback) { Error error = new Error(); Player[] players = new Player[0]; if (Room != null) { error.Code = StatusCode.OK; players = new Player[Room.Players.Count]; for (int i = 0; i < Room.Players.Count; ++i) { Guid playerGuid = Room.Players[i]; players[i] = m_players[playerGuid]; } } else { error.Code = StatusCode.NotFound; error.Message = string.Format("Room {0} not found", roomId); } if (m_lag == 0) { callback(error, players); } else { Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, players)); } }
public void ReadyToPlay(Guid clientId, ServerEventHandler callback) { if (!m_clientIdToPlayers.ContainsKey(clientId)) { if (m_room != null) { if (m_room.CreatorClientId != clientId && m_room.Mode != GameMode.Replay) { Error error = new Error(StatusCode.NotRegistered); callback(error); return; } } else { Error error = new Error(StatusCode.NotRegistered); callback(error); return; } } if (m_room == null || m_room.Mode != GameMode.Replay) { if (!m_readyToPlayClients.Contains(clientId)) { m_readyToPlayClients.Add(clientId); } } TryToInitEngine(callback); }
void BroadcastEvent(object sender, ServerEventArgs e, ServerEventHandler handler) { foreach (ServerEventHandler item in handler.GetInvocationList()) { item.BeginInvoke(sender, e, null, null); } }
public void GetRoom(Guid clientId, Guid roomId, ServerEventHandler <Room> callback) { Error error = new Error(); Room room; if (Room != null && Room.Id == roomId) { error.Code = StatusCode.OK; room = Room; } else { error.Code = StatusCode.NotFound; room = null; } if (m_lag == 0) { callback(error, room); } else { Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, room)); } }
public void Pong(Guid clientId, ServerEventHandler callback) { Error error = new Error(StatusCode.OK); if (!m_clientIdToPlayers.ContainsKey(clientId)) { error.Code = StatusCode.NotRegistered; callback(error); return; } callback(error); if (m_botControlManager != null) { m_botControlManager.Ping(clientId); } RTTInfo rttInfo = m_pingTimer.Pong(clientId, () => OnPingPongCompleted(error, clientId)); m_pingTimer.Ping(clientId); if (Ping != null) { m_pingArgs.Arg = rttInfo; m_pingArgs.Targets = new[] { clientId }; Ping(error, m_pingArgs); } }
public void SubmitResponse(Guid clientId, ClientRequest response, ServerEventHandler <ClientRequest> callback) { Error error = new Error(); error.Code = StatusCode.OK; if (!m_initialized) { error.Code = StatusCode.NotAllowed; error.Message = "Match is not initialized"; } else if (!enabled) { error.Code = StatusCode.Paused; error.Message = "Match is paused"; } else { Cmd cmd = response.Cmd; if (cmd != null && cmd.Code != CmdCode.DenyBotCtrl && cmd.Code != CmdCode.GrantBotCtrl) { m_engine.SubmitResponse(response); } } if (m_lag == 0) { callback(error, response); } else { m_job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, response)); } }
public void SendMessage(Guid clientId, ChatMessage message, ServerEventHandler <Guid> callback) { Error error = new Error(); if (m_lag == 0) { if (ChatMessage != null) { ChatMessage(error, message); } callback(error, message.MessageId); } else { m_job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => { if (ChatMessage != null) { ChatMessage(error, message); } callback(error, message.MessageId); }); } }
public void SubmitResponse(Guid clientId, ClientRequest response, ServerEventHandler <ClientRequest> callback) { Error error = new Error(StatusCode.OK); if (!m_clientIdToPlayers.ContainsKey(clientId)) { error.Code = StatusCode.NotRegistered; callback(error, response); return; } if (!m_initialized) { error.Code = StatusCode.NotAllowed; error.Message = "Match is not initialized"; } else if (!enabled) { error.Code = StatusCode.Paused; error.Message = "Match is paused"; } else { m_engine.SubmitResponse(response); } callback(error, response); }
public void Pong(Guid clientId, ServerEventHandler callback) { Error error = new Error(); error.Code = StatusCode.OK; m_job.Submit(() => { Thread.Sleep(400); return(null); }, result => { callback(error); RTTInfo rttInfo = m_pingTimer.Pong(clientId, () => { OnPong(error); }); m_pingTimer.Ping(clientId); if (Ping != null) { Ping(error, rttInfo); } }); }
public void DestroyRoom(Guid clientId, Guid roomId, ServerEventHandler <Guid> callback) { Error error = new Error(); error.Code = StatusCode.OK; if (Room != null) { for (int i = 0; i < Room.Players.Count; ++i) { Guid playerId = Room.Players[i]; Player player = m_players[playerId]; if (player.IsBot) { m_players.Remove(playerId); } } } Room = null; if (m_lag == 0) { callback(error, roomId); } else { Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, roomId)); } }
private void DownloadMapDataById(Guid mapId, ServerEventHandler <byte[]> callback) { byte[] mapData = new byte[0]; Error error = new Error(); string dataPath = m_persistentDataPath + "/Maps/"; string filePath = dataPath + mapId + ".data"; if (!File.Exists(filePath)) { error.Code = StatusCode.NotFound; callback(error, mapData); } else { error.Code = StatusCode.OK; try { mapData = File.ReadAllBytes(filePath); } catch (Exception e) { error.Code = StatusCode.UnhandledException; error.Message = e.Message; } callback(error, mapData); } }
/// <summary> /// Use new ThreadPool thread for each new client connection. /// </summary> public override async Task ServeAsync(CancellationToken cancellationToken) { ServerCancellationToken = cancellationToken; try { try { ServerTransport.Listen(); } catch (TTransportException ttx) { LogError("Error, could not listen on ServerTransport: " + ttx); return; } //Fire the preServe server event when server is up but before any client connections if (ServerEventHandler != null) { await ServerEventHandler.PreServeAsync(cancellationToken); } while (!(stop || ServerCancellationToken.IsCancellationRequested)) { try { TTransport client = await ServerTransport.AcceptAsync(cancellationToken); _ = Task.Run(async() => await ExecuteAsync(client), cancellationToken); // intentionally ignoring retval } catch (TaskCanceledException) { stop = true; } catch (TTransportException ttx) { if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted) { LogError(ttx.ToString()); } } } if (stop) { try { ServerTransport.Close(); } catch (TTransportException ttx) { LogError("TServerTransport failed on close: " + ttx.Message); } stop = false; } } finally { ServerCancellationToken = default; } }
public void UnregisterClient(Guid clientId, ServerEventHandler callback) { Error error = new Error(StatusCode.OK); if (m_initialized) { m_readyToBeUnregisteredClients.Add(new DisconnectedClient(clientId, m_time.Time + GameConstants.WaitForReconnectTimeout)); BeginUnregisterClient(clientId); callback(error); } else { BeginUnregisterClient(clientId); UnregisterClient(clientId); m_pingTimer.OnClientDisconnected(clientId, () => OnPingPongCompleted(error, clientId)); if (!HasError(error)) { TryToInitEngine(callback); } else { callback(error); } } }
public void RegisterClient(Guid clientId, ServerEventHandler callback) { if (!m_registeredClients.Contains(clientId)) { m_registeredClients.Add(clientId); } else { int index = m_readyToBeUnregisteredClients.IndexOf(new DisconnectedClient(clientId, 0)); if (index >= 0) { m_readyToBeUnregisteredClients.RemoveAt(index); Dictionary <Guid, Player> disconnectedPlayers; if (m_clientIdToPlayers.TryGetValue(clientId, out disconnectedPlayers)) { foreach (Guid playerId in disconnectedPlayers.Keys) { int playerIndex = m_room.Players.IndexOf(playerId); m_botControlManager.Disconnect(playerIndex); Player player = m_players[playerId]; m_botControlManager.Connect(clientId, player, playerIndex); } if (!m_botControlManager.HasActiveClient) { m_botControlManager.ActivateNext(); } } } } callback(new Error(StatusCode.OK)); }
public void DestroyBot(Guid clientId, Guid botId, ServerEventHandler <Guid, Room> callback) { Error error = new Error(); Room room = null; if (Room != null) { room = Room; error.Code = StatusCode.OK; Room.Players.Remove(botId); Room.ReadyToLaunchPlayers.Remove(botId); m_players.Remove(botId); } else { error.Code = StatusCode.NotFound; error.Message = "Room was not found"; } if (m_lag == 0) { callback(error, botId, room); } else { Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, botId, room)); } }
public void Logoff(Guid clientId, Guid[] playerIds, ServerEventHandler <Guid[]> callback) { RemoteCall rpc = new RemoteCall( RemoteCall.Proc.LogoffMultiple, clientId, RemoteArg.Create(playerIds)); Call(rpc, (error, result) => { playerIds = result.Get <Guid[]>(0); if (!HasError(error)) { if (playerIds != null) { for (int i = 0; i < playerIds.Length; ++i) { m_localPlayers.Remove(playerIds[i]); } } } callback(error, playerIds); }); }
public void Logoff(Guid clientId, Guid[] playerIds, ServerEventHandler <Guid[]> callback) { Error error = new Error(); List <Guid> loggedOffPlayers = new List <Guid>(); for (int i = 0; i < playerIds.Length; i++) { if (m_loggedInPlayers.Contains(playerIds[i])) { error.Code = StatusCode.OK; if (m_loggedInPlayers.Contains(playerIds[i])) { m_loggedInPlayers.Remove(playerIds[i]); loggedOffPlayers.Add(playerIds[i]); m_stats.PlayersCount--; } } } if (m_lag == 0) { callback(error, loggedOffPlayers.ToArray()); } else { Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, loggedOffPlayers.ToArray())); } }
public void CreateBots(Guid clientId, string[] botNames, BotType[] botTypes, ServerEventHandler <Guid[], Room> callback) //bot guids and room with bots { Error error = new Error(); Room room = null; Guid[] botIds = new Guid[botNames.Length]; if (Room != null) { room = Room; int expectedPlayersCount = room.Players.Count + botNames.Length; if (Room.MapInfo == null) { error.Code = StatusCode.NotFound; error.Message = string.Format("MapInfo for room {0} was not found", room.Id); } else { if (expectedPlayersCount > Room.MapInfo.MaxPlayers) { error.Code = StatusCode.TooMuchPlayersInRoom; } else { error.Code = StatusCode.OK; for (int i = 0; i < botNames.Length; ++i) { Guid botId = Guid.NewGuid(); Player bot = new Player { Id = botId, Name = botNames[i], BotType = botTypes[i] }; Room.Players.Add(botId); Room.ReadyToLaunchPlayers.Add(botId); m_players.Add(botId, bot); } } } } else { error.Code = StatusCode.NotFound; error.Message = "Room was not found"; } if (m_lag == 0) { callback(error, botIds, room); } else { Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, botIds, room)); } }
private void DownloadMapDataById(Guid mapId, ServerEventHandler <MapData> callback) { MapData mapData = null; Error error = new Error(); string dataPath = m_persistentDataPath + "/Maps/"; string filePath = dataPath + mapId + ".data"; if (!File.Exists(filePath)) { error.Code = StatusCode.NotFound; if (m_lag == 0) { callback(error, mapData); } else { Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, mapData)); } } else { Job.Submit(() => { error.Code = StatusCode.OK; ProtobufSerializer serializer = null; var pool = Dependencies.Serializer; try { if (pool != null) { serializer = pool.Acquire(); } byte[] mapDataBytes = File.ReadAllBytes(filePath); mapData = serializer.Deserialize <MapData>(mapDataBytes); } catch (Exception e) { error.Code = StatusCode.UnhandledException; error.Message = e.Message; } finally { if (serializer != null && pool != null) { pool.Release(serializer); } } return(null); }, result => { callback(error, mapData); }); } }
public void CreateBot(Guid clientId, string botName, BotType botType, ServerEventHandler <Guid, Room> callback) { if (m_instance != null && m_instance != this) { throw new InvalidOperationException(); } Error error = new Error(); Room room = null; Guid botId = Guid.Empty; if (Room != null) { room = Room; int expectedPlayersCount = room.Players.Count + 1; if (Room.MapInfo == null) { error.Code = StatusCode.NotFound; error.Message = string.Format("MapInfo for room {0} was not found", room.Id); } else { if (expectedPlayersCount > Room.MapInfo.MaxPlayers) { error.Code = StatusCode.TooMuchPlayersInRoom; } else { error.Code = StatusCode.OK; botId = Guid.NewGuid(); Player bot = new Player { Id = botId, Name = botName, BotType = botType }; Room.Players.Add(botId); Room.ReadyToLaunchPlayers.Add(botId); m_players.Add(botId, bot); } } } else { error.Code = StatusCode.NotFound; error.Message = "Room was not found"; } if (m_lag == 0) { callback(error, botId, room); } else { Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, botId, room)); } }
public void Pong(Guid clientId, ServerEventHandler callback) { RemoteCall rpc = new RemoteCall( RemoteCall.Proc.Pong, clientId); Call(rpc, (error, result) => callback(error)); }
public void GetReplay(Guid clientId, ServerEventHandler <ReplayData, Room> callback) { RemoteCall rpc = new RemoteCall( RemoteCall.Proc.GetReplay, clientId); Call(rpc, (error, result) => callback(error, result.Get <ReplayData>(0), result.Get <Room>(1))); }
public void GetPlayers(Guid clientId, ServerEventHandler <Player[]> callback) { RemoteCall rpc = new RemoteCall( RemoteCall.Proc.GetPlayers, clientId); Call(rpc, (error, result) => callback(error, result.Get <Player[]>(0))); }
public void Launch(Guid clientId, ServerEventHandler <string> callback) { RemoteCall rpc = new RemoteCall( RemoteCall.Proc.Launch, clientId); Call(rpc, (error, result) => callback(error, result.Get <string>(0))); }
public void GetStats(Guid clientId, ServerEventHandler <ServerStats> callback) { RemoteCall rpc = new RemoteCall( RemoteCall.Proc.GetStats, clientId); Call(rpc, (error, result) => callback(error, result.Get <ServerStats>(0))); }
public void RegisterClient(Guid clientId, ServerEventHandler callback) { RemoteCall rpc = new RemoteCall( RemoteCall.Proc.RegisterClient, clientId); Call(rpc, (error, result) => callback(error)); }
public ServerQTClient(BaseServerManager _manager, TcpClient _client, clientType _type) : base(_client, _type) { manager = _manager; eventHandler = new ServerEventHandler(this); onMessageRecieved += handleMessage; onMessageSent += handleSentMessage; }
/// <summary> /// Use new ThreadPool thread for each new client connection. /// </summary> public override async Task ServeAsync(CancellationToken cancellationToken) { ServerCancellationToken = cancellationToken; try { try { ServerTransport.Listen(); } catch (TTransportException ttx) { LogError("Error, could not listen on ServerTransport: " + ttx); return; } //Fire the preServe server event when server is up but before any client connections if (ServerEventHandler != null) { await ServerEventHandler.PreServeAsync(cancellationToken); } while (!stop) { int failureCount = 0; try { TTransport client = await ServerTransport.AcceptAsync(cancellationToken); ThreadPool.QueueUserWorkItem(this.Execute, client); } catch (TTransportException ttx) { if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted) { ++failureCount; LogError(ttx.ToString()); } } } if (stop) { try { ServerTransport.Close(); } catch (TTransportException ttx) { LogError("TServerTransport failed on close: " + ttx.Message); } stop = false; } } finally { ServerCancellationToken = default(CancellationToken); } }
public void GetTaskTemplates(Guid clientId, Guid playerId, ServerEventHandler <SerializedTask[], SerializedNamedTaskLaunchInfo[]> callback) { RemoteCall rpc = new RemoteCall( RemoteCall.Proc.GetTaskTemplates, clientId, RemoteArg.Create(playerId)); Call(rpc, (error, result) => callback(error, result.Get <SerializedTask[]>(0), result.Get <SerializedNamedTaskLaunchInfo[]>(1))); }
public void Pause(Guid clientId, bool pause, ServerEventHandler callback) { RemoteCall rpc = new RemoteCall( RemoteCall.Proc.Pause, clientId, RemoteArg.Create(pause)); Call(rpc, (error, result) => callback(error)); }
public WorkToClient(ServerEventHandler handler, NotifyEventCode nec, uint retCode, object param1, object param2) { m_handler = handler; m_nec = nec; m_retCode = retCode; m_param1 = param1; m_param2 = param2; }