public void Dispose() { lock (eventLock) { quitting = true; manager.Dispose(); if (bridge != null) { bridge.Dispose(); } } }
void OnConnectionChanged(IBridge bridge, bool connected) { lock (eventLock) { if (!connected && bridge == this.bridge) { // Previous connection is no longer available. bridge.Dispose(); this.connected = false; ReportConnection(false); // Scan. NewManager(); } else if (connected && this.connected && bridge != this.bridge) { // Another connection was already made. bridge.Dispose(); } } }
private void quitBtn_Click(object sender, System.EventArgs e) { Close(); bridge?.Dispose(); }
void OnInputChanged(IBridge bridge, byte[] input) { lock (eventLock) { if (quitting) { bridge.Dispose(); } else if (connected) { if (this.bridge == bridge) { int pin; int state; foreach (byte b in input) { DecodeState(b, out pin, out state); states[pin] = state; // First message indicates state, not change. if (setup[pin]) { counts[pin, state] += (ulong)factor[pin]; ReportInput(pin, state); } else { setup[pin] = true; } } } else { // Another worker had previously hanshook. bridge.Dispose(); } } else { // Verify signature. if (!buffer.ContainsKey(bridge)) { buffer[bridge] = ""; } buffer[bridge] += Encoding.ASCII.GetString(input); if (buffer[bridge].IndexOf(greeting) >= 0) { // 2) Expect 255 back. this.bridge = bridge; connected = true; manager.Dispose(); // Clear buffer. buffer.Clear(); ReportConnection(true); } else if (buffer[bridge].IndexOf("protocol:\"r or d?\"\n") >= 0) { // 1) Receive protocol:"r or d?" and respond with r. bridge.Write(new byte[] { (byte)'r' }); } else if (greeting.IndexOf(buffer[bridge]) == -1) { // Not a match. bridge.Dispose(); } } } }