Esempio n. 1
0
        private int ReceiveRecord(byte[] buf, int off, int len, int waitMillis)
        {
            if (mRecordQueue.Available > 0)
            {
                int length = 0;
                if (mRecordQueue.Available >= RECORD_HEADER_LENGTH)
                {
                    byte[] lengthBytes = new byte[2];
                    mRecordQueue.Read(lengthBytes, 0, 2, 11);
                    length = TlsUtilities.ReadUint16(lengthBytes, 0);
                }

                int received = System.Math.Min(mRecordQueue.Available, RECORD_HEADER_LENGTH + length);
                mRecordQueue.RemoveData(buf, off, received, 0);
                return(received);
            }

            {
                int received = mTransport.Receive(buf, off, len, waitMillis);
                if (received >= RECORD_HEADER_LENGTH)
                {
                    int fragmentLength = TlsUtilities.ReadUint16(buf, off + 11);
                    int recordLength   = RECORD_HEADER_LENGTH + fragmentLength;
                    if (received > recordLength)
                    {
                        mRecordQueue.AddData(buf, off + recordLength, received - recordLength);
                        received = recordLength;
                    }
                }
                return(received);
            }
        }
Esempio n. 2
0
        private int ReceiveDatagram(byte[] buf, int off, int len, int waitMillis)
        {
            //try
            //{
            //    return mTransport.Receive(buf, off, len, waitMillis);
            //}
            //catch (SocketTimeoutException e)
            //{
            //    return -1;
            //}
            //catch (InterruptedIOException e)
            //{
            //    e.bytesTransferred = 0;
            //    throw e;
            //}

            try
            {
                return(mTransport.Receive(buf, off, len, waitMillis));
            }
            catch (SocketException e)
            {
                if (TlsUtilities.IsTimeout(e))
                {
                    return(-1);
                }

                throw e;
            }
        }
Esempio n. 3
0
        private int ReceiveRecord(byte[] buf, int off, int len, int waitMillis)
        {
            if (mRecordQueue.Available > 0)
            {
                int num = 0;
                if (mRecordQueue.Available >= 13)
                {
                    byte[] buf2 = new byte[2];
                    mRecordQueue.Read(buf2, 0, 2, 11);
                    num = TlsUtilities.ReadUint16(buf2, 0);
                }
                int num2 = Math.Min(mRecordQueue.Available, 13 + num);
                mRecordQueue.RemoveData(buf, off, num2, 0);
                return(num2);
            }
            int num3 = mTransport.Receive(buf, off, len, waitMillis);

            if (num3 >= 13)
            {
                int num4 = TlsUtilities.ReadUint16(buf, off + 11);
                int num5 = 13 + num4;
                if (num3 > num5)
                {
                    mRecordQueue.AddData(buf, off + num5, num3 - num5);
                    num3 = num5;
                }
            }
            return(num3);
        }
Esempio n. 4
0
        private int ReceiveDatagram(byte[] buf, int off, int len, int waitMillis)
        {
            try
            {
                return(mTransport.Receive(buf, off, len, waitMillis));
            }
            catch (TlsTimeoutException)
            {
                return(-1);
            }
#if !PORTABLE || DOTNET
            catch (SocketException e)
            {
                if (TlsUtilities.IsTimeout(e))
                {
                    return(-1);
                }

                throw e;
            }
#endif
            //catch (InterruptedIOException e)
            //{
            //    e.bytesTransferred = 0;
            //    throw e;
            //}
        }
