java.nio.channels.NetworkChannel Java Examples

The following examples show how to use java.nio.channels.NetworkChannel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NetSystem.java    From Mycat-NIO with Apache License 2.0 6 votes vote down vote up
public void setSocketParams(Connection con, boolean isFrontChannel) throws IOException {
	int sorcvbuf = 0;
	int sosndbuf = 0;
	int soNoDelay = 0;
	if (isFrontChannel) {
		sorcvbuf = netConfig.getFrontsocketsorcvbuf();
		sosndbuf = netConfig.getFrontsocketsosndbuf();
		soNoDelay = netConfig.getFrontSocketNoDelay();
	} else {
		sorcvbuf = netConfig.getBacksocketsorcvbuf();
		sosndbuf = netConfig.getBacksocketsosndbuf();
		soNoDelay = netConfig.getBackSocketNoDelay();
	}
	NetworkChannel channel = con.getChannel();
	channel.setOption(StandardSocketOptions.SO_RCVBUF, sorcvbuf);
	channel.setOption(StandardSocketOptions.SO_SNDBUF, sosndbuf);
	channel.setOption(StandardSocketOptions.TCP_NODELAY, soNoDelay == 1);
	channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);

	con.setMaxPacketSize(netConfig.getMaxPacketSize());
	con.setPacketHeaderSize(netConfig.getPacketHeaderSize());

}
 
Example #2
Source File: MySQLConnectionFactory.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
public MySQLConnection make(MySQLDataSource pool, ResponseHandler handler,
                            String schema) throws IOException {

    DataSourceConfig dsc = pool.getConfig();
    NetworkChannel channel = openSocketChannel(DbleServer.getInstance().isAIO());

    MySQLConnection c = new MySQLConnection(channel, pool.isReadNode(), pool.isAutocommitSynced(), pool.isIsolationSynced());
    c.setSocketParams(false);
    c.setHost(dsc.getIp());
    c.setPort(dsc.getPort());
    c.setUser(dsc.getUser());
    c.setPassword(dsc.getPassword());
    c.setSchema(schema);
    c.setHandler(new MySQLConnectionAuthenticator(c, handler));
    c.setPool(pool);
    c.setIdleTimeout(pool.getConfig().getIdleTimeout());
    if (channel instanceof AsynchronousSocketChannel) {
        ((AsynchronousSocketChannel) channel).connect(
                new InetSocketAddress(dsc.getIp(), dsc.getPort()), c,
                (CompletionHandler) DbleServer.getInstance().getConnector());
    } else {
        ((NIOConnector) DbleServer.getInstance().getConnector()).postConnect(c);
    }
    return c;
}
 
Example #3
Source File: MySQLConnection.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
public MySQLConnection(NetworkChannel channel, boolean fromSlaveDB, boolean autocommitSynced, boolean isolationSynced) {
    super(channel);
    this.lastTime = TimeUtil.currentTimeMillis();
    this.autocommitSynced = autocommitSynced;
    boolean sysAutocommit = DbleServer.getInstance().getConfig().getSystem().getAutocommit() == 1;
    this.autocommit = sysAutocommit == autocommitSynced; // T + T-> T, T + F-> F, F +T ->F, F + F->T
    this.fromSlaveDB = fromSlaveDB;
    this.isolationSynced = isolationSynced;
    if (isolationSynced) {
        this.txIsolation = DbleServer.getInstance().getConfig().getSystem().getTxIsolation();
    } else {
        /* if the txIsolation in server.xml is different from the isolation level in MySQL node,
         * it need to sync the status firstly for new idle connection*/
        this.txIsolation = -1;
    }
    this.complexQuery = false;
    this.usrVariables = new LinkedHashMap<>();
    this.sysVariables = new LinkedHashMap<>();
}
 
Example #4
Source File: AIOAcceptor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private void accept(NetworkChannel channel, Long id) {
	try {
		FrontendConnection c = factory.make(channel);
		c.setAccepted(true);
		c.setId(id);
		NIOProcessor processor = MycatServer.getInstance().nextProcessor();
		c.setProcessor(processor);
		c.register();
	} catch (Exception e) {
	    LOGGER.error("AioAcceptorError", e);
		closeChannel(channel);
	}
}
 
