Example #1
0
        public virtual void Close()
        {
            bool locked = false;

            try
            {
                IOmonitor.IdleForLock(_IOprocessing);
                //
                if (_OutputDevice != null && _OutputDevice.IsInitialized && _OutputDevice.InitializedBy == ParentIDE.Machine)
                {
                    locked = false;
                    if (_OutputDevice.IsLockingRequiredForDispose())
                    {
                        IOmonitor.IdleForLock(_OutputDevice, out locked);
                    }
                    _OutputDevice.Dispose();
                    if (locked)
                    {
                        IOmonitor.Unlock(_OutputDevice);
                    }
                }
                _OutputBuffer.Clear();
            }
            finally
            {
                if (locked)
                {
                    IOmonitor.Unlock(_OutputDevice);
                }
                //
                IOmonitor.Unlock(_IOprocessing);
            }
        }
Example #2
0
 public virtual void Close()
 {
     try
     {
         IOmonitor.IdleForLock(_IOprocessing);
         //
         bool locked;
         if (_InputDevice != null && _InputDevice.IsInitialized && _InputDevice.InitializedBy == ParentIDE.Machine)
         {
             locked = false;
             if (_InputDevice.IsLockingRequiredForDispose())
             {
                 IOmonitor.IdleForLock(_InputDevice, out locked);
             }
             _InputDevice.Dispose();
             if (locked)
             {
                 IOmonitor.Unlock(_InputDevice);
             }
         }
         _InputBuffer     = new byte[0];
         _InBuffPtr       = 0;
         _InputWordBuffer = new string[0];
         _WdBuffPtr       = 0;
     }
     finally
     {
         //
         IOmonitor.Unlock(_IOprocessing);
     }
 }
Example #3
0
        protected virtual void DirectInput(out byte[] inData, int length)
        {
            bool locked = false;

            try
            {
                if ((InputDeviceThreadSafe.Initialize & _InputDeviceIsThreadSafeFor) == 0)
                {
                    IOmonitor.IdleForLock(_InputDevice, out locked);
                    if (!_InputDevice.IsInitialized)
                    {
                        _InputDevice.Initialize(this);
                    }
                }
                else
                if (!_InputDevice.IsInitialized)
                {
                    _InputDevice.Initialize(this);
                }

                if (!locked && (InputDeviceThreadSafe.Input & _InputDeviceIsThreadSafeFor) == 0)
                {
                    IOmonitor.IdleForLock(_InputDevice, out locked);
                }
                {
                    IOmonitor.IdleWhile(() => !_InputDevice.IsReadyToInput);
                    IOutputDevice ioDevice = _InputDevice as IOutputDevice;
                    if (ioDevice != null)
                    {
                        if (ioDevice.IsReadyToOutput && _bInputForeword != null)
                        {
                            ioDevice.Output(this, _bInputForeword);
                        }

                        _InputDevice.Input(this, out inData, length);

                        if (ioDevice.IsReadyToOutput && _bInputAfterword != null)
                        {
                            ioDevice.Output(this, _bInputAfterword);
                        }
                    }
                    else
                    {
                        _InputDevice.Input(this, out inData, length);
                    }
                }
            }
            finally
            {
                if (locked)
                {
                    IOmonitor.Unlock(_InputDevice);
                }
            }
        }
