/// <summary> /// Send a message to the peer node, asynchronously. /// </summary> /// <param name="data">Data to send to the peer node.</param> /// <returns>Task with Boolean indicating if the message was sent successfully.</returns> public async Task <bool> SendAsync(byte[] data) { if (_ClusterClient != null) { if (_ClusterClient.Connected) { return(await _ClusterClient.SendAsync(data)); } } else if (String.IsNullOrEmpty(_CurrPeerIpPort)) { if (Debug) { Console.WriteLine("No peer connected"); } return(false); } else if (_ClusterServer != null) { if (_ClusterServer.IsConnected(_CurrPeerIpPort)) { return(await _ClusterServer.SendAsync(_CurrPeerIpPort, data)); } } if (Debug) { Console.WriteLine("Neither server or client are healthy"); } return(false); }
/// <summary> /// Send a message to the peer using a stream. /// </summary> /// <param name="metadata">Dictionary containing metadata.</param> /// <param name="contentLength">The amount of data to read from the stream.</param> /// <param name="stream">The stream containing the data.</param> /// <returns>True if successful.</returns> public async Task <bool> SendAsync(Dictionary <object, object> metadata, long contentLength, Stream stream) { if (contentLength < 0) { throw new ArgumentException("Content length must be zero or greater."); } if (stream == null || !stream.CanRead) { throw new ArgumentException("Cannot read from supplied stream."); } if (_ClusterClient != null) { if (_ClusterClient.Connected) { return(await _ClusterClient.SendAsync(metadata, contentLength, stream)); } } else if (String.IsNullOrEmpty(_CurrPeerIpPort)) { Logger?.Invoke("[ClusterNode] No peer connected"); return(false); } else if (_ClusterServer != null) { if (_ClusterServer.IsConnected(_CurrPeerIpPort)) { return(await _ClusterServer.SendAsync(_CurrPeerIpPort, metadata, contentLength, stream)); } } Logger?.Invoke("[ClusterNode] Neither server or client are healthy"); return(false); }