Exemplo n.º 1
0
 protected TcpIpNode(int identifier, int bufferLength, char endOfLine, SynchronizationContext synchronizationContext,
                     Func <ITcpIpNode, int, int, char, SynchronizationContext, Action <object, int, SynchronizationContext, string>,
                           Action <object, byte[], int>, ITcpReceiver> getTcpReceiver,
                     Action <object, int, SynchronizationContext, string> onDataItemReceived,
                     Action <object, byte[], int> onDataReceived)
 {
     ThrowIf.ArgumentIsOutOfRange(bufferLength, bLength => bLength < 1, nameof(bufferLength));
     Identifier             = identifier;
     BufferLength           = bufferLength;
     EndOfLine              = endOfLine;
     SynchronizationContext = synchronizationContext;
     _getTcpReceiver        = getTcpReceiver ?? _DEFAULTGETTCPRECEIVER;
     _onDataItemReceived    = onDataItemReceived;
     _onDataReceived        = onDataReceived;
 }
Exemplo n.º 2
0
 public TcpReceiver(ITcpIpNode tcpIpNode, int identifier, int bufferLength, char endOfLine, SynchronizationContext synchronizationContext,
                    Action <object, int, SynchronizationContext, string> onDataItemReceived, Action <object, byte[], int> onDataReceived)
 {
     ThrowIf.ArgumentIsNull(tcpIpNode, nameof(tcpIpNode));
     ThrowIf.ArgumentIsOutOfRange(bufferLength, bLength => bLength < 1, nameof(bufferLength));
     TcpIpNode              = tcpIpNode;
     Identifier             = identifier;
     BufferLength           = bufferLength;
     EndOfLine              = endOfLine;
     SynchronizationContext = synchronizationContext;
     if (onDataItemReceived != null)
     {
         DataItemReceived += (o, e) => onDataItemReceived(o, e.Identifier, e.SynchronizationContext, e.Data);
     }
     if (onDataReceived != null)
     {
         DataReceived += (o, e) => onDataReceived(o, e.Buffer, e.BytesReceived);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Reads a buffer (a byte array) converts the buffer's bytes into strings.  The converted strings are returned via the <see cref="NewString"/> event.
        /// </summary>
        /// <param name="buffer">The buffer to read data from.</param>
        /// <param name="bytesToRead">The number of bytes to read from the buffer.</param>
        public void GetStrings(byte[] buffer, int bytesToRead)
        {
            ThrowIf.ArgumentIsNull(buffer, nameof(buffer));
            ThrowIf.ArgumentIsOutOfRange(bytesToRead, n => n < 1, nameof(bytesToRead));
            int indexBegin = 0;
            int indexEnd   = Array.IndexOf(buffer, Delimiter, indexBegin, bytesToRead);

            if (indexEnd < 0)
            {
                _remainder += Encoding.GetString(buffer, indexBegin, bytesToRead);
            }
            else
            {
                do
                {
                    string s = Encoding.GetString(buffer, indexBegin, indexEnd - indexBegin);
                    string result;
                    if (_remainder.Length == 0)
                    {
                        result = s;
                    }
                    else
                    {
                        result     = _remainder + s;
                        _remainder = String.Empty;
                    }
                    OnNewString(Identifier, SynchronizationContext, result);
                    indexBegin = indexEnd + 1;
                    indexEnd   = Array.IndexOf(buffer, Delimiter, indexBegin, bytesToRead - indexBegin);
                } while (indexEnd > indexBegin);
                if (indexBegin > 0 && indexBegin < bytesToRead)
                {
                    _remainder = Encoding.GetString(buffer, indexBegin, bytesToRead - indexBegin);
                }
            }
        }
Exemplo n.º 4
0
 public static void ArgumentIsOutOfRange <T>(this T value, Func <T, bool> isOutOfRange, string paramName)
 {
     ThrowIf.ArgumentIsOutOfRange(value, isOutOfRange, paramName, ArgumentOutOfRangeExceptionMessage);
 }