Exemplo n.º 1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            if (Disposed)
            {
                return;
            }

            try
            {
                var state     = (StateObjectSsl)ar.AsyncState;
                var stream    = state.workStream;
                var byteCount = stream.EndRead(ar);

                if (byteCount > 0)
                {
                    deviceReceiveBuffer.OnReceive(state.buffer.Take(byteCount).ToArray());
                    sslStream.BeginRead(stateObject.buffer, 0, StateObjectSsl.BufferSize, ReceiveCallback, stateObject);
                }
            }
            catch (Exception ex)
            {
                logger.Log(ex, "DeviceListener.ReceiveCallback");
                Dispose();
                Thread.Sleep(1000);
                DoStartListening();
            }
        }
        /// <summary>
        /// Received data from the device.
        /// </summary>
        private void DataReceived(IAsyncResult ar)
        {
            if (ar == null || deviceReceiveBuffer == null || receiveBuffer == null || IsDisposed)
            {
                return;
            }

            SslStream stream    = (SslStream)ar.AsyncState;
            int       byteCount = -1;

            try
            {
                byteCount = stream.EndRead(ar);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                CloseConnection();
            }
            if (byteCount > 0)
            {
                deviceReceiveBuffer.OnReceive(receiveBuffer.Take(byteCount).ToArray());
            }
            else
            {
                Thread.Sleep(500);
            }
            StartReceive();
        }
        private void DataReceived(IAsyncResult ar)
        {
            SslStream stream    = (SslStream)ar.AsyncState;
            int       byteCount = -1;

            try
            {
                byteCount = stream.EndRead(ar);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Dispose();
            }
            if (byteCount > 0)
            {
                deviceReceiveBuffer.OnReceive(receiveBuffer.Take(byteCount).ToArray());
            }
            StartReceive();
        }