internal override bool Connect(TheQueuedSender pSender) { if (pSender == null) // || pSender.IsConnecting) return false; if (pSender.IsConnected) return true; IsClient = true; if (MyQSender == null) MyQSender = pSender; Uri TargetUri = TheCommonUtils.CUri(pSender.MyTargetNodeChannel.TargetUrl, true); string TargetUriPath = TargetUri.AbsolutePath; OwnerNodeID = MyQSender.MyTargetNodeChannel.cdeMID; Uri tTarget = new Uri(TargetUri, TheBaseAssets.MyScopeManager.GetISBPath(TargetUriPath, TheCommonUtils.GetOriginST(pSender.MyTargetNodeChannel), pSender.MyTargetNodeChannel.SenderType, 1, Guid.Empty, true)); tTarget = tTarget.SetWSInfo(tTarget.Port, ""); try { IsActive = true; CloseFired = false; ConnectSuccess = true; if (websocket != null && !websocket.IsAlive) { try { websocket.OnError -= websocket_OnError; websocket.OnOpen -= websocket_Opened; websocket.OnMessage -= websocket_OnMessage; websocket.OnClose -= websocket_OnClose; websocket.Close(); TheBaseAssets.MySYSLOG.WriteToLog(8812, new TSM("WSProcessorConnect", string.Format("Websockets were still valid but not alive: closed and will recreate! IsConnecting:{0}", pSender.IsConnecting), eMsgLevel.l4_Message)); } catch { TheBaseAssets.MySYSLOG.WriteToLog(8812, new TSM("WSProcessorConnect", string.Format("Websockets were still valid but not alive: Error while closing, will recreate. Possible socket leak! IsConnecting:{0}", pSender.IsConnecting), eMsgLevel.l4_Message)); } finally { websocket = null; } } if (websocket == null) { websocket = new WebSocket(tTarget.ToString()); if (!string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ProxyUrl)) { try { websocket.SetProxy(TheBaseAssets.MyServiceHostInfo.ProxyUrl, TheBaseAssets.MyServiceHostInfo.ProxyUID, TheBaseAssets.MyServiceHostInfo.ProxyPWD); TheBaseAssets.MySYSLOG.WriteToLog(8812, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM("WSProcessorCSWS", $"Websockets proxy set: {TheBaseAssets.MyServiceHostInfo.ProxyUrl} UID: {!string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ProxyUID)} PWD: {!string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ProxyPWD)}", eMsgLevel.l4_Message)); } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(4365, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("WSProcessor", "Error setting Proxy credentials:", eMsgLevel.l1_Error, e.ToString())); } } websocket.Log.Level = LogLevel.Fatal; websocket.Log.Output = sinkLog; websocket.SslConfiguration.ServerCertificateValidationCallback += TheCommCore.ValidateServerCertificate; var myTargetNodeChannel = MyQSender?.MyTargetNodeChannel; if (!string.IsNullOrEmpty(myTargetNodeChannel?.ClientCertificateThumb)) { try { X509Certificate2Collection certs = TheCertificates.GetClientCertificatesByThumbprint(myTargetNodeChannel?.ClientCertificateThumb); if (certs?.Count < 1) { TheBaseAssets.MySYSLOG.WriteToLog(4365, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("WSProcessor", $"Error setting Client Certificate - Requested Cert with Thumbprint {myTargetNodeChannel?.ClientCertificateThumb} not found", eMsgLevel.l1_Error)); return false; } websocket.SslConfiguration.ClientCertificates = certs; websocket.SslConfiguration.ClientCertificateSelectionCallback = wsSharpClientCertSelector; } catch (Exception certex) { TheBaseAssets.MySYSLOG.WriteToLog(4365, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("WSProcessor", "Error setting Client Certificate", eMsgLevel.l1_Error, certex.ToString())); return false; } } websocket.OnError += websocket_OnError; websocket.OnOpen += websocket_Opened; websocket.OnMessage += websocket_OnMessage; websocket.OnClose += websocket_OnClose; websocket.Connect(); } else TheBaseAssets.MySYSLOG.WriteToLog(8812, new TSM("WSProcessorConnect", string.Format("Websockets are still valid and will be recycled! IsConnecting:{0}",pSender.IsConnecting),eMsgLevel.l4_Message)); } catch (Exception ex) { Shutdown(true, "1650:Connect Failed: " + ex); } return ConnectSuccess; }
internal override bool Connect(TheQueuedSender pSender) { if (pSender == null /*|| pSender.MyTargetNodeChannel==null*/) { return(false); } if (pSender.IsConnected) { return(true); } IsClient = true; if (MyQSender == null) { MyQSender = pSender; } var _MyTargetNodeChannel = pSender?.MyTargetNodeChannel; Uri TargetUri = TheCommonUtils.CUri(_MyTargetNodeChannel?.TargetUrl, true); if (TargetUri == null) { TheBaseAssets.MySYSLOG.WriteToLog(4365, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("WSProcessor", "Invalid Target URL", eMsgLevel.l1_Error, $"{_MyTargetNodeChannel?.ToString()}")); return(false); } OwnerNodeID = _MyTargetNodeChannel.cdeMID; string TargetUriPath = TargetUri.AbsolutePath; Uri tTarget = new Uri(TargetUri, TheBaseAssets.MyScopeManager.GetISBPath(TargetUriPath, TheCommonUtils.GetOriginST(_MyTargetNodeChannel), _MyTargetNodeChannel.SenderType, 1, Guid.Empty, true)); tTarget = tTarget.SetWSInfo(tTarget.Port, ""); string connectFailureReason = ""; try { IsActive = true; CloseFired = false; ConnectSuccess = false; ClientWebSocket wsClientSocket = new ClientWebSocket(); websocket = wsClientSocket; if (!string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ProxyUrl)) { try { System.Net.NetworkCredential tNet = null; if (!string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ProxyUID)) { tNet = new System.Net.NetworkCredential(TheBaseAssets.MyServiceHostInfo.ProxyUID, TheBaseAssets.MyServiceHostInfo.ProxyPWD); } if (wsClientSocket.Options != null) { wsClientSocket.Options.Proxy = new System.Net.WebProxy(TheBaseAssets.MyServiceHostInfo.ProxyUrl, true, null, tNet); TheBaseAssets.MySYSLOG.WriteToLog(4365, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM("WSProcessor", $"Web Proxy set: {TheBaseAssets.MyServiceHostInfo.ProxyUrl} UID: {!string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ProxyUID)} PWD: {!string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ProxyPWD)}", eMsgLevel.l4_Message)); } } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(4365, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("WSProcessor", "Error setting Proxy credentials:", eMsgLevel.l1_Error, e.ToString())); } } if (!string.IsNullOrEmpty(_MyTargetNodeChannel?.ClientCertificateThumb)) { try { X509Certificate2Collection cert = TheCertificates.GetClientCertificatesByThumbprint(_MyTargetNodeChannel?.ClientCertificateThumb); if (cert?.Count < 1) { TheBaseAssets.MySYSLOG.WriteToLog(4365, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("WSProcessor", $"Error setting Client Certificate - Requested Cert with Thumbprint {_MyTargetNodeChannel?.ClientCertificateThumb} not found", eMsgLevel.l1_Error)); return(false); } wsClientSocket.Options.ClientCertificates = cert; } catch (Exception certex) { TheBaseAssets.MySYSLOG.WriteToLog(4365, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("WSProcessor", "Error setting Client Certificate", eMsgLevel.l1_Error, certex.ToString())); return(false); } } Task myTask = wsClientSocket.ConnectAsync(tTarget, CancellationToken.None); myTask.Wait(TheBaseAssets.MyServiceHostInfo.TO.WsTimeOut * 12); if (myTask.IsCompleted) { // CODE REVIEW MH: ProcessWS synchronously can run for quite a bit until the first await. Should we start this as a new task? ConnectSuccess = true; var taskNoWait = ProcessWS(); //Sets Connecting and Connected when ready } else { TheBaseAssets.MySYSLOG.WriteToLog(4365, new TSM("WSProcessor", $"WSClient Connect Request timed out Task:{myTask.Status}", eMsgLevel.l2_Warning)); connectFailureReason += "Connect Request Timeout"; } } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(4365, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM("WSProcessor", "ClientConnect Request Failed:", eMsgLevel.l1_Error, e.ToString())); connectFailureReason += TheCommonUtils.GetAggregateExceptionMessage(e, false); } if (ConnectSuccess) //This prevents double shutdown as the eventconnecte with ConnectSuccess=false will cause a shutdown again { eventConnected?.Invoke(this, ConnectSuccess, $"1602:{connectFailureReason}"); } else { Shutdown(true, $"1603:WSConnect was not successful ({connectFailureReason})"); } return(ConnectSuccess); }