Esempio n. 1
0
        private void TheadWaitOne()
        {
            try
            {
                if (WorkerCallBack == null)
                {
                    //Cria o evento assíncrono que será disparado quando ocorrer um imput
                    //de dados
                    WorkerCallBack = new AsyncCallback(OnDataReceived);
                }

                // Encapsula o client socket solicitante e o clientNumber.
                MDSSocketPacket _SocketPacket = new MDSSocketPacket();
                _SocketPacket._Socket = _ClientSocket;

                //Inicializa a recepção assíncrona dos Dados
                AsyncResult = _ClientSocket.BeginReceive(_SocketPacket.dataBuffer,
                                                         0, _SocketPacket.dataBuffer.Length,
                                                         SocketFlags.None,
                                                         WorkerCallBack,
                                                         _SocketPacket);
            }
            catch (SocketException se)
            {
                throw (se);
            }
        }
Esempio n. 2
0
 private void MDSSocketClose(MDSSocketPacket _SocketPacket)
 {
     try
     {
         // Finaliza a instancia do Socket
         _SocketPacket._Socket.Shutdown(SocketShutdown.Both);
         _SocketPacket._Socket.Close();
         _SocketPacket._Socket = null;
         _SocketPacket         = null;
     }
     catch (Exception ex)
     {
         logger.Error("MDSSocketClose(): " + ex.Message, ex);
     }
     finally
     {
         bConectado = false;
     }
 }
Esempio n. 3
0
        private void OnDataReceived(IAsyncResult asyn)
        {
            string Instrumento = string.Empty;

            MDSSocketPacket socketData = (MDSSocketPacket)asyn.AsyncState;

            try
            {
                lock (this)
                {
                    int ResultBytesLength = socketData._Socket.EndReceive(asyn);

                    // Falha de conexão.
                    if (ResultBytesLength <= 0)
                    {
                        logger.Warn("Encerrou conexao com MDS (" + ResultBytesLength + ") recebendo tamanho");
                        this.MDSSocketClose(socketData);
                        return;
                    }

                    // Se recebeu menos que 4 bytes, recebe os bytes restantes para compor o tamanho
                    while (ResultBytesLength < 4)
                    {
                        int BytesReceived = socketData._Socket.Receive(socketData.dataBuffer,
                                                                       ResultBytesLength,
                                                                       4 - ResultBytesLength,
                                                                       SocketFlags.None);
                        if (BytesReceived <= 0)
                        {
                            logger.Warn("Encerrou conexao com MDS (" + ResultBytesLength + ") recebendo complemento do tamanho");
                            this.MDSSocketClose(socketData);
                            return;
                        }

                        ResultBytesLength += BytesReceived;
                    }

                    char[] charsLength = new char[ResultBytesLength];

                    System.Text.Decoder _Decoder = System.Text.Encoding.UTF8.GetDecoder();

                    _Decoder.GetChars(
                        socketData.dataBuffer,
                        0,
                        ResultBytesLength,
                        charsLength,
                        0);

                    int BufferSize = 0;
                    int pos        = 0;

                    BufferSize += (((int)socketData.dataBuffer[pos++]) & 0xFF) << 24;
                    BufferSize += (((int)socketData.dataBuffer[pos++]) & 0xFF) << 16;
                    BufferSize += (((int)socketData.dataBuffer[pos++]) & 0xFF) << 8;
                    BufferSize += (((int)socketData.dataBuffer[pos++]) & 0xFF);

                    int    TotalBytesReceived = 0;
                    string Message            = string.Empty;

                    while (TotalBytesReceived < BufferSize)
                    {
                        int ToReceive = BufferSize - TotalBytesReceived;

                        byte[] dataBuffer = new byte[ToReceive];

                        int BytesReceived = socketData._Socket.Receive(dataBuffer,
                                                                       ToReceive,
                                                                       SocketFlags.None
                                                                       );
                        if (BytesReceived <= 0)
                        {
                            logger.Warn("Encerrou conexao com MDS (" + ResultBytesLength + ") recebendo dados");
                            this.MDSSocketClose(socketData);
                            return;
                        }


                        char[] charLenght = new char[BytesReceived];

                        System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();

                        decoder.GetChars(dataBuffer, 0, BytesReceived, charLenght, 0);

                        Message += new string(charLenght);

                        TotalBytesReceived += BytesReceived;
                    }

                    if ((Message != null) || (Message != ""))
                    {
                        Instrumento = Message.Substring(0, 8);
                        Message     = Message.Remove(0, 8);

                        QueueManager.Instance.EnqueueSinal(Instrumento, Message);

                        //ThreadPool.QueueUserWorkItem(new WaitCallback(
                        // delegate(object required)
                        // {
                        //     this.MessageBroker(
                        //         Instrumento.Trim(),
                        //         Message);
                        // }));
                    }

                    LastPacket = DateTime.Now;
                    LastMsg    = Message;

                    TheadWaitOne();
                }
            }
            catch (SocketException se)
            {
                logger.Error("OnDataReceived(SocketEx): " + se.Message + " ErrorCode: " + se.ErrorCode +
                             " Native: " + se.NativeErrorCode +
                             " SocketError: " + se.SocketErrorCode, se);

                logger.Warn("OnDataReceived(): Encerrando conexao com MDS");

                // Falha de conexão
                this.MDSSocketClose(socketData);
            }
            catch (Exception ex)
            {
                logger.Error("OnDataReceived(): " + ex.Message, ex);
                this.MDSSocketClose(socketData);
            }
        }