
com.generallycloud.baseio.component.ChannelAcceptor Maven / Gradle / Ivy
/*
* Copyright 2015-2017 GenerallyCloud.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.generallycloud.baseio.component;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import com.generallycloud.baseio.LifeCycleUtil;
import com.generallycloud.baseio.TimeoutException;
import com.generallycloud.baseio.buffer.ByteBuf;
import com.generallycloud.baseio.common.CloseUtil;
import com.generallycloud.baseio.concurrent.Waiter;
import com.generallycloud.baseio.log.Logger;
import com.generallycloud.baseio.log.LoggerFactory;
import com.generallycloud.baseio.protocol.Frame;
/**
* @author wangkai
*
*/
public class ChannelAcceptor extends ChannelContext {
private Logger logger = LoggerFactory.getLogger(getClass());
private ServerSocketChannel selectableChannel;
private NioEventLoopGroup bindGroup;
public ChannelAcceptor(NioEventLoopGroup group) {
this(group, "0.0.0.0", 0);
}
public ChannelAcceptor(NioEventLoopGroup group, int port) {
this(group, "0.0.0.0", port);
}
public ChannelAcceptor(NioEventLoopGroup group, String host, int port) {
super(group, host, port);
}
public ChannelAcceptor(String host, int port) {
this(new NioEventLoopGroup(), host, port);
}
public ChannelAcceptor(int port) {
this("0.0.0.0", port);
}
public void bind() throws IOException {
bind(50);
}
public synchronized void bind(int backlog) throws IOException {
if (isActive()) {
return;
}
String name = "bind-" + getHost() + ":" + getPort();
this.bindGroup = new NioEventLoopGroup(name);
this.bindGroup.setEnableMemoryPool(false);
this.bindGroup.setEnableMemoryPoolDirect(false);
this.bindGroup.setChannelReadBuffer(0);
this.bindGroup.setWriteBuffers(0);
this.getProcessorGroup().setContext(this);
LifeCycleUtil.start(this);
LifeCycleUtil.start(bindGroup);
final NioEventLoopGroup bindGroup = this.bindGroup;
final Waiter
© 2015 - 2025 Weber Informatics LLC | Privacy Policy