public async Task Send(byte[] message) { if (SelectedDevice?.State != DeviceState.Connected) { return; } var size = message.Length; var lastDataPacketSize = size % Settings.PacketSize; var numberOfCompletePackages = size / Settings.PacketSize; for (int i = 0; i < numberOfCompletePackages; i++) { var data = new ArraySegment <byte>(message, i * Settings.PacketSize, Settings.PacketSize); await WriteToDevice(data.ToArray()); } if (lastDataPacketSize > 0) { var data = new ArraySegment <byte>(message, numberOfCompletePackages * Settings.PacketSize, lastDataPacketSize); await WriteToDevice(data.ToArray()); } BytesSent?.Invoke(this, message); }
/// <summary> /// Write byte array to client. /// </summary> /// <param name="buffer"></param> public void WriteToClient(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } TcpStream.Write(buffer, 0, buffer.Length); BytesSent?.Invoke(this, new TcpServerDataEventArgs(this, buffer, buffer.Length)); TcpStream.Flush(); }
public void Connect(string portName) { Disconnect(); Arduino = new ArduinoSerial(portName); Arduino.BytesSent += (port, bytes) => BytesSent?.Invoke(port, bytes); Arduino.BytesReceived += (port, bytes) => BytesReceived?.Invoke(port, bytes); Arduino.Connect(false); _thread = new Thread(Loop); _thread.IsBackground = true; _thread.Start(); }
private Task OnBytesSent(int count) { if (count > 0) { if (BytesSent != null) { return(BytesSent.InvokeAllAsync(this, new BytesSentEventArgs(count))); } } return(Task.CompletedTask); // return a fake completed task to continue on awaiting }
public ConnectResult TryConnect(string portName, bool sayhello) { if (portName == "") { return(ConnectResult.InvalidArgument); } Disconnect(); EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.AutoReset); ConnectResult result = ConnectResult.None; Arduino = new ArduinoSerial(portName); Arduino.BytesSent += (port, bytes) => BytesSent?.Invoke(port, bytes); Arduino.BytesReceived += (port, bytes) => BytesReceived?.Invoke(port, bytes); ArduinoSerial.StatusChangedHandler statuschanged = status => { lock (ewh) { if (result != ConnectResult.None) { return; } if (status == ArduinoSerial.Status.Connected || status == ArduinoSerial.Status.ConnectedUnsafe) { result = ConnectResult.Success; ewh.Set(); } if (status == ArduinoSerial.Status.Error) { result = ConnectResult.Error; ewh.Set(); } } }; Arduino.StatusChanged += statuschanged; Arduino.Connect(sayhello); if (!ewh.WaitOne(300) && sayhello) { Arduino.Disconnect(); Arduino = null; return(ConnectResult.Timeout); } if (result != ConnectResult.Success) { Arduino.Disconnect(); Arduino = null; return(result); } Arduino.StatusChanged -= statuschanged; _thread = new Thread(Loop); _thread.IsBackground = true; _thread.Start(); return(ConnectResult.Success); }
public static void AddBytesSent(int increment) { if (initializationSuccesfull) { if (BytesSent != null) { BytesSent.IncrementBy(increment); } if (GlobalBytesSent != null) { GlobalBytesSent.IncrementBy(increment); } GlobalLog.Print("NetworkingPerfCounters::AddBytesSent(" + increment.ToString() + ")"); } }
private void OnBytesSent(INetworkNode To, int NumberOfBytes) { if ((BytesSent == null) || (To == null) || (NumberOfBytes <= 0)) { return; } Task.Run(() => { BytesSent?.Invoke(this, new InternetBytesTransferredEventArgs { Remote = To, Local = this, Direction = CommunicationDirection.Outbound, NumBytes = NumberOfBytes }); }); }
public void RegisterMetrics(MetricFactory metrics) { if (!_socketCounters.Enabled) { return; } OutgoingConnectionEstablished = metrics.CreateCounter("dotnet_sockets_connections_established_outgoing_total", "The total number of outgoing established TCP connections"); var lastEstablishedOutgoing = 0.0; _socketCounters.Events.OutgoingConnectionsEstablished += e => { OutgoingConnectionEstablished.Inc(e.Mean - lastEstablishedOutgoing); lastEstablishedOutgoing = e.Mean; }; IncomingConnectionEstablished = metrics.CreateCounter("dotnet_sockets_connections_established_incoming_total", "The total number of incoming established TCP connections"); var lastEstablishedIncoming = 0.0; _socketCounters.Events.IncomingConnectionsEstablished += e => { IncomingConnectionEstablished.Inc(e.Mean - lastEstablishedIncoming); lastEstablishedIncoming = e.Mean; }; BytesReceived = metrics.CreateCounter("dotnet_sockets_bytes_received_total", "The total number of bytes received over the network"); var lastReceived = 0.0; _socketCounters.Events.BytesReceived += e => { BytesReceived.Inc(e.Mean - lastReceived); lastReceived = e.Mean; }; var lastSent = 0.0; BytesSent = metrics.CreateCounter("dotnet_sockets_bytes_sent_total", "The total number of bytes sent over the network"); _socketCounters.Events.BytesSent += e => { BytesSent.Inc(e.Mean - lastSent); lastSent = e.Mean; }; }
public async Task upload(int file, string tokenId, string tokenKey, string parent) { // var client = _clientFactory.CreateClient(); // var getTask = client.GetAsync(link, HttpCompletionOption.ResponseHeadersRead); // HttpResponseMessage response = await getTask.ConfigureAwait(false); // response.EnsureSuccessStatusCode(); // var lengthHeader = response.Content.Headers.TryGetValues("Content-Length", out var lengthStringArray); // var lengthString = lengthStringArray?.FirstOrDefault(); // long.TryParse(lengthString, out var length); // HttpContent c = response.Content; // var stream = c != null ? await c.ReadAsStreamAsync().ConfigureAwait(false) : // System.IO.Stream.Null; var streamResult = await _downloadersService.GetFileStream(file); var rfile = await _downloadersService.GetFileById(file); var subPath = new List <string>(); if (rfile.Show != null) { subPath.Add(rfile.Show.Name); } if (rfile.Episode != null) { subPath.Add($"Season {rfile.Episode.Season}"); } var length = streamResult.Length; Action <long> onProgress = (BytesSent) => { if (length > 0) { double progressPercent = (double)BytesSent / (double)length * 100; _logger.LogInformation($"Uploaded ${rfile.ToString()} {Math.Round(progressPercent, 2)}% {BytesSent.HumanReadableFileSize()} / {length.HumanReadableFileSize()}"); } else { _logger.LogInformation($"Uploaded {BytesSent.HumanReadableFileSize()} bytes"); } }; await upload(streamResult.Stream, tokenId, tokenKey, rfile.Name, parent, streamResult.Length, subPath.ToArray(), onProgress : onProgress); }
void VideosInsertRequest_ProgressChanged(IUploadProgress Progress) { BytesSent?.Invoke(Progress.BytesSent); }
/// <summary> /// Write byte array to client. /// </summary> /// <param name="buffer"></param> public void WriteToClient(byte[] buffer) { TcpStream.Write(buffer, 0, buffer.Length); BytesSent?.Invoke(this, new TcpServerDataEventArgs(this, buffer, buffer.Length)); TcpStream.Flush(); }