//当设备应该下线时(对方通知,它自己要下线了),完成这个代理本身的清理工作。 internal void Offline() { AppModel.Instance.RemoveDevice(this); State = DeviceState.OffLine; try { Udp?.Close(); } catch (Exception ex) { } }
public async Task ReceiveData(CancellationToken token) { StringBuilder sb = new StringBuilder(); while (true) { if (token.IsCancellationRequested) { UDP.Close(); break; } var buffer = await UDP.ReceiveAsync(); var remoteInfo = buffer.RemoteEndPoint; var bufferAsString = Encoding.UTF8.GetString(buffer.Buffer); sb.Append(bufferAsString); if (bufferAsString.EndsWith("\r\n", StringComparison.InvariantCultureIgnoreCase)) { var fullString = sb.ToString().Trim(); sb.Clear(); fullLineEventHandler.Invoke(this, new UDPJsonLineReceivedEventArgs { JSONString = fullString, RemoteInfo = remoteInfo }); } else if (bufferAsString.StartsWith("{", StringComparison.InvariantCultureIgnoreCase) && bufferAsString.EndsWith("}", StringComparison.InvariantCultureIgnoreCase) && bufferAsString.Contains("Publish")) { var fullString = sb.ToString(); var obj = JsonConvert.DeserializeObject(fullString); sb.Clear(); fullLineEventHandler.Invoke(this, new UDPJsonLineReceivedEventArgs { JSON = obj, JSONString = fullString, RemoteInfo = remoteInfo }); } } return; }
public void Close() { udp.Close(); state = State.Closed; }