示例#1
0
        private void MessageBroker(object Objects)
        {
            try
            {
                // Converte os objetos recebidos por parametro pela Thread principal.
                List <object> LstObjects = (List <object>)(Objects);
                string        recv       = LstObjects[0].ToString();

                // Header
                Header _GenericHeader;
                IntPtr pBufHeader = Marshal.StringToBSTR(recv.ToString());
                _GenericHeader = (Header)(Marshal.PtrToStructure(pBufHeader, typeof(Header)));

                // Mensagem de SignIn (MsgCode = A1)
                if (_GenericHeader.pStrTipoMensagem == SignIn)
                {
                    logger.Info("Efetuando login...");

                    // Recorta os bytes da mensagem
                    SignIn _stAuthentication;
                    IntPtr pBuf = Marshal.StringToBSTR(recv.ToString());
                    _stAuthentication = (SignIn)(Marshal.PtrToStructure(pBuf, typeof(SignIn)));

                    // Obtem retorno da solicitação de autenticação do banco de dados.
                    object ResultSet = new BAuthenticationServer().InsertAccess(_stAuthentication);

                    // Inicializa a composição da mensagem A2
                    MDSSignIn _MDSSignIn = new MDSSignIn();

                    // Verifica pela quantidade de bytes se o valor do campo é um guid ou um status de erro.
                    if (ResultSet.ToString().Length != BytesOfUID)
                    {
                        // STATUS DE ERRO
                        _MDSSignIn.pStrErrorCode = GenericMessage.GetPosition(2, ((int)ResultSet).ToString(), ' ');
                        _MDSSignIn.pStrUniqueId  = GenericMessage.GetPosition(36, string.Empty, ' ');

                        // Byte = 0 falha
                        _MDSSignIn.pStrStatusRequest = "0";
                    }
                    else
                    {
                        // SUCESSO pStrErrorCode = vazio.
                        _MDSSignIn.pStrErrorCode = GenericMessage.GetPosition(2, string.Empty, ' ');
                        _MDSSignIn.pStrUniqueId  = ResultSet.ToString();

                        // Byte = 1 sucesso
                        _MDSSignIn.pStrStatusRequest = "1";
                    }

                    _MDSSignIn.pStrIdCliente = _stAuthentication.pStrIdCliente;
                    _MDSSignIn.pStrIdSistema = _stAuthentication.pStrIdSistema;

                    byte[] ByteArray = new Message().MDSSignIn(_MDSSignIn);

                    //Envia a resposta da autenticação para o Client {msgCode = A2}
                    Socket _currentSocket = (Socket)LstObjects[1];
                    string response       = new Message().MDSSignInAsString(_MDSSignIn);

                    logger.Debug("Resposta MDS [" + response + "]");
                    SendData(response, _currentSocket);
                    //((Socket)(LstObjects[1])).Send(ByteArray);
                }
                else
                {
                    // Mensagem de SignOut (msgCode = A5)
                    if (_GenericHeader.pStrTipoMensagem == SignOut)
                    {
                        logger.Info("Efetuando logout...");

                        SignOut _stAuthentication;
                        IntPtr  pBuf = Marshal.StringToBSTR(recv.ToString());
                        _stAuthentication = (SignOut)(Marshal.PtrToStructure(pBuf, typeof(SignOut)));

                        new BAuthenticationServer().UpdateAccess(_stAuthentication);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("MessageBroker(): " + ex.Message, ex);
            }
        }