package com.ld.igds.protocol.gps;
|
|
import com.ld.igds.protocol.utl.ServerUtils;
|
import com.ld.io.api.*;
|
import com.ld.io.netty.NettyServer;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.util.List;
|
|
/**
|
* GPS-808协议服务
|
*
|
*
|
*/
|
@Slf4j
|
public class Gps808ServerEngine {
|
|
public static final int port = 9006;
|
|
public static NettyServer ioServer = null;
|
// 配置消息接收类
|
private static MessageConsume messageConsume = new MessageConsume();
|
// 监听会话的创建与销毁
|
private static SessionListener ioSessionListener = new SessionListener();
|
|
private static HeartbeatProvider heartbeatProvider = new HeartbeatProviderImpl();
|
|
public static void start() throws InterruptedException {
|
// 配置Server的配置
|
IoServerOption ioServerOption = new IoServerOption(port);
|
// 拆包器配置
|
ioServerOption.setSplitDecoderType(SplitByteDecoderType.NO_LIMIT);
|
//ioServerOption.setDelimiter(BytesUtil.hexStrToBytes("7E"));
|
|
// 配置心跳执行60分钟执行一次
|
ioServerOption.setReaderIdleTime(10 * 60);
|
|
ioServer = new NettyServer(ioServerOption, messageConsume,
|
ioSessionListener, heartbeatProvider);
|
ioServer.startup();
|
|
log.info("* ========================");
|
log.info("* ");
|
log.info("* GPS-808协议服务启动成功,端口号={}", Gps808ServerEngine.port);
|
log.info("* ");
|
log.info("* ========================");
|
}
|
|
/**
|
* 发送信息
|
*
|
* @param ip
|
* @param port
|
* @param msg
|
* @return
|
*/
|
public static InvokeResult push(String ip, int port, byte[] msg) {
|
IoSessionQuery sessionFactory = ioServer.getSessionQuery();
|
List<IoSession> sessions = sessionFactory.getAllSession();
|
if (null == sessions) {
|
return InvokeResult.CHANNEL_CLOSED;
|
}
|
IoSession session = null;
|
for (IoSession ioSession : sessions) {
|
if (ServerUtils.getServerKey(ip, port).equals(
|
ioSession.getBusinessKey())) {
|
session = ioSession;
|
break;
|
}
|
}
|
if (null == session) {
|
return InvokeResult.CHANNEL_CLOSED;
|
}
|
return session.invoke(msg);
|
}
|
|
}
|