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

            Memory <byte> encodedMessage = message.Encode();
            //if (Log.IsDebugEnabled && message is McpeBatch)
            //	Log.Debug($"0x{encodedMessage[0]:x2}\n{Package.HexDump(encodedMessage)}");

            int orderingIndex = 0;

            if (!(message is ConnectedPong) && !(message is DetectLostConnections))
            {
                reliability = Reliability.ReliableOrdered;
            }

            CryptoContext cryptoContext = session.CryptoContext;

            if (cryptoContext != null && !(message is ConnectedPong) && !(message is DetectLostConnections))
            {
                lock (session.EncodeSync)
                {
                    reliability = Reliability.ReliableOrdered;

                    var isBatch = message is McpeWrapper;

                    if (!message.ForceClear && session.CryptoContext.UseEncryption)
                    {
                        if (isBatch)
                        {
                            encodedMessage = encodedMessage.Slice(1);
                        }
                        else
                        {
                            encodedMessage = Compression.Compress(encodedMessage, true, encodedMessage.Length > 1000 ? CompressionLevel.Fastest : CompressionLevel.NoCompression);
                        }

                        McpeWrapper wrapper = McpeWrapper.CreateObject();
                        wrapper.payload = CryptoUtils.Encrypt(encodedMessage, cryptoContext);
                        encodedMessage  = wrapper.Encode();
                        wrapper.PutPool();
                    }
                    else if (!isBatch)
                    {
                        McpeWrapper wrapper = McpeWrapper.CreateObject();
                        wrapper.payload = Compression.Compress(encodedMessage, true, encodedMessage.Length > 1000 ? CompressionLevel.Fastest : CompressionLevel.NoCompression);
                        encodedMessage  = wrapper.Encode();
                        wrapper.PutPool();
                    }
                    //if (Log.IsDebugEnabled)
                    //	Log.Debug($"0x{encodedMessage[0]:x2}\n{Package.HexDump(encodedMessage)}");
                }
            }
            //if (Log.IsDebugEnabled)
            //	Log.Debug($"0x{encodedMessage[0]:x2}\n{Package.HexDump(encodedMessage)}");

            if (reliability == Reliability.ReliableOrdered)
            {
                orderingIndex = Interlocked.Increment(ref session.OrderingIndex);
            }

            if (encodedMessage.IsEmpty)
            {
                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.ToArray();

                messageParts.Add(messagePart);
            }
            else
            {
                foreach ((int from, int to)span in ArraySplit(encodedMessage.Length, 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 = encodedMessage.Slice(span.from, span.to);

                    messageParts.Add(messagePart);
                }
            }

            return(messageParts);
        }
Example #2
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);
        }