Esempio n. 5
0
        void startRcv()
        {
            Association me = this;

            _rcv = new Thread(() =>
            {
                try
                {
                    byte[] buf = new byte[_transp.GetReceiveLimit()];
                    while (_rcv != null)
                    {
                        try
                        {
                            int length = _transp.Receive(buf, 0, buf.Length, TICK);
                            if (length == DtlsSrtpTransport.DTLS_RECEIVE_ERROR_CODE)
                            {
                                // The DTLS transport has been closed or i no longer available.
                                break;
                            }
                            //logger.LogDebug("SCTP message received: " + Packet.getHex(buf, 0, length));
                            ByteBuffer pbb = new ByteBuffer(buf);
                            pbb.Limit      = length;
                            Packet rec     = new Packet(pbb);
                            deal(rec);
                        }
                        catch (SocketException e)
                        {
                            // ignore. it should be a timeout.
                            switch (e.SocketErrorCode)
                            {
                            case SocketError.TimedOut:
                                logger.LogDebug("tick time out");
                                break;

                            default:
                                throw e;
                            }
                        }
                    }
                    logger.LogDebug("SCTP message receive was empty, closing association listener.");

                    _transp.Close();
                }
                catch (EndOfStreamException eof)
                {
                    unexpectedClose(eof);
                    logger.LogDebug(eof.ToString());
                }
                catch (Exception ex)
                {
                    logger.LogDebug("Association receive failed " + ex.GetType().Name + " " + ex.ToString());
                }
            });
            _rcv.Priority = ThreadPriority.AboveNormal;
            _rcv.Name     = "AssocRcv" + __assocNo;
            _rcv.Start();
        }
Esempio n. 6
0
        public virtual int Receive(byte[] buf, int off, int len, int waitMillis)
        {
            int length = transport.Receive(buf, off, len, waitMillis);

            if (length >= 0)
            {
                DumpDatagram("Received", buf, off, length);
            }
            return(length);
        }
Esempio n. 7
0
        void startRcv()
        {
            Association me = this;

            _rcv = new Thread(() => {
                try {
                    byte[] buf = new byte[_transp.GetReceiveLimit()];
                    while (_rcv != null)
                    {
                        try {
                            int length = _transp.Receive(buf, 0, buf.Length, TICK);
                            if (length == -1)
                            {
                                Logger.Trace("Probably tick time out");
                                continue;
                            }
                            Logger.Trace("DTLS message received\n" + Packet.getHex(buf, 0, length));
                            ByteBuffer pbb = new ByteBuffer(buf);
                            pbb.Limit      = length;
                            Packet rec     = new Packet(pbb);
                            Logger.Debug("SCTP message parsed\n" + rec.ToString());
                            deal(rec);
                        }
                        catch (SocketException e) {
                            // ignore. it should be a timeout.
                            switch (e.SocketErrorCode)
                            {
                            case SocketError.TimedOut:
                                Logger.Trace("tick time out");
                                break;

                            default:
                                throw e;
                            }
                        }
                    }
                    Logger.Trace("SCTP message recv null\n Shutting down.");

                    _transp.Close();
                }
                catch (EndOfStreamException eof) {
                    unexpectedClose(eof);
                    Logger.Debug(eof.ToString());
                }
                catch (Exception ex) {
                    Logger.Debug("Association rcv failed " + ex.GetType().Name + " " + ex.ToString());
                }
            });
            _rcv.Priority = ThreadPriority.AboveNormal;
            _rcv.Name     = "AssocRcv" + __assocNo;
            _rcv.Start();
        }
Esempio n. 8
0
        public static async Task <int> ReceiveAsync(this DatagramTransport transport, byte[] buf, int off, int len)
        {
            SpinWait spinWait = new SpinWait();

            while (true)
            {
                int count = transport.Receive(buf, off, len, 0);
                if (count == -1)
                {
                    spinWait.SpinOnce();
                    await Task.Yield();
                }
                else
                {
                    Console.WriteLine(count);
                    return(count);
                }
            }
        }
        public virtual int Receive(byte[] buf, int off, int len, int waitMillis)
        {
            long endMillis = DateTimeUtilities.CurrentUnixMs() + waitMillis;

            for (;;)
            {
                int length = transport.Receive(buf, off, len, waitMillis);
                if (length < 0 || !LostPacket(percentPacketLossReceiving))
                {
                    return(length);
                }

                Console.WriteLine("PACKET LOSS (" + length + " byte packet not received)");

                long now = DateTimeUtilities.CurrentUnixMs();
                if (now >= endMillis)
                {
                    return(-1);
                }

                waitMillis = (int)(endMillis - now);
            }
        }