Beispiel #1
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                int readLength = this.socket.EndReceive(ar);

                if (readLength == 0)
                {
                    this.OnErrorEvent(new Exception("close!"));
                    return;
                }

                this._buffer.Position = this._buffer.Position + readLength;

                if (this._cache.Size == 0 && this._buffer.Position >= SocketHelper.PREFIX_LENGTH)
                {
                    byte[] perfixBytes = new byte[SocketHelper.PREFIX_LENGTH];
                    Array.Copy(this._buffer.Data, 0, perfixBytes, 0, perfixBytes.Length);
                    this._cache.Size     = SocketHelper.ToPrefixLength(perfixBytes);
                    this._cache.Data     = new byte[this._cache.Size];
                    this._cache.Position = this._buffer.Position - SocketHelper.PREFIX_LENGTH;
                    Array.Copy(this._buffer.Data, SocketHelper.PREFIX_LENGTH, this._cache.Data, 0, this._cache.Position);
                    this._buffer.Clear();
                }
                else if (this._cache.Size > 0)
                {
                    Array.Copy(this._buffer.Data, 0, this._cache.Data, this._cache.Position, this._buffer.Position);
                    this._cache.Position = this._cache.Position + this._buffer.Position;
                    this._buffer.Clear();
                }

                this.OnReadingEvent(this._cache.Position, this._cache.Size);

                if (this._cache.Size > 0 && this._cache.Position == this._cache.Size)
                {
                    this.OnReceiveEvent(this._cache.Data, this._cache.Size);
                    this._cache.Clear();
                }

                this.socket.BeginReceive(this._buffer.Data, this._buffer.Position, this._buffer.Size - this._buffer.Position,
                                         SocketFlags.None, this.OnReceive, null);
            }
            catch (Exception ex)
            {
                this.OnErrorEvent(ex);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <returns></returns>
        public byte[] Receive()
        {
            byte[] buffer = new byte[SocketHelper.PREFIX_LENGTH];
            int    count  = 0;

            while (count < SocketHelper.PREFIX_LENGTH)
            {
                count += this.socket.Receive(buffer, count, SocketHelper.PREFIX_LENGTH - count, SocketFlags.None);
            }
            int length = SocketHelper.ToPrefixLength(buffer);

            byte[] data = new byte[length];
            count = 0;
            while (count < length)
            {
                count += this.socket.Receive(data, count, length - count, SocketFlags.None);
            }

            return(data);
        }
Beispiel #3
0
        protected override bool ReceiveData(BufferModel buffer, CacheModel cache, out List <byte[]> data)
        {
            data = null;
            if (cache.Size == 0 && buffer.Position < SocketHelper.PREFIX_LENGTH)
            {
                return(true);
            }
            else
            {
                int start = 0;
                while (start < cache.Position + buffer.Position)
                {
                    int len = 0;
                    if (cache.Position > 0)
                    {
                        len = cache.Size;
                    }
                    else
                    {
                        byte[] arrlen = new byte[SocketHelper.PREFIX_LENGTH];
                        Array.Copy(buffer.Data, start, arrlen, 0, arrlen.Length);
                        len = SocketHelper.ToPrefixLength(arrlen);
                    }

                    if (len <= 0 || len > SocketHelper.MAX_PREFIX_LENGTH)
                    {
                        return(false);
                    }

                    if (start + len <= cache.Position + buffer.Position)
                    {
                        if (cache.Position > 0)
                        {
                            Array.Copy(buffer.Data, start, cache.Data, cache.Position, len - cache.Position);
                            if (data == null)
                            {
                                data = new List <byte[]>(3);
                            }
                            data.Add(cache.Data);
                            start = len - cache.Position;
                            cache.Clear();
                        }
                        else
                        {
                            var temp = new byte[len];
                            Array.Copy(buffer.Data, start, temp, 0, temp.Length);
                            if (data == null)
                            {
                                data = new List <byte[]>(3);
                            }
                            data.Add(temp);
                            start += len;
                        }

                        if (start == buffer.Position)
                        {
                            buffer.Clear();
                        }
                    }
                    else
                    {
                        if (cache.Position == 0)
                        {
                            cache.Data = new byte[len];
                            cache.Size = len;
                        }
                        Array.Copy(buffer.Data, start, cache.Data, cache.Position, buffer.Position - start);
                        cache.Position = cache.Position + buffer.Position - start;
                        start          = cache.Position;
                        buffer.Clear();
                    }
                }
            }

            return(true);
        }