Ejemplo n.º 1
0
 private void websocket_Opened(object sender, EventArgs e)
 {
     //MyQSender.IsConnected = true;
     eventConnected?.Invoke(this, true,null);
     byte[] tSendBuffer = TheCommCore.SetConnectingBuffer(MyQSender);
     PostToSocket(null, tSendBuffer, false, true);
 }
Ejemplo n.º 2
0
        public async Task ProcessWS()
        {
            TheDiagnostics.SetThreadName("WSProcessWS");
            if (MyQSender == null)
            {
                TheBaseAssets.MySYSLOG.WriteToLog(4366, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("TheWSProcessor", "ProcessWS - no QSender Specified! ", eMsgLevel.l1_Error));
                return;
            }
            int tMaxMsgSize = 0;
            ArraySegment <byte> buffer;

            byte[] receiveBuffer;
            try
            {
                if (IsClient)
                {
                    tMaxMsgSize = TheCommonUtils.GetMaxMessageSize(MyQSender.MyTargetNodeChannel.SenderType);
                }
                else
                {
                    tMaxMsgSize = TheCommonUtils.GetMaxMessageSize(TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.SenderType);
                }
                receiveBuffer = new byte[tMaxMsgSize];
                buffer        = new ArraySegment <byte>(receiveBuffer);
                TheBaseAssets.MySYSLOG.WriteToLog(4367, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("TheWSProcessor", "New ProcessWS thread started ", eMsgLevel.l4_Message));
            }
            catch (Exception e)
            {
                TheBaseAssets.MySYSLOG.WriteToLog(4367, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("TheWSProcessor", "Failure while starting new ProcessWS thread", eMsgLevel.l4_Message, e.ToString()));
                return;
            }

            CloseFired = false;
            bool   HasFaulted = false;
            string tCause     = "1604:Thread ended";

            try
            {
                if (IsSocketReady(websocket))
                {
                    if (IsClient)
                    {
                        //MyQSender.IsConnected = true;
                        byte[] tSendBuffer = TheCommCore.SetConnectingBuffer(MyQSender);
                        PostToSocket(null, tSendBuffer, false, true);
                    }
                }

                while (TheBaseAssets.MasterSwitch && IsActive && IsSocketReady(websocket))
                {
                    WebSocketReceiveResult receiveResult = await websocket.ReceiveAsync(buffer, CancellationToken.None);

                    if (receiveResult.MessageType == WebSocketMessageType.Close)
                    {
                        tCause = "1605:WebSocket Closed";
                        break;
                    }
                    //LogBufferToFile("wsinputlog.dat", buffer.Array, buffer.Offset, receiveResult.Count);
                    if (MyQSender == null || !MyQSender.IsAlive)
                    {
                        tCause = "1606:QSender No longer alive";
                        break;
                    }

                    int    offset          = receiveResult.Count;
                    bool   IsUsingTArray   = false;
                    byte[] tPostData       = null;
                    int    tPostDataLength = 0;
                    if (receiveResult.EndOfMessage == false)
                    {
                        List <byte[]> tTelList = new List <byte[]>();
                        byte[]        tArray   = null;
                        while (receiveResult.EndOfMessage == false)
                        {
                            if (IsUsingTArray)
                            {
                                var arraySeg = new ArraySegment <byte>(tArray, offset, tMaxMsgSize - offset);
                                receiveResult = await websocket.ReceiveAsync(arraySeg, CancellationToken.None);

                                //LogBufferToFile("wsinputlog.dat", arraySeg.Array, arraySeg.Offset, receiveResult.Count);
                            }
                            else
                            {
                                var arraySeg = new ArraySegment <byte>(receiveBuffer, offset, tMaxMsgSize - offset);
                                receiveResult = await websocket.ReceiveAsync(arraySeg, CancellationToken.None);

                                //LogBufferToFile("wsinputlog.dat", arraySeg.Array, arraySeg.Offset, receiveResult.Count);
                            }
                            if (receiveResult.Count == 0)
                            {
                                if (receiveResult.Count + offset != tMaxMsgSize)
                                {
                                    TheBaseAssets.MySYSLOG.WriteToLog(4369, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("ProcessWS", string.Format("WS Buffer count wrong: Max={0} Offset={1} Count={2}", tMaxMsgSize, offset, receiveResult.Count), eMsgLevel.l1_Error));
                                }
                                tArray = new byte[tMaxMsgSize];
                                if (!IsUsingTArray)
                                {
                                    tTelList.Add(receiveBuffer);
                                }
                                tTelList.Add(tArray);
                                IsUsingTArray = true;
                                offset        = 0;
                            }
                            else
                            {
                                offset += receiveResult.Count;
                            }
                        }
                        if (tTelList.Count > 0)
                        {
                            tPostData = new byte[((tTelList.Count - 1) * tMaxMsgSize) + offset];
                        }
                        tPostDataLength = 0;
                        for (int i = 0; i < tTelList.Count - 1; i++)
                        {
                            byte[] tb = tTelList[i];
                            TheCommonUtils.cdeBlockCopy(tb, 0, tPostData, i * tMaxMsgSize, tMaxMsgSize);
                            tb = null;
                        }
                        if (IsUsingTArray && offset > 0 && tTelList.Count > 0 && tPostData != null)
                        {
                            TheCommonUtils.cdeBlockCopy(tTelList[tTelList.Count - 1], 0, tPostData, (tTelList.Count - 1) * tMaxMsgSize, offset);
                            tPostDataLength = tPostData.Length;
                        }
                        //tTelList.Clear();
                    }
                    if (!IsUsingTArray)
                    {
                        tPostData = new byte[offset];
                        TheCommonUtils.cdeBlockCopy(receiveBuffer, 0, tPostData, 0, offset);
                        tPostDataLength = offset;
                    }
                    //LogBufferToFile("wsinputbactchedlog.dat", tPostData, 0, tPostData.Length);
                    TheCommonUtils.cdeRunAsync("ProcessFromWS", true, (o) => ProcessIncomingData(null, (byte [])o, ((byte[])o).Length), tPostData);
                }
            }
            catch (Exception eee)
            {
                HasFaulted = true;
                if (eee.Source == "System")
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(4369, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("ProcessWS", "ProcessWS Loop has failed because WebSocket was closed during ReceiveAsync.", eMsgLevel.l1_Error));
                }
                else
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(4369, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("ProcessWS", "ProcessWS Loop has failed.", eMsgLevel.l1_Error, eee.ToString()));
                }
            }
            TheBaseAssets.MySYSLOG.WriteToLog(4369, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("ProcessWS", $"ProcessWS Loop for {OwnerNodeID} has ended. Faulted:{HasFaulted} HasWS:{websocket != null}", eMsgLevel.l3_ImportantMessage));
            ConnectSuccess = false;
            if (HasFaulted || (websocket != null && websocket.State != WebSocketState.Closed))
            {
                tCause = string.Format("1607:WebSocket Faulted:{0} WS State:{1}", HasFaulted, websocket != null?websocket.State.ToString():"is null");
            }
            Shutdown(true, tCause);
        }