Browse Source

Socket config added. #107

master
Nikita 11 years ago
parent
commit
f0920349c7
  1. 10
      src/main/java/com/corundumstudio/socketio/Configuration.java
  2. 83
      src/main/java/com/corundumstudio/socketio/SocketConfig.java
  3. 24
      src/main/java/com/corundumstudio/socketio/SocketIOServer.java

10
src/main/java/com/corundumstudio/socketio/Configuration.java

@ -58,6 +58,8 @@ public class Configuration {
private boolean preferDirectBuffer = true;
private SocketConfig socketConfig = new SocketConfig();
private StoreFactory storeFactory = new MemoryStoreFactory();
private JsonSupport jsonSupport = new JacksonJsonSupport(this);
@ -101,6 +103,7 @@ public class Configuration {
setStoreFactory(conf.getStoreFactory());
setAuthorizationListener(conf.getAuthorizationListener());
setExceptionListener(conf.getExceptionListener());
setSocketConfig(conf.getSocketConfig());
}
private String join(Transport[] transports) {
@ -388,4 +391,11 @@ public class Configuration {
return exceptionListener;
}
public SocketConfig getSocketConfig() {
return socketConfig;
}
public void setSocketConfig(SocketConfig socketConfig) {
this.socketConfig = socketConfig;
}
}

83
src/main/java/com/corundumstudio/socketio/SocketConfig.java

@ -0,0 +1,83 @@
/**
* Copyright 2012 Nikita Koksharov
*
* 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.corundumstudio.socketio;
public class SocketConfig {
private boolean tcpNoDelay = true;
private int tcpSendBufferSize = -1;
private int tcpReceiveBufferSize = -1;
private boolean tcpKeepAlive = false;
private int soLinger = -1;
private boolean reuseAddress = false;
private int acceptBackLog = 1024;
public boolean isTcpNoDelay() {
return tcpNoDelay;
}
public void setTcpNoDelay(boolean tcpNoDelay) {
this.tcpNoDelay = tcpNoDelay;
}
public int getTcpSendBufferSize() {
return tcpSendBufferSize;
}
public void setTcpSendBufferSize(int tcpSendBufferSize) {
this.tcpSendBufferSize = tcpSendBufferSize;
}
public int getTcpReceiveBufferSize() {
return tcpReceiveBufferSize;
}
public void setTcpReceiveBufferSize(int tcpReceiveBufferSize) {
this.tcpReceiveBufferSize = tcpReceiveBufferSize;
}
public boolean isTcpKeepAlive() {
return tcpKeepAlive;
}
public void setTcpKeepAlive(boolean tcpKeepAlive) {
this.tcpKeepAlive = tcpKeepAlive;
}
public int getSoLinger() {
return soLinger;
}
public void setSoLinger(int soLinger) {
this.soLinger = soLinger;
}
public boolean isReuseAddress() {
return reuseAddress;
}
public void setReuseAddress(boolean reuseAddress) {
this.reuseAddress = reuseAddress;
}
public int getAcceptBackLog() {
return acceptBackLog;
}
public void setAcceptBackLog(int acceptBackLog) {
this.acceptBackLog = acceptBackLog;
}
}

24
src/main/java/com/corundumstudio/socketio/SocketIOServer.java

@ -18,10 +18,13 @@ package com.corundumstudio.socketio;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.FixedRecvByteBufAllocator;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.Collection;
import org.slf4j.Logger;
@ -100,10 +103,9 @@ public class SocketIOServer implements ClientListeners {
pipelineFactory.start(configCopy, namespacesHub);
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_KEEPALIVE, true)
.channel(NioServerSocketChannel.class)
.childHandler(pipelineFactory);
applyConnectionOptions(b);
InetSocketAddress addr = new InetSocketAddress(configCopy.getPort());
if (configCopy.getHostname() != null) {
@ -111,10 +113,28 @@ public class SocketIOServer implements ClientListeners {
}
b.bind(addr).syncUninterruptibly();
log.info("Session store / pubsub factory used: {}", configCopy.getStoreFactory());
log.info("SocketIO server started at port: {}", configCopy.getPort());
}
protected void applyConnectionOptions(ServerBootstrap bootstrap) {
SocketConfig config = configCopy.getSocketConfig();
bootstrap.childOption(ChannelOption.TCP_NODELAY, config.isTcpNoDelay());
if (config.getTcpSendBufferSize() != -1) {
bootstrap.childOption(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize());
}
if (config.getTcpReceiveBufferSize() != -1) {
bootstrap.childOption(ChannelOption.SO_RCVBUF, config.getTcpReceiveBufferSize());
bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(config.getTcpReceiveBufferSize()));
}
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive());
bootstrap.option(ChannelOption.SO_LINGER, config.getSoLinger());
bootstrap.option(ChannelOption.SO_REUSEADDR, config.isReuseAddress());
bootstrap.option(ChannelOption.SO_BACKLOG, config.getAcceptBackLog());
}
protected void initGroups() {
bossGroup = new NioEventLoopGroup(configCopy.getBossThreads());
workerGroup = new NioEventLoopGroup(configCopy.getWorkerThreads());

Loading…
Cancel
Save