void MessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { using (DarkRiftReader reader = message.GetReader()) { switch (message.Tag) { ////////////////////////////////////////////////////////////////////////////// //// LOGIC case MessageTags.VIDEO_RESOLUTION: { uint checkflag = reader.ReadUInt32(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); camera.targetTexture = new RenderTexture(width, height, 0); Debug.Log($"W : {width}, H: {height}"); } break; } } } }
void DeserialisePos(object data) { //Here we decode the stream, the data will arrive as a DarkRiftReader so we need to cast to it //and then read the data off in EXACTLY the same order we wrote it. if (data is DarkRiftReader) { //Cast in a using statement because we are using streams and therefore it //is important that the memory is deallocated afterwards, you wont be able //to use this more than once though. using (DarkRiftReader reader = (DarkRiftReader)data) { //Then read! transform.position = new Vector3( reader.ReadSingle(), reader.ReadSingle(), 0 ); } } else { Debug.LogError("Should have recieved a DarkRiftReciever but didn't! (Got: " + data.GetType() + ")"); transform.position = transform.position; } }
void DeserialisePosA(object data) { if (data is DarkRiftReader) { using (DarkRiftReader reader = (DarkRiftReader)data) { //Then read! transform.position = new Vector3( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle() ); //transform.position = Vector3.Lerp(transform.position, new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()), Time.deltaTime*25); /* * plValues.running = reader.ReadBoolean(); * plValues.grounded = reader.ReadBoolean(); * plValues.state = reader.ReadInt32(); * plValues.speed = reader.ReadSingle(); * plValues.hor = reader.ReadSingle(); * plValues.ver = reader.ReadSingle();*/ } } else { Debug.LogError("Should have recieved a DarkRiftReciever but didn't! (Got: " + data.GetType() + ")"); transform.position = transform.position; } }
private void SpawnPlayer(object sender, MessageReceivedEventArgs e) { using (Message m = e.GetMessage()) { using (DarkRiftReader reader = m.GetReader()) { while (reader.Position != reader.Length) { ushort id = reader.ReadUInt16(); ushort teamID = reader.ReadUInt16(); //Do stuff that deals with above info if (id == client.ID) { Vector3 pos = new Vector3(Random.Range(-5f, 5f), 0.5f, Random.Range(-5f, 5f)); //Set this as the local player RequestNetworkSpawn(localPlayerGameObject, pos, Quaternion.identity); } else { //Register player to some player manager or something } } } } }
void SpawnPlayer(DarkRiftReader reader) { Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3 rotation = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); uint id = reader.ReadUInt32(); if (id == client.ID) { GameObject o = Instantiate( playerPrefab, position, Quaternion.Euler(rotation) ) as GameObject; BlockCharacter character = o.GetComponent <BlockCharacter>(); character.PlayerID = id; character.Setup(client, blockWorld); } else { GameObject o = Instantiate( networkPlayerPrefab, position, Quaternion.Euler(rotation) ) as GameObject; BlockNetworkCharacter character = o.GetComponent <BlockNetworkCharacter>(); characterManager.AddCharacter(id, character); } }
private void Client_MessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (isConnected()) { if (message.Tag == Tags.JoinWorldMessage) { using (DarkRiftReader reader = message.GetReader()) { JoinWorldClientDTO data = reader.ReadSerializable <JoinWorldClientDTO>(); MurmurServer.MoveClientToWorldChannel(e.Client, data.WorldName); //Creates a channel for this world if it not already exists } } if (message.Tag == Tags.CreateWorldMessage) { using (DarkRiftReader reader = message.GetReader()) { CreateWorldClientDTO data = reader.ReadSerializable <CreateWorldClientDTO>(); MurmurServer.CreateChannelForWorld(data.WorldName); } } } } }
void DespawnPlayer(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { networkPlayerManager.DestroyPlayer(reader.ReadUInt16()); } }
private void OnCarDamage(Message message, IClient client) { using (DarkRiftReader reader = message.GetReader()) { CarDamage damage = reader.ReadSerializable <CarDamage>(); WorldTrain train = worldTrains.FirstOrDefault(t => t.Guid == damage.Guid); if (train == null) { train = new WorldTrain() { Guid = damage.Guid, }; worldTrains.Add(train); } switch (damage.DamageType) { case DamageType.Car: train.CarHealth = damage.NewHealth; train.CarHealthData = damage.Data; break; case DamageType.Cargo: train.CargoHealth = damage.NewHealth; break; } } Logger.Trace("[SERVER] > TRAIN_DAMAGE"); ReliableSendToOthers(message, client); }
private void UpdateCoupleCockState(Message message, IClient sender) { if (worldTrains != null) { using (DarkRiftReader reader = message.GetReader()) { TrainCouplerCockChange cockStateChanged = reader.ReadSerializable <TrainCouplerCockChange>(); WorldTrain train = worldTrains.FirstOrDefault(t => t.Guid == cockStateChanged.TrainIdCoupler); if (train == null) { train = new WorldTrain() { Guid = cockStateChanged.TrainIdCoupler, }; worldTrains.Add(train); } if (cockStateChanged.IsCouplerFront) { train.IsFrontCouplerCockOpen = cockStateChanged.IsOpen; } else { train.IsRearCouplerCockOpen = cockStateChanged.IsOpen; } } } Logger.Trace("[SERVER] > TRAIN_COUPLE_COCK"); ReliableSendToOthers(message, sender); }
void OnPlayerInformationMessage(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.PlayerInformationTag) { using (DarkRiftReader reader = message.GetReader()) { string playerName = reader.ReadString(); // Update player information players[e.Client].playerName = playerName; // Update all players using (DarkRiftWriter writer = DarkRiftWriter.Create()) { writer.Write(e.Client.ID); writer.Write(playerName); message.Serialize(writer); } foreach (IClient client in ClientManager.GetAllClients()) { client.SendMessage(message, e.SendMode); } } } } }
private void NameInfoMessage(object sender, MessageReceivedEventArgs e) { //Read new name and edit it locally using (Message message = e.GetMessage() as Message) { using (DarkRiftReader reader = message.GetReader()) { string name = reader.ReadString(); players[e.Client].name = name; } } //Tell all other users a user has changed their name using (DarkRiftWriter changeNameWriter = DarkRiftWriter.Create()) { changeNameWriter.Write(e.Client.ID); changeNameWriter.Write(players[e.Client].name); using (Message changeNameMessage = Message.Create(Tags.NameUpdateTag, changeNameWriter)) { foreach (IClient client in serverReference.Server.ClientManager.GetAllClients()) { if (client.ID != e.Client.ID) { Debug.Log("Client has changed their name: " + e.Client.ID); client.SendMessage(changeNameMessage, SendMode.Reliable); } } } } }
public void WeaponUpdateMessageRecieved(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.WeaponFireStartTag || message.Tag == Tags.WeaponFireEndTag || message.Tag == Tags.WeaponActionTag) { using (DarkRiftReader reader = message.GetReader()) { WeaponFireClientDTO data = reader.ReadSerializable <WeaponFireClientDTO>(); //Read the weapon fire dto off the stack using (DarkRiftWriter fireStartWriter = DarkRiftWriter.Create()) { WeaponFireServerDTO fireData = new WeaponFireServerDTO(); fireData.playerID = e.Client.ID; fireData.fireNum = data.fireNum; fireStartWriter.Write(fireData); int extrastart = reader.Position; int extralength = reader.Length - reader.Position; byte[] rawExtradata = reader.ReadRaw(extralength); fireStartWriter.WriteRaw(rawExtradata, 0, extralength); //Write the extra data from the message //TODO: Think about security implications of sending raw data from client to all clients using (Message fireStartMessage = Message.Create(message.Tag, fireStartWriter)) //Repeat the incoming tagname as all message bodies are the same { foreach (IClient client in worldManager.clients[e.Client].World.GetClients().Where(x => x != e.Client)) { client.SendMessage(fireStartMessage, SendMode.Reliable); } } } } } } }
void MessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.MovePlayerTag) { using (DarkRiftReader reader = message.GetReader()) { ushort id = reader.ReadUInt16(); Vector3 newPosition = new Vector3(reader.ReadSingle(), reader.ReadSingle(), 0); if (networkPlayers.ContainsKey(id)) { networkPlayers[id].SetMovePosition(newPosition); } } } else if (message.Tag == Tags.SetRadiusTag) { using (DarkRiftReader reader = message.GetReader()) { ushort id = reader.ReadUInt16(); if (networkPlayers.ContainsKey(id)) { networkPlayers[id].SetRadius(reader.ReadSingle()); } } } } }
void ClientConnected(object sender, MessageReceivedEventArgs e) { string playerName; using (Message message = e.GetMessage() as Message) { using (DarkRiftReader reader = message.GetReader()) { playerName = reader.ReadString(); } } players[e.Client].name = playerName; //Send updated name to everyone using (DarkRiftWriter playerWriter = DarkRiftWriter.Create()) { playerWriter.Write(players[e.Client].name); playerWriter.Write(players[e.Client].ID); using (Message newPlayerMessage = Message.Create(Tags.ClientNameUpdateTag, playerWriter)) { foreach (IClient client in ClientManager.GetAllClients()) { client.SendMessage(newPlayerMessage, SendMode.Reliable); } } } }
private void GetLobbyRequestAndSend(MessageReceivedEventArgs e, DarkRiftReader r) { ushort page = r.ReadUInt16(); if (page == 0) { page = 1; } List <Lobby_Match> m = GetLobbysPerPage(page); if (m.Count == 0) { using (Message mes = Message.CreateEmpty(UDRMS_Tags.getLobbyMatchs)) e.Client.SendMessage(mes, SendMode.Reliable); return; } using (DarkRiftWriter w = DarkRiftWriter.Create()) { foreach (Lobby_Match match in m) { w.Write(match); // w.Write(match.matchID); // w.Write(match.matchOwner.client.ID); // w.Write((ushort)match.matchPlayers.Count); } using (Message mes = Message.Create(UDRMS_Tags.getLobbyMatchs, w)) e.Client.SendMessage(mes, SendMode.Reliable); } }
/// <summary> /// Called when data is received by this character. /// </summary> /// <param name="tag">The tag.</param> /// <param name="subject">The subject.</param> /// <param name="data">The data.</param> void HandleOnData(byte tag, ushort subject, object data) { //If it's a Sync tag we need to sync! if (tag == SYNC_TAG) { using (DarkRiftReader reader = data as DarkRiftReader) { //Get the number of characters this sync information has ushort count = reader.ReadUInt16(); //Update the move position with the received position for each character for (int i = 0; i < count; i++) { //Read the character's ID ushort id = reader.ReadUInt16(); try { //Set move position objectSpawner[id].movePosition = new Vector3( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle() ); } catch (KeyNotFoundException) { //Probably not aware of them yet! } } } } }
public void ReceiveAuthorityRequest(Message message) { if (buffer.NotSyncedAddToBuffer(IsSynced, ReceiveAuthorityRequest, message)) { return; } using (DarkRiftReader reader = message.GetReader()) { Main.Log($"[CLIENT] < TURNTABLE_AUTH_REQUEST"); while (reader.Position < reader.Length) { RequestAuthority authRequest = reader.ReadSerializable <RequestAuthority>(); TurntableController turntable = turntables.FirstOrDefault(j => j.transform.position == authRequest.Position + WorldMover.currentMove); if (turntable) { NetworkTurntableSync sync = turntable.GetComponent <NetworkTurntableSync>(); sync.IsAnyoneInControlArea = true; sync.playerAuthId = authRequest.PlayerId; } } } }
void MovementMessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.MovePlayerTag) { using (DarkRiftReader reader = message.GetReader()) { float newX = reader.ReadSingle(); float newY = reader.ReadSingle(); Player player = players[e.Client]; player.X = newX; player.Y = newY; using (DarkRiftWriter writer = DarkRiftWriter.Create()) { writer.Write(player.ID); writer.Write(player.X); writer.Write(player.Y); message.Serialize(writer); } foreach (IClient c in ClientManager.GetAllClients().Where(x => x != e.Client)) { c.SendMessage(message, e.SendMode); } } } } }
void UseMessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { using (DarkRiftReader reader = message.GetReader()) { ushort objID = reader.ReadUInt16(); ushort useTag = reader.ReadUInt16(); using (DarkRiftWriter writer = DarkRiftWriter.Create()) { writer.Write(objID); writer.Write(useTag); using (Message useMessage = Message.Create(USE_TAG, writer)) { foreach (IClient c in ClientManager.GetAllClients().Where(x => x != e.Client)) { c.SendMessage(useMessage, SendMode.Reliable); } } } } } }
private void DespawnPlayer(Message message) { using (DarkRiftReader reader = message.GetReader()) { this.networkPlayerManager.DestroyPlayer(reader.ReadUInt16()); } }
private void OnNextJobGeneratedMessage(Message message) { using (DarkRiftReader reader = message.GetReader()) { Main.Log($"[CLIENT] < JOB_NEXT_JOB"); while (reader.Position < reader.Length) { Job generatedJob = reader.ReadSerializable <Job>(); jobs.FirstOrDefault(j => j.ChainId == generatedJob.ChainId && j.IsCurrentJob).IsCurrentJob = false; Job referencedJob = jobs.FirstOrDefault(j => j.Id == generatedJob.Id); if (referencedJob != null) { referencedJob.GameId = generatedJob.GameId; referencedJob.IsCurrentJob = true; if (referencedJob.Definition) { referencedJob.Definition.JobGenerated += OnNextJobInChainGenerated; referencedJob.Definition.ForceJobId(referencedJob.GameId); referencedJob.Definition.TryToGenerateJob(); } } } } }
public void ParseMessage(Message message) { using (DarkRiftReader reader = message.GetReader()) { ushort incomingClientId = reader.ReadUInt16(); if (incomingClientId == _characterFacade.Id) { float vertical = reader.ReadSingle(); float horizontal = reader.ReadSingle(); bool primaryAction = reader.ReadBoolean(); bool secondaryAction = reader.ReadBoolean(); float directionX = reader.ReadSingle(); float directionY = reader.ReadSingle(); Vector2 direction = new Vector2(directionX, directionY); float positionX = reader.ReadSingle(); float positionY = reader.ReadSingle(); Vector2 position = new Vector2(positionX, positionY); _controlState.Vertical = vertical; _controlState.Horizontal = horizontal; _controlState.PrimaryAction = false; _controlState.SecondaryAction = secondaryAction; _controlState.Direction = direction; _controlState.Position = position; } } _timer = 0.0f; }
public static DarkRiftReader DecryptReaderRSA(this DarkRiftReader reader, RSAParameters privateKey) { if (reader.Length > 256) { throw new ArgumentOutOfRangeException(nameof(reader), "reader mustn't contain more then 256 bytes"); } byte[] data = new byte[256]; for (int i = 0; i < 256; i++) { data[i] = reader.ReadByte(); } data = DecryptRSA(data, privateKey); DarkRiftWriter writer = DarkRiftWriter.Create(); writer.WriteRaw(data, 0, data.Length); Message message = Message.Create(0, writer); DarkRiftReader newReader = message.GetReader(); message.Dispose(); writer.Dispose(); reader.Dispose(); return(newReader); }
void InitializePlayers(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { //Each spawn packet is 14 bytes. //ID is one byte, each position float is 2 bytes if (reader.Length % 14 != 0) { Debug.LogWarning("Received malformed spawn packet"); return; } //read until end of stream while (reader.Position < reader.Length) { ushort id = reader.ReadUInt16(); ushort parentCarID = reader.ReadUInt16(); Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3 rotation = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); PlayerInfo newPlayer = new PlayerInfo(id, parentCarID, position, rotation); InitialPlayerInfo.Add(newPlayer); } } objectManager.InitializedPlayers = true; }
private void OnMessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage()) { if (message.Tag == Tags.MovePlayerTag) { using (DarkRiftReader reader = message.GetReader()) { ushort id = reader.ReadUInt16(); if (networkPlayers.ContainsKey(id)) { Vector3 newPosition = new Vector3(reader.ReadSingle(), reader.ReadSingle()); networkPlayers[id].SetMovePosition(newPosition); } } } else if (message.Tag == Tags.RadiusUpdateFlag) { using (DarkRiftReader reader = message.GetReader()) { ushort id = reader.ReadUInt16(); if (networkPlayers.ContainsKey(id)) { float newRadius = reader.ReadSingle(); networkPlayers[id].SetRadius(newRadius); } } } } }
private void DeleteObject(MessageReceivedEventArgs e, ObjectType objectType) { ushort id; using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { id = reader.ReadUInt16(); } switch (objectType) { case ObjectType.Enemy: enemies.Destroy(id); break; case ObjectType.Player: players.Destroy(id); break; default: Debug.LogError("TODO: Write error message"); break; } }
private void SpawnFood(Message message) { using (DarkRiftReader reader = message.GetReader()) { if (reader.Length % 17 != 0) { Debug.LogWarning("Received malformed food spawn packet."); return; } while (reader.Position < reader.Length) { ushort id = reader.ReadUInt16(); Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle()); float radius = reader.ReadSingle(); Color32 color = new Color32( reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), 255 ); GameObject obj; obj = Instantiate(this.foodPrefab, position, Quaternion.identity) as GameObject; AgarObject agarObj = obj.GetComponent <AgarObject>(); agarObj.SetRadius(radius); agarObj.SetColor(color); agarObj.SetMovePosition(position); this.Add(id, agarObj); } } }
void MessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { // MOVE PLAYER if (message.Tag == Tags.MovePlayerTag) { try { using (DarkRiftReader reader = message.GetReader()) { // Wrap In While so we dont go outsite the bounts of the message while (reader.Position < reader.Length) { //Read message Vector3 newPosition = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3 newRotation = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); ushort id = reader.ReadUInt16(); //Update characters to move to new positions networkPlayers[id].NewPosition = newPosition; networkPlayers[id].NewRotation = newRotation; } } } catch (Exception ex) { Debug.LogError("Error: " + ex + "\r\n\r\n" + ex.StackTrace); } } // PLAYER ANIMATION STATE if (message.Tag == Tags.AnimationPlayerTag) { try { using (DarkRiftReader reader = message.GetReader()) { // Wrap In While so we dont go outsite the bounts of the message while (reader.Position < reader.Length) { //Read message int speed = reader.ReadInt32(); byte jump = reader.ReadByte(); byte grounded = reader.ReadByte(); ushort id = reader.ReadUInt16(); //Update characters to move to new positions networkPlayers[id].speed = speed; networkPlayers[id].jump = jump; networkPlayers[id].grounded = grounded; } } } catch (Exception ex) { Debug.LogError("Error: " + ex + "\r\n\r\n" + ex.StackTrace); } } } }
/// <summary> /// Spawns a new player from the data received from the server. /// </summary> /// <param name="reader">The reader from the server.</param> void SpawnPlayer(DarkRiftReader reader) { //Extract the positions Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3 rotation = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); //Extract their ID ushort id = reader.ReadUInt16(); //If it's a player for us then spawn us our prefab and set it up if (id == client.ID) { GameObject o = Instantiate( playerPrefab, position, Quaternion.Euler(rotation) ) as GameObject; BlockCharacter character = o.GetComponent <BlockCharacter>(); character.PlayerID = id; character.Setup(client, blockWorld); } //If it's for another player then spawn a network player and and to the manager. else { GameObject o = Instantiate( networkPlayerPrefab, position, Quaternion.Euler(rotation) ) as GameObject; BlockNetworkCharacter character = o.GetComponent <BlockNetworkCharacter>(); characterManager.AddCharacter(id, character); } }
// ------------------- GETTING MESSAGES FROM SERVER -------------------- private void OnMessageReceived(object sender, MessageReceivedEventArgs e) { switch ((Tags.Tag)e.Tag) { case Tags.Tag.GOT_MATCH: // TODO: start a new match - move the match scene using (Message msg = e.GetMessage()) { using (DarkRiftReader reader = msg.GetReader()) { ushort matchID = reader.ReadUInt16(); ushort currentPlayerClientID = reader.ReadUInt16(); MatchModel.CurrentMatch = new MatchModel(matchID, currentPlayerClientID); } } GotMatch = true; break; case Tags.Tag.SERVER_CONFIRM_SLATE_TAKEN: using (Message msg = e.GetMessage()) { using (DarkRiftReader reader = msg.GetReader()) { ushort slateIndex = reader.ReadUInt16(); ushort clientID = reader.ReadUInt16(); byte gameState = reader.ReadByte(); ushort winnerClientID = gameState == 1 ? reader.ReadUInt16() : (ushort)0; MatchModel.CurrentMatch.ServerReportSlateTaken(slateIndex, clientID == client.ID, gameState, winnerClientID); } } break; } }