Example #4
0
        public virtual void Output(byte[] outData)
        {
            try
            {
                IOmonitor.IdleForLock(_IOprocessing);
                //
                byte[] outdevData;

                switch (_OutputConverting)
                {
                case OutputConvertings.String:
                    outdevData = Encoding.Convert(ParentIDE.Machine.CharSet, _OutputDevice.CharSet
                                                  , outData);
                    break;

                case OutputConvertings.ParsingNumber:
                    outdevData = XBinary.ConvertBinaryNumberBytesToTextNumberBytes(outData
                                                                                   , ParentIDE.Machine.ByteOrder, _OutputDevice.CharSet);
                    Array.Resize <byte>(ref outdevData, outdevData.Length + _bOutSeparator.Length);
                    Array.Copy(_bOutSeparator, 0, outdevData, outdevData.Length
                               - _bOutSeparator.Length, _bOutSeparator.Length);
                    break;

                case OutputConvertings.None:
                    outdevData = outData;
                    break;

                default:
                    throw new Exception();
                }

                bool outDone, locked;

                if (!OutputAsyncAllowed)
                {
                    locked = false;
                    if ((OutputDeviceThreadSafe.Initialize & _OutputDeviceIsThreadSafeFor) == 0)
                    {
                        IOmonitor.IdleForLock(_OutputDevice, out locked);
                        if (!_OutputDevice.IsInitialized)
                        {
                            _OutputDevice.Initialize(this);
                        }
                    }
                    else
                    if (!_OutputDevice.IsInitialized)
                    {
                        _OutputDevice.Initialize(this);
                    }

                    if (!locked && (OutputDeviceThreadSafe.Output & _OutputDeviceIsThreadSafeFor) == 0)
                    {
                        IOmonitor.IdleForLock(_OutputDevice, out locked);
                    }
                    if (_bOutputForeword != null)
                    {
                        _OutputDevice.Output(this, _bOutputForeword);
                    }
                    _OutputDevice.Output(this, outdevData);
                    if (_bOutputAfterword != null)
                    {
                        _OutputDevice.Output(this, _bOutputAfterword);
                    }
                    if (locked)
                    {
                        IOmonitor.Unlock(_OutputDevice);
                    }
                }
                else
                // using output buffer:
                {
                    if (_BackgroundOutputTask == null)
                    // I. if buffer is empty
                    {
                        locked = false;
                        if ((OutputDeviceThreadSafe.Initialize & _OutputDeviceIsThreadSafeFor) == 0)
                        {
                            IOmonitor.TryLock(_OutputDevice, out locked);
                            if (locked && !_OutputDevice.IsInitialized)
                            {
                                _OutputDevice.Initialize(this);
                            }
                        }
                        else
                        if (!_OutputDevice.IsInitialized)
                        {
                            _OutputDevice.Initialize(this);
                        }

                        // try instant output synchronously
                        outDone = false;
                        if ((OutputDeviceThreadSafe.Output & _OutputDeviceIsThreadSafeFor) == 0)
                        {
                            if (locked || IOmonitor.TryLock(_OutputDevice))
                            {
                                if (_OutputDevice.IsReadyToOutput)
                                {
                                    if (_bOutputForeword != null)
                                    {
                                        _OutputDevice.Output(this, _bOutputForeword);
                                    }
                                    _OutputDevice.Output(this, outdevData);
                                    if (_bOutputAfterword != null)
                                    {
                                        _OutputDevice.Output(this, _bOutputAfterword);
                                    }
                                    outDone = true;
                                }
                                IOmonitor.Unlock(_OutputDevice);
                            }
                        }
                        else
                        if (_OutputDevice.IsReadyToOutput)
                        {
                            if (_bOutputForeword != null)
                            {
                                _OutputDevice.Output(this, _bOutputForeword);
                            }
                            _OutputDevice.Output(this, outdevData);
                            if (_bOutputAfterword != null)
                            {
                                _OutputDevice.Output(this, _bOutputAfterword);
                            }
                            outDone = true;
                        }

                        // if sync output didn't occure, starting async task for outputting buffer
                        if (!outDone)
                        {
                            lock (_OutputBuffer)
                            {
                                _OutputBuffer.AddRange(outdevData);
                                _BackgroundOutputTask = Task.Run(_BackgroundOutputAction);
                            }
                        }
                    }
                    else
                    // II. if buffer is not empty
                    {
                        // initializing output device if it needs to
                        if ((OutputDeviceThreadSafe.Initialize & _OutputDeviceIsThreadSafeFor) == 0)
                        {
                            if (IOmonitor.TryLock(_OutputDevice))
                            {
                                if (!_OutputDevice.IsInitialized)
                                {
                                    _OutputDevice.Initialize(this);
                                }
                                IOmonitor.Unlock(_OutputDevice);
                            }
                        }
                        else
                        if (!_OutputDevice.IsInitialized)
                        {
                            _OutputDevice.Initialize(this);
                        }

                        // populating buffer
                        lock (_OutputBuffer)
                            _OutputBuffer.AddRange(outdevData);
                    }
                }
            }
            finally
            {
                IOmonitor.Unlock(_OutputDevice);
                //
                IOmonitor.Unlock(_IOprocessing);
            }
        }
