package com.fzzy.protocol.zldz.server;
|
|
import com.fzzy.api.utils.BytesUtil;
|
import com.fzzy.protocol.fzzy.server.ServerUtils;
|
import com.ld.io.api.*;
|
import com.ld.io.netty.NettyServer;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.util.List;
|
|
/**
|
* 配置主服务信息
|
*
|
*/
|
@Slf4j
|
public class ZldzServerEngine {
|
|
public static final int PORT = 19302;
|
|
public static NettyServer ioServer = null;
|
// 配置消息接收类
|
private static MsgConsumer messageConsume = new MsgConsumer();
|
// 监听会话的创建与销毁
|
private static SessionListener ioSessionListener = new SessionListener();
|
|
// 心跳提供 ,该服务不需要心跳维持
|
private static HeartbeatProvider heartbeatProvider = new HeartbeatProviderImpl();
|
|
|
public static void start() {
|
start(PORT);
|
}
|
|
public static void start(int port) {
|
// 配置Server的配置
|
IoServerOption ioServerOption = new IoServerOption(port);
|
|
ioServerOption.setSplitDecoderType(SplitByteDecoderType.DELIMITER_SYMBOL);
|
ioServerOption.setDelimiter(BytesUtil.hexStrToBytes(ZldzServerUtil.MSG_END));
|
|
// 配置心跳执行时间
|
ioServerOption.setReaderIdleTime(60 * 60);
|
|
ioServer = new NettyServer(ioServerOption, messageConsume, ioSessionListener, heartbeatProvider);
|
|
ioServer.startup();
|
|
log.info("* ========================");
|
log.info("* ");
|
log.info("* FZZY-ZLDZ纯粮情协议,端口号={}", port);
|
log.info("* ");
|
log.info("* ========================");
|
|
|
System.out.println("* ========================");
|
System.out.println("* ");
|
System.out.println("* FZZY-ZLDZ纯粮情协议,端口号=" + port);
|
System.out.println("* ");
|
System.out.println("* ========================");
|
}
|
|
/**
|
* 发送信息
|
*
|
* @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();
|
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);
|
}
|
}
|