/// <summary> Event for multiple items packet message </summary> void OnAddItems(NetworkMessage addItemsMsg) { ItemArrayMessage itemArrayMsg = addItemsMsg.ReadMessage <ItemArrayMessage>(); #if DEBUG if (itemArrayMsg.items.Length <= 0) { Debugging.PrintScreen("Received item package is not valid"); } else { Debugging.PrintScreen("Received package with multiple items"); } #endif foreach (Item tempItem in itemArrayMsg.items) { if (tempItem == null) { continue; } items.Add(tempItem); } }
/// <summary> Event for AddItem message from client to server </summary> void OnAddItem(NetworkMessage addItemMsg) { #if DEBUG Debugging.PrintScreen("Got item from client"); #endif ItemMessage itemMessage = addItemMsg.ReadMessage <ItemMessage>(); Item item = itemMessage.item; RedirectItem(addItemMsg.conn.connectionId, item); }
/// <summary> Setup application as server </summary> public override void Setup(NetworkManager networkManager) { this.networkManager = networkManager; #if DEBUG Debugging.PrintScreen("Setting up server"); #endif DatabaseManager.Connect(); NetworkServer.Listen(networkManager.serverPort); NetworkServer.RegisterHandler(MsgType.Connect, OnClientConnected); NetworkServer.RegisterHandler(NetworkMessageType.AddItem, OnAddItem); }
/// <summary> Setup application as client </summary> public override void Setup(NetworkManager networkManager) { this.networkManager = networkManager; #if DEBUG Debugging.PrintScreen("Setting up client"); #endif networkClient = new NetworkClient(); networkClient.RegisterHandler(MsgType.Connect, OnConnected); networkClient.RegisterHandler(NetworkMessageType.AddItem, OnAddItem); networkClient.RegisterHandler(NetworkMessageType.AddItems, OnAddItems); networkClient.Connect(networkManager.serverIp, networkManager.serverPort); }
/// <summary> Event for new connection with a client </summary> void OnClientConnected(NetworkMessage clientConnectedMsg) { #if DEBUG Debugging.PrintScreen("Client connected"); #endif List <Weapon> weapons = DatabaseManager.GetWeapons(); SendItems(clientConnectedMsg.conn.connectionId, weapons.ToArray(), weapons.Count); List <Armor> armors = DatabaseManager.GetArmors(); SendItems(clientConnectedMsg.conn.connectionId, armors.ToArray(), armors.Count); List <Consumable> consumables = DatabaseManager.GetConsumable(); SendItems(clientConnectedMsg.conn.connectionId, consumables.ToArray(), consumables.Count); }
/// <summary> Convert item array into network packages and send them to a client </summary> public void SendItems(int connectionId, Item[] items, int arraySize) { /// Create packages while there are unpackaged items left int i = 0; while (i < arraySize) { /// Create a package with the max size of maxItemsPerPackage and send it to client int itemsToSendCount = arraySize - i; int packageSize = itemsToSendCount > maxItemsPerPackage ? maxItemsPerPackage : itemsToSendCount; Item[] itemPackage = new Item[packageSize]; for (int ii = 0; ii < packageSize; ii++, i++) { if (i >= arraySize) { #if DEBUG Debugging.PrintScreen("Error while converting items into packages"); #endif break; } if (i >= arraySize) { itemPackage[ii] = items[i]; } } #if DEBUG foreach (Item item in itemPackage) { if (item == null) { Debugging.PrintScreen("Item in package is null which will cause a serialization error"); } } #endif ItemArrayMessage itemArrayMsg = new ItemArrayMessage(); itemArrayMsg.items = itemPackage; NetworkServer.SendToClient(connectionId, NetworkMessageType.AddItems, itemArrayMsg); } #if DEBUG Debugging.PrintScreen("Sent items to client"); #endif }
/// <summary> Event for single item packet message </summary> void OnAddItem(NetworkMessage addItemMsg) { ItemMessage itemMsg = addItemMsg.ReadMessage <ItemMessage>(); if (itemMsg.item == null) { #if DEBUG Debugging.PrintScreen("Received item is not valid"); #endif return; } #if DEBUG Debugging.PrintScreen("Received package with one item"); #endif items.Add(itemMsg.item); }
/// <summary> Event for new connection to server </summary void OnConnected(NetworkMessage connectedMsg) { #if DEBUG Debugging.PrintScreen("Connected to server"); #endif }