use of org.apache.mina.filter.codec.ProtocolCodecFilter in project dubbo by alibaba.
the class MinaClient method doOpen.
@Override
protected void doOpen() throws Throwable {
connectorKey = getUrl().toFullString();
SocketConnector c = CONNECTORS.get(connectorKey);
if (c != null) {
connector = c;
} else {
// set thread pool.
connector = new SocketConnector(Constants.DEFAULT_IO_THREADS, Executors.newCachedThreadPool(new NamedThreadFactory("MinaClientWorker", true)));
// config
SocketConnectorConfig cfg = (SocketConnectorConfig) connector.getDefaultConfig();
cfg.setThreadModel(ThreadModel.MANUAL);
cfg.getSessionConfig().setTcpNoDelay(true);
cfg.getSessionConfig().setKeepAlive(true);
int timeout = getConnectTimeout();
cfg.setConnectTimeout(timeout < 1000 ? 1 : timeout / 1000);
// set codec.
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
CONNECTORS.put(connectorKey, connector);
}
}
use of org.apache.mina.filter.codec.ProtocolCodecFilter in project vert.x by eclipse.
the class FakeDNSServer method start.
@Override
public void start() throws IOException {
UdpTransport transport = new UdpTransport(ipAddress, port);
setTransports(transport);
acceptor = transport.getAcceptor();
acceptor.setHandler(new DnsProtocolHandler(this, new RecordStore() {
@Override
public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException {
RecordStore actual = store;
if (actual == null) {
return Collections.emptySet();
} else {
return actual.getRecords(question);
}
}
}) {
@Override
public void sessionCreated(IoSession session) throws Exception {
// Use our own codec to support AAAA testing
session.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new TestDnsProtocolUdpCodecFactory()));
}
@Override
public void messageReceived(IoSession session, Object message) {
if (message instanceof DnsMessage) {
synchronized (FakeDNSServer.this) {
currentMessage.add((DnsMessage) message);
}
}
super.messageReceived(session, message);
}
});
// Allow the port to be reused even if the socket is in TIME_WAIT state
((DatagramSessionConfig) acceptor.getSessionConfig()).setReuseAddress(true);
// Start the listener
acceptor.bind();
}
use of org.apache.mina.filter.codec.ProtocolCodecFilter in project pancm_project by xuwujing.
the class MinaServer method main.
/**
* The entry point of application.
*
* @param args the input arguments
*/
/* 启动此类 提示服务端运行成功后
* Windows 命令 输入 telnet 127.0.0.1 3305
* 然后输入消息 message
* 消息为bye的时候关闭连接
* */
public static void main(String[] args) {
IoAcceptor ia = null;
try {
// 创建一个非堵塞的server端Socket
ia = new NioSocketAcceptor();
// 创建 协议编码解码过滤器ProtocolCodecFilter
// ProtocolCodecFilter pf=new ProtocolCodecFilter(new TextLineCodecFactory(Charset
// .forName("UTF-8"),
// LineDelimiter.WINDOWS.getValue(),
// LineDelimiter.WINDOWS.getValue()));
// 设置端口
InetSocketAddress pt = new InetSocketAddress(PORT);
// 设置过滤器(使用Mina提供的文本换行符编解码器)
ia.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
// 设置读取数据的缓存区大小
ia.getSessionConfig().setReadBufferSize(2048);
// 读写通道10秒内无操作进入空闲状态
ia.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
// 绑定逻辑处理器
ia.setHandler(new MinaServerHandler());
// 绑定端口
ia.bind(pt);
logger.info("服务端启动成功...端口号为:" + PORT);
} catch (Exception e) {
logger.error("服务器的异常..." + e);
e.printStackTrace();
}
}
use of org.apache.mina.filter.codec.ProtocolCodecFilter in project zm-mailbox by Zimbra.
the class NioServer method start.
/**
* Starts the server. Binds the server port and starts the connection
* handler. Optionally adds an SSLFilter if ssl is enabled.
*
* @throws IOException if an I/O error occured while starting the server
*/
@Override
public void start() {
ServerConfig sc = getConfig();
DefaultIoFilterChainBuilder fc = acceptor.getFilterChain();
if (sc.isSslEnabled()) {
fc.addFirst("ssl", newSSLFilter());
}
fc.addLast("executer", executorFilter);
fc.addLast("logger", new NioLoggingFilter(this, false));
fc.addLast("codec", new ProtocolCodecFilter(getProtocolCodecFactory()));
for (IoFilter filter : FILTERS.get(getClass())) {
// insert custom filters
fc.addLast(filter.getClass().getName(), filter);
}
acceptor.getSessionConfig().setBothIdleTime(sc.getMaxIdleTime());
acceptor.getSessionConfig().setWriteTimeout(sc.getWriteTimeout());
acceptor.setHandler(new NioHandlerDispatcher(this));
try {
acceptor.bind();
} catch (Throwable e) {
Zimbra.halt(getName() + " failed to start", e);
}
getLog().info("Starting %s on %s", getName(), acceptor.getLocalAddress());
}
use of org.apache.mina.filter.codec.ProtocolCodecFilter in project MtgDesktopCompanion by nicho92.
the class ConsoleServer method start.
@Override
public void start() throws IOException {
acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName(getString("ENCODING")))));
acceptor.getSessionConfig().setReadBufferSize(Integer.parseInt(getString("BUFFER-SIZE")));
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, Integer.parseInt(getString("IDLE-TIME")));
acceptor.setHandler(new MTGConsoleHandler());
acceptor.bind(new InetSocketAddress(Integer.parseInt(getString("SERVER-PORT"))));
logger.info("Server started on port " + getString("SERVER-PORT"));
}
Aggregations