Esempio n. 1
0
        public BitWriter(System.IO.Stream source, int cacheSize = 32, bool leaveOpen = false)
            : base()
        {
            if (source == null) throw new ArgumentNullException("source");

            m_Source = source;

            m_StreamPosition = m_Source.Position;

            m_StreamLength = m_Source.Length;

            m_LeaveOpen = leaveOpen;

            m_Cache = new MemorySegment(cacheSize);
        }
Esempio n. 2
0
 public static System.ArraySegment <byte> ToByteArraySegment(this MemorySegment segment)
 {
     return(new ArraySegment <byte>(segment.Array, segment.Offset, segment.Count));
 }
Esempio n. 3
0
 public PopCommand(MemorySegment segment, int index)
     : base("pop", segment, index)
 {
 }
Esempio n. 4
0
        //CurrentLength, ExpectedLength;

        #endregion

        #region Information

        //ReceivedFrom{EndPoint, DateTime}, SentTo {EndPoint, DateTime}

        #endregion

        //AlwaysIncomplete
        static int StaticCompleteFrom(System.Net.Sockets.Socket socket, MemorySegment buffer)
        {
            return(int.MinValue);
        }
Esempio n. 5
0
 public BufferConsumer(MemorySegment memorySegment, IBufferRecycler recycler, bool isBuffer)
     : this(memorySegment, recycler, memorySegment.Size, isBuffer)
 {
 }
