// returns true if this was the last entry public void Unsubscribe(IPAddress sourceAddress, IPAddress destAddress, SocketDataReceivedEventHandler callback) { lock (addressEntries) { int addressCount = 0; bool isMulticast = IsMulticast(destAddress); bool isBroadcast = IsBroadcast(destAddress); bool isSpecialAddress = isMulticast || isBroadcast; int removeIndex = -1; for (int i = 0; i < addressEntries.Count; i++) { AddressEntry entry = addressEntries[i]; if (entry.callback.Equals(callback) && entry.sourceAddress.Equals(sourceAddress) && entry.destAddress.Equals(destAddress)) { removeIndex = i; } else { if (isSpecialAddress && entry.destAddress.Equals(destAddress)) { addressCount++; } } } if (removeIndex != -1) { addressEntries.RemoveAt(removeIndex); if (isMulticast && addressCount == 0) { // remove the multicast address socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new MulticastOption(destAddress)); } else if (isBroadcast && addressCount == 0) { socket.EnableBroadcast = false; } } } }
// set source to any to receive from any source address, otherwise will only report those source addresses that match // set dest to any to receive data for any destination address, localhost to get only packets destined to the local // unicast address, set to any other address (including multicast, broadcast) to get packets for only that destination public void Subscribe(IPAddress sourceAddress, IPAddress destAddress, SocketDataReceivedEventHandler callback) { AddressEntry entry = new AddressEntry(sourceAddress, destAddress, callback); lock (addressEntries) { // determine if this is a multicast destination if (IsMulticast(destAddress)) { // it is, so join the group if we aren't already in it bool inGroup = false; foreach (AddressEntry ent in addressEntries) { if (ent.destAddress.Equals(destAddress)) { inGroup = true; } } if (!inGroup) { socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(destAddress)); } } if (IsBroadcast(destAddress)) { if (!socket.EnableBroadcast) { socket.EnableBroadcast = true; } } // add to the list of address entries addressEntries.Add(entry); } }
public void CloseCall() { this.isUdpConnectionMapped = false; this.udpReceiver.StopReceiver(); this.udpReceiver.SocketDataReceived -= udpReceiver_SocketDataReceived; this.callHelper.CloseCall(); this.callHelper.DataAvailable -= callHelper_DataAvailable; }
public void CloseCall(string receiver, string sender, string id) { if (this.callHelper != null && this.callHelper.IsCallEstablished) { this.isUdpConnectionMapped = false; this.udpReceiver.StopReceiver(); this.udpReceiver.SocketDataReceived -= udpReceiver_SocketDataReceived; this.callHelper.CloseCall(); this.callHelper.DataAvailable -= callHelper_DataAvailable; this.SendMessage(receiver, id, sender, "/call/close"); } }
public AddressEntry(IPAddress sourceAddress, IPAddress destAddress, SocketDataReceivedEventHandler callback) { this.sourceAddress = sourceAddress; this.destAddress = destAddress; this.callback = callback; }