/// <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); }
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; }
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); } }
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); } }
/// <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); } } }
public NetworkStreamProxy(NetworkStream networkStream) { ThrowIf.ArgumentIsNull(networkStream, nameof(networkStream)); _networkStream = networkStream; }
public static async Task StartAsync(IPEndPoint ipEndPoint, TcpIpServerOptions tcpIpServerOptions) { ThrowIf.ArgumentIsNull(ipEndPoint, nameof(ipEndPoint)); await StartAsync(ipEndPoint.Address, ipEndPoint.Port, tcpIpServerOptions).ConfigureAwait(false); }
public TcpListenerProxy(IPAddress ipAddress, int port) { ThrowIf.ArgumentIsNull(ipAddress, nameof(ipAddress)); _tcpListener = new TcpListener(ipAddress, port); }
public static void ArgumentIsOutOfRange <T>(this T value, Func <T, bool> isOutOfRange, string paramName) { ThrowIf.ArgumentIsOutOfRange(value, isOutOfRange, paramName, ArgumentOutOfRangeExceptionMessage); }
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); } } }
public async Task ConnectAsync(IPAddress ipAddress, int port) { ThrowIf.ArgumentIsNull(ipAddress, nameof(ipAddress)); await ConnectAsyncImpl(ipAddress, port).ConfigureAwait(false); }