cli.getOptionValue("loglevel", "INFO"))); NioDatagramAcceptor acceptor = new NioDatagramAcceptor(); final SwitchHandler handler = new SwitchHandler(acceptor); acceptor.setHandler(handler); DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); LoggingFilter loggingFilter = new LoggingFilter(); loggingFilter.setMessageReceivedLogLevel(LogLevel.TRACE); chain.addLast("logger", loggingFilter); DatagramSessionConfig dcfg = acceptor.getSessionConfig(); dcfg.setReuseAddress(true); acceptor.addListener(new IoServiceListener() { acceptor.bind(bindAddress); + acceptor.getLocalAddress().getPort());
@Override public void setDefaultLocalAddress(InetSocketAddress localAddress) { setDefaultLocalAddress((SocketAddress) localAddress); }
/** * {@inheritDoc} */ @Override protected void dispose0() throws Exception { unbind(); startupAcceptor(); wakeup(); }
IoSession session = acceptor.newSession(telex.getTo(), acceptor.getLocalAddress()); line.setSession(session); session.setAttribute("line", line);
} else { acceptor = new NioDatagramAcceptor(); acceptor.setHandler(new UDPIOHandler()); acceptor.setDefaultLocalAddress(isa); DatagramSessionConfig dcfg = acceptor.getSessionConfig(); dcfg.setReadBufferSize(64 * 1024); dcfg.setSendBufferSize(64 * 1024); dcfg.setReuseAddress(true); acceptor.bind();
private void readHandle(DatagramChannel handle) throws Exception { IoBuffer readBuf = IoBuffer.allocate(getSessionConfig().getReadBufferSize()); SocketAddress remoteAddress = receive(handle, readBuf); if (remoteAddress != null) { IoSession session = newSessionWithoutLock(remoteAddress, localAddress(handle)); readBuf.flip(); if (!session.isReadSuspended()) { session.getFilterChain().fireMessageReceived(readBuf); } } }
@Override protected DatagramChannel open(SocketAddress localAddress) throws Exception { final DatagramChannel c = DatagramChannel.open(); boolean success = false; try { new NioDatagramSessionConfig(c).setAll(getSessionConfig()); c.configureBlocking(false); c.socket().bind(localAddress); c.register(selector, SelectionKey.OP_READ); success = true; } finally { if (!success) { close(c); } } return c; }
private IoSession newSessionWithoutLock(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { DatagramChannel handle = boundHandles.get(localAddress); if (handle == null) { throw new IllegalArgumentException("Unknown local address: " + localAddress); } IoSession session; synchronized (sessionRecycler) { session = sessionRecycler.recycle(remoteAddress); if (session != null) { return session; } // If a new session needs to be created. NioSession newSession = newSession(this, handle, remoteAddress); getSessionRecycler().put(newSession); session = newSession; } initSession(session, null, null); try { this.getFilterChainBuilder().buildFilterChain(session.getFilterChain()); getListeners().fireSessionCreated(session); } catch (Exception e) { ExceptionMonitor.getInstance().exceptionCaught(e); } return session; }
DatagramChannel handle = open(socketAddress); newHandles.put(localAddress(handle), handle); getListeners().fireServiceActivated(); req.setDone(); for (DatagramChannel handle : newHandles.values()) { try { close(handle); } catch (Exception e) { ExceptionMonitor.getInstance().exceptionCaught(e); wakeup();
synchronized (this) { if (!isRunning) { DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); if (factory == null) { factory = new DefaultProtocolCodecFactory(); DatagramSessionConfig dc = acceptor.getSessionConfig(); dc.setReuseAddress(minaServerConfig.isReuseAddress()); dc.setReceiveBufferSize(minaServerConfig.getReceiveBufferSize()); dc.setCloseOnPortUnreachable(true); acceptor.setHandler(ioHandler); try { acceptor.bind(new InetSocketAddress(minaServerConfig.getPort())); LOGGER.warn("已开始监听UDP端口:{}", minaServerConfig.getPort()); } catch (IOException e) {
/** * Helper method to create an IoAcceptor */ private IoAcceptor createAcceptor( String address, int port ) { NioDatagramAcceptor acceptor = new NioDatagramAcceptor(); InetSocketAddress socketAddress = null; // The address can be null here, if one want to connect using the wildcard address if ( address == null ) { // Create a socket listening on the wildcard address socketAddress = new InetSocketAddress( port ); } else { socketAddress = new InetSocketAddress( address, port ); } acceptor.setDefaultLocalAddress( socketAddress ); return acceptor; }
int selected = select(SELECT_TIMEOUT); processReadySessions(selectedHandles()); if (selectable && isDisposing()) { selectable = false; try { destroy(); } catch (Exception e) { ExceptionMonitor.getInstance().exceptionCaught(e);
private int unregisterHandles() { int nHandles = 0; for (;;) { AcceptorOperationFuture request = cancelQueue.poll(); if (request == null) { break; } // close the channels for (SocketAddress socketAddress : request.getLocalAddresses()) { DatagramChannel handle = boundHandles.remove(socketAddress); if (handle == null) { continue; } try { close(handle); wakeup(); // wake up again to trigger thread death } catch (Exception e) { ExceptionMonitor.getInstance().exceptionCaught(e); } finally { nHandles++; } } request.setDone(); } return nHandles; }
/** * Creates a new instance. */ private NioDatagramAcceptor(IoSessionConfig sessionConfig, Executor executor) { super(sessionConfig, executor); try { init(); selectable = true; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeIoException("Failed to initialize.", e); } finally { if (!selectable) { try { destroy(); } catch (Exception e) { ExceptionMonitor.getInstance().exceptionCaught(e); } } } }
public UdpServer(MinaServerConfig minaServerConfig, IoHandler ioHandler) { this.minaServerConfig = minaServerConfig; this.ioHandler = ioHandler; acceptor = new NioDatagramAcceptor(); }
} else { acceptor = new NioDatagramAcceptor(); acceptor.setHandler(new UDPIOHandler()); acceptor.setDefaultLocalAddress(isa); DatagramSessionConfig dcfg = acceptor.getSessionConfig(); dcfg.setReadBufferSize(64 * 1024); dcfg.setSendBufferSize(64 * 1024); dcfg.setReuseAddress(true); acceptor.bind();
private void readHandle(DatagramChannel handle) throws Exception { IoBuffer readBuf = IoBuffer.allocate(getSessionConfig().getReadBufferSize()); SocketAddress remoteAddress = receive(handle, readBuf); if (remoteAddress != null) { IoSession session = newSessionWithoutLock(remoteAddress, localAddress(handle)); readBuf.flip(); if (!session.isReadSuspended()) { session.getFilterChain().fireMessageReceived(readBuf); } } }
protected DatagramChannel open(SocketAddress localAddress) throws Exception { final DatagramChannel ch = DatagramChannel.open(); boolean success = false; try { new NioDatagramSessionConfig(ch).setAll(getSessionConfig()); ch.configureBlocking(false); try { ch.socket().bind(localAddress); } catch (IOException ioe) { // Add some info regarding the address we try to bind to the // message String newMessage = "Error while binding on " + localAddress + "\n" + "original message : " + ioe.getMessage(); Exception e = new IOException(newMessage); e.initCause(ioe.getCause()); // And close the channel ch.close(); throw e; } ch.register(selector, SelectionKey.OP_READ); success = true; } finally { if (!success) { close(ch); } } return ch; }
private IoSession newSessionWithoutLock(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { DatagramChannel handle = boundHandles.get(localAddress); if (handle == null) { throw new IllegalArgumentException("Unknown local address: " + localAddress); } IoSession session; synchronized (sessionRecycler) { session = sessionRecycler.recycle(remoteAddress); if (session != null) { return session; } // If a new session needs to be created. NioSession newSession = newSession(this, handle, remoteAddress); getSessionRecycler().put(newSession); session = newSession; } initSession(session, null, null); try { this.getFilterChainBuilder().buildFilterChain(session.getFilterChain()); getListeners().fireSessionCreated(session); } catch (Exception e) { ExceptionMonitor.getInstance().exceptionCaught(e); } return session; }