/// <summary> /// 数据缓冲区 /// </summary> /// <param name="bufferCount">数据缓冲区计数</param> /// <param name="index">起始位置</param> /// <param name="count">字节数量</param> internal Buffer(BufferCount bufferCount, int index, int count) { this.bufferCount = bufferCount; array.Set(bufferCount.Buffer.Buffer, index, count); ++bufferCount.Count; }
/// <summary> /// 继续接收数据 /// </summary> /// <returns>是否接收成功</returns> private unsafe bool receive() { #if DOTNET2 if (buffer == null) { buffer = new BufferCount(); bufferIndex = buffer.Buffer.StartIndex; bufferEndIndex = bufferIndex + BufferPool.Size; } socket.BeginReceive(buffer.Buffer.Buffer, bufferIndex, bufferEndIndex - bufferIndex, SocketFlags.None, out socketError, onReceiveAsyncCallback, socket); return(socketError == SocketError.Success); #else START: if (buffer == null) { buffer = new BufferCount(); bufferIndex = buffer.Buffer.StartIndex; bufferEndIndex = bufferIndex + BufferPool.Size; #if !DotNetStandard while (Interlocked.CompareExchange(ref receiveAsyncLock, 1, 0) != 0) { Thread.Sleep(0); } #endif async.SetBuffer(buffer.Buffer.Buffer, bufferIndex, BufferPool.Size); if (socket.ReceiveAsync(async)) { #if !DotNetStandard Interlocked.Exchange(ref receiveAsyncLock, 0); #endif return(true); } } else { #if !DotNetStandard while (Interlocked.CompareExchange(ref receiveAsyncLock, 1, 0) != 0) { Thread.Sleep(0); } #endif async.SetBuffer(bufferIndex, bufferEndIndex - bufferIndex); if (socket.ReceiveAsync(async)) { #if !DotNetStandard Interlocked.Exchange(ref receiveAsyncLock, 0); #endif return(true); } } #if !DotNetStandard Interlocked.Exchange(ref receiveAsyncLock, 0); #endif if (async.SocketError == SocketError.Success) { onReceive(async.BytesTransferred); goto START; } return(false); #endif }