Example #1
0
        private void TheadWaitOne()
        {
            try
            {
                if (WorkerCallBack == null)
                {
                    //Cria o evento assíncrono que será disparado quando ocorrer um input
                    //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);
            }
        }
Example #2
0
 public void MDSSocketClose(MDSSocketPacket _SocketPacket)
 {
     try
     {
         // Finaliza a instancia do Socket
         _SocketPacket._Socket.Shutdown(SocketShutdown.Both);
         _SocketPacket._Socket.Close();
         _SocketPacket._Socket = null;
         _SocketPacket         = null;
     }
     catch {}
     finally
     {
         bConectado = false;
     }
 }
Example #3
0
        private int ReceiveData(int BytesResult, int BufferSize, int xBufferSize, MDSSocketPacket socketData)
        {
            string Message = string.Empty;
            int    posx    = 0;

            byte[] Buffer =
                new byte[BytesResult];

            int ByteRest = socketData._Socket.Receive(Buffer,
                                                      BytesResult,
                                                      SocketFlags.None
                                                      );

            switch (ByteRest)
            {
            case 1:
                xBufferSize += (((int)Buffer[posx++]) & 0xFF);
                break;

            case 2:
                xBufferSize += (((int)Buffer[posx++]) & 0xFF) << 8;
                xBufferSize += (((int)Buffer[posx++]) & 0xFF);
                break;

            case 3:
                xBufferSize += (((int)Buffer[posx++]) & 0xFF) << 16;
                xBufferSize += (((int)Buffer[posx++]) & 0xFF) << 8;
                xBufferSize += (((int)Buffer[posx++]) & 0xFF);
                break;

            case 4:
                xBufferSize += (((int)Buffer[posx++]) & 0xFF) << 24;
                xBufferSize += (((int)Buffer[posx++]) & 0xFF) << 16;
                xBufferSize += (((int)Buffer[posx++]) & 0xFF) << 8;
                xBufferSize += (((int)Buffer[posx++]) & 0xFF);
                break;
            }

            return(xBufferSize);
        }
Example #4
0
        private void OnDataReceived(IAsyncResult asyn)
        {
            string Instrumento = string.Empty;

            int BufferAlocate       = 0;
            int BufferReceiveLenght = 4;

            MDSSocketPacket socketData = (MDSSocketPacket)asyn.AsyncState;

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

                    // Falha de conexão.
                    if (ResultBytesLength <= 0)
                    {
                        this.MDSSocketClose(socketData);
                        return;
                    }

                    int xBufferSize = 0;
                    int posx        = 0;


                    if (BufferReceiveLenght > ResultBytesLength)
                    {
                        switch (ResultBytesLength)
                        {
                        case 1:
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF) << 24;
                            break;

                        case 2:
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF) << 24;
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF) << 16;
                            break;

                        case 3:
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF) << 24;
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF) << 16;
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF) << 8;
                            break;

                        case 4:
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF) << 24;
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF) << 16;
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF) << 8;
                            xBufferSize += (((int)socketData.dataBuffer[posx++]) & 0xFF);
                            break;
                        }

                        BufferAlocate = ReceiveData(
                            (BufferReceiveLenght - ResultBytesLength),
                            4,
                            xBufferSize,
                            socketData
                            );
                    }


                    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;

                    byte[] dataBuffer;

                    if (BufferAlocate == 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);

                        dataBuffer = new byte[BufferSize];
                    }
                    else
                    {
                        dataBuffer = new byte[BufferAlocate];
                    }



                    int BytesReceive = socketData._Socket.Receive(dataBuffer,
                                                                  dataBuffer.Length,
                                                                  SocketFlags.None
                                                                  );

                    char[] charLenght = new char[BytesReceive];
                    string Message    = string.Empty;

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

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

                    Message += new string(charLenght);

                    if (BytesReceive < BufferSize)
                    {
                        Message +=
                            this.ReceiveData(
                                BytesReceive,
                                BufferSize,
                                socketData._Socket
                                );
                    }

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

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

                    BufferAlocate = 0;

                    LastPacket = DateTime.Now;
                    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);
            }
        }