protected override async Task Write(object value, WriteBuffer buf, LengthCache lengthCache, [CanBeNull] NpgsqlParameter parameter, bool async, CancellationToken cancellationToken) { ArraySegment <byte> segment; if (value is ArraySegment <byte> ) { segment = (ArraySegment <byte>)value; if (!(parameter == null || parameter.Size <= 0 || parameter.Size >= segment.Count)) { segment = new ArraySegment <byte>(segment.Array, segment.Offset, parameter.Size); } } else { var array = (byte[])value; var len = parameter == null || parameter.Size <= 0 || parameter.Size >= array.Length ? array.Length : parameter.Size; segment = new ArraySegment <byte>(array, 0, len); } // The entire segment fits in our buffer, copy it as usual. if (segment.Count <= buf.WriteSpaceLeft) { buf.WriteBytes(segment.Array, segment.Offset, segment.Count); return; } // The segment is larger than our buffer. Flush whatever is currently in the buffer and // write the array directly to the socket. await buf.Flush(async, cancellationToken); buf.DirectWrite(segment.Array, segment.Offset, segment.Count); }
internal async Task WritePassword(byte[] payload, int offset, int count, bool async, CancellationToken cancellationToken = default) { if (WriteBuffer.WriteSpaceLeft < sizeof(byte) + sizeof(int)) { await WriteBuffer.Flush(async, cancellationToken); } WriteBuffer.WriteByte(FrontendMessageCode.Password); WriteBuffer.WriteInt32(sizeof(int) + count); if (count <= WriteBuffer.WriteSpaceLeft) { // The entire array fits in our WriteBuffer, copy it into the WriteBuffer as usual. WriteBuffer.WriteBytes(payload, offset, count); return; } await WriteBuffer.Flush(async, cancellationToken); await WriteBuffer.DirectWrite(new ReadOnlyMemory <byte>(payload, offset, count), async, cancellationToken); }
internal override async Task Write(WriteBuffer buf, bool async, CancellationToken cancellationToken) { if (buf.WriteSpaceLeft < 1 + 5) { await buf.Flush(async); } buf.WriteByte(Code); buf.WriteInt32(4 + PayloadLength); if (PayloadLength <= buf.WriteSpaceLeft) { // The entire array fits in our buffer, copy it into the buffer as usual. buf.WriteBytes(Payload, PayloadOffset, Payload.Length); return; } await buf.Flush(async); buf.DirectWrite(Payload, PayloadOffset, PayloadLength); }