/// <summary> /// Opens a broadcast channel that uses a instance to /// broadcast messages across a collection of servers. This is typically used on networks /// that do not support multicast. /// </summary> /// <param name="settings">The settings to use for the <see cref="UdpBroadcastClient" />.</param> public void OpenUdpBroadcast(UdpBroadcastClientSettings settings) { using (TimedLock.Lock(router.SyncRoot)) { this.broadcastClient = new UdpBroadcastClient(settings); this.broadcastClient.PacketReceived += new UdpBroadcastDelegate(OnBroadcastReceive); this.transport = Transport.Multicast; this.localEP = router.NormalizeEP(new ChannelEP(Transport.Udp, router.UdpEP)); this.port = 0; this.sendQueue = null; this.onSend = null; this.sendMsg = null; this.cbSend = 0; this.sendBuf = null; this.msgBuf = new byte[TcpConst.MTU]; this.cloudEP = null; this.onSocketReceive = null; this.recvBuf = null; router.Trace(1, "UDP: OpenUdpBroadcast", null, null); // Mark the channel as open. this.isOpen = true; } }
private System.Net.EndPoint recvEP; // Receives the transmitting socket's endpoint /// <summary> /// Constructor. /// </summary> /// <param name="router">The associated message router.</param> public UdpChannel(MsgRouter router) { this.router = router; this.isOpen = false; this.sock = null; this.port = 0; this.recvEP = new IPEndPoint(IPAddress.Any, 0); this.multicastInit = false; this.broadcastClient = null; }
/// <summary> /// Opens a UDP unicast socket. /// </summary> /// <param name="ep">The UDP endpoint to open.</param> /// <remarks> /// <para> /// Pass <b>ep.Address=IPAddress.Any</b> if the channel should be opened on all /// network adapters. /// </para> /// <para> /// Pass <b>ep.Port=0</b> if Windows should assign the socket's port. The /// port assigned can be determined via the <see cref="Port" /> property. /// </para> /// </remarks> public void OpenUnicast(IPEndPoint ep) { using (TimedLock.Lock(router.SyncRoot)) { this.sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); this.sock.Bind(ep); this.transport = Transport.Udp; this.localEP = router.NormalizeEP(new ChannelEP(Transport.Udp, (IPEndPoint)sock.LocalEndPoint)); this.port = localEP.NetEP.Port; this.sendQueue = new PriorityQueue <Msg>(); this.onSend = new AsyncCallback(OnSend); this.sendMsg = null; this.cbSend = 0; this.sendBuf = null; this.msgBuf = new byte[TcpConst.MTU]; this.cloudEP = null; this.multicastInit = false; this.broadcastClient = null; sendQueue.CountLimit = router.UdpMsgQueueCountMax; sendQueue.SizeLimit = router.UdpMsgQueueSizeMax; this.onSocketReceive = new AsyncCallback(OnSocketReceive); this.recvBuf = new byte[TcpConst.MTU]; router.Trace(1, "UDP: OpenUnicast", "localEP=" + localEP.NetEP.ToString(), null); sock.IgnoreUdpConnectionReset = true; sock.SendBufferSize = router.UdpUnicastSockConfig.SendBufferSize; sock.ReceiveBufferSize = router.UdpUnicastSockConfig.ReceiveBufferSize; // Initiate the first async receive operation on this socket sock.BeginReceiveFrom(recvBuf, 0, recvBuf.Length, SocketFlags.None, ref recvEP, onSocketReceive, null); // Mark the channel as open. this.isOpen = true; } }
/// <summary> /// Closes the channel if it's currently open. /// </summary> public void Close() { using (TimedLock.Lock(router.SyncRoot)) { if (!isOpen) { return; } if (sock != null) { if (localEP != null) { router.Trace(1, "UDP: Close", "LocalEP=" + localEP.NetEP.ToString(), null); } sock.Close(); sock = null; } else if (broadcastClient != null) { router.Trace(1, "UDP: Close(UDP-BROADCAST)", null, null); broadcastClient.Close(); broadcastClient = null; } isOpen = false; port = 0; sendQueue = null; onSend = null; sendBuf = null; sendMsg = null; onSocketReceive = null; recvBuf = null; } }