Exemple #1
0
        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);
            }
        }