Example #5
Source File: AbstractConnection.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
public AbstractConnection(NetworkChannel channel) {
    this.channel = channel;
    boolean isAIO = (channel instanceof AsynchronousChannel);
    if (isAIO) {
        socketWR = new AIOSocketWR(this);
    } else {
        socketWR = new NIOSocketWR(this);
    }
    this.startupTime = TimeUtil.currentTimeMillis();
    this.lastReadTime = startupTime;
    this.lastWriteTime = startupTime;
}
 
Example #6
Source File: FrontendConnectionFactory.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
public FrontendConnection make(NetworkChannel channel) throws IOException {
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);

    FrontendConnection c = getConnection(channel);
    c.setSocketParams(true);
    return c;
}
 
Example #7
Source File: BackendConnectionFactory.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
protected NetworkChannel openSocketChannel(boolean isAIO)
        throws IOException {
    if (isAIO) {
        return AsynchronousSocketChannel.open(DbleServer.getInstance().getNextAsyncChannelGroup());
    } else {
        SocketChannel channel = null;
        channel = SocketChannel.open();
        channel.configureBlocking(false);
        return channel;
    }

}
 
Example #8
Source File: ServerConnectionFactory.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected FrontendConnection getConnection(NetworkChannel channel) throws IOException {
    ServerConnection c = new ServerConnection(channel);
    c.setSocketParams(true);
    c.setPrivileges(ServerPrivileges.instance());
    c.setQueryHandler(new ServerQueryHandler(c));
    c.setLoadDataInfileHandler(new ServerLoadDataInfileHandler(c));
    c.setPrepareHandler(new ServerPrepareHandler(c));
    SystemConfig sys = DbleServer.getInstance().getConfig().getSystem();
    c.setTxIsolation(sys.getTxIsolation());
    c.setSession2(new NonBlockingSession(c));
    return c;
}
 
Example #9
Source File: ServerConnection.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
public ServerConnection(NetworkChannel channel)
        throws IOException {
    super(channel);
    this.txInterrupted = false;
    this.autocommit = DbleServer.getInstance().getConfig().getSystem().getAutocommit() == 1;
    this.txID = new AtomicLong(1);
    this.sptprepare = new ServerSptPrepare(this);
    this.usrVariables = new LinkedHashMap<>();
    this.sysVariables = new LinkedHashMap<>();
}
 
Example #10
Source File: ManagerConnectionFactory.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected FrontendConnection getConnection(NetworkChannel channel) throws IOException {
    ManagerConnection c = new ManagerConnection(channel);
    c.setSocketParams(true);
    c.setPrivileges(ManagerPrivileges.instance());
    c.setHandler(new ManagerAuthenticator(c));
    c.setQueryHandler(new ManagerQueryHandler(c));
    return c;
}
 
Example #11
Source File: MySQLConnectionFactory.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public MySQLConnection make(MySQLDataSource pool, ResponseHandler handler,
		String schema) throws IOException {

	DBHostConfig dsc = pool.getConfig();
	NetworkChannel channel = openSocketChannel(MycatServer.getInstance()
			.isAIO());

	MySQLConnection c = new MySQLConnection(channel, pool.isReadNode());
	MycatServer.getInstance().getConfig().setSocketParams(c, false);
	c.setHost(dsc.getIp());
	c.setPort(dsc.getPort());
	c.setUser(dsc.getUser());
	c.setPassword(dsc.getPassword());
	c.setSchema(schema);
	c.setHandler(new MySQLConnectionAuthenticator(c, handler));
	c.setPool(pool);
	c.setIdleTimeout(pool.getConfig().getIdleTimeout());
	if (channel instanceof AsynchronousSocketChannel) {
		((AsynchronousSocketChannel) channel).connect(
				new InetSocketAddress(dsc.getIp(), dsc.getPort()), c,
				(CompletionHandler) MycatServer.getInstance()
						.getConnector());
	} else {
		((NIOConnector) MycatServer.getInstance().getConnector())
				.postConnect(c);

	}
	return c;
}
 
