IPWorks MQ 2020 Python Edition

Questions / Feedback?

AzureRelayReceiver Class

Properties   Methods   Events   Configuration Settings   Errors  

An Azure Relay Service listener to accept and communicate with clients.

Syntax

class ipworksmq.AzureRelayReceiver

Remarks

The AzureRelayReceiver class implements the listener role in the Azure Relay service. The class will connect to the server and listen for incoming connections. Once a connection is established data can be exchange freely in both directions allowing a flexible messaging environment.

Authenticating and Listening

Authentication to Azure Relay is performed using the Shared Access Key Name and Shared Access Key created from the Azure portal for the Relay. To begin listening for incoming connections set listening to True.

When set to True the class will immediately attempt to make a connection to the Azure Relay Service and begin listening. The following properties are applicable when setting listening:

To stop listening set listening to False. To shutdown the server including existing connections call shutdown.

Handling Connections

When a connection is made the on_connection_request event fires with information about the connecting client. From within this event the client connection may be accepted (default) or rejected.

If the client connection is accepted the on_connection_connected and on_connection_ready_to_send events fire when the connection completes and is ready to send and receive data.

When data is received from the client the on_connection_data_in event fires with the received data.

To send data to the client set azure_relay_connection_data_to_send or call send, send_file, or send_text.

When the client disconnects the on_connection_disconnected event fires. To initiate the client disconnection call disconnect.

Handling HTTP Connections

Azure Relay also supports HTTP connections which follow a request/response model. When an HTTP request is received the on_http_request event fires with information about the request including the request body. Save the ConnectionId value obtained within on_http_request for use with send_http_response.

To send a HTTP response call send_http_response with the ConnectionId, status code and description, and any response day to be sent back to the client.

Forwarding Connections

The class supports forwarding incoming connection to a separate destination. This functionality allows AzureRelayReceiver to act as a proxy for incoming requests. When forwarding_host and forwarding_port are set anytime a connection is made the class will establish a separate connection to the forwarding_host and forward all incoming traffic. Responses from the forwarding_host are then automatically sent back over the connection.

When connection forwarding is used no special steps are needed to send or receive data. Data flows freely between the connected client and the forwarding_host.

Note: Forwarding is not supported for HTTP requests.

Code Example (WebSockets)


Azurerelayreceiver listener = new Azurerelayreceiver();
listener.AccessKey = "9oKRDwjl0s440MlLUi4qHxDL34j1FS6K3t5TRoJ216c=";
listener.AccessKeyName = "RootManageSharedAccessKey";
listener.NamespaceAddress = "myrelay.servicebus.windows.net";
listener.HybridConnection = "hc1";

listener.OnConnectionRequest += (s, e) =>
{
  Console.WriteLine("Connection Request From: " + e.RemoteAddress + ":" + e.RemotePort);
  e.Accept = true;
};

listener.OnConnectionReadyToSend += (s, e) =>
{
  Console.WriteLine("Connection [" + e.ConnectionId + "] connected and ready to send and receive.");
};

listener.OnConnectionDataIn += (s, e) =>
{
  Console.WriteLine("Received data from " + e.ConnectionId + ": " + e.Text);
  //echo the data back
  listener.SendText(e.ConnectionId, e.Text);
  Console.WriteLine("Echoed received data back");
};

listener.Listening = true;

while (true)
  listener.DoEvents();

Code Example (HTTP)


Azurerelaylistener listener = new Azurerelaylistener();
listener.AccessKey = "9oKRDwjl0s440MlLUi4qHxDL34j1FS6K3t5TRoJ216c=";
listener.AccessKeyName = "RootManageSharedAccessKey";
listener.NamespaceAddress = "myrelay.servicebus.windows.net";
listener.HybridConnection = "hc1";

listener.OnHTTPRequest += (s, e) => {
  Console.WriteLine("HTTP Request from " + e.RemoteAddress + ":" + e.RemotePort);
  Console.WriteLine("HTTP Method: " + e.RequestMethod);
  Console.WriteLine("HTTP Request: " + e.RequestData);
  myConnectionId = e.ConnectionId;
};

//Send a response using the ConnectionId value from the HTTPRequest event
listener.SendHTTPResponse(myConnectionId, 200, "OK", myResponseBody);

Property List


The following is the full list of the properties of the class with short descriptions. Click on the links for further details.

