static void StartMulticastAcceptor() { IPAddress localIPAddr = IPAddress.Any; AsyncDatagramAcceptor acceptor = new AsyncDatagramAcceptor(); acceptor.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8))); // Define a MulticastOption object specifying the multicast group // address and the local IPAddress. // The multicast group address is the same as the address used by the client. MulticastOption mcastOption = new MulticastOption(mcastAddress, localIPAddr); acceptor.SessionConfig.MulticastOption = mcastOption; acceptor.SessionOpened += (s, e) => { Console.WriteLine("Opened: {0}", e.Session.RemoteEndPoint); }; acceptor.MessageReceived += (s, e) => { Console.WriteLine("Received from {0}: {1}", e.Session.RemoteEndPoint, e.Message); }; acceptor.Bind(new IPEndPoint(localIPAddr, mcastPort)); Console.WriteLine("Acceptor: current multicast group is: " + mcastOption.Group); Console.WriteLine("Acceptor: current multicast local address is: " + mcastOption.LocalAddress); Console.WriteLine("Waiting for multicast packets......."); }
static void Main(string[] args) { AsyncDatagramAcceptor acceptor = new AsyncDatagramAcceptor(); acceptor.FilterChain.AddLast("logger", new LoggingFilter()); acceptor.SessionConfig.ReuseAddress = true; acceptor.ExceptionCaught += (s, e) => { Console.WriteLine(e.Exception); e.Session.Close(true); }; acceptor.MessageReceived += (s, e) => { IoBuffer buf = e.Message as IoBuffer; if (buf != null) { Console.WriteLine("New value for {0}: {1}", e.Session.RemoteEndPoint, buf.GetInt64()); } }; acceptor.SessionCreated += (s, e) => { Console.WriteLine("Session created..."); }; acceptor.SessionOpened += (s, e) => { Console.WriteLine("Session opened..."); }; acceptor.SessionClosed += (s, e) => { Console.WriteLine("Session closed..."); }; acceptor.SessionIdle += (s, e) => { Console.WriteLine("Session idle..."); }; acceptor.Bind(new IPEndPoint(IPAddress.Any, port)); Console.WriteLine("UDPServer listening on port " + port); Console.ReadLine(); }
public void Start() { if (locator == null) throw new ApplicationException(); acceptor = new AsyncDatagramAcceptor(); //acceptor.FilterChain.AddLast("logger", new LoggingFilter()); acceptor.FilterChain.AddLast("RTPS", new ProtocolCodecFilter(new MessageCodecFactory())); acceptor.SessionConfig.ReuseAddress = true; acceptor.ExceptionCaught += (s, e) => { Console.WriteLine(e.Exception); e.Session.Close(true); }; acceptor.MessageReceived += (s, e) => { Message msg = e.Message as Message; log.DebugFormat("New value for {0}", e.Session.RemoteEndPoint); }; acceptor.SessionCreated += (s, e) => { log.Debug("Session created..."); }; acceptor.SessionOpened += (s, e) => { log.Debug("Session opened..."); }; acceptor.SessionClosed += (s, e) => { log.Debug("Session closed..."); }; acceptor.SessionIdle += (s, e) => { log.Debug("Session idle..."); }; acceptor.Bind(new IPEndPoint(locator.SocketAddress, locator.Port)); log.DebugFormat("Listening on udp://{0}:{1} for {2}", uri.Host, locator.Port, discovery ? "discovery traffic" : "user traffic"); }
public void TestSessionIdle() { int READER_IDLE_TIME = 3; //seconds int WRITER_IDLE_TIME = READER_IDLE_TIME + 2; //seconds int BOTH_IDLE_TIME = WRITER_IDLE_TIME + 2; //seconds AsyncDatagramAcceptor acceptor = new AsyncDatagramAcceptor(); acceptor.SessionConfig.SetIdleTime(IdleStatus.BothIdle, BOTH_IDLE_TIME); acceptor.SessionConfig.SetIdleTime(IdleStatus.ReaderIdle, READER_IDLE_TIME); acceptor.SessionConfig.SetIdleTime(IdleStatus.WriterIdle, WRITER_IDLE_TIME); IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234); acceptor.SessionIdle += (s, e) => { if (e.IdleStatus == IdleStatus.BothIdle) { bothIdleReceived = true; } else if (e.IdleStatus == IdleStatus.ReaderIdle) { readerIdleReceived = true; } else if (e.IdleStatus == IdleStatus.WriterIdle) { writerIdleReceived = true; } lock (mutex) { System.Threading.Monitor.PulseAll(mutex); } }; acceptor.Bind(ep); IoSession session = acceptor.NewSession(new IPEndPoint(IPAddress.Loopback, 1024), ep); //check properties to be copied from acceptor to session Assert.AreEqual(BOTH_IDLE_TIME, session.Config.BothIdleTime); Assert.AreEqual(READER_IDLE_TIME, session.Config.ReaderIdleTime); Assert.AreEqual(WRITER_IDLE_TIME, session.Config.WriterIdleTime); //verify that IDLE events really received by handler DateTime startTime = DateTime.Now; lock (mutex) { while (!readerIdleReceived && (DateTime.Now - startTime).TotalMilliseconds < (READER_IDLE_TIME + 1) * 1000) { try { System.Threading.Monitor.Wait(mutex, READER_IDLE_TIME * 1000); } catch (Exception e) { Console.WriteLine(e); } } } Assert.IsTrue(readerIdleReceived); lock (mutex) { while (!writerIdleReceived && (DateTime.Now - startTime).TotalMilliseconds < (WRITER_IDLE_TIME + 1) * 1000) { try { System.Threading.Monitor.Wait(mutex, (WRITER_IDLE_TIME - READER_IDLE_TIME) * 1000); } catch (Exception e) { Console.WriteLine(e); } } } Assert.IsTrue(writerIdleReceived); lock (mutex) { while (!bothIdleReceived && (DateTime.Now - startTime).TotalMilliseconds < (BOTH_IDLE_TIME + 1) * 1000) { try { System.Threading.Monitor.Wait(mutex, (BOTH_IDLE_TIME - WRITER_IDLE_TIME) * 1000); } catch (Exception e) { Console.WriteLine(e); } } } Assert.IsTrue(bothIdleReceived); }
/// <summary> /// Creates a new acceptor-side session instance. /// </summary> internal AsyncDatagramSession(IoService service, IoProcessor<AsyncDatagramSession> processor, AsyncDatagramAcceptor.SocketContext ctx, EndPoint remoteEP, Boolean reuseBuffer) : base(service, processor, new DefaultDatagramSessionConfig(), ctx.Socket, ctx.Socket.LocalEndPoint, remoteEP, reuseBuffer) { _socketContext = ctx; }
public void SetUp() { acceptor = new AsyncDatagramAcceptor(); connector = new AsyncDatagramConnector(); }
public void Start() { if (locator == null) throw new ApplicationException(); IPEndPoint ep = new IPEndPoint(locator.SocketAddress, locator.Port); bool isMultiCastAddr; if (ep.AddressFamily == AddressFamily.InterNetwork) //IP v4 { byte byteIp = ep.Address.GetAddressBytes()[0]; isMultiCastAddr = (byteIp >= 224 && byteIp < 240) ? true : false; } else if (ep.AddressFamily == AddressFamily.InterNetworkV6) { isMultiCastAddr = ep.Address.IsIPv6Multicast; } else { throw new NotImplementedException("Address family not supported yet: " + ep.AddressFamily); } if (isMultiCastAddr) { acceptor = new AsyncDatagramAcceptor(); // Define a MulticastOption object specifying the multicast group // address and the local IPAddress. // The multicast group address is the same as the address used by the client. MulticastOption mcastOption = new MulticastOption(locator.SocketAddress, IPAddress.Any); acceptor.SessionConfig.MulticastOption = mcastOption; acceptor.SessionConfig.ExclusiveAddressUse = false; acceptor.SessionConfig.ReuseAddress = true; } else acceptor = new AsyncDatagramAcceptor(); //acceptor.FilterChain.AddLast("logger", new LoggingFilter()); acceptor.FilterChain.AddLast("RTPS", new ProtocolCodecFilter(new MessageCodecFactory())); acceptor.SessionConfig.EnableBroadcast = true; acceptor.ExceptionCaught += (s, e) => { Console.WriteLine(e.Exception); e.Session.Close(true); }; acceptor.MessageReceived += (s, e) => { Message msg = e.Message as Message; if (MessageReceived != null) MessageReceived(s, new RTPSMessageEventArgs(e.Session, msg)); //if (log.IsDebugEnabled) //{ // log.DebugFormat("New Message has arrived from {0}", e.Session.RemoteEndPoint); // log.DebugFormat("Message Header: {0}", msg.Header); // foreach (var submsg in msg.SubMessages) // { // log.DebugFormat("SubMessage: {0}", submsg); // if (submsg is Data) // { // Data d = submsg as Data; // foreach (var par in d.InlineQos.Value) // log.DebugFormat("InlineQos: {0}", par); // } // } //} }; acceptor.SessionCreated += (s, e) => { log.Debug("Session created..."); }; acceptor.SessionOpened += (s, e) => { log.Debug("Session opened..."); }; acceptor.SessionClosed += (s, e) => { log.Debug("Session closed..."); }; acceptor.SessionIdle += (s, e) => { log.Debug("Session idle..."); }; if (isMultiCastAddr) acceptor.Bind(new IPEndPoint(IPAddress.Any, locator.Port)); else acceptor.Bind(new IPEndPoint(locator.SocketAddress, locator.Port)); log.DebugFormat("Listening on udp://{0}:{1} for {2}", locator.SocketAddress, locator.Port, IsDiscovery ? "IsDiscovery traffic" : "user traffic"); }
public void TestSessionIdle() { int READER_IDLE_TIME = 3;//seconds int WRITER_IDLE_TIME = READER_IDLE_TIME + 2;//seconds int BOTH_IDLE_TIME = WRITER_IDLE_TIME + 2;//seconds AsyncDatagramAcceptor acceptor = new AsyncDatagramAcceptor(); acceptor.SessionConfig.SetIdleTime(IdleStatus.BothIdle, BOTH_IDLE_TIME); acceptor.SessionConfig.SetIdleTime(IdleStatus.ReaderIdle, READER_IDLE_TIME); acceptor.SessionConfig.SetIdleTime(IdleStatus.WriterIdle, WRITER_IDLE_TIME); IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234); acceptor.SessionIdle += (s, e) => { if (e.IdleStatus == IdleStatus.BothIdle) { bothIdleReceived = true; } else if (e.IdleStatus == IdleStatus.ReaderIdle) { readerIdleReceived = true; } else if (e.IdleStatus == IdleStatus.WriterIdle) { writerIdleReceived = true; } lock (mutex) { System.Threading.Monitor.PulseAll(mutex); } }; acceptor.Bind(ep); IoSession session = acceptor.NewSession(new IPEndPoint(IPAddress.Loopback, 1024), ep); //check properties to be copied from acceptor to session Assert.AreEqual(BOTH_IDLE_TIME, session.Config.BothIdleTime); Assert.AreEqual(READER_IDLE_TIME, session.Config.ReaderIdleTime); Assert.AreEqual(WRITER_IDLE_TIME, session.Config.WriterIdleTime); //verify that IDLE events really received by handler DateTime startTime = DateTime.Now; lock (mutex) { while (!readerIdleReceived && (DateTime.Now - startTime).TotalMilliseconds < (READER_IDLE_TIME + 1) * 1000) try { System.Threading.Monitor.Wait(mutex, READER_IDLE_TIME * 1000); } catch (Exception e) { Console.WriteLine(e); } } Assert.IsTrue(readerIdleReceived); lock (mutex) { while (!writerIdleReceived && (DateTime.Now - startTime).TotalMilliseconds < (WRITER_IDLE_TIME + 1) * 1000) try { System.Threading.Monitor.Wait(mutex, (WRITER_IDLE_TIME - READER_IDLE_TIME) * 1000); } catch (Exception e) { Console.WriteLine(e); } } Assert.IsTrue(writerIdleReceived); lock (mutex) { while (!bothIdleReceived && (DateTime.Now - startTime).TotalMilliseconds < (BOTH_IDLE_TIME + 1) * 1000) try { System.Threading.Monitor.Wait(mutex, (BOTH_IDLE_TIME - WRITER_IDLE_TIME) * 1000); } catch (Exception e) { Console.WriteLine(e); } } Assert.IsTrue(bothIdleReceived); }