Example #12
Source File: MySQLConnection.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public MySQLConnection(NetworkChannel channel, boolean fromSlaveDB) {
	super(channel);
	this.clientFlags = CLIENT_FLAGS;
	this.lastTime = TimeUtil.currentTimeMillis();
	this.isQuit = new AtomicBoolean(false);
	this.autocommit = true;
	this.fromSlaveDB = fromSlaveDB;
	// 设为默认值,免得每个初始化好的连接都要去同步一下
	this.txIsolation = MycatServer.getInstance().getConfig().getSystem().getTxIsolation();
	this.txReadonly = false;
}
 
Example #13
Source File: PostgreSQLBackendConnectionFactory.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public PostgreSQLBackendConnection make(PostgreSQLDataSource pool,
		ResponseHandler handler, final String schema) throws IOException {

	final DBHostConfig dsc = pool.getConfig();
	NetworkChannel channel = this.openSocketChannel(MycatServer
			.getInstance().isAIO());

	final PostgreSQLBackendConnection c = new PostgreSQLBackendConnection(
			channel, pool.isReadNode());
	MycatServer.getInstance().getConfig().setSocketParams(c, false);
	// 设置NIOHandler
	c.setHandler(new PostgreSQLBackendConnectionHandler(c));
	c.setHost(dsc.getIp());
	c.setPort(dsc.getPort());
	c.setUser(dsc.getUser());
	c.setPassword(dsc.getPassword());
	c.setSchema(schema);
	c.setPool(pool);
	c.setResponseHandler(handler);
	c.setIdleTimeout(pool.getConfig().getIdleTimeout());
	if (channel instanceof AsynchronousSocketChannel) {
		((AsynchronousSocketChannel) channel).connect(
				new InetSocketAddress(dsc.getIp(), dsc.getPort()), c,
				(CompletionHandler) MycatServer.getInstance()
						.getConnector());
	} else {
		((NIOConnector) MycatServer.getInstance().getConnector())
				.postConnect(c);

	}
	return c;
}
 
Example #14
Source File: AIOAcceptor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private static void closeChannel(NetworkChannel channel) {
	if (channel == null) {
		return;
	}
	try {
		channel.close();
	} catch (IOException e) {
        LOGGER.error("AioAcceptorError", e);
	}
}
 
Example #15
Source File: AbstractConnection.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public AbstractConnection(NetworkChannel channel) {
	this.channel = channel;
	boolean isAIO = (channel instanceof AsynchronousChannel);
	if (isAIO) {
		socketWR = new AIOSocketWR(this);
	} else {
		socketWR = new NIOSocketWR(this);
	}
	this.isClosed = new AtomicBoolean(false);
	this.startupTime = TimeUtil.currentTimeMillis();
	this.lastReadTime = startupTime;
	this.lastWriteTime = startupTime;
}
 
Example #16
Source File: AbstractJsseEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected final InetSocketAddress getLocalAddress() throws IOException {
    NetworkChannel serverSock = getServerSocket();
    if (serverSock == null) {
        return null;
    }
    SocketAddress sa = serverSock.getLocalAddress();
    if (sa instanceof InetSocketAddress) {
        return (InetSocketAddress) sa;
    }
    return null;
}
 
Example #17
Source File: BackendConnectionFactory.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
protected NetworkChannel openSocketChannel(boolean isAIO)
		throws IOException {
	if (isAIO) {
		return AsynchronousSocketChannel
               .open(MycatServer.getInstance().getNextAsyncChannelGroup());
	} else {
		SocketChannel channel = null;
		channel = SocketChannel.open();
		channel.configureBlocking(false);
		return channel;
	}

}
 
