Example #1
0
        public void BitStream_IntDelta_Random()
        {
            var buffer = new byte[1024 * 64];
            var output = new BitOutputStream(buffer);

            var  values   = new int[1024];
            var  random   = new Random(1032);
            long previous = 0;

            for (int i = 0; i < 1024; ++i)
            {
                values[i] = random.Next(int.MinValue, int.MaxValue);
                output.WriteIntDelta(values[i], previous);
                previous = values[i];
            }
            output.Flush();

            var input = new BitInputStream(buffer);

            previous = 0;
            for (int i = 0; i < 1024; ++i)
            {
                var value = input.ReadIntDelta(previous);
                Assert.AreEqual(values[i], value);
                previous = value;
            }
        }
Example #2
0
    protected int CompleteSendPackage(TPackageInfo info, ref BitOutputStream output)
    {
        info.SentTime = NetworkUtils.stopwatch.ElapsedMilliseconds;
        info.Content  = (NetworkMessage)m_PackageBuffer[0];
        int packageSize = output.Flush();

        byte[] data = new byte[packageSize];
        NetworkUtils.MemCopy(m_PackageBuffer, 0, data, 0, packageSize);

        counters.bytesOut += data.Length;
        Transport.SendData(ConnectionId, TransportEvent.Type.Data, data);

        counters.packagesOut++;
        ++outSequence;

        return(packageSize);
    }
Example #3
0
        public void BitStream_UIntPacked_RandomUInt()
        {
            var buffer = new byte[1024 * 64];
            var output = new BitOutputStream(buffer);

            var values = new uint[1024];
            var random = new Random(1032);

            for (int i = 0; i < 1024; ++i)
            {
                values[i] = (uint)random.Next(int.MaxValue);
                output.WriteUIntPacked(values[i]);
            }
            output.Flush();

            var input = new BitInputStream(buffer);

            for (int i = 0; i < 1024; ++i)
            {
                var value = input.ReadUIntPacked();
                Assert.AreEqual(values[i], value);
            }
        }
Example #4
0
    protected int CompleteSendPackage(TPackageInfo info, ref BitOutputStream output)
    {
        Profiler.BeginSample("NetworkConnection.CompleteSendPackage()");

        info.sentTime = NetworkUtils.stopwatch.ElapsedMilliseconds;
        info.content  = (NetworkMessage)m_PackageBuffer[0];
        int packageSize = output.Flush();

        GameDebug.Assert(packageSize < NetworkConfig.maxPackageSize, "packageSize < NetworkConfig.maxPackageSize");

        if (debugSendStreamWriter != null)
        {
            debugSendStreamWriter.Write(m_PackageBuffer, 0, packageSize);
            debugSendStreamWriter.Write((UInt32)0xedededed);
        }

        if (packageSize > NetworkConfig.packageFragmentSize)
        {
            // Package is too big and needs to be sent as fragments
            var numFragments = packageSize / NetworkConfig.packageFragmentSize;
            //GameDebug.Log("FRAGMENTING: " + connectionId + ": " + packageSize + " (" + numFragments + ")");
            var lastFragmentSize = packageSize % NetworkConfig.packageFragmentSize;
            if (lastFragmentSize != 0)
            {
                ++numFragments;
            }
            else
            {
                lastFragmentSize = NetworkConfig.packageFragmentSize;
            }

            for (var i = 0; i < numFragments; ++i)
            {
                var fragmentSize = i < numFragments - 1 ? NetworkConfig.packageFragmentSize : lastFragmentSize;

                var fragmentOutput = new BitOutputStream(m_FragmentBuffer);
                fragmentOutput.WriteBits((uint)NetworkMessage.FRAGMENT, 8); // Package fragment identifier
                fragmentOutput.WriteBits(Sequence.ToUInt16(outSequence), 16);
                fragmentOutput.WriteBits((uint)numFragments, 8);
                fragmentOutput.WriteBits((uint)i, 8);
                fragmentOutput.WriteBits((uint)fragmentSize, 16);
                fragmentOutput.WriteBytes(m_PackageBuffer, i * NetworkConfig.packageFragmentSize, fragmentSize);
                int fragmentPackageSize = fragmentOutput.Flush();

                transport.SendData(connectionId, m_FragmentBuffer, fragmentPackageSize);
                counters.packagesOut++;
                counters.bytesOut += fragmentPackageSize;
            }
            counters.fragmentedPackagesOut++;
        }
        else
        {
            transport.SendData(connectionId, m_PackageBuffer, packageSize);
            counters.packagesOut++;
            counters.bytesOut += packageSize;
        }

        ++outSequence;

        Profiler.EndSample();

        return(packageSize);
    }