access_keyThe Shared Access Key.
access_key_nameThe Shared Access Key Name.
connectedShows whether the class is connected.
azure_relay_connection_countThe number of records in the AzureRelayConnection arrays.
azure_relay_connection_accept_dataSetting this property to False, temporarily disables data reception (and the ConnectionDataIn event) on the connection.
azure_relay_connection_addressThis property holds the rendezvous URL to which the connection specific websocket connection will be made.
azure_relay_connection_bytes_sentThis property shows how many bytes were sent after the last assignment to DataToSend .
azure_relay_connection_connectedThis property is used to disconnect individual connections and/or show their status.
azure_relay_connection_connect_headersA JSON object containing the HTTP headers that have been supplied by the sender to the Azure Relay service.
azure_relay_connection_connection_idThis property contains an identifier generated by the class to identify each connection.
azure_relay_connection_data_formatThe format of the data being sent.
azure_relay_connection_data_to_sendThis property contains a string of data to be sent to the remote host.
azure_relay_connection_extensionsThe WebSocket extensions sent by the client in the initial WebSocket connection request.
azure_relay_connection_hostThe Host header value of the connected client.
azure_relay_connection_idThis property holds the Id of the connection.
azure_relay_connection_ready_to_sendIndicates whether the class is ready to send data.
azure_relay_connection_remote_addressThis property holds the IP address of the connecting client.
azure_relay_connection_remote_portThis property holds the port of the connecting client.
azure_relay_connection_sub_protocolsThe subprotocols (application-level protocols layered over the WebSocket Protocol) sent by the client in the initial WebSocket connection request.
azure_relay_connection_timeoutThis property specifies a timeout for the class.
default_timeoutAn initial timeout value to be used by incoming connections.
firewall_auto_detectThis property tells the class whether or not to automatically detect and use firewall system settings, if available.
firewall_typeThis property determines the type of firewall to connect through.
firewall_hostThis property contains the name or IP address of firewall (optional).
firewall_passwordThis property contains a password if authentication is to be used when connecting through the firewall.
firewall_portThis property contains the TCP port for the firewall Host .
firewall_userThis property contains a user name if authentication is to be used connecting through a firewall.
forwarding_hostThe address to which traffic will automatically be forwarded.
forwarding_portThe port to which traffic will automatically be forwarded.
hybrid_connectionThe hybrid connection name.
listeningIf True, connects to the Azure Relay Service and listens for incoming connections.
local_hostThe name of the local host or user-assigned IP interface through which connections are initiated or accepted.
namespace_addressThe namespace address of the relay.
proxy_auth_schemeThis property is used to tell the class which type of authorization to perform when connecting to the proxy.
proxy_auto_detectThis property tells the class whether or not to automatically detect and use proxy system settings, if available.
proxy_passwordThis property contains a password if authentication is to be used for the proxy.
proxy_portThis property contains the TCP port for the proxy Server (default 80).
proxy_serverIf a proxy Server is given, then the HTTP request is sent to the proxy instead of the server otherwise specified.
proxy_sslThis property determines when to use SSL for the connection to the proxy.
proxy_userThis property contains a user name, if authentication is to be used for the proxy.
ssl_accept_server_cert_encodedThe certificate (PEM/base64 encoded).
ssl_cert_encodedThe certificate (PEM/base64 encoded).
ssl_cert_storeThe name of the certificate store for the client certificate.
ssl_cert_store_passwordIf the certificate store is of a type that requires a password, this property is used to specify that password in order to open the certificate store.
ssl_cert_store_typeThe type of certificate store for this certificate.
ssl_cert_subjectThe subject of the certificate used for client authentication.
ssl_server_cert_encodedThe certificate (PEM/base64 encoded).

Method List


The following is the full list of the methods of the class with short descriptions. Click on the links for further details.

configSets or retrieves a configuration setting.
disconnectDisconnect the specified client.
do_eventsProcesses events from the internal message queue.
interruptInterrupts a synchronous send to the remote host.
sendSends binary data to the client.
send_fileSend file to the remote host.
send_http_responseSend the HTTP response.
send_textSends text data to the client.
shutdownShuts down the server.

Event List


The following is the full list of the events fired by the class with short descriptions. Click on the links for further details.

on_connectedFired immediately after a connection completes (or fails).
on_connection_connectedFired when a client has connected.
on_connection_data_inFired when data is received.
on_connection_disconnectedFired when a WebSocket connection is disconnected.
on_connection_errorInformation about errors during data delivery.
on_connection_ready_to_sendFired when the class is ready to send data.
on_connection_requestFires when a WebSocket connection is requested.
on_connection_statusFired to indicate changes in connection state.
on_disconnectedFired when a connection is closed.
on_errorInformation about errors during data delivery.
on_headerFired every time a header line comes in.
on_http_requestFires when an HTTP request is received.
on_logFires once for each log message.
on_redirectFired when a redirection is received from the server.
on_ssl_server_authenticationFired after the server presents its certificate to the client.
on_ssl_statusShows the progress of the secure connection.

