Exemplo n.º 1
0
 public async Task ReceiveAsync(ITcpClient client)
 {
     try
     {
         ThrowIf.ArgumentIsNull(client, nameof(client));
         byte[]         buffer        = new byte[BufferLength];
         INetworkStream networkStream = default;
         try
         {
             networkStream = client.GetStream();
         }
         catch (Exception exception)
         {
             if (!Disconnecting)
             {
                 throw new TcpIpGetStreamFailedException(exception);
             }
         }
         StreamBufferReader2 streamBufferReader = StreamBufferReader2.Create(Identifier, Encoding.ASCII,
                                                                             Convert.ToByte(EndOfLine), SynchronizationContext,
                                                                             OnDataItemReceived);
         await ReceiveAsync(networkStream, streamBufferReader, buffer).ConfigureAwait(false);
     }
     catch (Exception exception)
     {
         if (!Disconnecting)
         {
             throw new TcpIpReceiveFailedException(exception);
         }
     }
 }
Exemplo n.º 2
0
        public virtual async Task <int> ReadAsync(INetworkStream networkStream, byte[] buffer)
        {
            ThrowIf.ArgumentIsNull(networkStream, nameof(networkStream));
            ThrowIf.ArgumentIsNull(buffer, nameof(buffer));
            int bytesReceived = await networkStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

            return(bytesReceived);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
     }
 }
Exemplo n.º 5
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.º 6
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.º 7
0
 public NetworkStreamProxy(NetworkStream networkStream)
 {
     ThrowIf.ArgumentIsNull(networkStream, nameof(networkStream));
     _networkStream = networkStream;
 }
Exemplo n.º 8
0
 public static async Task StartAsync(IPEndPoint ipEndPoint, TcpIpServerOptions tcpIpServerOptions)
 {
     ThrowIf.ArgumentIsNull(ipEndPoint, nameof(ipEndPoint));
     await StartAsync(ipEndPoint.Address, ipEndPoint.Port, tcpIpServerOptions).ConfigureAwait(false);
 }
Exemplo n.º 9
0
 public TcpListenerProxy(IPAddress ipAddress, int port)
 {
     ThrowIf.ArgumentIsNull(ipAddress, nameof(ipAddress));
     _tcpListener = new TcpListener(ipAddress, port);
 }
Exemplo n.º 10
0
 public async Task ConnectAsync(IPAddress ipAddress, int port)
 {
     ThrowIf.ArgumentIsNull(ipAddress, nameof(ipAddress));
     await ConnectAsyncImpl(ipAddress, port).ConfigureAwait(false);
 }