示例#1
0
        /// <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);
            }
        }
示例#2
0
        /// <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
        }