Configuration Settings


The following is a list of configuration settings for the class with short descriptions. Click on the links for further details.

AccessTokenReturns an access token for use outside of the classes.
AutoRenewThresholdThe threshold in seconds after which the token is renewed.
AzureRelayKeepAliveTimeThe inactivity period in seconds before a ping packet is sent to keep the connection alive.
DiagnosticIdA diagnostic id used to enable end-to-end tracing.
LogLevelThe level of detail that is logged.
TokenValidityThe validity time in seconds of the access token.
BufferMessageIndicates whether or not the entire message is buffered before firing the DataIn event.
DisconnectStatusCodeSpecifies the status code when closing a connection.
DisconnectStatusDescriptionSpecifies the message associated with the disconnect status code.
MaxFrameSizeSpecifies the maximum size of the outgoing message in bytes before fragmentation occurs.
MessageLength[ConnectionId]The length of the message (in bytes) when sending asynchronously.
WaitForCloseResponseDetermines whether or not the class will forcibly close a connection.
AllowedClientsA comma-separated list of host names or IP addresses that can access the class.
BindExclusivelyWhether or not the class considers a local port reserved for exclusive use.
ConnectionUIDThe unique connectionId for a connection.
DefaultConnectionTimeoutThe inactivity timeout applied to the SSL handshake.
InBufferSizeThe size in bytes of the incoming queue of the socket.
KeepAliveIntervalThe retry interval, in milliseconds, to be used when a TCP keep-alive packet is sent and no response is received.
KeepAliveRetryCountThe number of keep-alive packets to be sent before the remotehost is considered disconnected.
KeepAliveTimeThe inactivity time in milliseconds before a TCP keep-alive packet is sent.
MaxConnectionsThe maximum number of connections available.
OutBufferSizeThe size in bytes of the outgoing queue of the socket.
TcpNoDelayWhether or not to delay when sending packets.
UseIOCPWhether to use the completion port I/O model.
UseIPv6Whether to use IPv6.
UseWindowsMessagesWhether to use the WSAAsyncSelect I/O model.
LogSSLPacketsControls whether SSL packets are logged when using the internal security API.
OpenSSLCADirThe path to a directory containing CA certificates.
OpenSSLCAFileName of the file containing the list of CA's trusted by your application.
OpenSSLCipherListA string that controls the ciphers to be used by SSL.
OpenSSLPrngSeedDataThe data to seed the pseudo random number generator (PRNG).
ReuseSSLSessionDetermines if the SSL session is reused.
SSLCACertFilePathsThe paths to CA certificate files on Unix/Linux.
SSLCACertsA newline separated list of CA certificate to use during SSL client authentication.
SSLCheckCRLWhether to check the Certificate Revocation List for the server certificate.
SSLCipherStrengthThe minimum cipher strength used for bulk encryption.
SSLEnabledCipherSuitesThe cipher suite to be used in an SSL negotiation.
SSLEnabledProtocolsUsed to enable/disable the supported security protocols.
SSLEnableRenegotiationWhether the renegotiation_info SSL extension is supported.
SSLIncludeCertChainWhether the entire certificate chain is included in the SSLServerAuthentication event.
SSLNegotiatedCipherReturns the negotiated ciphersuite.
SSLNegotiatedCipherStrengthReturns the negotiated ciphersuite strength.
SSLNegotiatedCipherSuiteReturns the negotiated ciphersuite.
SSLNegotiatedKeyExchangeReturns the negotiated key exchange algorithm.
SSLNegotiatedKeyExchangeStrengthReturns the negotiated key exchange algorithm strength.
SSLNegotiatedProtocolReturns the negotiated protocol version.
SSLProviderThe name of the security provider to use.
SSLSecurityFlagsFlags that control certificate verification.
SSLServerCACertsA newline separated list of CA certificate to use during SSL server certificate validation.
TLS12SignatureAlgorithmsDefines the allowed TLS 1.2 signature algorithms when UseInternalSecurityAPI is True.
TLS12SupportedGroupsThe supported groups for ECC.
TLS13KeyShareGroupsThe groups for which to pregenerate key shares.
TLS13SignatureAlgorithmsThe allowed certificate signature algorithms.
TLS13SupportedGroupsThe supported groups for (EC)DHE key exchange.

Copyright (c) 2022 /n software inc. - All rights reserved.
IPWorks MQ 2020 Python Edition - Version 20.0 [Build 8162]