Example #5
0
        public virtual int Input(out byte[] inData, int length)
        {
            try
            {
                IOmonitor.IdleForLock(_IOprocessing);
                //
                int    resultLength = -1;
                byte[] inBuff;

                int   parentMachineRequestCharCount, inputDeviceRequestByteCount;
                Int64 number;

                InputConvertings inputConverting = _InputConverting;

                inData = new byte[length];

                if (!InputFromBufferAllowed)
                {
                    switch (inputConverting)
                    {
                    case InputConvertings.None:
                        DirectInput(out inBuff, length);
                        resultLength = inBuff.Length <= length ? inBuff.Length : length;
                        Array.Copy(inBuff, inData, resultLength);
                        break;

                    case InputConvertings.String:
                        parentMachineRequestCharCount = ParentIDE.Machine.CharSet.GetMaxCharCount(length);
                        inputDeviceRequestByteCount   = _InputDevice.CharSet.GetMaxByteCount(parentMachineRequestCharCount);
                        DirectInput(out inBuff, inputDeviceRequestByteCount);
                        inBuff = Encoding.Convert(_InputDevice.CharSet, ParentIDE.Machine.CharSet
                                                  , inBuff);
                        resultLength = inBuff.Length <= length ? inBuff.Length : length;
                        Array.Copy(inBuff, inData, resultLength);
                        break;

                    case InputConvertings.ParsingNumber:
                        byte[] inBuff2;
                        parentMachineRequestCharCount = XBinary.MaxDigitCount(256, 10, length) + 1;
                        inputDeviceRequestByteCount   = _InputDevice.CharSet.GetMaxByteCount(parentMachineRequestCharCount);
                        DirectInput(out inBuff, inputDeviceRequestByteCount);
                        if (XBinary.TryParseTextNumberBytesToBinaryNumberBytes(inBuff
                                                                               , _InputDevice.CharSet, out inBuff2, length, ParentIDE.Machine.ByteOrder))
                        {
                            inData       = inBuff2;
                            resultLength = length;
                        }
                        else
                        {
                            resultLength = 0;
                        }
                        break;

                    default:
                        throw new Exception();
                    }
                }
                else
                {
                    // using input buffer:
                    switch (inputConverting)
                    {
                    case InputConvertings.None:
                        if (_InputBuffer.Length == 0)
                        {
                            DirectInput(out _InputBuffer, length);
                        }
                        //
                        if (_InputBuffer.Length == 0)
                        {
                            resultLength = 0;
                        }
                        else if (_InputBuffer.Length - _InBuffPtr <= length)
                        {
                            resultLength = _InputBuffer.Length - _InBuffPtr;
                            Array.Copy(_InputBuffer, _InBuffPtr, inData, 0, resultLength);
                            _InputBuffer = new byte[0];
                            _InBuffPtr   = 0;
                        }
                        else if (_InputBuffer.Length - _InBuffPtr > length)
                        {
                            resultLength = length;
                            Array.Copy(_InputBuffer, _InBuffPtr, inData, 0, resultLength);
                            _InBuffPtr += resultLength;
                        }
                        break;

                    case InputConvertings.String:
                        if (_InputBuffer.Length == 0)
                        {
                            parentMachineRequestCharCount = ParentIDE.Machine.CharSet.GetMaxCharCount(length);
                            inputDeviceRequestByteCount   = _InputDevice.CharSet.GetMaxByteCount(parentMachineRequestCharCount);
                            DirectInput(out inBuff, inputDeviceRequestByteCount);
                            _InputBuffer = Encoding.Convert(_InputDevice.CharSet, ParentIDE.Machine.CharSet
                                                            , inBuff);
                        }
                        //
                        if (_InputBuffer.Length == 0)
                        {
                            resultLength = 0;
                        }
                        else if (_InputBuffer.Length - _InBuffPtr <= length)
                        {
                            resultLength = _InputBuffer.Length - _InBuffPtr;
                            Array.Copy(_InputBuffer, _InBuffPtr, inData, 0, resultLength);
                            _InputBuffer = new byte[0];
                            _InBuffPtr   = 0;
                        }
                        else if (_InputBuffer.Length - _InBuffPtr > length)
                        {
                            resultLength = length;
                            Array.Copy(_InputBuffer, _InBuffPtr, inData, 0, resultLength);
                            _InBuffPtr += resultLength;
                        }
                        break;

                    case InputConvertings.ParsingNumber:
                        if (_InputWordBuffer.Length == 0)
                        {
                            parentMachineRequestCharCount = XBinary.MaxDigitCount(256, 10, length) + 1;
                            inputDeviceRequestByteCount   = _InputDevice.CharSet.GetMaxByteCount(parentMachineRequestCharCount);
                            DirectInput(out inBuff, inputDeviceRequestByteCount);
                            byte[] unicodeLineBytes = Encoding.Convert(_InputDevice.CharSet, Encoding.Unicode, inBuff);
                            char[] unicodeLineChars = new char[Encoding.Unicode.GetCharCount(unicodeLineBytes, 0, unicodeLineBytes.Length)];
                            Encoding.Unicode.GetChars(unicodeLineBytes, 0, unicodeLineBytes.Length, unicodeLineChars, 0);
                            string newLine = new string(unicodeLineChars);
                            _InputWordBuffer = newLine.Split(Separators, StringSplitOptions.RemoveEmptyEntries);
                        }
                        //
                        if (_InputWordBuffer.Length == 0)
                        {
                            resultLength = 0;
                        }
                        else if (_InputWordBuffer.Length - _WdBuffPtr > 0)
                        {
                            if (Int64.TryParse(_InputWordBuffer[_WdBuffPtr], out number))
                            {
                                inData = XBinary.FormatSignedNumberBytes(BitConverter.GetBytes(number), length
                                                                         , XBinary.HostMachineByteOrder, ParentIDE.Machine.ByteOrder);
                                resultLength = length;
                            }
                            else
                            {
                                resultLength = 0;
                            }
                            //
                            _WdBuffPtr++;
                            if (_InputWordBuffer.Length == _WdBuffPtr)
                            {
                                _InputWordBuffer = new string[0];
                                _WdBuffPtr       = 0;
                            }
                        }
                        break;

                    default:
                        throw new Exception();
                    }
                }

                return(resultLength);
            }
            finally
            {
                //
                IOmonitor.Unlock(_IOprocessing);
            }
        }