Example #18
Source File: MycatConfig.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public void setSocketParams(AbstractConnection con, boolean isFrontChannel)
		throws IOException {
	
	int sorcvbuf = 0;
	int sosndbuf = 0;
	int soNoDelay = 0;
	if ( isFrontChannel ) {
		sorcvbuf = system.getFrontsocketsorcvbuf();
		sosndbuf = system.getFrontsocketsosndbuf();
		soNoDelay = system.getFrontSocketNoDelay();
	} else {
		sorcvbuf = system.getBacksocketsorcvbuf();
		sosndbuf = system.getBacksocketsosndbuf();
		soNoDelay = system.getBackSocketNoDelay();
	}
	
	NetworkChannel channel = con.getChannel();
	channel.setOption(StandardSocketOptions.SO_RCVBUF, sorcvbuf);
	channel.setOption(StandardSocketOptions.SO_SNDBUF, sosndbuf);
	channel.setOption(StandardSocketOptions.TCP_NODELAY, soNoDelay == 1);
	channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
	
	con.setMaxPacketSize(system.getMaxPacketSize());
	con.setPacketHeaderSize(system.getPacketHeaderSize());
	con.setIdleTimeout(system.getIdleTimeout());
	con.setCharset(system.getCharset());

}
 
Example #19
Source File: ServerConnectionFactory.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected FrontendConnection getConnection(NetworkChannel channel) throws IOException {
    SystemConfig sys = MycatServer.getInstance().getConfig().getSystem();
    ServerConnection c = new ServerConnection(channel);
    MycatServer.getInstance().getConfig().setSocketParams(c, true);
    c.setPrivileges(MycatPrivileges.instance());
    c.setQueryHandler(new ServerQueryHandler(c));
    c.setLoadDataInfileHandler(new ServerLoadDataInfileHandler(c));
    c.setPrepareHandler(new ServerPrepareHandler(c,sys.getMaxPreparedStmtCount()));
    c.setTxIsolation(sys.getTxIsolation());
    c.setSession2(new NonBlockingSession(c));
    return c;
}
 
Example #20
Source File: ServerConnection.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public ServerConnection(NetworkChannel channel)
		throws IOException {
	super(channel);
	this.txInterrupted = false;
	this.autocommit = true;
	this.preAcStates = true;
	this.txReadonly = false;
}
 
Example #21
Source File: FrontendConnectionFactory.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public FrontendConnection make(NetworkChannel channel) throws IOException {
	channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
	channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);

	FrontendConnection c = getConnection(channel);
	MycatServer.getInstance().getConfig().setSocketParams(c, true);
	return c;
}
 
Example #22
Source File: ManagerConnectionFactory.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected FrontendConnection getConnection(NetworkChannel channel) throws IOException {
    ManagerConnection c = new ManagerConnection(channel);
    MycatServer.getInstance().getConfig().setSocketParams(c, true);
    c.setPrivileges(MycatPrivileges.instance());
    c.setQueryHandler(new ManagerQueryHandler(c));
    return c;
}
 
Example #23
Source File: ManagerConnection.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
public ManagerConnection(NetworkChannel channel) throws IOException {
    super(channel);
}
 
Example #24
Source File: Nio2Endpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
protected NetworkChannel getServerSocket() {
    return serverSock;
}
 
Example #25
Source File: NioEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
protected NetworkChannel getServerSocket() {
    return serverSock;
}
 
Example #26
Source File: AbstractConnection.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
public NetworkChannel getChannel() {
    return channel;
}
 
Example #27
Source File: SqueakTCPSocket.java    From trufflesqueak with MIT License 4 votes vote down vote up
@Override
protected NetworkChannel asNetworkChannel() {
    return listening ? serverChannel : clientChannel;
}
 
Example #28
Source File: SqueakUDPSocket.java    From trufflesqueak with MIT License 4 votes vote down vote up
@Override
protected NetworkChannel asNetworkChannel() {
    return channel;
}
 
Example #29
Source File: FrontendConnectionFactory.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
protected abstract FrontendConnection getConnection(NetworkChannel channel)
throws IOException;
 
Example #30
Source File: ManagerConnection.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public ManagerConnection(NetworkChannel channel) throws IOException {
	super(channel);
}