Skip to content

Commit b371ca7

Browse files
committed
Refactored Pop3Client a bit to reduce ! usage
1 parent 77cc4f0 commit b371ca7

3 files changed

Lines changed: 31 additions & 21 deletions

File tree

MailKit/Net/Pop3/AsyncPop3Client.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,11 @@ async Task OnAuthenticatedAsync (string message, CancellationToken cancellationT
139139
/// </exception>
140140
public override async Task AuthenticateAsync (SaslMechanism mechanism, CancellationToken cancellationToken = default)
141141
{
142-
CheckCanAuthenticate (mechanism, cancellationToken);
142+
var saslUri = CheckCanAuthenticate (mechanism, cancellationToken);
143143

144144
using var operation = engine.StartNetworkOperation (NetworkOperationKind.Authenticate);
145145

146146
try {
147-
var saslUri = new Uri ("pop://" + engine.Uri!.Host);
148147
var ctx = GetSaslAuthContext (mechanism, saslUri);
149148

150149
var pc = await ctx.AuthenticateAsync (cancellationToken).ConfigureAwait (false);
@@ -221,12 +220,11 @@ public override async Task AuthenticateAsync (SaslMechanism mechanism, Cancellat
221220
/// </exception>
222221
public override async Task AuthenticateAsync (Encoding encoding, ICredentials credentials, CancellationToken cancellationToken = default)
223222
{
224-
CheckCanAuthenticate (encoding, credentials, cancellationToken);
223+
var saslUri = CheckCanAuthenticate (encoding, credentials, cancellationToken);
225224

226225
using var operation = engine.StartNetworkOperation (NetworkOperationKind.Authenticate);
227226

228227
try {
229-
var saslUri = new Uri ("pop://" + engine.Uri!.Host);
230228
string userName, password;
231229
NetworkCredential? cred;
232230
string? message = null;

MailKit/Net/Pop3/Pop3Client.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,13 @@ Pop3Engine Engine {
680680

681681
void OnDataReceived (Pop3Engine pop3, Pop3Command pc, string text, CancellationToken cancellationToken)
682682
{
683+
pop3.CheckConnected ();
684+
683685
while (pc.Status == Pop3CommandStatus.Continue && !mechanism.IsAuthenticated) {
684686
var challenge = mechanism.Challenge (text, cancellationToken);
685687
var buf = Encoding.ASCII.GetBytes (challenge + "\r\n");
686688

687-
pop3.Stream!.Write (buf, 0, buf.Length, cancellationToken);
689+
pop3.Stream.Write (buf, 0, buf.Length, cancellationToken);
688690
pop3.Stream.Flush (cancellationToken);
689691

690692
var response = pop3.ReadLine (cancellationToken).TrimEnd ();
@@ -700,11 +702,13 @@ void OnDataReceived (Pop3Engine pop3, Pop3Command pc, string text, CancellationT
700702

701703
async Task OnDataReceivedAsync (Pop3Engine pop3, Pop3Command pc, string text, CancellationToken cancellationToken)
702704
{
705+
pop3.CheckConnected ();
706+
703707
while (pc.Status == Pop3CommandStatus.Continue && !mechanism.IsAuthenticated) {
704708
var challenge = await mechanism.ChallengeAsync (text, cancellationToken).ConfigureAwait (false);
705709
var buf = Encoding.ASCII.GetBytes (challenge + "\r\n");
706710

707-
await pop3.Stream!.WriteAsync (buf, 0, buf.Length, cancellationToken).ConfigureAwait (false);
711+
await pop3.Stream.WriteAsync (buf, 0, buf.Length, cancellationToken).ConfigureAwait (false);
708712
await pop3.Stream.FlushAsync (cancellationToken).ConfigureAwait (false);
709713

710714
var response = (await pop3.ReadLineAsync (cancellationToken).ConfigureAwait (false)).TrimEnd ();
@@ -764,12 +768,12 @@ public async Task<Pop3Command> AuthenticateAsync (CancellationToken cancellation
764768
}
765769
}
766770

767-
void CheckCanAuthenticate (SaslMechanism mechanism, CancellationToken cancellationToken)
771+
Uri CheckCanAuthenticate (SaslMechanism mechanism, CancellationToken cancellationToken)
768772
{
769773
if (mechanism == null)
770774
throw new ArgumentNullException (nameof (mechanism));
771775

772-
if (!IsConnected)
776+
if (!engine.IsConnected)
773777
throw new ServiceNotConnectedException ("The Pop3Client must be connected before you can authenticate.");
774778

775779
if (IsAuthenticated)
@@ -778,6 +782,8 @@ void CheckCanAuthenticate (SaslMechanism mechanism, CancellationToken cancellati
778782
CheckDisposed ();
779783

780784
cancellationToken.ThrowIfCancellationRequested ();
785+
786+
return new Uri ("pop://" + engine.Uri.Host);
781787
}
782788

783789
SaslAuthContext GetSaslAuthContext (SaslMechanism mechanism, Uri saslUri)
@@ -841,12 +847,11 @@ void OnAuthenticated (string message, CancellationToken cancellationToken)
841847
/// </exception>
842848
public override void Authenticate (SaslMechanism mechanism, CancellationToken cancellationToken = default)
843849
{
844-
CheckCanAuthenticate (mechanism, cancellationToken);
850+
var saslUri = CheckCanAuthenticate (mechanism, cancellationToken);
845851

846852
using var operation = engine.StartNetworkOperation (NetworkOperationKind.Authenticate);
847853

848854
try {
849-
var saslUri = new Uri ("pop://" + engine.Uri!.Host);
850855
var ctx = GetSaslAuthContext (mechanism, saslUri);
851856

852857
var pc = ctx.Authenticate (cancellationToken);
@@ -863,21 +868,25 @@ public override void Authenticate (SaslMechanism mechanism, CancellationToken ca
863868
}
864869
}
865870

866-
void CheckCanAuthenticate (Encoding encoding, ICredentials credentials, CancellationToken cancellationToken)
871+
Uri CheckCanAuthenticate (Encoding encoding, ICredentials credentials, CancellationToken cancellationToken)
867872
{
868873
if (encoding == null)
869874
throw new ArgumentNullException (nameof (encoding));
870875

871876
if (credentials == null)
872877
throw new ArgumentNullException (nameof (credentials));
873878

874-
if (!IsConnected)
879+
if (!engine.IsConnected)
875880
throw new ServiceNotConnectedException ("The Pop3Client must be connected before you can authenticate.");
876881

877882
if (IsAuthenticated)
878883
throw new InvalidOperationException ("The Pop3Client is already authenticated.");
879884

880885
CheckDisposed ();
886+
887+
cancellationToken.ThrowIfCancellationRequested ();
888+
889+
return new Uri ("pop://" + engine.Uri.Host);
881890
}
882891

883892
string GetApopCommand (Encoding encoding, NetworkCredential cred)
@@ -956,12 +965,11 @@ string GetApopCommand (Encoding encoding, NetworkCredential cred)
956965
/// </exception>
957966
public override void Authenticate (Encoding encoding, ICredentials credentials, CancellationToken cancellationToken = default)
958967
{
959-
CheckCanAuthenticate (encoding, credentials, cancellationToken);
968+
var saslUri = CheckCanAuthenticate (encoding, credentials, cancellationToken);
960969

961970
using var operation = engine.StartNetworkOperation (NetworkOperationKind.Authenticate);
962971

963972
try {
964-
var saslUri = new Uri ("pop://" + engine.Uri!.Host);
965973
string userName, password;
966974
NetworkCredential? cred;
967975
string? message = null;
@@ -2264,35 +2272,39 @@ protected void Update (int n)
22642272

22652273
void OnDataReceived (Pop3Engine engine, Pop3Command pc, CancellationToken cancellationToken)
22662274
{
2275+
engine.CheckConnected ();
2276+
22672277
try {
2268-
engine.Stream!.Mode = Pop3StreamMode.Data;
2278+
engine.Stream.Mode = Pop3StreamMode.Data;
22692279

22702280
var item = Parse (engine.Stream, cancellationToken);
22712281

22722282
downloaded![idx++] = item;
22732283
} catch (FormatException ex) {
22742284
pc.Exception = CreatePop3ParseException (ex, "Failed to parse data.");
22752285

2276-
engine.Stream!.CopyTo (Stream.Null, 4096);
2286+
engine.Stream.CopyTo (Stream.Null, 4096);
22772287
} finally {
2278-
engine.Stream!.Mode = Pop3StreamMode.Line;
2288+
engine.Stream.Mode = Pop3StreamMode.Line;
22792289
}
22802290
}
22812291

22822292
async Task OnDataReceivedAsync (Pop3Engine engine, Pop3Command pc, CancellationToken cancellationToken)
22832293
{
2294+
engine.CheckConnected ();
2295+
22842296
try {
2285-
engine.Stream!.Mode = Pop3StreamMode.Data;
2297+
engine.Stream.Mode = Pop3StreamMode.Data;
22862298

22872299
var item = await ParseAsync (engine.Stream, cancellationToken).ConfigureAwait (false);
22882300

22892301
downloaded![idx++] = item;
22902302
} catch (FormatException ex) {
22912303
pc.Exception = CreatePop3ParseException (ex, "Failed to parse data.");
22922304

2293-
await engine.Stream!.CopyToAsync (Stream.Null, 4096, cancellationToken).ConfigureAwait (false);
2305+
await engine.Stream.CopyToAsync (Stream.Null, 4096, cancellationToken).ConfigureAwait (false);
22942306
} finally {
2295-
engine.Stream!.Mode = Pop3StreamMode.Line;
2307+
engine.Stream.Mode = Pop3StreamMode.Line;
22962308
}
22972309
}
22982310

MailKit/Net/Pop3/Pop3Engine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public int LoginDelay {
209209
}
210210

211211
[MemberNotNull (nameof (Stream))]
212-
void CheckConnected ()
212+
internal void CheckConnected ()
213213
{
214214
if (Stream == null)
215215
throw new InvalidOperationException ();

0 commit comments

Comments
 (0)