Exemple #1
0
        public void Connect(EndPoint remoteEndPoint)
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            var e       = m_SaeaManager.Pop();
            var isAsync = false;

            try
            {
                e.RemoteEndPoint = remoteEndPoint;
                e.SetBuffer(e.Offset, 0);
                e.UserToken = this;
                isAsync     = m_Socket.ConnectAsync(e);
            }
            catch (System.Exception)
            {
                m_SaeaManager.Push(e);
                m_Handler.OnBegin(this, false);
                isAsync = true;
            }
            finally
            {
                if (!isAsync)
                {
                    ProcessConnect(e);
                }
            }
        }
Exemple #2
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException("invalid off count.", nameof(count));
            }
            if (!m_IsOpen)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if ((m_Position += count) > m_Length)
            {
                m_Length = m_Position;
            }

            while (m_Position > m_Capacity)
            {
                var saea = m_SaeaManager.Pop();

                m_Capacity += saea.Count;
                m_Saeas.Add(saea);
            }

            while (count > 0)
            {
                var curSaea     = m_Saeas[m_CurIdx];
                var curCapacity = curSaea.Count - m_CurPosition;

                if (curCapacity == 0)
                {
                    m_CurPosition = 0;
                    m_CurIdx++;
                    continue;
                }
                else
                {
                    var tmpCount = count;

                    if (tmpCount > curCapacity)
                    {
                        tmpCount = curCapacity;
                    }

                    if (tmpCount <= 8)
                    {
                        var num2 = tmpCount;

                        while (--num2 >= 0)
                        {
                            curSaea.Buffer[curSaea.Offset + m_CurPosition + num2] = buffer[offset + num2];
                        }
                    }
                    else
                    {
                        Buffer.BlockCopy(buffer, offset, curSaea.Buffer, curSaea.Offset + m_CurPosition, tmpCount);
                    }

                    m_CurPosition += tmpCount;
                    offset        += tmpCount;
                    count         -= tmpCount;
                }
            }
        }