private static void StreamReceived(object sender, StreamReceivedFromClientEventArgs args) { try { Console.Write("Stream from " + args.IpPort + " [" + args.ContentLength + " bytes]: "); int bytesRead = 0; int bufferSize = 65536; byte[] buffer = new byte[bufferSize]; long bytesRemaining = args.ContentLength; if (args.DataStream != null && args.DataStream.CanRead) { while (bytesRemaining > 0) { bytesRead = args.DataStream.Read(buffer, 0, buffer.Length); Console.WriteLine("Read " + bytesRead); if (bytesRead > 0) { byte[] consoleBuffer = new byte[bytesRead]; Buffer.BlockCopy(buffer, 0, consoleBuffer, 0, bytesRead); Console.Write(Encoding.UTF8.GetString(consoleBuffer)); } bytesRemaining -= bytesRead; } Console.WriteLine(""); if (args.Metadata != null && args.Metadata.Count > 0) { Console.WriteLine("Metadata:"); foreach (KeyValuePair <object, object> curr in args.Metadata) { Console.WriteLine(" " + curr.Key.ToString() + ": " + curr.Value.ToString()); } } } else { Console.WriteLine("[null]"); } } catch (Exception e) { LogException("StreamReceived", e); } }
private static void ServerStreamReceived(object sender, StreamReceivedFromClientEventArgs args) { // Console.WriteLine("[server] stream from " + args.IpPort + ": " + BytesToHex(Md5(args.Data)) + " (" + args.Data.Length + " bytes)"); try { string md5 = BytesToHex(Md5(args.Data)); if (!md5.Equals(_DataLargeMd5) && !md5.Equals(_DataSmallMd5)) { Interlocked.Increment(ref _Failure); Console.WriteLine("[server] [stream] [async] Data MD5 validation failed"); } else { Interlocked.Increment(ref _Success); // Console.WriteLine("[server] [stream] [async] Data MD5 validation success: " + md5); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// Event handler for receiving stream from Joined. Currently made for downloading files. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void StreamReceived(object sender, StreamReceivedFromClientEventArgs args) { UIHandler.WriteLog($"Downloading {DownloadFileTo.Replace(Filed.RootPath, "")}...", Color.Green); UIHandler.ResetProgressBarValue(); gettingFile = true; long bytesRemaining = args.ContentLength; int bytesRead = 0; byte[] buffer = new byte[bufferSize]; UIHandler.SetProgressBarMaxValue((int)bytesRemaining); Directory.CreateDirectory(Path.GetDirectoryName(DownloadFileTo)); using (FileStream fs = new FileStream(DownloadFileTo, FileMode.CreateNew)) { while (bytesRemaining > 0) { bytesRead = args.DataStream.Read(buffer, 0, buffer.Length); if (bytesRead > 0) { fs.Write(buffer, 0, bytesRead); UIHandler.IncrementProgressBar(bytesRead); bytesRemaining -= bytesRead; } } } Filed.FilesData.Add(new FileData(Filed.RootPath, DownloadFileTo)); Filed.ChangeFileModifiedStatusByRelativePath(DownloadFileTo.Replace(Filed.RootPath, ""), FileModifiedStatus.Changed); UIHandler.WriteLog($"Downloaded {DownloadFileTo.Replace(Filed.RootPath, "")}", Color.Green); DownloadFileTo = ""; servH.Events.StreamReceived -= StreamReceived; gettingFile = false; }
static void ServerStreamReceived(object sender, StreamReceivedFromClientEventArgs args) { Console.Write("Server received " + args.ContentLength + " bytes from " + args.IpPort + ": MD5 " + Md5(args.DataStream)); }
private void MeshServerStreamReceived(object sender, StreamReceivedFromClientEventArgs args) { Logger?.Invoke("[MeshServer] Message received from client " + args.IpPort + ": " + args.ContentLength + " bytes"); MessageReceived?.Invoke(this, args); }
private void StreamReceived(object sender, StreamReceivedFromClientEventArgs args) { Logger?.Invoke("[ClusterServer] Stream received from " + args.IpPort + ": " + args.ContentLength + " bytes"); MessageReceived?.Invoke(this, new MessageReceivedEventArgs(args.Metadata, args.ContentLength, args.DataStream)); }