Skip to content

Commit 3873a1b

Browse files
authored
Add WebSocketContext class (#263)
1 parent cb9119e commit 3873a1b

6 files changed

Lines changed: 71 additions & 7 deletions

File tree

nanoFramework.System.Net.Http.Server/System.Net.Http.Server.nfproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@
115115
<Compile Include="..\nanoFramework.System.Net.Http\Http\System.Net.HttpUtility.cs">
116116
<Link>Http\System.Net.HttpUtility.cs</Link>
117117
</Compile>
118+
<Compile Include="..\nanoFramework.System.Net.Http\Http\System.Net.WebSocketContext.cs">
119+
<Link>Http\System.Net.WebSocketContext.cs</Link>
120+
</Compile>
118121
</ItemGroup>
119122
<ItemGroup>
120123
<None Include="..\nanoFramework.System.Net.Http\key.snk" />

nanoFramework.System.Net.Http/Http/System.Net.HttpListenerContext.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public class HttpListenerContext
3030
/// This stream is used for writing data.
3131
/// This stream owns the socket.
3232
/// </summary>
33-
private OutputNetworkStreamWrapper m_clientOutputStream;
33+
internal OutputNetworkStreamWrapper m_clientOutputStream;
3434

3535
/// <summary>
3636
/// Member with network stream connected to client.
3737
/// This stream is used for Reading data.
3838
/// This stream does not own the socket.
3939
/// </summary>
40-
private InputNetworkStreamWrapper m_clientInputStream;
40+
internal InputNetworkStreamWrapper m_clientInputStream;
4141

4242
/// <summary>
4343
/// Instance of the request from client.
@@ -50,7 +50,7 @@ public class HttpListenerContext
5050
/// Instance of the response to client.
5151
///
5252
/// </summary>
53-
private HttpListenerResponse m_ResponseToClient;
53+
internal HttpListenerResponse m_ResponseToClient;
5454

5555
/// <summary>
5656
/// Internal constructor, used each time client connects.
@@ -143,6 +143,25 @@ public void Close()
143143
Close(-2);
144144
}
145145

146+
/// <summary>
147+
/// Get WebsocketContext for WebsocketServer.
148+
/// This will release all bindings and resources from the HttpListner rendering HttpListnerContext unusable.
149+
/// </summary>
150+
internal WebSocketContext GetWebsocketContext()
151+
{
152+
var webSocketContext = new WebSocketContext(m_clientOutputStream.m_Socket, m_clientOutputStream.m_Stream, Request.Headers);
153+
154+
m_ResponseToClient.m_Listener.RemoveClientStream(m_ResponseToClient.m_clientStream);
155+
156+
m_ResponseToClient = null;
157+
m_clientOutputStream = null;
158+
m_clientInputStream = null;
159+
160+
161+
162+
return webSocketContext;
163+
}
164+
146165
/// <summary>
147166
/// Closes the stream attached to this listener context.
148167
/// </summary>

nanoFramework.System.Net.Http/Http/System.Net.HttpListenerResponse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ public sealed class HttpListenerResponse : IDisposable
6767
/// <summary>
6868
/// Array of connected client streams
6969
/// </summary>
70-
private HttpListener m_Listener;
70+
internal HttpListener m_Listener;
7171

7272
/// <summary>
7373
/// Member with network stream connected to client.
7474
/// After call to Close() the stream is closed, no further writing allowed.
7575
/// </summary>
76-
private OutputNetworkStreamWrapper m_clientStream;
76+
internal OutputNetworkStreamWrapper m_clientStream;
7777

7878
/// <summary>
7979
/// The value of the HTTP Location header in this response.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Text;
3+
using System.Net.Sockets;
4+
5+
namespace System.Net
6+
{
7+
/// <summary>
8+
/// Provides access to the networkStream and socket for creating a websocket
9+
/// <itemref>HttpListener</itemref> class. This class cannot be inherited.
10+
/// </summary>
11+
internal class WebSocketContext
12+
{
13+
/// <summary>
14+
/// Actual network or SSL stream connected to the client.
15+
/// It could be SSL stream, so NetworkStream is not exact type, m_Stream would be derived from NetworkStream
16+
/// </summary>
17+
internal NetworkStream NetworkStream;
18+
19+
/// <summary>
20+
/// This is a socket connected to client.
21+
/// OutputNetworkStreamWrapper owns the socket, not NetworkStream.
22+
/// If connection is persistent, then the m_Socket is transferred to the list of
23+
/// </summary>
24+
internal Socket Socket;
25+
26+
/// <summary>
27+
/// Gets the collection of header name/value pairs sent in the request.
28+
/// </summary>
29+
/// <value>A <itemref>WebHeaderCollection</itemref> that contains the
30+
/// HTTP headers included in the request.</value>
31+
internal WebHeaderCollection Headers;
32+
33+
internal WebSocketContext(Socket socket, NetworkStream networkStream, WebHeaderCollection headers)
34+
{
35+
Socket = socket;
36+
NetworkStream = networkStream;
37+
Headers = headers;
38+
39+
}
40+
}
41+
}

nanoFramework.System.Net.Http/System.Net.Http.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Compile Include="Http\System.Net._ValidationHelper.cs" />
7171
<Compile Include="Http\System.Uri.cs" />
7272
<Compile Include="Http\System.Net.HttpUtility.cs" />
73+
<Compile Include="Http\System.Net.WebSocketContext.cs" />
7374
<Compile Include="Properties\AssemblyInfo.cs" />
7475
</ItemGroup>
7576
<ItemGroup>

version.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.3.7-preview.{height}",
3+
"version": "1.4.0-preview.{height}",
44
"assemblyVersion": {
55
"precision": "revision"
66
},
@@ -21,4 +21,4 @@
2121
"versionIncrement": "build",
2222
"firstUnstableTag": "preview"
2323
}
24-
}
24+
}

0 commit comments

Comments
 (0)