Skip to content

Commit befb61b

Browse files
committed
Refactor frontend message handling
* Frontend messages have been replaced with simple function calls on the connector. Rather than populating a message instance and then writing it, we now just call a method with parameters. * Considerable reduction in code and complexity, plus no more need to cache frontend message instances on the connector. * Made naming more consistent: write methods don't (necessarily) flush, etc. * Refactored SASL authentication to better separate protocol-level logic from encryption logic. * Enabled C# 8 for static local functions. Closes npgsql#2353
1 parent d5a4393 commit befb61b

28 files changed

Lines changed: 743 additions & 1153 deletions

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<!-- Language configuration -->
1818
<PropertyGroup>
19-
<LangVersion>latest</LangVersion>
19+
<LangVersion>8.0</LangVersion>
2020
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
2121
</PropertyGroup>
2222

Npgsql.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<s:Boolean x:Key="/Default/UserDictionary/Words/=Npgsql_0027s/@EntryIndexedValue">True</s:Boolean>
8585
<s:Boolean x:Key="/Default/UserDictionary/Words/=PGTZ/@EntryIndexedValue">True</s:Boolean>
8686
<s:Boolean x:Key="/Default/UserDictionary/Words/=Postgre/@EntryIndexedValue">True</s:Boolean>
87+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pregenerated/@EntryIndexedValue">True</s:Boolean>
8788
<s:Boolean x:Key="/Default/UserDictionary/Words/=P_0020keepaliv/@EntryIndexedValue">True</s:Boolean>
8889
<s:Boolean x:Key="/Default/UserDictionary/Words/=resultset/@EntryIndexedValue">True</s:Boolean>
8990
<s:Boolean x:Key="/Default/UserDictionary/Words/=UNLISTEN/@EntryIndexedValue">True</s:Boolean>

src/Npgsql/BackendMessages/CopyMessages.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,10 @@ internal CopyDataMessage Load(int len)
8989
}
9090
}
9191

92-
/// <remarks>
93-
/// Note: This message is both a frontend and a backend message
94-
/// </remarks>
95-
class CopyDoneMessage : SimpleFrontendMessage, IBackendMessage
92+
class CopyDoneMessage : IBackendMessage
9693
{
9794
public BackendMessageCode Code => BackendMessageCode.CopyDone;
9895
internal static readonly CopyDoneMessage Instance = new CopyDoneMessage();
9996
CopyDoneMessage() { }
100-
101-
internal override int Length => 5;
102-
103-
internal override void WriteFully(NpgsqlWriteBuffer buf)
104-
{
105-
buf.WriteByte((byte)BackendMessageCode.CopyDone);
106-
buf.WriteInt32(4);
107-
}
10897
}
10998
}

src/Npgsql/Common.cs

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,6 @@ interface IBackendMessage
1111
BackendMessageCode Code { get; }
1212
}
1313

14-
/// <summary>
15-
/// Base class for all classes which represent a message sent to the PostgreSQL backend.
16-
/// Concrete classes which directly inherit this represent arbitrary-length messages which can chunked.
17-
/// </summary>
18-
abstract class FrontendMessage
19-
{
20-
/// <param name="buf">the buffer into which to write the message.</param>
21-
/// <param name="async"></param>
22-
/// <returns>
23-
/// Whether there was enough space in the buffer to contain the entire message.
24-
/// If false, the buffer should be flushed and write should be called again.
25-
/// </returns>
26-
internal abstract Task Write(NpgsqlWriteBuffer buf, bool async);
27-
28-
/// <summary>
29-
/// Returns how many messages PostgreSQL is expected to send in response to this message.
30-
/// Used for message prepending.
31-
/// </summary>
32-
internal virtual int ResponseMessageCount => 1;
33-
}
34-
35-
/// <summary>
36-
/// Represents a simple frontend message which is typically small and fits well within
37-
/// the write buffer. The message is first queries for the number of bytes it requires,
38-
/// and then writes itself out.
39-
/// </summary>
40-
abstract class SimpleFrontendMessage : FrontendMessage
41-
{
42-
/// <summary>
43-
/// Returns the number of bytes needed to write this message.
44-
/// </summary>
45-
internal abstract int Length { get; }
46-
47-
/// <summary>
48-
/// Writes the message contents into the buffer.
49-
/// </summary>
50-
internal abstract void WriteFully(NpgsqlWriteBuffer buf);
51-
52-
internal sealed override Task Write(NpgsqlWriteBuffer buf, bool async)
53-
{
54-
if (buf.WriteSpaceLeft < Length)
55-
return FlushAndWrite(buf, async);
56-
Debug.Assert(Length <= buf.WriteSpaceLeft, $"Message of type {GetType().Name} has length {Length} which is bigger than the buffer ({buf.WriteSpaceLeft})");
57-
WriteFully(buf);
58-
return Task.CompletedTask;
59-
}
60-
61-
async Task FlushAndWrite(NpgsqlWriteBuffer buf, bool async)
62-
{
63-
await buf.Flush(async);
64-
Debug.Assert(Length <= buf.WriteSpaceLeft, $"Message of type {GetType().Name} has length {Length} which is bigger than the buffer ({buf.WriteSpaceLeft})");
65-
WriteFully(buf);
66-
}
67-
}
68-
6914
enum BackendMessageCode : byte
7015
{
7116
AuthenticationRequest = (byte)'R',
@@ -95,6 +40,21 @@ enum BackendMessageCode : byte
9540
RowDescription = (byte)'T',
9641
}
9742

43+
static class FrontendMessageCode
44+
{
45+
internal const byte Describe = (byte)'D';
46+
internal const byte Sync = (byte)'S';
47+
internal const byte Execute = (byte)'E';
48+
internal const byte Parse = (byte)'P';
49+
internal const byte Bind = (byte)'B';
50+
internal const byte Close = (byte)'C';
51+
internal const byte Query = (byte)'Q';
52+
internal const byte CopyDone = (byte)'c';
53+
internal const byte CopyFail = (byte)'f';
54+
internal const byte Terminate = (byte)'X';
55+
internal const byte Password = (byte)'p';
56+
}
57+
9858
enum StatementOrPortal : byte
9959
{
10060
Statement = (byte)'S',

src/Npgsql/FrontendMessages/BindMessage.cs

Lines changed: 0 additions & 137 deletions
This file was deleted.

src/Npgsql/FrontendMessages/CancelRequestMessage.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/Npgsql/FrontendMessages/CloseMessage.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Npgsql/FrontendMessages/CopyFailMessage.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)