-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for AMQP over WebSockets (#304)
* Add support for AMQP over WebSockets
- Loading branch information
1 parent
07fa957
commit 87372c8
Showing
22 changed files
with
848 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Amqp/AmqpWebSocketListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
namespace Microsoft.Azure.Devices.Edge.Hub.Amqp | ||
{ | ||
using System; | ||
using System.Net; | ||
using System.Net.WebSockets; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Devices.Edge.Util; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Azure.Amqp.Transport; | ||
using Microsoft.Azure.Devices.Edge.Hub.Core; | ||
|
||
class AmqpWebSocketListener : TransportListener, IWebSocketListener | ||
{ | ||
public string SubProtocol => Constants.WebSocketSubProtocol; | ||
|
||
public AmqpWebSocketListener() | ||
: base(Constants.WebSocketListenerName) | ||
{ | ||
} | ||
|
||
public async Task ProcessWebSocketRequestAsync(WebSocket webSocket, Option<EndPoint> localEndPoint, EndPoint remoteEndPoint, string correlationId) | ||
{ | ||
try | ||
{ | ||
var taskCompletion = new TaskCompletionSource<bool>(); | ||
|
||
string localEndpointValue = localEndPoint.Expect(() => new ArgumentNullException(nameof(localEndPoint))).ToString(); | ||
var transport = new ServerWebSocketTransport(webSocket, localEndpointValue, remoteEndPoint.ToString(), correlationId); | ||
transport.Open(); | ||
|
||
var args = new TransportAsyncCallbackArgs { Transport = transport, CompletedSynchronously = false }; | ||
this.OnTransportAccepted(args); | ||
|
||
Events.EstablishedConnection(correlationId); | ||
|
||
transport.Closed += (sender, eventArgs) => | ||
{ | ||
taskCompletion.SetResult(true); | ||
}; | ||
|
||
//wait until websocket is closed | ||
await taskCompletion.Task; | ||
} | ||
catch (Exception ex) when(!ex.IsFatal()) | ||
{ | ||
Events.FailedAcceptWebSocket(correlationId, ex); | ||
throw; | ||
} | ||
} | ||
|
||
protected override void OnListen() | ||
{ | ||
|
||
} | ||
|
||
static class Events | ||
{ | ||
static readonly ILogger Log = Logger.Factory.CreateLogger<AmqpWebSocketListener>(); | ||
const int IdStart = AmqpEventIds.AmqpWebSocketListener; | ||
|
||
enum EventIds | ||
{ | ||
Established = IdStart, | ||
Exception | ||
} | ||
|
||
public static void EstablishedConnection(string correlationId) | ||
{ | ||
Log.LogInformation((int)EventIds.Established, $"Connection established CorrelationId {correlationId}"); | ||
} | ||
|
||
public static void FailedAcceptWebSocket(string correlationId, Exception ex) | ||
{ | ||
Log.LogWarning((int)EventIds.Exception, ex, $"Connection failed CorrelationId {correlationId}"); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ub/src/Microsoft.Azure.Devices.Edge.Hub.Amqp/Microsoft.Azure.Devices.Edge.Hub.Amqp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.