public void SetMapTileMatrix(MapTileType[][] tileMatrix) { MapTileMatrix = tileMatrix; //broadcast the new map to all clients. MulticastNullTerminated(CommandStringHelper.GetUpdateMapCommandStringFromTileMatrix(tileMatrix)); }
protected override void OnConnected(TcpSession session) { var currentGameServerSession = _gameServerSessionsById[session.Id]; //send map tiles to the connected client. currentGameServerSession.SendNullTerminated(CommandStringHelper.GetUpdateMapCommandStringFromTileMatrix(MapTileMatrix)); //send the client's ID to the client. currentGameServerSession.SendNullTerminated(CommandStringHelper.GetClientIdCommandStringFromClientId(session.Id)); //send a message to all other clients that a player has joined, and attach the player's initial position. MulticastNullTerminated(CommandStringHelper.GetPlayerUpdateCommandStringFromGameServerSession(currentGameServerSession)); }
protected override void OnReceived(byte[] buffer, long offset, long size) { var text = Encoding.UTF8.GetString(buffer, (int)offset, (int)size); var commands = CommandStringHelper.SplitReceivedTextByEndOfCommandMarkers(text); foreach (var command in commands) { Console.WriteLine($"Received from client {Id}: {command}"); var split = command.Split(':'); var commandName = split[0]; var commandArguments = split?.Length > 1 ? split[1].Split(';') : Array.Empty <string>(); HandleReceivedCommand(commandName, commandArguments); } }
private void HandleReceivedCommand(string commandName, string[] commandArguments) { switch (commandName) { case "POSITIONUPDATE": { //parse the position X and Y coordinates from the command. var playerPosition = new PlayerPosition() { X = double.Parse(commandArguments[0], Program.UnitedStatesCulture), Y = double.Parse(commandArguments[1], Program.UnitedStatesCulture) }; PlayerPosition = playerPosition; //broadcast a player update to all the server's clients. _server.MulticastNullTerminated( CommandStringHelper.GetPlayerUpdateCommandStringFromGameServerSession(this)); break; } } }
public void MulticastNullTerminated(string text) { Console.WriteLine($"Sending to all clients: {text}"); Multicast(CommandStringHelper.InsertEndOfCommandMarker(text)); }
public void SendNullTerminated(string text) { Console.WriteLine($"Sending to client {Id}: {text}"); Send(CommandStringHelper.InsertEndOfCommandMarker(text)); }