/// <summary> /// Initializes a new instance of the <see cref="EtpServer"/> class. /// </summary> /// <param name="webSocket">The web socket.</param> /// <param name="etpVersion">The ETP version for the session.</param> /// <param name="encoding">The ETP encoding for the session.</param> /// <param name="info">The server's information.</param> /// <param name="parameters">The server's parameters.</param> /// <param name="headers">The WebSocket or HTTP headers.</param> public EtpServer(EtpServerWebSocket webSocket, EtpVersion etpVersion, EtpEncoding encoding, EtpEndpointInfo info, EtpEndpointParameters parameters = null, IDictionary <string, string> headers = null) : base(etpVersion, encoding, info, parameters, headers, false, webSocket.WebSocketSession.SessionID, false) { _webSocket = webSocket; _session = _webSocket.WebSocketSession; _session.SocketSession.Closed += OnSocketSessionClosed; }
/// <summary> /// Initializes a new instance of the <see cref="EtpClient"/> class. /// </summary> /// <param name="uri">The ETP server URI.</param> /// <param name="etpVersion">The ETP version for the session.</param> /// <param name="encoding">The ETP encoding for the session.</param> /// <param name="info">The client's information.</param> /// <param name="parameters">The client's parameters.</param> /// <param name="authorization">The client's authorization details.</param> /// <param name="headers">The WebSocket headers.</param> public EtpClient(string uri, EtpVersion etpVersion, EtpEncoding encoding, EtpEndpointInfo info, EtpEndpointParameters parameters, Security.Authorization authorization = null, IDictionary <string, string> headers = null) : base(etpVersion, encoding, info, parameters, headers, true, null, false) { Headers.SetAuthorization(authorization); _socket = new W4N.WebSocket(uri, subProtocol: EtpFactory.GetSubProtocol(EtpVersion), cookies: null, customHeaderItems: Headers.ToList(), userAgent: info.ApplicationName); }
/// <summary> /// Initializes a new instance of the <see cref="EtpClient"/> class. /// </summary> /// <param name="uri">The ETP server URI.</param> /// <param name="etpVersion">The ETP version for the session.</param> /// <param name="encoding">The ETP encoding for the session.</param> /// <param name="info">The client's information.</param> /// <param name="parameters">The client's parameters.</param> /// <param name="authorization">The client's authorization details.</param> /// <param name="headers">The WebSocket headers.</param> public EtpClient(string uri, EtpVersion etpVersion, EtpEncoding encoding, EtpEndpointInfo info, EtpEndpointParameters parameters = null, Security.Authorization authorization = null, IDictionary <string, string> headers = null) : base(etpVersion, encoding, new ClientWebSocket(), info, parameters, headers, true, null) { Headers.SetAuthorization(authorization); ClientSocket.Options.AddSubProtocol(EtpFactory.GetSubProtocol(EtpVersion)); foreach (var item in Headers) { ClientSocket.Options.SetRequestHeader(item.Key, item.Value); } Uri = new Uri(uri); // NOTE: User-Agent cannot be set on a .NET Framework ClientWebSocket: // https://github.com/dotnet/corefx/issues/26627 }
/// <summary> /// Sets the encoding header. /// </summary> /// <param name="headers">The header dictionary to set the encoding in.</param> /// <param name="encoding">The encoding to set.</param> public static void SetEncoding(this IDictionary <string, string> headers, EtpEncoding encoding) { if (headers == null) { return; } var value = encoding.ToHeaderValue(); if (value == null) { headers.Remove(EtpHeaders.Encoding); } else { headers[EtpHeaders.Encoding] = value; } }
/// <summary> /// Initializes a new instance of the <see cref="EtpSessionNativeBase"/> class. /// </summary> /// <param name="webSocket">The web socket.</param> /// <param name="etpVersion">The ETP version.</param> /// <param name="encoding">The ETP encoding for the session.</param> /// <param name="info">The endpoint's information.</param> /// <param name="parameters">The endpoint's parameters.</param> /// <param name="headers">The WebSocket or HTTP headers.</param> /// <param name="isClient">Whether or not this is the client-side of the session.</param> /// <param name="sessionId">The session ID if this is a server.</param> public EtpSessionNativeBase(EtpVersion etpVersion, EtpEncoding encoding, WebSocket webSocket, EtpEndpointInfo info, EtpEndpointParameters parameters, IDictionary <string, string> headers, bool isClient, string sessionId) : base(etpVersion, encoding, info, parameters, headers, isClient, sessionId, !isClient) { Socket = webSocket; }
/// <summary> /// Creates an <see cref="IEtpServer"/> instance. /// </summary> /// <param name="webSocket">The websocket to create the server for.</param> /// <param name="etpVersion">The ETP version for the session.</param> /// <param name="encoding">The ETP encoding for the session.</param> /// <param name="headers">The websocket headers.</param> /// <returns>The created server.</returns> protected override IEtpServer CreateServerCore(IEtpServerWebSocket webSocket, EtpVersion etpVersion, EtpEncoding encoding, IDictionary <string, string> headers) { var ws = webSocket as EtpServerWebSocket; if (ws == null) { throw new ArgumentException("Must be a native websocket", nameof(webSocket)); } return(new EtpServer(ws.WebSocket, etpVersion, encoding, EndpointInfo, parameters: EndpointParameters.CloneForVersion(etpVersion), headers: headers)); }
/// <summary> /// Converts an encoding to the string value used in headers. /// </summary> /// <param name="encoding">The encoding to get the string value for.</param> /// <returns>The string value.</returns> public static string ToHeaderValue(this EtpEncoding encoding) { FieldInfo fi = encoding.GetType().GetField(encoding.ToString()); return(fi.GetCustomAttributes(typeof(DescriptionAttribute), false)?.Cast <DescriptionAttribute>().FirstOrDefault()?.Description); }
/// <summary> /// Initializes a new instance of the <see cref="EtpServer"/> class. /// </summary> /// <param name="webSocket">The web socket.</param> /// <param name="etpVersion">The ETP version for the session.</param> /// <param name="encoding">The ETP encoding for the session.</param> /// <param name="info">The server's information.</param> /// <param name="parameters">The server's parameters.</param> /// <param name="headers">The WebSocket or HTTP headers.</param> public EtpServer(WebSocket webSocket, EtpVersion etpVersion, EtpEncoding encoding, EtpEndpointInfo info, EtpEndpointParameters parameters = null, IDictionary <string, string> headers = null) : base(etpVersion, encoding, webSocket, info, parameters, headers, false, Guid.NewGuid().ToString()) { }
/// <summary> /// Creates an <see cref="IEtpServer"/> instance. /// </summary> /// <param name="webSocket">The websocket to create the server for.</param> /// <param name="etpVersion">The ETP version for the session.</param> /// <param name="encoding">The ETP encoding for the session.</param> /// <param name="headers">The websocket headers.</param> /// <returns>The created server.</returns> protected abstract IEtpServer CreateServerCore(IEtpServerWebSocket webSocket, EtpVersion etpVersion, EtpEncoding encoding, IDictionary <string, string> headers);
/// <summary> /// Creates an <see cref="IEtpServer"/> instance. /// </summary> /// <param name="webSocket">The websocket to create the server for.</param> /// <param name="etpVersion">The ETP version for the session.</param> /// <param name="encoding">The ETP encoding for the session.</param> /// <param name="headers">The websocket headers.</param> /// <returns>The created server.</returns> public IEtpServer CreateServer(IEtpServerWebSocket webSocket, EtpVersion etpVersion, EtpEncoding encoding, IDictionary <string, string> headers) { if (webSocket == null) { throw new ArgumentNullException(nameof(webSocket)); } if (!IsEtpVersionSupported(etpVersion)) { throw new ArgumentException($"Unsupported ETP version: {etpVersion.ToVersionString()}"); } var server = CreateServerCore(webSocket, etpVersion, encoding, headers ?? new Dictionary <string, string>()); Logger.Debug(Log("[{0}] Server created.", server.SessionKey)); server.SessionClosed += OnServerSessionClosed; Servers[server.SessionId] = server; ServerCreated?.Invoke(this, new EtpServerEventArgs(server)); return(server); }
/// <summary> /// Checks if the specified encoding is supported. /// </summary> /// <param name="encoding">The encoding to check.</param> /// <returns><c>true</c> if the encoding is supported; <c>false</c> otherwise.</returns> public bool IsEncodingSupported(EtpEncoding encoding) { return(SupportedEncodings.Any(e => e == encoding)); }
/// <summary> /// Creates an <see cref="EtpClient"/> instance configurated with the /// current connection and authorization parameters. /// </summary> /// <param name="webSocketType">The WebSocket type.</param> /// <param name="etpVersion">The ETP version.</param> /// <param name="url">The WebSocket URL.</param> /// <param name="authorization">The client's authorization details.</param> /// <param name="etpEncoding">The encoding to use.</param> /// <returns>A new <see cref="IEtpClient"/> instance.</returns> protected IEtpClient CreateClient(WebSocketType webSocketType, EtpVersion etpVersion, string url, Security.Authorization authorization = null, EtpEncoding etpEncoding = EtpEncoding.Binary) { var version = GetType().Assembly.GetName().Version.ToString(); if (authorization == null) { authorization = Security.Authorization.Basic(TestSettings.Username, TestSettings.Password); } var endpointInfo = EtpFactory.CreateClientEndpointInfo(GetType().AssemblyQualifiedName, version, "ETP DevKit Integration Test"); var client = EtpFactory.CreateClient(webSocketType, url, etpVersion, etpEncoding, endpointInfo, authorization: authorization); if (etpVersion == EtpVersion.v11) { client.Register(new v11.Protocol.ChannelStreaming.ChannelStreamingConsumerHandler()); client.Register(new v11.Protocol.Discovery.DiscoveryCustomerHandler()); client.Register(new v11.Protocol.Store.StoreCustomerHandler()); } else { client.Register(new v12.Protocol.ChannelStreaming.ChannelStreamingConsumerHandler()); client.Register(new v12.Protocol.Discovery.DiscoveryCustomerHandler()); client.Register(new v12.Protocol.Store.StoreCustomerHandler()); } return(client); }