void sinkUploadDataCompleted(TheRequestData eResult) { if (eResult != null && eResult.ResponseBuffer != null && eResult.ResponseBuffer.Length > 0) { try { TheCDEKPIs.IncrementKPI(eKPINames.QSReceivedTSM); TheCDEKPIs.IncrementKPI(eKPINames.QKBReceived, eResult.ResponseBuffer.Length); if (eResult.StatusCode != 200) { TheBaseAssets.MySYSLOG.WriteToLog(243, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("QueuedSender", $"Server responded with not-ok ...Code: {eResult.StatusCode} error: {TheCommonUtils.CArray2UTF8String(eResult.ResponseBuffer)}", eMsgLevel.l2_Warning), true); FireSenderProblem(new TheRequestData() { ErrorDescription = $"{eResult.StatusCode}:Server responded with not-ok ...Code: {eResult.StatusCode}" }); return; } List <TheDeviceMessage> tDevList = null; if (eResult.ResponseMimeType.Contains("zip")) { tDevList = TheDeviceMessage.DeserializeJSONToObject(TheCommonUtils.cdeDecompressToString(eResult.ResponseBuffer)); } else { tDevList = TheDeviceMessage.DeserializeJSONToObject(TheCommonUtils.CArray2UTF8String(eResult.ResponseBuffer)); } if (tDevList != null && tDevList.Count > 0) { eResult.StatusCode = 0; TheCorePubSub.ProcessClientDeviceMessage(this, eResult, tDevList); } } catch (Exception ee) { TheBaseAssets.MySYSLOG.WriteToLog(243, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("QueuedSender", $"Error in {MyTargetNodeChannel?.ToMLString()}", eMsgLevel.l1_Error, ee.ToString())); } } else { if (MyTargetNodeChannel.SenderType != cdeSenderType.CDE_SERVICE) { TheBaseAssets.MySYSLOG.WriteToLog(243, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM("QueuedSender", $"Did not receive any data from {MyTargetNodeChannel?.ToMLString()}", eMsgLevel.l6_Debug)); } } }
internal void ProcessIncomingData(string pPostString, byte[] pPostData, int pPostDataLength) { if (!IsActive) { return; } if (MySessionRequestData == null || MySessionRequestData?.SessionState?.HasExpired == true) { TheBaseAssets.MySYSLOG.WriteToLog(4360, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("TheWSProcessor", $"Incoming Data on expired session ({MySessionRequestData?.SessionState?.HasExpired}) detected - shutting down websockets", eMsgLevel.l6_Debug)); Shutdown(false, "1600:Incoming Data on expired session detected - shutting down websockets"); return; } TheRequestData tRequestData = TheRequestData.CloneForWS(MySessionRequestData); TheCDEKPIs.IncrementKPI(eKPINames.QSReceivedTSM); tRequestData.PostData = pPostData; tRequestData.PostDataIdx = 0; if (pPostData == null) { tRequestData.PostDataLength = 0; } else { tRequestData.PostDataLength = pPostDataLength > 0 ? pPostDataLength : pPostData.Length; TheCDEKPIs.IncrementKPI(eKPINames.QKBReceived, tRequestData.PostDataLength); } if (IsClient) { string cmdString = ""; try { List <TheDeviceMessage> tDevList; if (tRequestData.PostData == null && !string.IsNullOrEmpty(pPostString)) { if (pPostString[0] != '[') { return; } tDevList = TheDeviceMessage.DeserializeJSONToObject(pPostString); } else { if (tRequestData.PostData == null) { return; //Edge Case but could happen - all what follows required ProcessClientDeviceMsg } cmdString = tRequestData.PostData[0] == (byte)'[' ? TheCommonUtils.CArray2UTF8String(tRequestData.PostData, 0, tRequestData.PostDataLength) : TheCommonUtils.cdeDecompressToString(tRequestData.PostData, 0, tRequestData.PostDataLength); tDevList = TheDeviceMessage.DeserializeJSONToObject(cmdString); } TheCorePubSub.ProcessClientDeviceMessage(MyQSender, tRequestData, tDevList); } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(4361, new TSM("WSClient", "Message-Received Processing Error", eMsgLevel.l1_Error, e.ToString())); } } else { try { if (tRequestData.PostData == null && !string.IsNullOrEmpty(pPostString)) { tRequestData.PostData = TheCommonUtils.CUTF8String2Array(pPostString); tRequestData.PostDataLength = tRequestData.PostData.Length; } if (TheCommCore.MyHttpService != null) { TheCommCore.MyHttpService.cdeProcessPost(tRequestData); } } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(4362, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("TheWSServer", "WebSocketServer-ProcessRequest Error", eMsgLevel.l1_Error, e.ToString())); } } if (MySessionRequestData.SessionState == null) { MySessionRequestData.SessionState = tRequestData.SessionState; } if (MySessionRequestData.DeviceID == Guid.Empty) { MySessionRequestData.DeviceID = tRequestData.DeviceID; } if (tRequestData.SessionState != null && MyQSender != null) { if (MyQSender.IsConnecting) { MyQSender.IsConnected = true; if (MyQSender.eventConnected != null) { TheCommonUtils.cdeRunAsync("QueueConnected", true, (p) => { MyQSender?.eventConnected?.Invoke(MyQSender, MyQSender.MyTargetNodeChannel); }); } MyQSender.MyISBlock?.FireEvent("Connected"); } //NEW3.124: Reset Heartbeat on Ws Post MyQSender.ResetHeartbeatTimer(false, tRequestData.SessionState); } else { TheBaseAssets.MySYSLOG.WriteToLog(4361, TSM.L(eDEBUG_LEVELS.ESSENTIALS)?null: new TSM("WSClient", $"No Request Session {tRequestData.SessionState!=null} or no QSender Found {MyQSender!=null}) IsClient={IsClient}", eMsgLevel.l2_Warning)); } try { if (tRequestData.ResponseBuffer != null) { PostToSocket(null, tRequestData.ResponseBuffer, IsClient, false); } } catch (Exception ex) // For debugging { //if (eventClosed != null) // eventClosed("Connection Error: " + ex.ToString()); Shutdown(true, "1601:PostToSocket Error: " + ex); } }
internal virtual void PostToSocket(TheDeviceMessage pMsg, byte[] pPostBuffer, bool pSendAsBinary, bool IsInitialConnect) { }
private void TheWSSenderThread() { if (IsSenderThreadRunning /*|| MyTargetNodeChannel==null*/) { return; } if (MyTargetNodeChannel == null) { TheBaseAssets.MySYSLOG.WriteToLog(2371, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("WSQueuedSender", $"WSSender Thread could not be started because MTNC is null", eMsgLevel.l4_Message)); return; } TheDiagnostics.SetThreadName($"QSender:{MyTargetNodeChannel} WSSenderThread", true); IsSenderThreadRunning = true; mre = new ManualResetEvent(false); CloudCounter++; StringBuilder tSendBufferStr = new StringBuilder(TheBaseAssets.MyServiceHostInfo?.IsMemoryOptimized == true ? 1024 : TheBaseAssets.MAX_MessageSize[(int)MyTargetNodeChannel.SenderType] * 2); int QDelay = TheCommonUtils.CInt(TheBaseAssets.MySettings.GetSetting("ThrottleWS")); if (QDelay < 2) { QDelay = 2; } if (MyTargetNodeChannel.SenderType == cdeSenderType.CDE_JAVAJASON && TheBaseAssets.MyServiceHostInfo.WsJsThrottle > QDelay) { QDelay = TheBaseAssets.MyServiceHostInfo.WsJsThrottle; } if (eventSenderThreadRunning != null) { TheCommonUtils.cdeRunAsync("EventSenderThreadRunning", true, (p) => { eventSenderThreadRunning(this); }); } MyISBlock?.FireEvent("SenderThreadCreated"); try { while (TheBaseAssets.MasterSwitch && IsAlive && MyWebSocketProcessor.IsActive && IsSenderThreadRunning) { while (TheBaseAssets.MasterSwitch && IsAlive && MyWebSocketProcessor.IsActive && IsSenderThreadRunning && (MyTargetNodeChannel.MySessionState == null || IsConnecting || IsInWSPost || !MyWebSocketProcessor.ProcessingAllowed || MyCoreQueue.Count == 0)) { if (IsInWSPost && MyCoreQueue.Count > 200) { TheBaseAssets.MySYSLOG.WriteToLog(235, new TSM("WSQueuedSender", $"IsInWSPost was still on and has been reset for {MyTargetNodeChannel?.ToMLString()}", eMsgLevel.l2_Warning)); IsInWSPost = false; } if (MyTargetNodeChannel.MySessionState == null && !IsConnecting && MyCoreQueue.Count > 0) { break; } mre.WaitOne(QDelay); // System.Diagnostics.Debug.WriteLine($"Waiting :{IsAlive} {MyCoreQueue.Count} {IsConnected} {IsConnecting} {IsInWSPost} {MyWebSocketProcessor.ProcessingAllowed}"); } if (!TheBaseAssets.MasterSwitch || !IsAlive || !MyWebSocketProcessor.IsActive || MyTargetNodeChannel.MySessionState == null) { TheBaseAssets.MySYSLOG.WriteToLog(235, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("WSQueuedSender", $"WSSender Thread Condition failed - Ending SenderThread IsAlive:{IsAlive} MyWSProccAlive:{(MyWebSocketProcessor == null ? false : MyWebSocketProcessor.IsActive)},SessionState:{(MyTargetNodeChannel?.MySessionState != null)} IsConnecting:{IsConnecting} IsConnected:{IsConnected}", eMsgLevel.l2_Warning)); break; } IsInWSPost = true; int MCQCount = 0; int IsBatchOn = 0; int FinalCnt = 0; #if CDE_NET35 tSendBufferStr = new StringBuilder(TheBaseAssets.MAX_MessageSize[(int)MyTargetNodeChannel.SenderType] * 2); #else tSendBufferStr.Clear(); #endif tSendBufferStr.Append("["); do { TheCoreQueueContent tQueued = GetNextMessage(out MCQCount); if (tQueued != null) { TheDeviceMessage tDev = new TheDeviceMessage(); if (tQueued.OrgMessage != null) { if (tQueued.OrgMessage.ToCloudOnly() && MyTargetNodeChannel.SenderType == cdeSenderType.CDE_CLOUDROUTE) { tQueued.OrgMessage.SetToCloudOnly(false); } tQueued.OrgMessage.GetNextSerial(tQueued.SubMsgCnt); tDev.MSG = tQueued.OrgMessage; } var tCurSessState = MyTargetNodeChannel.MySessionState; if (MyTargetNodeChannel.SenderType == cdeSenderType.CDE_JAVAJASON) { if (tDev.MSG == null) { // we have a pickup: never send to browser if (IsBatchOn > 0) { continue; //ignore HB as other messages are already in the queue } else { IsInWSPost = false; continue; //dont sent empty Message to Browser - HB not necessary } } else { tDev.MSG.SEID = null; //SECURITY: Don't send SEID to Browser tDev.MSG.UID = null; //SECURITY: Don't send SEID to Browser tDev.MSG.SID = null; //SECURITY: Don't send SID to Browser } tDev.DID = MyTargetNodeChannel.cdeMID.ToString(); } else { if (tDev.MSG == null) // CODE REVIEW: DO we also need to detect other simple topics with response message and ensure they get into their own batch? { // we have a pickup (only to be sent if nothing else is being sent) if (IsBatchOn > 0) { // Ignore pickups if another message is already being sent (can happen with race condition between check before pickup enqueue and dequeue here - another TSM could be enqueue in that time) continue; } else { // Close the batch here so that no other TSM gets into the same batch // Possible optimization: check if other TSMs are available and skip the pickup here as well IsBatchOn = -1; // IsInWSPost = false; } } tDev.DID = TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID.ToString(); if (!string.IsNullOrEmpty(tQueued?.OrgMessage?.SID)) { tDev.SID = tQueued.OrgMessage.SID; } else { if (TheBaseAssets.MyServiceHostInfo.EnableFastSecurity) { tDev.SID = tCurSessState.SScopeID; //SECURITY: All tDevs will have same Session Scrambled ScopeID - 4.209: SID from TSM if set. } else { tDev.SID = TheBaseAssets.MyScopeManager.GetScrambledScopeID(tCurSessState.SScopeID, false); //GRSI: high frequency } } } TheCDEKPIs.IncrementKPI(eKPINames.QSSent); tDev.TOP = tQueued.Topic; tDev.FID = tCurSessState.GetNextSerial().ToString(); if (TheCommonUtils.IsDeviceSenderType(MyTargetNodeChannel.SenderType)) //IDST-OK: Must create RSA for Devices { TheCommonUtils.CreateRSAKeys(tCurSessState); if (TheBaseAssets.MyServiceHostInfo.SecurityLevel > 3) { tDev.RSA = tCurSessState.RSAPublic; //TODO: Make depending on switch (HighSecurity=RSAGeneration every three seconds } } if (MyTargetNodeChannel.SenderType != cdeSenderType.CDE_CLOUDROUTE) //CODE-REVIEW: 4.0113 is this still valid: Must not reset HB if talking to cloud due to Cloud-Disconnect issue { ResetHeartbeatTimer(false, tCurSessState); } if (!cdeSenderType.CDE_JAVAJASON.Equals(MyTargetNodeChannel.SenderType)) { tDev.NPA = TheBaseAssets.MyScopeManager.GetISBPath(TheBaseAssets.MyServiceHostInfo.RootDir, MyTargetNodeChannel.SenderType, TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.SenderType, tCurSessState.FID, tCurSessState.cdeMID, true); } if (MyTargetNodeChannel.MySessionState == null || MyTargetNodeChannel.MySessionState.HasExpired) { throw new Exception($"Session was deleted or has expired ({MyTargetNodeChannel?.MySessionState?.HasExpired})"); } #region Batch Serialization IsBatchOn++; FinalCnt++; var tToAdd = TheCommonUtils.SerializeObjectToJSONString(tDev); if (MCQCount == 0 || tQueued.IsChunked || IsBatchOn > TheBaseAssets.MyServiceHostInfo.MaxBatchedTelegrams) { if (MCQCount != 0) { tDev.CNT = MCQCount; } IsBatchOn = 0; } else { if (tSendBufferStr.Length + tToAdd.Length > TheBaseAssets.MAX_MessageSize[(int)MyTargetNodeChannel.SenderType]) { tDev.CNT = MCQCount; IsBatchOn = 0; } } if (tSendBufferStr.Length > 1) { tSendBufferStr.Append(","); } tSendBufferStr.Append(tToAdd); #endregion } else { IsBatchOn = 0; } } while (IsBatchOn > 0 && IsInWSPost && TheBaseAssets.MasterSwitch); if (!IsInWSPost || tSendBufferStr.Length < 2) { IsInWSPost = false; continue; } tSendBufferStr.Append("]"); if (FinalCnt > 1) { TheBaseAssets.MySYSLOG.WriteToLog(235, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("WSQueuedSender", $"Batched:{FinalCnt}", eMsgLevel.l3_ImportantMessage)); } if (!cdeSenderType.CDE_JAVAJASON.Equals(MyTargetNodeChannel.SenderType)) { MyWebSocketProcessor.PostToSocket(null, TheCommonUtils.cdeCompressString(tSendBufferStr.ToString()), true, false); } else { MyWebSocketProcessor.PostToSocket(null, TheCommonUtils.CUTF8String2Array(tSendBufferStr.ToString()), false, false); } IsInWSPost = false; } TheBaseAssets.MySYSLOG.WriteToLog(235, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("WSQueuedSender", $"WSQSenderThread was closed for {MyTargetNodeChannel?.ToMLString()} IsAlive:{IsAlive} MyWSProccAlive:{(MyWebSocketProcessor == null ? "Is Null" : MyWebSocketProcessor?.IsActive.ToString())},SessionState:{(MyTargetNodeChannel?.MySessionState != null)} IsConnecting:{IsConnecting} IsConnected:{IsConnected}", eMsgLevel.l1_Error)); } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(235, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("WSQueuedSender", "Exception in WSSenderThread.", eMsgLevel.l1_Error, "Error:" + e)); } finally { IsInWSPost = false; IsSenderThreadRunning = false; StopHeartBeat(); } if (IsAlive || (MyWebSocketProcessor != null && MyWebSocketProcessor.IsActive)) { IsAlive = false; if (MyWebSocketProcessor != null) { MyWebSocketProcessor.Shutdown(true, "1310:SenderThread Closed"); } } CloudCounter--; }
internal static void SetResponseBuffer(TheRequestData pRequestData, TheChannelInfo pChannelInfo, bool pSendPulse, string NopTopic, Guid owner, string pRefreshToken) { if (string.IsNullOrEmpty(NopTopic)) { TheQueuedSender tQ = null; if (pChannelInfo != null && pChannelInfo.cdeMID != Guid.Empty) //Send to Device First { tQ = TheQueuedSenderRegistry.GetSenderByGuid(pChannelInfo.cdeMID); } if (tQ != null) { if (pRequestData.WebSocket != null) { if (tQ.GetQueLength() == 0) { tQ.SendPickupMessage(); } return; } tQ.GetNextBackChannelBuffer(pRequestData); } } if (string.IsNullOrEmpty(NopTopic) && pRequestData.WebSocket != null) { return; //NEW:3.084 && !pSendPulse removed NEW:V3B3:2014-7-22 removed && pRequestData.ResponseBuffer != null } if (pRequestData.ResponseBuffer == null && pChannelInfo != null && (pSendPulse || (pChannelInfo.cdeMID != Guid.Empty || TheQueuedSenderRegistry.IsNodeIdInSenderList(pChannelInfo.cdeMID)))) { TheDeviceMessage tDev = new TheDeviceMessage { CNT = 0 }; //tDev.MET = 0; if (!string.IsNullOrEmpty(NopTopic)) { tDev.MSG = new TSM(); //Can be set without ORG and SID if (owner != Guid.Empty) { tDev.MSG.OWN = owner.ToString(); } if (NopTopic == "CDE_WSINIT") { NopTopic = TheCommCore.SetConnectingBufferStr(pChannelInfo, null); } } if (pChannelInfo.SenderType != cdeSenderType.CDE_JAVAJASON) //4.209: No longer sending SID to Browser; { if (TheBaseAssets.MyServiceHostInfo.EnableFastSecurity) { tDev.SID = pRequestData.SessionState.SScopeID; //SECURITY: All responses will have same Scrambled ScopeID - but ok because this is init telegram or HB } else { tDev.SID = TheBaseAssets.MyScopeManager.GetScrambledScopeID(pRequestData.SessionState.SScopeID, false); //GRSI: high frequency } } else { if (!string.IsNullOrEmpty(pRefreshToken)) { tDev.SID = pRefreshToken; } } tDev.FID = pRequestData.SessionState.GetNextSerial().ToString(); if (TheCommonUtils.IsDeviceSenderType(pChannelInfo.SenderType)) //IDST-OK: Must create RSA for Devices { TheCommonUtils.CreateRSAKeys(pRequestData.SessionState); tDev.RSA = pRequestData.SessionState.RSAPublic; } tDev.NPA = TheBaseAssets.MyScopeManager.GetISBPath(TheBaseAssets.MyServiceHostInfo.RootDir, pChannelInfo.SenderType, TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.SenderType, pRequestData.SessionState.FID, pRequestData.SessionState.cdeMID, pRequestData.WebSocket != null); tDev.CNT = pSendPulse ? 1 : 0; tDev.TOP = NopTopic; tDev.DID = pChannelInfo.SenderType == cdeSenderType.CDE_JAVAJASON ? pChannelInfo.cdeMID.ToString() : TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID.ToString(); //There will be only one Message here - single poke or Mini Command or NOP Pickup List <TheDeviceMessage> tDevList = new List <TheDeviceMessage> { tDev }; if (pChannelInfo.SenderType == cdeSenderType.CDE_JAVAJASON || TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.SenderType == cdeSenderType.CDE_MINI || pChannelInfo.SenderType == cdeSenderType.CDE_MINI || pRequestData.WebSocket != null) { pRequestData.ResponseBuffer = TheCommonUtils.CUTF8String2Array(TheCommonUtils.SerializeObjectToJSONString(tDevList)); pRequestData.ResponseMimeType = "application/json"; } else { pRequestData.ResponseBuffer = TheCommonUtils.cdeCompressString(TheCommonUtils.SerializeObjectToJSONString(tDevList)); pRequestData.ResponseMimeType = "application/x-gzip"; } } if (pRequestData.ResponseBuffer == null && pChannelInfo != null && pChannelInfo.cdeMID != Guid.Empty) { TheBaseAssets.MySYSLOG.WriteToLog(290, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("CoreComm", $"Nothing to Send Back to {pChannelInfo?.ToMLString()}", eMsgLevel.l7_HostDebugMessage)); } }
internal static void ExecuteCommand(string pInTopic, TheQueuedSender pQSender, bool DoSendBackBuffer, TheRequestData pRequestData, List <TheDeviceMessage> pDevMessageList) { if (pRequestData == null) { return; } #region Precheck on Execute Conditions if (pQSender?.MyTargetNodeChannel == null) { TheBaseAssets.MySYSLOG.WriteToLog(285, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("CoreComm", "Enter Execute Command: OrgChannel is Null - not allowed ", eMsgLevel.l2_Warning)); pRequestData.ResponseBufferStr = "ERR: Illegal Request"; pRequestData.StatusCode = 400;// (int)nsCDEngine.Communication.HttpService.eHttpStatusCode.NotAcceptable; return; } if (!TheBaseAssets.MasterSwitch) { pRequestData.ResponseBufferStr = "ERR: Service is shutting Down..."; return; } if (pRequestData.SessionState == null && TheBaseAssets.MySession != null) { TheBaseAssets.MySYSLOG.WriteToLog(285, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("CoreComm", "Enter Execute Command: Session no longer alive - Topic:" + pInTopic, eMsgLevel.l2_Warning)); pRequestData.ResponseBufferStr = "ERR: Session no longer alive"; pRequestData.StatusCode = 401; //(int)nsCDEngine.Communication.HttpService.eHttpStatusCode.NotAcceptable; return; } #endregion try { if (pDevMessageList == null) { #region Received BINARY Data in Post Data - Parsing //REVIEW: This should no longer be used in the future...all telegrams should come in as HTTP Bodies if (pRequestData.PostData != null && pRequestData.PostData.Length > 0 && pRequestData.PostDataIdx >= 0) { int tPostDataLength = pRequestData.PostData.Length; if (pRequestData.PostDataLength > 0) { tPostDataLength = pRequestData.PostDataLength; } if ((tPostDataLength - pRequestData.PostDataIdx) == 1) { if (pQSender.MyTargetNodeChannel.SenderType == cdeSenderType.NOTSET) { pQSender.MyTargetNodeChannel.SenderType = (cdeSenderType)(pRequestData.PostData[0] - 0x30); TheBaseAssets.MySYSLOG.WriteToLog(285, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("CoreComm", $"ChannelChange: Was NOTSET and now is now: {pQSender.MyTargetNodeChannel.SenderType}", eMsgLevel.l3_ImportantMessage)); } } else { try { string tStr = null; bool isBin = false; if (pRequestData.ResponseMimeType.ToLower().Contains("zip")) { tStr = TheCommonUtils.cdeDecompressToString(pRequestData.PostData, pRequestData.PostDataIdx, tPostDataLength - pRequestData.PostDataIdx); isBin = true; } else { tStr = TheCommonUtils.CArray2UTF8String(pRequestData.PostData, pRequestData.PostDataIdx, tPostDataLength - pRequestData.PostDataIdx); } pDevMessageList = TheDeviceMessage.DeserializeJSONToObject(tStr); if (pQSender.MyTargetNodeChannel.SenderType == cdeSenderType.NOTSET) { pQSender.MyTargetNodeChannel.SenderType = isBin ? cdeSenderType.CDE_SERVICE : cdeSenderType.CDE_JAVAJASON; TheBaseAssets.MySYSLOG.WriteToLog(285, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("CoreComm", $"ChannelChange: Was NOTSET and now is now: {pQSender.MyTargetNodeChannel.SenderType}", eMsgLevel.l3_ImportantMessage)); } } catch (Exception eee) { string remot = "unkown"; if (pRequestData.SessionState != null && pRequestData.SessionState.MyDevice != Guid.Empty) { remot = pRequestData.SessionState.MyDevice.ToString(); } TheBaseAssets.MySYSLOG.WriteToLog(285, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("CoreComm", "Execute Command received JSON package failed", eMsgLevel.l2_Warning, $"{pRequestData.RequestUri} {remot} {eee}")); } } } #endregion } DoExecuteCommand(pInTopic, pQSender, DoSendBackBuffer, pRequestData, pDevMessageList); } catch (Exception eeee) { TheBaseAssets.MySYSLOG.WriteToLog(285, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("CoreComm", "Execute Command failed", eMsgLevel.l2_Warning, $"{pRequestData.RequestUri} {eeee}")); } }
public void TheRESTSenderThread() { if (IsSenderThreadRunning) { return; } try { TheDiagnostics.SetThreadName($"QSender:{MyTargetNodeChannel} SenderThread", true); MyREST = new TheREST(); IsSenderThreadRunning = true; StringBuilder tSendBufferStr = new StringBuilder(TheBaseAssets.MyServiceHostInfo?.IsMemoryOptimized == true ? 1024 : TheBaseAssets.MAX_MessageSize[(int)MyTargetNodeChannel.SenderType] * 2); TargetUri = TheCommonUtils.CUri(MyTargetNodeChannel.TargetUrl, true); TargetUriPath = TargetUri.AbsolutePath; int QDelay = TheCommonUtils.CInt(TheBaseAssets.MySettings.GetSetting("ThrottleWS")); if (QDelay < 2) { QDelay = 2; } if (MyTargetNodeChannel.SenderType == cdeSenderType.CDE_JAVAJASON && TheBaseAssets.MyServiceHostInfo.WsJsThrottle > QDelay) { QDelay = TheBaseAssets.MyServiceHostInfo.WsJsThrottle; } mre = new ManualResetEvent(false); if (eventSenderThreadRunning != null) { TheCommonUtils.cdeRunAsync("EventSenderThreadRunning", true, (p) => { eventSenderThreadRunning(this); }); } MyISBlock?.FireEvent("SenderThreadCreated"); while (TheBaseAssets.MasterSwitch && IsAlive && IsSenderThreadRunning) { if (QDelay > 2) { mre.WaitOne(QDelay); //Throtteling should be here not in the wait loop } int WaitCounter = 0; while (TheBaseAssets.MasterSwitch && IsAlive && IsSenderThreadRunning && ((MyCoreQueue.Count == 0 && IsConnected) || IsConnecting || IsInPost || MyREST.IsPosting > (IsConnected?TheBaseAssets.MyServiceHostInfo.ParallelPosts:0))) { // CODE REVIEW: This was seen looping after service route to local node was closed (due to node shutdown): IsCOnnecting/IsConnected/IsInPost were false; ISAlive true. MyREST.IsPosting was 8. Queue count ~345 // This should be fixed now with IsSenderThreadRunning in the loop (I was wrong :) this happens only with cloud connections where POST fails and the IsPosting was not reset properly). // Update: If found the same problem. Sometime IsPosting is not reduced properly when a Timeout or other error occured during Posting if (IsInPost && MyCoreQueue.Count > 100) { TheBaseAssets.MySYSLOG.WriteToLog(235, new TSM("QueuedSender", $"IsInPost was still on and has been reset for {MyTargetNodeChannel?.ToMLString()}", eMsgLevel.l2_Warning)); IsInPost = false; } if (!IsConnected) { if (IsConnecting && MyREST.IsPosting == 0) { TheBaseAssets.MySYSLOG.WriteToLog(235, new TSM("QueuedSender", $"IsConnecting is set but no Pending Post {MyTargetNodeChannel?.ToMLString()} - resetting IsConnecting", eMsgLevel.l2_Warning)); IsConnecting = false; } if (!IsConnecting && MyREST.IsPosting > (IsConnected ? 10 : 0)) { WaitCounter++; if (WaitCounter > (10000 / QDelay)) { TheBaseAssets.MySYSLOG.WriteToLog(235, new TSM("QueuedSender", $"MyREST.IsPosting ({MyREST.IsPosting}) was higher then expected...possible POST problem. Resetting POST Counter for {MyTargetNodeChannel?.ToMLString()}", eMsgLevel.l2_Warning)); MyREST.IsPosting = 0; } } } mre.WaitOne(QDelay); } if (!TheBaseAssets.MasterSwitch || !IsAlive || !IsSenderThreadRunning) { IsInPost = false; break; } IsInPost = true; if (MyTargetNodeChannel.SenderType == cdeSenderType.CDE_BACKCHANNEL || MyTargetNodeChannel.SenderType == cdeSenderType.CDE_PHONE || MyTargetNodeChannel.SenderType == cdeSenderType.CDE_DEVICE || MyTargetNodeChannel.SenderType == cdeSenderType.CDE_JAVAJASON) { TheBaseAssets.MySYSLOG.WriteToLog(235, new TSM("QueuedSender", $"QSender Should NEVER GO HERE because SenderType is {MyTargetNodeChannel?.ToMLString()}", eMsgLevel.l1_Error)); IsInPost = false; IsSenderThreadRunning = false; return; } //NEW3.124: Sender Loop has to terminate if Cloud was Disabled. if (MyTargetNodeChannel.SenderType == cdeSenderType.CDE_CLOUDROUTE && (TheBaseAssets.MyServiceHostInfo.IsCloudDisabled || string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ServiceRoute) || !TheBaseAssets.MyServiceHostInfo.HasServiceRoute(TargetUri))) //ServiceRoute can contain multiple routes... { break; } TheCoreQueueContent tQueued = null; int MCQCount = 0; int IsBatchOn = 0; #if CDE_NET35 tSendBufferStr = new StringBuilder(TheBaseAssets.MAX_MessageSize[(int)MyTargetNodeChannel.SenderType] * 2); #else tSendBufferStr.Clear(); #endif tSendBufferStr.Append("["); byte[] BinSendBuffer = null; do { if (!IsConnected) { tQueued = new TheCoreQueueContent(); IsConnecting = true; ConnectRetries = 0; if (CloudInRetry) { var count = Interlocked.Increment(ref CloudRetryCount); if (count > 10) { int delaySeconds = TheBaseAssets.MyServiceHostInfo.HeartbeatDelay * TheBaseAssets.MyServiceHostInfo.TO.HeartBeatRate; TheBaseAssets.MySYSLOG.WriteToLog(235, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("QueuedSender", $"Too many retries for {MyTargetNodeChannel?.ToMLString()}: {count}. Waiting {delaySeconds} seconds.", eMsgLevel.l2_Warning)); TheCommonUtils.SleepOneEye((uint)delaySeconds * 1000, 100); Interlocked.Exchange(ref CloudRetryCount, 0); } if (!TheBaseAssets.MasterSwitch || !IsAlive || !IsSenderThreadRunning || (MyTargetNodeChannel.SenderType == cdeSenderType.CDE_CLOUDROUTE && (TheBaseAssets.MyServiceHostInfo.IsCloudDisabled || string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ServiceRoute) || !TheBaseAssets.MyServiceHostInfo.HasServiceRoute(TargetUri)))) { //Very rare but required after Sleep throw new Exception("QSender had to be terminated in Cloud Retry due to disabling of ServiceRoute or C-DEngine Shutdown"); } }//NEW3.124: Sender Loop has to terminate if Cloud was Disabled. if (MyTargetNodeChannel.SenderType == cdeSenderType.CDE_CLOUDROUTE && (TheBaseAssets.MyServiceHostInfo.IsCloudDisabled || string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.ServiceRoute) || !TheBaseAssets.MyServiceHostInfo.HasServiceRoute(TargetUri))) { break; } } else { if (CloudInRetry) { CloudInRetry = false; IsInPost = false; continue; } tQueued = GetNextMessage(out MCQCount); } if (tQueued != null) { TheCDEKPIs.IncrementKPI(eKPINames.QSSent); if (tQueued.OrgMessage == null && !IsConnecting) { if (MySubscriptions.Count == 0) { if (IsBatchOn == 0) //Only jump out if no telegrams are in batch.. { IsInPost = false; continue; } } if (IsBatchOn == 0) //If telegrams are in the batch dont send short message { BinSendBuffer = new byte[1]; BinSendBuffer[0] = (byte)((int)(TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.SenderType)).ToString()[0]; break; } } if (tQueued.OrgMessage != null && tQueued.OrgMessage.ToCloudOnly() && MyTargetNodeChannel.SenderType == cdeSenderType.CDE_CLOUDROUTE) { tQueued.OrgMessage.SetToCloudOnly(false); } TheDeviceMessage tDev = new TheDeviceMessage { DID = TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID.ToString() // MyTargetNodeChannel.cdeMID.ToString(); }; if (MyTargetNodeChannel.MySessionState != null) { //BUGFIX: After fast Cloud Disconnect/reconnect-Relay lost Scopeing because it Scope was not properly stored in SessionState if (string.IsNullOrEmpty(MyTargetNodeChannel.MySessionState.SScopeID) && TheBaseAssets.MyScopeManager.IsScopingEnabled) { MyTargetNodeChannel.MySessionState.SScopeID = TheBaseAssets.MyScopeManager.GetScrambledScopeID(); //GRSI: rare } if (TheBaseAssets.MyServiceHostInfo.EnableFastSecurity) { tDev.SID = MyTargetNodeChannel.MySessionState.SScopeID; } else { tDev.SID = TheBaseAssets.MyScopeManager.GetScrambledScopeID(MyTargetNodeChannel.MySessionState.SScopeID, false); //GRSI: high frequency! } } else { tDev.SID = TheBaseAssets.MyScopeManager.GetScrambledScopeID(); //GRSI: rare } tDev.MSG = tQueued.OrgMessage; tDev.FID = "1"; if (IsConnecting) { tDev.TOP = TheCommCore.SetConnectingBufferStr(MyTargetNodeChannel, null); } else { tDev.TOP = tQueued.Topic; tQueued.OrgMessage?.GetNextSerial(tQueued.SubMsgCnt); if (MyTargetNodeChannel.MySessionState != null) { tDev.FID = MyTargetNodeChannel.MySessionState.GetNextSerial().ToString(); //(SendCounter + 1).ToString(); //V3B3: New was empty //V4.1010: Same as WS now } } #region Batch Serialization IsBatchOn++; if (MCQCount == 0 || IsBatchOn > TheBaseAssets.MyServiceHostInfo.MaxBatchedTelegrams || tQueued.IsChunked) { if (MCQCount != 0) { tDev.CNT = MCQCount; } IsBatchOn = 0; } else { if (tSendBufferStr.Length > TheBaseAssets.MAX_MessageSize[(int)MyTargetNodeChannel.SenderType]) { tDev.CNT = MCQCount; IsBatchOn = 0; } } if (tSendBufferStr.Length > 1) { tSendBufferStr.Append(","); } tSendBufferStr.Append(TheCommonUtils.SerializeObjectToJSONString(tDev)); #endregion } else { IsBatchOn = 0; } } while (IsBatchOn > 0 && IsInPost && TheBaseAssets.MasterSwitch); if ((!IsInPost || tSendBufferStr.Length < 2) && BinSendBuffer == null) { IsInPost = false; continue; } string tMimeType = "application/octet-stream"; if (BinSendBuffer == null) { tSendBufferStr.Append("]"); if (MyTargetNodeChannel.SenderType == cdeSenderType.CDE_MINI || TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.SenderType == cdeSenderType.CDE_MINI) { tMimeType = "application/json"; BinSendBuffer = TheCommonUtils.CUTF8String2Array(tSendBufferStr.ToString()); } else { tMimeType = "application/x-gzip"; BinSendBuffer = TheCommonUtils.cdeCompressString(tSendBufferStr.ToString()); } } if (MyTargetNodeChannel == null) { TheBaseAssets.MySYSLOG.WriteToLog(235, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("QueuedSender", $"Channel was deleted - exiting SenderThread", eMsgLevel.l2_Warning)); IsInPost = false; break; } string ISBPath = new Uri(TargetUri, TheBaseAssets.MyScopeManager.GetISBPath(TargetUriPath, TheCommonUtils.GetOriginST(MyTargetNodeChannel), MyTargetNodeChannel.SenderType, MyTargetNodeChannel.MySessionState == null? 1: MyTargetNodeChannel.MySessionState.FID /*Interlocked.Increment(ref SendCounter)*/, (MyTargetNodeChannel.MySessionState == null || !IsConnected) ?Guid.Empty: MyTargetNodeChannel.MySessionState.cdeMID, MyTargetNodeChannel.IsWebSocket)).ToString(); //V3B4: changed from TheBaseAssets.MyServiceHostInfo.MyDeviceInfo TheRequestData pData = new TheRequestData { RemoteAddress = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false), RequestUri = new Uri(ISBPath), PostData = BinSendBuffer, ResponseMimeType = tMimeType, DeviceID = TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID, HttpMethod = "POST" }; if (!TheCertificates.SetClientCert(pData, MyTargetNodeChannel?.ClientCertificateThumb)) { throw new Exception("Client Certificate could not be added"); } MyREST.PostRESTAsync(pData, sinkUploadDataCompleted, FireSenderProblem); if (BinSendBuffer != null) { TheCDEKPIs.IncrementKPI(eKPINames.QKBSent, BinSendBuffer.Length); } IsInPost = false; } FlushQueue(); TheBaseAssets.MySYSLOG.WriteToLog(235, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("QueuedSender", $"QSenderThread was closed for {MyTargetNodeChannel?.ToMLString()}", eMsgLevel.l6_Debug)); } catch (Exception e) { IsInPost = false; TheBaseAssets.MySYSLOG.WriteToLog(235, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("QueuedSender", "Exception in jcSenderThread.", eMsgLevel.l1_Error, "Error:" + e)); if (MyTargetNodeChannel?.SenderType != cdeSenderType.CDE_BACKCHANNEL) { FireSenderProblem(new TheRequestData() { ErrorDescription = $"1308:{e}" }); } } finally { IsSenderThreadRunning = false; IsAlive = false; IsInPost = false; CloudInRetry = false; StopHeartBeat(); } }
internal override void PostToSocket(TheDeviceMessage pMsg, byte[] pPostBuffer, bool PostAsBinary, bool IsInitialConnect) { TheDiagnostics.SetThreadName("WSPostToSocketCSWS:" + (MyQSender.MyTargetNodeChannel?.ToString() ?? "DEAD")); if (MyQSender != null && !MyQSender.IsConnected && !IsInitialConnect) { Shutdown(true, "1653:QSenderCSWS not connected but Posting in illegal state"); return; } if (!ProcessingAllowed) //NEW:V3BETA2: New Waiting Algorythm WaitUntilProcessingAllowed(); if (!IsActive || !TheBaseAssets.MasterSwitch) return; if (websocket == null && webSocketSession==null) { eventClosed?.Invoke("1654:WebSockets are down"); return; } ProcessingAllowed = false; try { if (pPostBuffer != null) { TheCDEKPIs.IncrementKPI(eKPINames.QKBSent, pPostBuffer.Length); if (PostAsBinary) { if (webSocketSession != null) webSocketSession.SendB(pPostBuffer); else { websocket?.Send(pPostBuffer); } } else { if (webSocketSession != null) webSocketSession.SendB(TheCommonUtils.CArray2UTF8String(pPostBuffer)); else { websocket?.Send(TheCommonUtils.CArray2UTF8String(pPostBuffer)); } } } else { string toSend = TheCommonUtils.SerializeObjectToJSONString(pMsg); if (PostAsBinary) { byte[] toSendb = TheCommonUtils.CUTF8String2Array(toSend); TheCDEKPIs.IncrementKPI(eKPINames.QKBSent, toSendb.Length); if (webSocketSession != null) webSocketSession.SendB(toSendb); else { websocket?.Send(toSendb); } } else { if (webSocketSession != null) webSocketSession.SendB(toSend); else { websocket?.Send(toSend); } } } } catch (Exception e) { Shutdown(true, "1655:DoPostToSocketCSWS had a fault: "+e); } if (mre != null) ProcessingAllowed = true; }
internal override void PostToSocket(TheDeviceMessage pMsg, byte[] pPostBuffer, bool SendAsBinary, bool IsInitialConnect) { TheDiagnostics.SetThreadName("WSPostToSocket:" + ((MyQSender?.MyTargetNodeChannel != null) ? MyQSender.MyTargetNodeChannel.ToString():"DEAD")); if (MyQSender?.MyTargetNodeChannel == null) { TheBaseAssets.MySYSLOG.WriteToLog(4369, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("ProcessWS", $"QSender8 not Has not QSender or TargetChannel {MyQSender}", eMsgLevel.l1_Error)); Shutdown(true, "1608:WS8 has not QSender but is Posting - illegal state"); return; } if (MyQSender != null && !MyQSender.IsConnected && !IsInitialConnect) { TheBaseAssets.MySYSLOG.WriteToLog(4369, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("ProcessWS", "QSender8 not connected but Posting - illegal state", eMsgLevel.l1_Error)); Shutdown(true, "1609:QSender8 not connected but Posting - illegal state"); return; } bool HasFaulted = false; lock (_postToSocketLock) // The lock needs to be at the websocket level: assumption is that there is at most one WSProcessor per socket! { if (!IsActive || !TheBaseAssets.MasterSwitch) { return; } ProcessingAllowed = false; string errorMsg = ""; try { if (!IsSocketReady(websocket)) { HasFaulted = true; errorMsg = "Socket Not Ready"; } else { // TODO MH - Move this outside of the lock so we can minimize the lock time? Or are there ordering constraints on the serialization for some reason? ArraySegment <byte> outputBuffer; if (pPostBuffer != null) { TheCDEKPIs.IncrementKPI(eKPINames.QKBSent, pPostBuffer.Length); outputBuffer = new ArraySegment <byte>(pPostBuffer); } else { string tStr = TheCommonUtils.SerializeObjectToJSONString(pMsg); TheCDEKPIs.IncrementKPI(eKPINames.QKBSent, tStr.Length); outputBuffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes(tStr)); } //LogBufferToFile("wsoutputputlog", outputBuffer.Array, outputBuffer.Offset, outputBuffer.Count); Task tTask = websocket.SendAsync(outputBuffer, SendAsBinary ? WebSocketMessageType.Binary : WebSocketMessageType.Text, true, TheBaseAssets.MasterSwitchCancelationToken); tTask.Wait(TheBaseAssets.MyServiceHostInfo.TO.WsTimeOut * 10); if (!tTask.IsCompleted) { var timeoutValue = TheBaseAssets.MyServiceHostInfo.TO.WsTimeOut * 10; TheBaseAssets.MySYSLOG.WriteToLog(43610, new TSM("TheWSProcessor", $"WebSocketServer-PostToSocket error: PostAsync Timeout {tTask.Status} after {timeoutValue} ms for {MyQSender?.MyTargetNodeChannel}", eMsgLevel.l1_Error)); errorMsg = $"SendAsync timed out after {timeoutValue} ms"; HasFaulted = true; } } } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(43610, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("TheWSProcessor", "WebSocketServer-PostToSocket Error:", eMsgLevel.l1_Error, ((int)TheBaseAssets.MyServiceHostInfo.DebugLevel > 1? e.ToString():e.Message))); errorMsg = $"PostToSocket failed: {e.Message}"; HasFaulted = true; } if (HasFaulted) { Shutdown(true, $"1610:{errorMsg}"); } //IsPosting = false; if (mre != null) { ProcessingAllowed = true; } } }