Refactored a few for ResponseStream.cs
This commit is contained in:
parent
b6ac7e73e1
commit
28030d0041
@ -55,11 +55,11 @@ namespace WebSocketSharp.Net
|
|||||||
private bool _chunked;
|
private bool _chunked;
|
||||||
private static readonly byte[] _crlf = new byte[] { 13, 10 };
|
private static readonly byte[] _crlf = new byte[] { 13, 10 };
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
private bool _ignoreWriteExceptions;
|
|
||||||
private HttpListenerResponse _response;
|
private HttpListenerResponse _response;
|
||||||
private Stream _stream;
|
private Stream _stream;
|
||||||
private bool _trailerSent;
|
|
||||||
private Action<byte[], int, int> _write;
|
private Action<byte[], int, int> _write;
|
||||||
|
private Action<byte[], int, int> _writeBody;
|
||||||
|
private Action<byte[], int, int> _writeChunked;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -70,7 +70,15 @@ namespace WebSocketSharp.Net
|
|||||||
{
|
{
|
||||||
_stream = stream;
|
_stream = stream;
|
||||||
_response = response;
|
_response = response;
|
||||||
_ignoreWriteExceptions = ignoreWriteExceptions;
|
|
||||||
|
if (ignoreWriteExceptions) {
|
||||||
|
_write = writeWithoutThrowingException;
|
||||||
|
_writeChunked = writeChunkedWithoutThrowingException;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_write = stream.Write;
|
||||||
|
_writeChunked = writeChunked;
|
||||||
|
}
|
||||||
|
|
||||||
_body = new MemoryStream ();
|
_body = new MemoryStream ();
|
||||||
}
|
}
|
||||||
@ -123,43 +131,38 @@ namespace WebSocketSharp.Net
|
|||||||
using (var headers = new MemoryStream ()) {
|
using (var headers = new MemoryStream ()) {
|
||||||
_response.SendHeaders (headers, closing);
|
_response.SendHeaders (headers, closing);
|
||||||
var start = headers.Position;
|
var start = headers.Position;
|
||||||
InternalWrite (headers.GetBuffer (), (int) start, (int) (headers.Length - start));
|
_write (headers.GetBuffer (), (int) start, (int) (headers.Length - start));
|
||||||
}
|
}
|
||||||
|
|
||||||
_chunked = _response.SendChunked;
|
_chunked = _response.SendChunked;
|
||||||
_write = _chunked ? (Action<byte[], int, int>) writeChunked : InternalWrite;
|
_writeBody = _chunked ? _writeChunked : _write;
|
||||||
}
|
}
|
||||||
|
|
||||||
flushBody ();
|
flushBody (closing);
|
||||||
if (closing && _chunked) {
|
if (closing && _chunked) {
|
||||||
var last = getChunkSizeBytes (0, true);
|
var last = getChunkSizeBytes (0, true);
|
||||||
InternalWrite (last, 0, last.Length);
|
_write (last, 0, last.Length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!closing)
|
private void flushBody (bool closing)
|
||||||
_body = new MemoryStream ();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void flushBody ()
|
|
||||||
{
|
{
|
||||||
using (_body) {
|
using (_body) {
|
||||||
var len = _body.Length;
|
var len = _body.Length;
|
||||||
if (len > Int32.MaxValue) {
|
if (len > Int32.MaxValue) {
|
||||||
_body.Position = 0;
|
_body.Position = 0;
|
||||||
|
|
||||||
var buffLen = 1024;
|
var buffLen = 1024;
|
||||||
var buff = new byte[buffLen];
|
var buff = new byte[buffLen];
|
||||||
|
|
||||||
var nread = 0;
|
var nread = 0;
|
||||||
while ((nread = _body.Read (buff, 0, buffLen)) > 0)
|
while ((nread = _body.Read (buff, 0, buffLen)) > 0)
|
||||||
_write (buff, 0, nread);
|
_writeBody (buff, 0, nread);
|
||||||
}
|
}
|
||||||
else if (len > 0) {
|
else if (len > 0) {
|
||||||
_write (_body.GetBuffer (), 0, (int) len);
|
_writeBody (_body.GetBuffer (), 0, (int) len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_body = null;
|
_body = !closing ? new MemoryStream () : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] getChunkSizeBytes (int size, bool final)
|
private static byte[] getChunkSizeBytes (int size, bool final)
|
||||||
@ -170,21 +173,27 @@ namespace WebSocketSharp.Net
|
|||||||
private void writeChunked (byte[] buffer, int offset, int count)
|
private void writeChunked (byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
var size = getChunkSizeBytes (count, false);
|
var size = getChunkSizeBytes (count, false);
|
||||||
if (_ignoreWriteExceptions) {
|
|
||||||
try {
|
|
||||||
_stream.Write (size, 0, size.Length);
|
_stream.Write (size, 0, size.Length);
|
||||||
_stream.Write (buffer, offset, count);
|
_stream.Write (buffer, offset, count);
|
||||||
_stream.Write (_crlf, 0, 2);
|
_stream.Write (_crlf, 0, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void writeChunkedWithoutThrowingException (byte[] buffer, int offset, int count)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
writeChunked (buffer, offset, count);
|
||||||
|
}
|
||||||
catch {
|
catch {
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_stream.Write (size, 0, size.Length);
|
private void writeWithoutThrowingException (byte[] buffer, int offset, int count)
|
||||||
|
{
|
||||||
|
try {
|
||||||
_stream.Write (buffer, offset, count);
|
_stream.Write (buffer, offset, count);
|
||||||
_stream.Write (_crlf, 0, 2);
|
}
|
||||||
|
catch {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -202,13 +211,12 @@ namespace WebSocketSharp.Net
|
|||||||
_response.Close ();
|
_response.Close ();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (_chunked) {
|
|
||||||
var last = getChunkSizeBytes (0, true);
|
|
||||||
InternalWrite (last, 0, last.Length);
|
|
||||||
}
|
|
||||||
|
|
||||||
_body.Dispose ();
|
_body.Dispose ();
|
||||||
_body = null;
|
_body = null;
|
||||||
|
if (_chunked) {
|
||||||
|
var last = getChunkSizeBytes (0, true);
|
||||||
|
_write (last, 0, last.Length);
|
||||||
|
}
|
||||||
|
|
||||||
_response.Abort ();
|
_response.Abort ();
|
||||||
}
|
}
|
||||||
@ -219,17 +227,7 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
internal void InternalWrite (byte[] buffer, int offset, int count)
|
internal void InternalWrite (byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
if (_ignoreWriteExceptions) {
|
_write (buffer, offset, count);
|
||||||
try {
|
|
||||||
_stream.Write (buffer, offset, count);
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_stream.Write (buffer, offset, count);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
Loading…
Reference in New Issue
Block a user