예제 #1
0
        /// <summary>
        /// Creates a new instance of the <see cref="StreamBufferReader"/> class.
        /// </summary>
        /// <param name="identifier">An integer to associate the stream to.</param>
        /// <param name="encoding">Data <see cref="Encoding"/> used by the stream.</param>
        /// <param name="delimiter">Determines boundaries for each string segment in the <see cref="Stream"/></param>
        /// <param name="onNewString">Action to assign to the <see cref="NewString"/> event of the <see cref="StreamBufferReader"/> class.</param>
        /// <returns>A new instance of the <see cref="StreamBufferReader"/> class</returns>
        public static StreamBufferReader2 Create(int identifier, Encoding encoding, byte delimiter,
                                                 SynchronizationContext synchronizationContext,
                                                 Action <int, SynchronizationContext, string> onNewString)
        {
            ThrowIf.ArgumentIsNull(onNewString, nameof(onNewString));
            StreamBufferReader2 streamBufferReader = new StreamBufferReader2(identifier, encoding, delimiter, synchronizationContext)
            {
                OnNewString = onNewString
            };

            return(streamBufferReader);
        }
예제 #2
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;
 }
예제 #3
0
 public async Task StartListeningAsync(IPAddress ipAddress, int port)
 {
     ThrowIf.ArgumentIsNull(ipAddress, nameof(ipAddress));
     Disconnecting = false;
     try
     {
         _tcpListener = GetTcpListener(ipAddress, port);
         if (_tcpListener != null)
         {
             StartTcpListener(_tcpListener);
             await AcceptTcpClientsAsync(_tcpListener).ConfigureAwait(false);
         }
     }
     catch (Exception exception)
     {
         throw new TcpIpListenerStartListeningAsyncException(exception);
     }
 }
예제 #4
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);
     }
 }
예제 #5
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);
                }
            }
        }
예제 #6
0
 public NetworkStreamProxy(NetworkStream networkStream)
 {
     ThrowIf.ArgumentIsNull(networkStream, nameof(networkStream));
     _networkStream = networkStream;
 }
예제 #7
0
 public static async Task StartAsync(IPEndPoint ipEndPoint, TcpIpServerOptions tcpIpServerOptions)
 {
     ThrowIf.ArgumentIsNull(ipEndPoint, nameof(ipEndPoint));
     await StartAsync(ipEndPoint.Address, ipEndPoint.Port, tcpIpServerOptions).ConfigureAwait(false);
 }
예제 #8
0
 public TcpListenerProxy(IPAddress ipAddress, int port)
 {
     ThrowIf.ArgumentIsNull(ipAddress, nameof(ipAddress));
     _tcpListener = new TcpListener(ipAddress, port);
 }
예제 #9
0
 public static void ArgumentIsOutOfRange <T>(this T value, Func <T, bool> isOutOfRange, string paramName)
 {
     ThrowIf.ArgumentIsOutOfRange(value, isOutOfRange, paramName, ArgumentOutOfRangeExceptionMessage);
 }
예제 #10
0
        protected void Send(string value, ITcpClient tcpClient)
        {
            value += EndOfLine;
            INetworkStream networkStream = default;

            byte[] bytes = default;
            try
            {
                ThrowIf.IsNull(tcpClient, "Error in Send().  tcpClient can't be null!");
                try
                {
                    bytes = Encoding.ASCII.GetBytes(value);
                }
                catch (Exception exception)
                {
                    if (!Disconnecting)
                    {
                        throw new TcpIpGetBytesFailedException(exception);
                    }
                }
                try
                {
                    networkStream = tcpClient.GetStream();
                }
                catch (Exception exception)
                {
                    if (!Disconnecting)
                    {
                        throw new TcpIpGetStreamFailedException(exception);
                    }
                }
                if (networkStream.CanWrite)
                {
                    try
                    {
                        networkStream.Write(bytes, 0, bytes.Length);
                    }
                    catch (Exception exception)
                    {
                        if (!Disconnecting)
                        {
                            throw new TcpIpNetworkStreamWriteFailedException(exception);
                        }
                    }
                }
                else
                {
                    if (!Disconnecting)
                    {
                        throw new TcpIpNetworkStreamIsNotWriteableException();
                    }
                }
            }
            catch (Exception exception)
            {
                if (!Disconnecting)
                {
                    throw new TcpIpSendFailedException(exception);
                }
            }
        }
예제 #11
0
 public async Task ConnectAsync(IPAddress ipAddress, int port)
 {
     ThrowIf.ArgumentIsNull(ipAddress, nameof(ipAddress));
     await ConnectAsyncImpl(ipAddress, port).ConfigureAwait(false);
 }