Skip to content

Commit f3122ac

Browse files
authored
Code rework from Soundcloud analysis (#63)
***NO_CI***
1 parent 2fcfb51 commit f3122ac

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

source/nanoFramework.System.Net.Http/Http/System.Net.HttpListener.cs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace System.Net
2222
/// </remarks>
2323
public class HttpListener
2424
{
25+
private readonly object lockObj = new object();
26+
2527
/// <summary>
2628
/// Indicates whether the listener is waiting on an http or https
2729
/// connection.
@@ -94,6 +96,7 @@ public class HttpListener
9496
/// </summary>
9597
private SslProtocols m_sslProtocols = SslProtocols.None;
9698

99+
#pragma warning disable S2292 // Trivial properties should be auto-implemented
97100
/// <summary>
98101
/// Gets or sets the TLS/SSL protocol used by the <see cref="HttpListener"/> class.
99102
/// </summary>
@@ -104,6 +107,8 @@ public class HttpListener
104107
/// This property is specific to nanoFramework. There is no equivalent in the .NET API.
105108
/// </remarks>
106109
public SslProtocols SslProtocols
110+
#pragma warning restore S2292 // Trivial properties should be auto-implemented
111+
// nanoFramework doesn't support auto-properties
107112
{
108113
get { return m_sslProtocols; }
109114
set { m_sslProtocols = value; }
@@ -244,17 +249,6 @@ internal void AddToWaitingConnections(OutputNetworkStreamWrapper outputStream)
244249
thWaitData.Start();
245250
}
246251

247-
private void WaitingConnectionThreadFunc2()
248-
{
249-
try
250-
{
251-
Console.WriteLine("test");
252-
}
253-
catch (Exception ex)
254-
{
255-
Console.WriteLine(ex.Message);
256-
}
257-
}
258252
/// <summary>
259253
/// Waits for new data from the client.
260254
/// </summary>
@@ -305,7 +299,7 @@ private void WaitingConnectionThreadFunc(OutputNetworkStreamWrapper outputStream
305299
/// </remarks>
306300
public void Abort()
307301
{
308-
lock (this)
302+
lock (lockObj)
309303
{
310304
// First we shut down the service.
311305
Close();
@@ -364,7 +358,7 @@ private void AcceptThreadFunc()
364358
}
365359
catch
366360
{
367-
361+
// empty on purpose
368362
}
369363
}
370364
catch (SocketException)
@@ -384,7 +378,6 @@ private void AcceptThreadFunc()
384378
break;
385379
}
386380

387-
retry++;
388381
continue;
389382
}
390383
catch
@@ -415,10 +408,8 @@ private void AcceptThreadFunc()
415408
// Once connection estiblished need to create secure stream and authenticate server.
416409
netStream = new SslStream(clientSock);
417410

418-
SslProtocols[] sslProtocols = new SslProtocols[] { m_sslProtocols };
419-
420411
// Throws exception if fails.
421-
((SslStream)netStream).AuthenticateAsServer(m_httpsCert, sslProtocols);
412+
((SslStream)netStream).AuthenticateAsServer(m_httpsCert, m_sslProtocols);
422413

423414
netStream.ReadTimeout = 10000;
424415
}
@@ -462,7 +453,7 @@ private void AcceptThreadFunc()
462453
/// </remarks>
463454
public void Start()
464455
{
465-
lock (this)
456+
lock (lockObj)
466457
{
467458
if (m_Closed) throw new ObjectDisposedException();
468459

@@ -479,14 +470,20 @@ public void Start()
479470
// set NoDelay to increase HTTP(s) response times
480471
m_listener.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
481472
}
482-
catch {}
473+
catch
474+
{
475+
// empty on purpose
476+
}
483477

484478
try
485479
{
486480
// Start server socket to accept incoming connections.
487481
m_listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
488482
}
489-
catch {}
483+
catch
484+
{
485+
// empty on purpose
486+
}
490487

491488
IPAddress addr = IPAddress.GetDefaultLocalAddress();
492489

@@ -515,7 +512,7 @@ public void Start()
515512
/// <see cref='Stop'/> method.</remarks>
516513
public void Close()
517514
{
518-
lock(this)
515+
lock (lockObj)
519516
{
520517
// close does not throw
521518
try
@@ -524,6 +521,7 @@ public void Close()
524521
}
525522
catch
526523
{
524+
// empty on purpose to catch any exceptions thrown when calling the Stop above
527525
}
528526

529527
m_Closed = true;
@@ -545,7 +543,7 @@ public void Stop()
545543
{
546544
// Need to lock access to object, because Stop can be called from a
547545
// different thread.
548-
lock (this)
546+
lock (lockObj)
549547
{
550548
if (m_Closed) throw new ObjectDisposedException();
551549

@@ -594,7 +592,7 @@ public void Stop()
594592
public HttpListenerContext GetContext()
595593
{
596594
// Protects access for simulteneous call for GetContext and Close or Stop.
597-
lock (this)
595+
lock (lockObj)
598596
{
599597
if (m_Closed) throw new ObjectDisposedException();
600598

@@ -656,18 +654,24 @@ public int MaximumResponseHeadersLength
656654
{
657655
if (value <= 0 && value != -1)
658656
{
657+
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
659658
throw new ArgumentOutOfRangeException();
659+
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
660+
// can't add description as that would increase the deployment image size
660661
}
661662

662663
m_maxResponseHeadersLen = value;
663664
}
664665
}
665666

667+
#pragma warning disable S2292 // Trivial properties should be auto-implemented
666668
/// <summary>
667669
/// The certificate used if <b>HttpListener</b> implements an https
668670
/// server.
669671
/// </summary>
670672
public X509Certificate HttpsCert
673+
#pragma warning restore S2292 // Trivial properties should be auto-implemented
674+
// nanoFramework doesn't support auto-properties
671675
{
672676
get { return m_httpsCert; }
673677
set { m_httpsCert = value; }

0 commit comments

Comments
 (0)