Fixed connection issues.
This commit is contained in:
parent
01a5dbccd6
commit
5f479a17e0
5 changed files with 23 additions and 8 deletions
|
@ -4,7 +4,6 @@ using System.Net;
|
||||||
using System.Net.Security;
|
using System.Net.Security;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Security.Authentication;
|
using System.Security.Authentication;
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace Hamakaze {
|
namespace Hamakaze {
|
||||||
public class HttpConnection : IDisposable {
|
public class HttpConnection : IDisposable {
|
||||||
|
@ -18,9 +17,9 @@ namespace Hamakaze {
|
||||||
public string Host { get; }
|
public string Host { get; }
|
||||||
public bool IsSecure { get; }
|
public bool IsSecure { get; }
|
||||||
|
|
||||||
public bool HasTimedOut => MaxRequests == 0 || (DateTimeOffset.Now - LastOperation) > MaxIdle;
|
public bool HasTimedOut => MaxRequests < 1 || (DateTimeOffset.Now - LastOperation) > MaxIdle;
|
||||||
|
|
||||||
public int MaxRequests { get; set; } = -1;
|
public int? MaxRequests { get; set; } = null;
|
||||||
public TimeSpan MaxIdle { get; set; } = TimeSpan.MaxValue;
|
public TimeSpan MaxIdle { get; set; } = TimeSpan.MaxValue;
|
||||||
public DateTimeOffset LastOperation { get; private set; } = DateTimeOffset.Now;
|
public DateTimeOffset LastOperation { get; private set; } = DateTimeOffset.Now;
|
||||||
|
|
||||||
|
@ -45,14 +44,19 @@ namespace Hamakaze {
|
||||||
if(IsSecure) {
|
if(IsSecure) {
|
||||||
SslStream = new SslStream(NetworkStream, false, (s, ce, ch, e) => e == SslPolicyErrors.None, null);
|
SslStream = new SslStream(NetworkStream, false, (s, ce, ch, e) => e == SslPolicyErrors.None, null);
|
||||||
Stream = SslStream;
|
Stream = SslStream;
|
||||||
SslStream.AuthenticateAsClient(Host, null, SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13, true);
|
SslStream.AuthenticateAsClient(
|
||||||
|
Host,
|
||||||
|
null,
|
||||||
|
SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13,
|
||||||
|
true
|
||||||
|
);
|
||||||
} else
|
} else
|
||||||
Stream = NetworkStream;
|
Stream = NetworkStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MarkUsed() {
|
public void MarkUsed() {
|
||||||
LastOperation = DateTimeOffset.Now;
|
LastOperation = DateTimeOffset.Now;
|
||||||
if(MaxRequests > 0)
|
if(MaxRequests != null)
|
||||||
--MaxRequests;
|
--MaxRequests;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,13 @@ namespace Hamakaze {
|
||||||
public HttpConnectionManagerLockException() : base(@"Failed to lock the connection manager in time.") { }
|
public HttpConnectionManagerLockException() : base(@"Failed to lock the connection manager in time.") { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class HttpRequestMessageException : HttpException {
|
||||||
|
public HttpRequestMessageException(string message) : base(message) { }
|
||||||
|
}
|
||||||
|
public class HttpRequestMessageStreamException : HttpRequestMessageException {
|
||||||
|
public HttpRequestMessageStreamException() : base(@"Provided Stream is not writable.") { }
|
||||||
|
}
|
||||||
|
|
||||||
public class HttpTaskException : HttpException {
|
public class HttpTaskException : HttpException {
|
||||||
public HttpTaskException(string message) : base(message) { }
|
public HttpTaskException(string message) : base(message) { }
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,6 +157,9 @@ namespace Hamakaze {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteTo(Stream stream, Action<long, long> onProgress = null) {
|
public void WriteTo(Stream stream, Action<long, long> onProgress = null) {
|
||||||
|
if(!stream.CanWrite)
|
||||||
|
throw new HttpRequestMessageStreamException();
|
||||||
|
|
||||||
using(StreamWriter sw = new(stream, new ASCIIEncoding(), leaveOpen: true)) {
|
using(StreamWriter sw = new(stream, new ASCIIEncoding(), leaveOpen: true)) {
|
||||||
sw.NewLine = "\r\n";
|
sw.NewLine = "\r\n";
|
||||||
sw.Write(Method);
|
sw.Write(Method);
|
||||||
|
|
|
@ -127,7 +127,7 @@ namespace Hamakaze {
|
||||||
for(; ; ) {
|
for(; ; ) {
|
||||||
byt = stream.ReadByte();
|
byt = stream.ReadByte();
|
||||||
if(byt == -1 && ms.Length == 0)
|
if(byt == -1 && ms.Length == 0)
|
||||||
return null;
|
throw new IOException(@"readLine: There is no data.");
|
||||||
|
|
||||||
ms.WriteByte((byte)byt);
|
ms.WriteByte((byte)byt);
|
||||||
|
|
||||||
|
|
|
@ -131,7 +131,7 @@ namespace Hamakaze {
|
||||||
try {
|
try {
|
||||||
Request.WriteTo(Connection.Stream, (p, t) => OnUploadProgress?.Invoke(this, p, t));
|
Request.WriteTo(Connection.Stream, (p, t) => OnUploadProgress?.Invoke(this, p, t));
|
||||||
break;
|
break;
|
||||||
} catch(IOException ex) {
|
} catch(HttpRequestMessageStreamException ex) {
|
||||||
Connection.Dispose();
|
Connection.Dispose();
|
||||||
Connection = Connections.GetConnection(Request.Host, endPoint, Request.IsSecure);
|
Connection = Connections.GetConnection(Request.Host, endPoint, Request.IsSecure);
|
||||||
|
|
||||||
|
@ -162,7 +162,8 @@ namespace Hamakaze {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Response.Connection == HttpConnectionHeader.CLOSE)
|
if(Response.Connection == HttpConnectionHeader.CLOSE
|
||||||
|
|| Response.ProtocolVersion.CompareTo(@"1.1") < 0)
|
||||||
Connection.Dispose();
|
Connection.Dispose();
|
||||||
if(Response == null)
|
if(Response == null)
|
||||||
Error(new HttpTaskRequestFailedException());
|
Error(new HttpTaskRequestFailedException());
|
||||||
|
|
Reference in a new issue