package com.fzzy.protocol.bhzn.server;
|
|
import com.fzzy.protocol.fzzy.server.ServerUtils;
|
import com.ld.io.api.*;
|
import com.ld.io.netty.NettyServer;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import java.util.List;
|
|
/**
|
* @Desc: 邦海智能纯粮情协议
|
* @author: Andy
|
* @update-time: 2023/5/11
|
*/
|
@Slf4j
|
@Service
|
public class BhznGrainV2ServerEngine {
|
|
public static final Integer PORT = 19301;
|
|
public static NettyServer ioServer = null;
|
|
|
// 配置消息接收类
|
private static BhznGrainV2MessageConsumer messageConsume = new BhznGrainV2MessageConsumer();
|
// 监听会话的创建与销毁
|
private static BhznGrainV2SessionListener ioSessionListener = new BhznGrainV2SessionListener();
|
// 心跳提供
|
private static HeartbeatProvider heartbeatProvider = new BhznGrainV2HeartbeatImpl();
|
|
|
public static void start(Integer port) {
|
IoServerOption ioServerOption = new IoServerOption();
|
|
if (null != port) {
|
ioServerOption.setPort(port);
|
} else {
|
ioServerOption.setPort(PORT);
|
}
|
|
//确认结尾标志
|
ioServerOption.setDelimiter(BhznGrainV2ServerUtils.MSG_END.getBytes());
|
|
// 配置系统心跳间隔
|
ioServerOption.setReaderIdleTime(5 * 60);
|
|
NettyServer ioServer = new NettyServer(ioServerOption, messageConsume, ioSessionListener, heartbeatProvider);
|
ioServer.startup();
|
|
|
log.info("* ");
|
log.info("* ========================");
|
log.info("* ");
|
log.info("* [GRAIN-SERVER:BHZN_GRAIN,PORT={}]" + port);
|
log.info("* ");
|
log.info("* ========================");
|
log.info("* ");
|
|
System.out.println("* ========================");
|
System.out.println("* ");
|
System.out.println("* [GRAIN-SERVER:BHZN_GRAIN,PORT={}]" + 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);
|
}
|
|
}
|