Example #1
0
        private static List <MessagePart> GetMessageParts(Package message, int mtuSize, Reliability reliability, PlayerNetworkSession session)
        {
            var messageParts = new List <MessagePart>();

            byte[] encodedMessage = message.Encode();

            int orderingIndex = 0;

            CryptoContext cryptoContext = session.CryptoContext;

            if (cryptoContext != null && !(message is ConnectedPong) && !(message is DetectLostConnections))
            {
                lock (session.EncodeSync)
                {
                    McpeWrapper wrapper = McpeWrapper.CreateObject();
                    reliability   = Reliability.ReliableOrdered;
                    orderingIndex = Interlocked.Increment(ref session.OrderingIndex);

                    if (!message.ForceClear && session.CryptoContext.UseEncryption)
                    {
                        wrapper.payload = CryptoUtils.Encrypt(encodedMessage, cryptoContext);
                    }
                    else
                    {
                        wrapper.payload = encodedMessage;
                    }

                    encodedMessage = wrapper.Encode();
                    //if (Log.IsDebugEnabled)
                    //	Log.Debug($"0x{encodedMessage[0]:x2}\n{Package.HexDump(encodedMessage)}");
                    wrapper.PutPool();
                }
            }
            //if (Log.IsDebugEnabled)
            //	Log.Debug($"0x{encodedMessage[0]:x2}\n{Package.HexDump(encodedMessage)}");

            if (encodedMessage == null)
            {
                return(messageParts);
            }

            int datagramHeaderSize = 100;
            int count = (int)Math.Ceiling(encodedMessage.Length / ((double)mtuSize - datagramHeaderSize));
            int index = 0;

            if (session.SplitPartId > short.MaxValue - 100)
            {
                Interlocked.CompareExchange(ref session.SplitPartId, 0, short.MaxValue);
            }

            short splitId = (short)Interlocked.Increment(ref session.SplitPartId);

            if (count <= 1)
            {
                MessagePart messagePart = MessagePart.CreateObject();
                messagePart.Header.Reliability           = reliability;
                messagePart.Header.ReliableMessageNumber = Interlocked.Increment(ref session.ReliableMessageNumber);
                messagePart.Header.HasSplit        = count > 1;
                messagePart.Header.PartCount       = count;
                messagePart.Header.PartId          = splitId;
                messagePart.Header.PartIndex       = index++;
                messagePart.Header.OrderingChannel = 0;
                messagePart.Header.OrderingIndex   = orderingIndex;
                messagePart.ContainedMessageId     = message.Id;
                messagePart.Buffer = encodedMessage;

                messageParts.Add(messagePart);
            }
            else
            {
                foreach (var bytes in ArraySplit(encodedMessage, mtuSize - datagramHeaderSize))
                {
                    MessagePart messagePart = MessagePart.CreateObject();
                    messagePart.Header.Reliability           = reliability;
                    messagePart.Header.ReliableMessageNumber = Interlocked.Increment(ref session.ReliableMessageNumber);
                    messagePart.Header.HasSplit        = count > 1;
                    messagePart.Header.PartCount       = count;
                    messagePart.Header.PartId          = splitId;
                    messagePart.Header.PartIndex       = index++;
                    messagePart.Header.OrderingChannel = 0;
                    messagePart.Header.OrderingIndex   = orderingIndex;
                    messagePart.ContainedMessageId     = message.Id;
                    messagePart.Buffer = bytes;

                    messageParts.Add(messagePart);
                }
            }

            return(messageParts);
        }