///<summary> /// update from the sever state /// </summary> ///<param name="sender"<>/param> ///<param name="e"></param> private void UpdateFromServerState(object sender, DarkRift.Client.MessageReceivedEventArgs e) { if (e.Tag == NetworkTags.InGame.BOUNCY_BALL_SYNC_POS) { // Get message data BouncyBallSyncMessageModel syncMessage = e.GetMessage().Deserialize <BouncyBallSyncMessageModel>(); // Time Stamp per Tom McDonnell Oct 27 syncMessage.timeReceived = new TimeSpan(DateTime.Now.Ticks).TotalMilliseconds; if (id == syncMessage.networkID) { // If the distance between client and server ball position is beyone nominal constraint if (Vector3.Distance(rigidbodyReference.transform.position, syncMessage.position) >= 0.05f) { // Update data rigidbodyReference.velocity = syncMessage.velocity; rigidbodyReference.transform.position = syncMessage.position; clientTick = syncMessage.serverTick; //...lastReceivedMessage = syncMessage; } } } }
private void onMessage(object sender, MessageReceivedEventArgs args) { using (Message message = args.GetMessage()) { switch ((NetworkingData.Tags)message.Tag) { case NetworkingData.Tags.GameStartData: Debug.Log("Got game start data."); OnGameStart(message.Deserialize <NetworkingData.GameStartData>()); break; case NetworkingData.Tags.GameUpdate: worldUpdateBuffer.Enqueue(message.Deserialize <NetworkingData.GameUpdateData>()); break; case NetworkingData.Tags.PlayerSpawn: Debug.Log("Got player spawn data."); SpawnPlayer(message.Deserialize <NetworkingData.PlayerSpawnData>()); break; case NetworkingData.Tags.PlayerDeSpawn: Debug.Log("Got player despawn data."); DespawnPlayer(message.Deserialize <NetworkingData.PlayerDespawnData>().Id); break; } } }
/// <summary> /// This function receives messages meant to move the representations with their controllable player counterparts. /// The function distinguishes between messages for different represenations using the networkID /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UpdateFromServerState(object sender, DarkRift.Client.MessageReceivedEventArgs e) { //if we can a message with the tag for moving the representation if (e.Tag == NetworkTags.InGame.REP_SYNC_POS) { //Get message data PlayerSyncMessageModel syncMessage = e.GetMessage().Deserialize <PlayerSyncMessageModel>(); //If this is the first time we receive the message if (Object.Equals(null, lastReceivedMessage) && id == syncMessage.networkID) { //Update data rigidbodyReference.velocity = syncMessage.velocity; rigidbodyReference.transform.position = syncMessage.position; clientTick = syncMessage.serverTick; lastReceivedMessage = syncMessage; } //If the message regards this object and is older than the previous one if (id == syncMessage.networkID && syncMessage.serverTick > lastReceivedMessage.serverTick) { lastReceivedMessage = syncMessage; } } }
private void Client_MessageReceived(object sender, DarkRift.Client.MessageReceivedEventArgs e) { // Add receive message to list, will read it later by `ClientReceive` function using (Message message = e.GetMessage()) { using (DarkRiftReader reader = message.GetReader()) { clientEventQueue.Enqueue(new TransportEventData() { type = ENetworkEvent.DataEvent, reader = new NetDataReader(reader.ReadRaw(reader.Length)), }); } } }
void MessageReceived(object sender, DarkRift.Client.MessageReceivedEventArgs e) { // Debug.Log("message received"); using (Message message = e.GetMessage()) { if (message.Tag == Tags.FoodSpawnTag) { SpawnFood(sender, e); } if (message.Tag == Tags.MoveFoodTag) { Debug.Log("food moving"); RespawnFood(sender, e); } } }
void RespawnFood(object sender, DarkRift.Client.MessageReceivedEventArgs e) { using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { if (reader.Length % 10 != 0) { Debug.LogWarning("Received malformed move packet."); return; } ushort id = reader.ReadUInt16(); Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle()); if (networkFood.ContainsKey(id)) { networkFood[id].transform.position = position; } } }
public void SpawnFood(object sender, DarkRift.Client.MessageReceivedEventArgs e) { Debug.Log("calling thisa "); using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { if (message.Tag == Tags.FoodSpawnTag) { if (reader.Length % 13 != 0) { Debug.LogWarning("Received malformed spawn packet."); return; } while (reader.Position < reader.Length) { ushort id = reader.ReadUInt16(); Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle()); Color32 color = new Color32( reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), 255 ); GameObject obj; obj = Instantiate(foodPrefab, position, Quaternion.identity) as GameObject; // Debug.Log("Spawning food with ID = " + id + "."); AgarObject agarObj = obj.GetComponent <AgarObject>(); agarObj.SetRadius(1); agarObj.SetColor(color); networkFood.Add(id, agarObj); } } } }
private void OnMessageFromMaster(object client, MessageReceivedEventArgs args) { var message = args.GetMessage(); if (message != null) { switch (message.Tag) { case MessageTags.RegisterRoomSuccess: HandleRegisterRoomSuccess(message); break; case MessageTags.RegisterSpawnedProcessSuccess: HandleRegisterSpawnedProcessSuccess(message); break; case MessageTags.CompleteSpawnProcessSuccess: WriteEvent("Ready for players.", LogType.Info); break; case MessageTags.CompleteSpawnProcessFailed: var msg = message.Deserialize <FailedMessage>(); WriteEvent("CompleteSpawnProcess failed: " + msg.Status + "(" + msg.Reason + ")", LogType.Warning); break; case MessageTags.ProvideRoomAccessCheck: HandleProvideRoomAccessCheck(message); break; case MessageTags.ValidateRoomAccessSuccess: HandleValidateRoomAccessSuccess(message); break; case MessageTags.ValidateRoomAccessFailed: HandleValidateRoomAccessFailed(message); break; } } }
private void UpdateFromServer(object sender, DarkRift.Client.MessageReceivedEventArgs e) { if (e.Tag == NetworkTags.InGame.BouncyBallSyncPos) { // Get message data var syncMessage = e.GetMessage().Deserialize <BouncyBallSyncMessage>(); // First time receiving message if (_lastMessage == null) { _rb.velocity = syncMessage.Velocity; _rb.transform.position = syncMessage.Position; _clientTick = syncMessage.ServerTick; _lastMessage = syncMessage; } if (ID == syncMessage.ID && syncMessage.ServerTick > _lastMessage.ServerTick) { _lastMessage = syncMessage; } } }
/// <summary> /// update from the server state /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UpdateFromServerState(object sender, DarkRift.Client.MessageReceivedEventArgs e) { if (e.Tag == NetworkTags.InGame.PIECE_SYNC_POS) { //Get message data SyncMessageModel syncMessage = e.GetMessage().Deserialize <SyncMessageModel>(); //If this is the first time we receive the message if (Object.Equals(null, lastReceivedMessage)) { //Update data piece.x = syncMessage.x; piece.y = syncMessage.y; BoardGenerator.instance.SetPieceLocation(piece, piece.x, piece.y); clientTick = syncMessage.serverTick; lastReceivedMessage = syncMessage; } //If the message regards this object and is older than the previous one if (id == syncMessage.networkID && syncMessage.serverTick > lastReceivedMessage.serverTick) { lastReceivedMessage = syncMessage; } } }
public void SpawnPlayer(object sender, DarkRift.Client.MessageReceivedEventArgs e) { Debug.Log("calling this "); using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { if (message.Tag == Tags.SpawnPlayerTag) { //if (reader.Length % 22 != 0) //{ // Debug.LogWarning("Received malformed spawn packet."); // return; //} while (reader.Position < reader.Length) { ushort id = reader.ReadUInt16(); string name = reader.ReadString(); Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle()); float radius = reader.ReadSingle(); Color32 color = new Color32( reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), 255 ); Debug.Log(id); GameObject obj; if (id == client.ID) { obj = Instantiate(controllablePrefab, position, Quaternion.identity) as GameObject; Player player = obj.GetComponent <Player>(); player.Client = client; player.name = name; if (obj.GetComponentInChildren <TextMeshPro>()) { Debug.Log("found it"); obj.GetComponentInChildren <TextMeshPro>().text = name; } else { Debug.Log("didnt find it"); } } else { obj = Instantiate(networkPrefab, position, Quaternion.identity) as GameObject; obj.GetComponentInChildren <TextMeshPro>().text = name; } AgarObject agarObj = obj.GetComponent <AgarObject>(); agarObj.SetRadius(radius); agarObj.SetColor(color); networkPlayerManager.Add(id, agarObj); } } } }
public void DespawnPlayer(object sender, DarkRift.Client.MessageReceivedEventArgs e) { using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) networkPlayerManager.DestroyPlayer(reader.ReadUInt16()); }
private void Client_MessageReceived(object sender, DarkRift.Client.MessageReceivedEventArgs e) { try { using (Message message = e.GetMessage()) using (DarkRiftReader reader = message.GetReader()) { OpCodes opCode = (OpCodes)message.Tag; switch (opCode) { case OpCodes.ServerConnectionData: string serverIP = reader.ReadString(); if (directConnectModule == null) { directConnectAddress = ""; return; } directConnectAddress = serverIP; if (showDebugLogs) { Debug.Log("Received direct connect info from server."); } break; case OpCodes.Authenticated: isAuthenticated = true; if (showDebugLogs) { Debug.Log("Authenticated with server."); } break; case OpCodes.AuthenticationRequest: using (DarkRiftWriter writer = DarkRiftWriter.Create()) { writer.Write(relayPassword); using (Message sendAuthenticationResponse = Message.Create((ushort)OpCodes.AuthenticationResponse, writer)) drClient.Client.SendMessage(sendAuthenticationResponse, SendMode.Reliable); } if (showDebugLogs) { Debug.Log("Server requested authentication key."); } break; case OpCodes.GetData: int dataLength = reader.ReadInt32(); byte[] receivedData = new byte[dataLength]; System.Buffer.BlockCopy(reader.ReadBytes(), 0, receivedData, 0, dataLength); if (isServer) { OnServerDataReceived?.Invoke(connectedRelayClients.GetByFirst(reader.ReadUInt16()), new ArraySegment <byte>(receivedData), e.SendMode == SendMode.Unreliable ? 1 : 0); } if (isClient) { OnClientDataReceived?.Invoke(new ArraySegment <byte>(receivedData), e.SendMode == SendMode.Unreliable ? 1 : 0); } break; case OpCodes.ServerLeft: if (isClient) { isClient = false; OnClientDisconnected?.Invoke(); } break; case OpCodes.PlayerDisconnected: if (isServer) { ushort user = reader.ReadUInt16(); OnServerDisconnected?.Invoke(connectedRelayClients.GetByFirst(user)); connectedRelayClients.Remove(user); if (showDebugLogs) { Debug.Log($"Client {user} left room."); } } break; case OpCodes.RoomCreated: serverID = reader.ReadUInt16(); isConnected = true; if (showDebugLogs) { Debug.Log("Server Created on relay."); } break; case OpCodes.ServerJoined: ushort clientID = reader.ReadUInt16(); if (isClient) { isConnected = true; OnClientConnected?.Invoke(); if (showDebugLogs) { Debug.Log("Successfully joined server."); } } if (isServer) { connectedRelayClients.Add(clientID, currentMemberID); OnServerConnected?.Invoke(currentMemberID); currentMemberID++; if (showDebugLogs) { Debug.Log($"Client {clientID} joined the server."); } } break; case OpCodes.ServerListResponse: int serverListCount = reader.ReadInt32(); relayServerList.Clear(); for (int i = 0; i < serverListCount; i++) { relayServerList.Add(new RelayServerInfo() { serverName = reader.ReadString(), currentPlayers = reader.ReadInt32(), maxPlayers = reader.ReadInt32(), serverID = reader.ReadUInt16(), serverData = reader.ReadString() }); } serverListUpdated?.Invoke(); if (showDebugLogs) { Debug.Log("Received Server List."); } break; } } } catch { // Server shouldnt send messed up data but we do have an unreliable channel, so eh. } }
public void MessageReceived(object sender, DarkRift.Client.MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.PlayerDeathTag) { Debug.Log("called death sucessfully"); using (DarkRiftReader reader = message.GetReader()) { ushort id = reader.ReadUInt16(); Vector3 newPosition = new Vector3(reader.ReadSingle(), reader.ReadSingle(), 0); if (networkPlayers.ContainsKey(id)) { networkPlayers[id].gameObject.transform.position = newPosition; networkPlayers[id].SetRadius(1); } } } if (message.Tag == Tags.ClientNameUpdateTag) { using (DarkRiftReader reader = message.GetReader()) { Debug.Log("called name refresh"); string n = reader.ReadString(); ushort ID = reader.ReadUInt16(); GameObject g = networkPlayers[ID].gameObject; if (g.GetComponent <Player>()) { g.GetComponent <Player>().pname = n; } else { g.GetComponentInChildren <TextMeshPro>().text = n; } g.GetComponent <SpriteRenderer>().enabled = true; } } if (message.Tag == Tags.SpawnPlayerTag) { Debug.Log("recieved player creation request"); spawner.SpawnPlayer(sender, e); } 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); } } } if (message.Tag == Tags.SetRadiusTag) { Debug.Log("setting radius"); using (DarkRiftReader reader = message.GetReader()) { ushort id = reader.ReadUInt16(); double r = reader.ReadDouble(); networkPlayers[id].SetRadius((float)r); } if (message.Tag == Tags.DespawnPlayerTag) { spawner.DespawnPlayer(sender, e); } } } }