Esempio n. 6
0
        /// <summary>
        /// Recieves data on a given socket and endpoint
        /// </summary>
        /// <param name="socket">The socket to receive data on</param>
        /// <returns>The number of bytes recieved</returns>             
        protected internal virtual int ReceiveData(Socket socket, ref EndPoint remote, out SocketError error, bool expectRtp = true, bool expectRtcp = true, MemorySegment buffer = null)
        {
            //Nothing bad happened yet.
            error = SocketError.SocketError;

            //Ensure the socket can poll
            if (IsDisposed || m_StopRequested || socket == null || m_Buffer.IsDisposed || remote == null) return 0;

            bool tcp = socket.ProtocolType == ProtocolType.Tcp;

            if (buffer == null) buffer = m_Buffer;

            //Cache the offset at the time of the call
            int offset = buffer.Offset, received = 0;

            try
            {
                //Determine how much data is 'Available'
                //int available = socket.ReceiveFrom(m_Buffer.Array, offset, m_Buffer.Count, SocketFlags.Peek, ref remote);

                error = SocketError.Success;

                ////If the receive was a success
                //if (available > 0)
                //{
                received = socket.ReceiveFrom(buffer.Array, offset, buffer.Count, SocketFlags.None, ref remote);

                //Under TCP use Framing to obtain the length of the packet as well as the context.
                if (tcp) return ProcessFrameData(buffer.Array, offset, received, socket);

                //Lookup the context to determine if the packet will fit
                var context = GetContextBySocket(socket);

                //If there was a context and packet cannot fit
                if (context != null && received > context.MaximumPacketSize)
                {
                    //Log the problem
                    Media.Common.ILoggingExtensions.Log(Logger, ToString() + "@ReceiveData - Cannot fit packet in buffer");

                    //Determine if there was enough data to determine if the packet was rtp or rtcp and indicate a failed reception
                    //if (received > RFC3550.CommonHeaderBits.Size)
                    //{
                    //    //context.m_FailedRtcpReceptions++;

                    //    //context.m_FailedRtpReceptions++;
                    //}

                    //remove the reference
                    context = null;
                }

                //Use the data received to parse and complete any recieved packets, should take a parseState
                using (var memory = new Common.MemorySegment(buffer.Array, offset, received))
                    ParseAndCompleteData(memory, expectRtcp, expectRtp);
                //}

            }
            catch (SocketException se)
            {
                error = (SocketError)se.ErrorCode;

                return received;
            }
            catch { throw; }

            //Return the amount of bytes received from this operation
            return received;
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a new RtcpPacket with the given paramters.
        /// Throws a <see cref="OverflowException"/> if padding is less than 0 or greater than byte.MaxValue.
        /// </summary>
        /// <param name="version">Sets <see cref="Version"/></param>
        /// <param name="payloadType">Sets <see cref="PayloadType"/></param>
        /// <param name="padding">The amount of padding octets if greater than 0</param>
        /// <param name="ssrc">Sets <see cref="SendersSyncrhonizationSourceIdentifier"/></param>
        /// <param name="blockCount">Sets <see cref="BlockCount"/> </param>
        /// <param name="lengthInWords">Sets <see cref="RtcpHeader.LengthInWordsMinusOne"/></param>
        public RtcpPacket(int version, int payloadType, int padding, int ssrc, int blockCount, int lengthInWords, int blockSize, int extensionSize)
        {
            //If the padding is greater than allow throw an overflow exception
            if (padding < 0 || padding > byte.MaxValue) Binary.CreateOverflowException("padding", padding, byte.MinValue.ToString(), byte.MaxValue.ToString());

            Header = new RtcpHeader(version, payloadType, padding > 0, blockCount, ssrc, lengthInWords);

            m_OwnsHeader = true;

            extensionSize = Binary.MachineWordsToBytes(Binary.BytesToMachineWords(extensionSize));

            //Calulcate the size of the Payload Segment
            int payloadSize = (blockSize * blockCount) + extensionSize;

            //Octet alignment should always be respected when creating the payload, this will avoid a few uncecessary resizes.
            int nullOctetsRequired = (extensionSize & 0x03);

            payloadSize += nullOctetsRequired + padding;

            //Allocate an array of byte equal to the size required
            m_OwnedOctets = new byte[payloadSize];

            //Segment the array to allow property access.
            Payload = new MemorySegment(m_OwnedOctets, 0, payloadSize);

            if (padding > 0) m_OwnedOctets[payloadSize - 1] = (byte)padding;
        }
Esempio n. 8
0
 public AbstractPoint(int byteIndex, PointType pointType = PointType.pBit, MemorySegment segment = MemorySegment.output)
 {
     Segment   = segment;
     PointType = pointType;
     ByteIndex = byteIndex;
 }
Esempio n. 9
0
        public override void Dispose()
        {
            //Write remaining bits
            Flush();

            if (IsDisposed) return;

            base.Dispose();

            m_StreamLength = m_StreamPosition = -1;

            m_Cache.Dispose();
            m_Cache = null;

            if (m_LeaveOpen) return;

            m_Source.Dispose();
            m_Source = null;
        }
Esempio n. 10
0
 protected MemoryCommand(string name, MemorySegment segment, int index)
     : base(name, CommandType.Memory)
 {
     _segment = segment;
     _index = index;
 }
 public override void PutNormalizedKey(bool record, MemorySegment target, int offset, int numBytes)
 {
     throw new NotImplementedException();
 }
Esempio n. 12
0
 public PushCommand(MemorySegment segment, int index)
     : base("push", segment, index)
 {
 }
Esempio n. 13
0
 public PushCommand(MemorySegment segment, int index)
     : base("push", segment, index)
 {
 }
Esempio n. 14
0
 /// <summary>
 /// Writes a normalized key for the given record into the target byte array, starting at the specified position and writing exactly the given number of bytes.
 /// </summary>
 /// <param name="record">The record for which to create the normalized key.</param>
 /// <param name="target">The byte array into which to write the normalized key bytes.</param>
 /// <param name="offset">The offset in the byte array, where to start writing the normalized key bytes.</param>
 /// <param name="numBytes">The number of bytes to be written exactly. </param>
 public abstract void PutNormalizedKey(T record, MemorySegment target, int offset, int numBytes);
Esempio n. 15
0
 /// <summary>
 /// Copies all bytes from the segment to dest
 /// </summary>
 /// <param name="segment"></param>
 /// <param name="dest"></param>
 /// <param name="destinationIndex">The offset in <paramref name="dest"/> to start copying</param>
 public static void CopyTo(this MemorySegment segment, byte[] dest, int destinationIndex)
 {
     CopyTo(segment, dest, destinationIndex, segment.Count);
 }
Esempio n. 16
0
        public void TestHeader()
        {
            IntPtr ptrFirst  = IntPtr.Zero;
            IntPtr ptrSecond = IntPtr.Zero;

            try
            {
                Assert.True(IntPtr.Size == 8);

                ptrFirst  = Alloc(10);
                ptrSecond = Alloc(10);

                MemorySegment.SetNext(ptrFirst, IntPtr.Zero);
                MemorySegment.SetPrev(ptrSecond, IntPtr.Zero);

                MemorySegment.SetSize(ptrFirst, 0);
                MemorySegment.SetSize(ptrSecond, 0);

                MemorySegment.SetUsed(ptrFirst, 0);
                MemorySegment.SetUsed(ptrSecond, 0);

                Assert.True(MemorySegment.GetNext(ptrFirst) == IntPtr.Zero);
                Assert.True(MemorySegment.GetPrev(ptrSecond) == IntPtr.Zero);

                Assert.True(MemorySegment.GetSize(ptrFirst) == 0);
                Assert.True(MemorySegment.GetSize(ptrSecond) == 0);

                Assert.True(MemorySegment.GetUsed(ptrFirst) == 0);
                Assert.True(MemorySegment.GetUsed(ptrSecond) == 0);

                MemorySegment.SetNext(ptrFirst, ptrSecond);
                MemorySegment.SetPrev(ptrSecond, ptrFirst);

                MemorySegment.SetSize(ptrFirst, 10);
                MemorySegment.SetSize(ptrSecond, 9);

                MemorySegment.SetUsed(ptrFirst, 8);
                MemorySegment.SetUsed(ptrSecond, 4);

                Assert.True(MemorySegment.GetNext(ptrFirst) == ptrSecond);
                Assert.True(MemorySegment.GetPrev(ptrSecond) == ptrFirst);

                Assert.True(MemorySegment.GetSize(ptrFirst) == 10);
                Assert.True(MemorySegment.GetSize(ptrSecond) == 9);

                Assert.True(MemorySegment.GetUsed(ptrFirst) == 8);
                Assert.True(MemorySegment.GetUsed(ptrSecond) == 4);

                Assert.True(
                    MemorySegment.GetUsed(ptrFirst) ==
                    MemorySegment.GetUsed(
                        MemorySegment.GetMemSegPtrByDataPtr(
                            MemorySegment.GetDataPtr(ptrFirst)
                            )
                        )
                    );
            }
            finally
            {
                Release(ptrFirst);
                Release(ptrSecond);
            }
        }
Esempio n. 17
0
 public static MemorySegment Subset(this MemorySegment segment, int offset, int length, bool shouldDispose = true)
 {
     //Should propably enforce that offset and length do not supercede existing length or this is not a true subset.
     return(new MemorySegment(segment.Array, offset, length, shouldDispose));
 }
Esempio n. 18
0
 public BufferBuilder(MemorySegment memorySegment, IBufferRecycler recycler)
 {
     _memorySegment = Preconditions.CheckNotNull(memorySegment);
     _recycler      = Preconditions.CheckNotNull(recycler);
 }
Esempio n. 19
0
        /// <summary>
        /// Creates a RtcpPacket instance from an existing RtcpHeader and payload.
        /// Check the IsValid property to see if the RtcpPacket is well formed.
        /// </summary>
        /// <param name="header">The existing RtpHeader (which is now owned by this instance)</param>
        /// <param name="payload">The data contained in the payload</param>
        public RtcpPacket(RtcpHeader header, MemorySegment payload, bool shouldDispose = true)
        {
            if (header == null) throw new ArgumentNullException("header");

            //The instance owns the header
            ShouldDispose = m_OwnsHeader = shouldDispose;

            Header = header;

            Payload = payload;
        }
Esempio n. 20
0
 protected MemoryCommand(string name, MemorySegment segment, int index) :
     base(name, CommandType.Memory)
 {
     _segment = segment;
     _index   = index;
 }
Esempio n. 21
0
 public PopCommand(MemorySegment segment, int index)
     : base("pop", segment, index)
 {
 }
 public override void PutNormalizedKey(double record, MemorySegment target, int offset, int numBytes) => throw new InvalidOperationException();