|
|
|
@ -1,12 +1,12 @@
|
|
|
|
|
/** |
|
|
|
|
* Copyright 2012 Nikita Koksharov |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* 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
|
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
* <p> |
|
|
|
|
* 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. |
|
|
|
@ -37,6 +37,7 @@ import io.netty.util.concurrent.FutureListener;
|
|
|
|
|
import java.net.InetSocketAddress; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.UUID; |
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
@ -46,24 +47,27 @@ import com.fr.third.socketio.namespace.NamespacesHub;
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Fully thread-safe. |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
public class SocketIOServer implements ClientListeners { |
|
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(SocketIOServer.class); |
|
|
|
|
|
|
|
|
|
private final Configuration configCopy; |
|
|
|
|
|
|
|
|
|
private final Configuration configuration; |
|
|
|
|
|
|
|
|
|
private final NamespacesHub namespacesHub; |
|
|
|
|
|
|
|
|
|
private final SocketIONamespace mainNamespace; |
|
|
|
|
|
|
|
|
|
private SocketIOChannelInitializer pipelineFactory = new SocketIOChannelInitializer(); |
|
|
|
|
|
|
|
|
|
private EventLoopGroup bossGroup; |
|
|
|
|
|
|
|
|
|
private EventLoopGroup workerGroup; |
|
|
|
|
|
|
|
|
|
public SocketIOServer(Configuration configuration) { |
|
|
|
|
|
|
|
|
|
this.configuration = configuration; |
|
|
|
|
this.configCopy = new Configuration(configuration); |
|
|
|
|
namespacesHub = new NamespacesHub(configCopy); |
|
|
|
@ -71,6 +75,7 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setPipelineFactory(SocketIOChannelInitializer pipelineFactory) { |
|
|
|
|
|
|
|
|
|
this.pipelineFactory = pipelineFactory; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -80,6 +85,7 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
* @return clients collection |
|
|
|
|
*/ |
|
|
|
|
public Collection<SocketIOClient> getAllClients() { |
|
|
|
|
|
|
|
|
|
return namespacesHub.get(Namespace.DEFAULT_NAME).getAllClients(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -90,6 +96,7 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
* @return client |
|
|
|
|
*/ |
|
|
|
|
public SocketIOClient getClient(UUID uuid) { |
|
|
|
|
|
|
|
|
|
return namespacesHub.get(Namespace.DEFAULT_NAME).getClient(uuid); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -99,10 +106,12 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
* @return namespaces collection |
|
|
|
|
*/ |
|
|
|
|
public Collection<SocketIONamespace> getAllNamespaces() { |
|
|
|
|
|
|
|
|
|
return namespacesHub.getAllNamespaces(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public BroadcastOperations getBroadcastOperations() { |
|
|
|
|
|
|
|
|
|
return new BroadcastOperations(getAllClients(), configCopy.getStoreFactory()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -114,6 +123,7 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
* @return broadcast operations |
|
|
|
|
*/ |
|
|
|
|
public BroadcastOperations getRoomOperations(String room) { |
|
|
|
|
|
|
|
|
|
Iterable<SocketIOClient> clients = namespacesHub.getRoomClients(room); |
|
|
|
|
return new BroadcastOperations(clients, configCopy.getStoreFactory()); |
|
|
|
|
} |
|
|
|
@ -122,6 +132,7 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
* Start server |
|
|
|
|
*/ |
|
|
|
|
public void start() { |
|
|
|
|
|
|
|
|
|
startAsync().syncUninterruptibly(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -131,6 +142,7 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
* @return void |
|
|
|
|
*/ |
|
|
|
|
public Future<Void> startAsync() { |
|
|
|
|
|
|
|
|
|
log.info("Session store / pubsub factory used: {}", configCopy.getStoreFactory()); |
|
|
|
|
initGroups(); |
|
|
|
|
|
|
|
|
@ -143,8 +155,8 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
|
|
|
|
|
ServerBootstrap b = new ServerBootstrap(); |
|
|
|
|
b.group(bossGroup, workerGroup) |
|
|
|
|
.channel(channelClass) |
|
|
|
|
.childHandler(pipelineFactory); |
|
|
|
|
.channel(channelClass) |
|
|
|
|
.childHandler(pipelineFactory); |
|
|
|
|
applyConnectionOptions(b); |
|
|
|
|
|
|
|
|
|
InetSocketAddress addr = new InetSocketAddress(configCopy.getPort()); |
|
|
|
@ -153,8 +165,10 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return b.bind(addr).addListener(new FutureListener<Void>() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void operationComplete(Future<Void> future) throws Exception { |
|
|
|
|
|
|
|
|
|
if (future.isSuccess()) { |
|
|
|
|
log.info("SocketIO server started at port: {}", configCopy.getPort()); |
|
|
|
|
} else { |
|
|
|
@ -165,6 +179,7 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void applyConnectionOptions(ServerBootstrap bootstrap) { |
|
|
|
|
|
|
|
|
|
SocketConfig config = configCopy.getSocketConfig(); |
|
|
|
|
bootstrap.childOption(ChannelOption.TCP_NODELAY, config.isTcpNoDelay()); |
|
|
|
|
if (config.getTcpSendBufferSize() != -1) { |
|
|
|
@ -182,6 +197,7 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected void initGroups() { |
|
|
|
|
|
|
|
|
|
if (configCopy.isUseLinuxNativeEpoll()) { |
|
|
|
|
bossGroup = new EpollEventLoopGroup(configCopy.getBossThreads()); |
|
|
|
|
workerGroup = new EpollEventLoopGroup(configCopy.getWorkerThreads()); |
|
|
|
@ -195,6 +211,7 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
* Stop server |
|
|
|
|
*/ |
|
|
|
|
public void stop() { |
|
|
|
|
|
|
|
|
|
bossGroup.shutdownGracefully().syncUninterruptibly(); |
|
|
|
|
workerGroup.shutdownGracefully().syncUninterruptibly(); |
|
|
|
|
|
|
|
|
@ -202,15 +219,31 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
log.info("SocketIO server stopped"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Stop server |
|
|
|
|
* 不等待 |
|
|
|
|
*/ |
|
|
|
|
public void stopImmediately() { |
|
|
|
|
|
|
|
|
|
bossGroup.shutdownGracefully(0L, 1L, TimeUnit.SECONDS).syncUninterruptibly(); |
|
|
|
|
workerGroup.shutdownGracefully(0L, 1L, TimeUnit.SECONDS).syncUninterruptibly(); |
|
|
|
|
|
|
|
|
|
pipelineFactory.stop(); |
|
|
|
|
log.info("SocketIO server stopped"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public SocketIONamespace addNamespace(String name) { |
|
|
|
|
|
|
|
|
|
return namespacesHub.create(name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public SocketIONamespace getNamespace(String name) { |
|
|
|
|
|
|
|
|
|
return namespacesHub.get(name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void removeNamespace(String name) { |
|
|
|
|
|
|
|
|
|
namespacesHub.remove(name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -222,46 +255,55 @@ public class SocketIOServer implements ClientListeners {
|
|
|
|
|
* @return Configuration object |
|
|
|
|
*/ |
|
|
|
|
public Configuration getConfiguration() { |
|
|
|
|
|
|
|
|
|
return configuration; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void addMultiTypeEventListener(String eventName, MultiTypeEventListener listener, Class<?>... eventClass) { |
|
|
|
|
|
|
|
|
|
mainNamespace.addMultiTypeEventListener(eventName, listener, eventClass); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public <T> void addEventListener(String eventName, Class<T> eventClass, DataListener<T> listener) { |
|
|
|
|
|
|
|
|
|
mainNamespace.addEventListener(eventName, eventClass, listener); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void removeAllListeners(String eventName) { |
|
|
|
|
|
|
|
|
|
mainNamespace.removeAllListeners(eventName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void addDisconnectListener(DisconnectListener listener) { |
|
|
|
|
|
|
|
|
|
mainNamespace.addDisconnectListener(listener); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void addConnectListener(ConnectListener listener) { |
|
|
|
|
|
|
|
|
|
mainNamespace.addConnectListener(listener); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void addPingListener(PingListener listener) { |
|
|
|
|
|
|
|
|
|
mainNamespace.addPingListener(listener); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void addListeners(Object listeners) { |
|
|
|
|
|
|
|
|
|
mainNamespace.addListeners(listeners); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void addListeners(Object listeners, Class<?> listenersClass) { |
|
|
|
|
|
|
|
|
|
mainNamespace.addListeners(listeners, listenersClass); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|