/// <summary> /// Reads an array of integers from an incoming message /// </summary> /// <param name="incomingMessage">The message</param> /// <returns>An array of integers</returns> internal static int[] ReadInt32Array(this IncomingMessage incomingMessage) { var lenght = incomingMessage.ReadInt32(); var array = new int[lenght]; for (int i = 0; i < lenght; i++) { array[i] = incomingMessage.ReadInt32(); } return(array); }
/// <summary> /// Reads an enumerable of RoomInfo objects from an incoming message /// </summary> /// <param name="incomingMessage">The message</param> /// <param name="roomInfoList">The list where the RoomInfo objects will be stored</param> internal static void ReadRoomInfoList(this IncomingMessage incomingMessage, List <RoomInfo> roomInfoList) { var lenght = incomingMessage.ReadInt32(); for (int i = 0; i < lenght; i++) { var newObj = RoomInfo.FromMessage(incomingMessage); roomInfoList.Add(newObj); } }
/// <summary> /// Handles the message. /// </summary> /// <param name="receivedmessage">The receivedmessage.</param> private void HandleMessage(IncomingMessage receivedmessage) { var fromNetworkId = receivedmessage.ReadString(); var type = (NetworkSyncType)receivedmessage.ReadInt32(); var fromNetworkBehaviorId = receivedmessage.ReadString(); var factoryId = receivedmessage.ReadString(); if (type == NetworkSyncType.Update) { NetworkBehavior behavior; if (this.entityBehaviors.TryGetValue(fromNetworkBehaviorId, out behavior)) { if (behavior.CreatedByBehavior) { behavior.ReadSyncData(receivedmessage); } } } else if (type == NetworkSyncType.Start) { foreach (var item in this.entityBehaviors.Where(x => !x.Value.CreatedByBehavior).ToList()) { this.SendResponseToRecreateThisEntity(item.Value); } } else if (type == NetworkSyncType.Create && !this.entityBehaviors.ContainsKey(fromNetworkBehaviorId)) { var behavior = this.CreateNewEntity(fromNetworkId, fromNetworkBehaviorId, receivedmessage, factoryId); } else if (type == NetworkSyncType.Remove) { NetworkBehavior behavior; if (this.entityBehaviors.TryGetValue(fromNetworkBehaviorId, out behavior)) { if (behavior.CreatedByBehavior) { this.RemoveNetworkEntity(behavior); } } } }
/// <summary> /// Handles the messages received from the clients. Only when this player is the host. /// </summary> private void HostMessageReceived(object sender, IncomingMessage receivedMessage) { var playerIdentifier = receivedMessage.ReadString(); var playerSpriteIndex = receivedMessage.ReadInt32(); var resultPlayerSpriteIndex = this.AssignPlayerSpriteIndex(playerIdentifier, playerSpriteIndex); var responseMessage = this.networkService.CreateServerMessage(); responseMessage.Write(playerIdentifier); responseMessage.Write(resultPlayerSpriteIndex); this.networkService.SendToClients(responseMessage, DeliveryMethod.ReliableUnordered); }
/// <summary> /// Handles the messages received from the host. /// </summary> private void ClientMessageReceived(object sender, IncomingMessage receivedMessage) { var playerIdentifier = receivedMessage.ReadString(); var playerSpriteIndex = receivedMessage.ReadInt32(); if (this.networkService.ClientIdentifier == playerIdentifier) { this.HandlePlayerSelectionResponse(playerSpriteIndex); } }
/// <summary> /// Reads a player id from the message /// </summary> /// <param name="incomingMessage">The message</param> /// <returns>The player id</returns> internal static int ReadPlayerId(this IncomingMessage incomingMessage) { return(incomingMessage.ReadInt32()); }