MergeAndMask() public static method

public static MergeAndMask ( int mask, ArraySegment header, ArraySegment data ) : byte[]
mask int
header ArraySegment
data ArraySegment
return byte[]
        private async void SendKeepAliveAsync()
        {
            // Check concurrent writes, pings & pongs, or closes
            if (!_writeLock.Wait(0))
            {
                // Sending real data is better than a ping, discard it.
                return;
            }
            try
            {
                if (State == WebSocketState.CloseSent || State >= WebSocketState.Closed)
                {
                    _keepAliveTimer.Dispose();
                    return;
                }

                int                 mask          = GetNextMask();
                FrameHeader         frameHeader   = new FrameHeader(true, Constants.OpCodes.PingFrame, _maskOutput, mask, PingBuffer.Length);
                ArraySegment <byte> headerSegment = frameHeader.Buffer;

                // TODO: CancelationToken / timeout?
                if (_maskOutput && mask != 0)
                {
                    byte[] maskedFrame = Utilities.MergeAndMask(mask, headerSegment, new ArraySegment <byte>(PingBuffer));
                    await _stream.WriteAsync(maskedFrame, 0, maskedFrame.Length);
                }
                else
                {
                    await _stream.WriteAsync(headerSegment.Array, headerSegment.Offset, headerSegment.Count);

                    await _stream.WriteAsync(PingBuffer, 0, PingBuffer.Length);
                }
            }
            catch (Exception)
            {
                // TODO: Log exception, this is a background thread.

                // Shut down, we must be in a faulted state;
                Abort();
            }
            finally
            {
                _writeLock.Release();
            }
        }
        public override async Task SendAsync(ArraySegment <byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
        {
            ValidateSegment(buffer);
            if (messageType != WebSocketMessageType.Binary && messageType != WebSocketMessageType.Text)
            {
                // Block control frames
                throw new ArgumentOutOfRangeException(nameof(messageType), messageType, string.Empty);
            }

            // Check concurrent writes, pings & pongs, or closes
            await _writeLock.WaitAsync(cancellationToken);

            try
            {
                ThrowIfDisposed();
                ThrowIfOutputClosed();

                int                 mask          = GetNextMask();
                int                 opcode        = _isOutgoingMessageInProgress ? Constants.OpCodes.ContinuationFrame : Utilities.GetOpCode(messageType);
                FrameHeader         frameHeader   = new FrameHeader(endOfMessage, opcode, _maskOutput, mask, buffer.Count);
                ArraySegment <byte> headerSegment = frameHeader.Buffer;

                if (_maskOutput && mask != 0)
                {
                    // TODO: For larger messages consider using a limited size buffer and masking & sending in segments.
                    byte[] maskedFrame = Utilities.MergeAndMask(mask, headerSegment, buffer);
                    await _stream.WriteAsync(maskedFrame, 0, maskedFrame.Length, cancellationToken);
                }
                else
                {
                    await _stream.WriteAsync(headerSegment.Array, headerSegment.Offset, headerSegment.Count, cancellationToken);

                    await _stream.WriteAsync(buffer.Array, buffer.Offset, buffer.Count, cancellationToken);
                }

                _isOutgoingMessageInProgress = !endOfMessage;
            }
            finally
            {
                _writeLock.Release();
            }
        }