/** * @param socketChannel * @param callbackIndex * @param obj * @param block */ public static boolean writeByteObject( SocketChannel socketChannel, int callbackIndex, Object obj, boolean block) { if (socketChannel == null) { return false; } if (obj != null) { try { byte[] bytes = obj.getClass() == String.class ? ((String) obj).getBytes(ContextUtils.getCharset()) : ServerResolverBody.ME.getObjectMapper().writeValueAsBytes(obj); if (block) { return writeByteBufferBlock(socketChannel, callbackIndex, bytes); } else { writeByteBuffer(socketChannel, callbackIndex, bytes); } } catch (Throwable e) { if (BeanFactoryUtils.getEnvironment().compareTo(Environment.DEBUG) <= 0) { e.printStackTrace(); } LOGGER.error("", e); return false; } } return true; }
/** @author absir */ @MaEntity( parent = {@MaMenu("支付管理")}, name = "支付") @Entity public class JPayHistory extends JbBase { @JaLang(value = "订单号", tag = "tradeId") @Id private String id; /** id */ @JaLang("交易号") @Column(length = 1024, unique = true) private String tradeNo; /** createTime */ @JaLang("创建时间") @JaEdit(types = "dateTime", groups = JaEdit.GROUP_LIST) private long createTime = ContextUtils.getContextTime(); /** @return the id */ public String getId() { return id; } /** @param id the id to set */ public void setId(String id) { this.id = id; } /** @return the tradeNo */ public String getTradeNo() { return tradeNo; } /** @param tradeNo the tradeNo to set */ public void setTradeNo(String tradeNo) { this.tradeNo = tradeNo; } /** @return the createTime */ public long getCreateTime() { return createTime; } /** @param createTime the createTime to set */ public void setCreateTime(long createTime) { this.createTime = createTime; } }
/** * @param socketChannel * @param callbackIndex * @param bytes */ public static void writeByteBuffer( final SocketChannel socketChannel, final int callbackIndex, final byte[] bytes) { if (bytes != null) { ContextUtils.getThreadPoolExecutor() .execute( new Runnable() { @Override public void run() { writeByteBufferBlock(socketChannel, callbackIndex, bytes); } }); } }
/* * (non-Javadoc) * * @see * com.absir.server.socket.resolver.SocketAuthorResolver#unRegister(java * .io.Serializable, java.nio.channels.SocketChannel, * com.absir.server.socket.JbServerContext) */ @Override public void unRegister(Serializable id, SocketChannel socketChannel, ServerContext serverContext) throws Throwable { if (id instanceof Long) { JbPlayerContext playerContext = ContextUtils.getContext(JbPlayerContext.COMPONENT.PLAYER_CONTEXT_CLASS, (Long) id); IFight fight = playerContext.getFight(); if (fight != null) { fight.disconnect(); } synchronized (playerContext) { if (playerContext.getSocketChannel() == socketChannel) { playerContext.setSocketChannel(null); } } } }
/* * (non-Javadoc) * * @see * com.absir.server.socket.resolver.SocketSessionResolver#register(java. * nio.channels.SocketChannel, com.absir.server.socket.ServerContext, * byte[], com.absir.server.socket.SocketBuffer) */ @Override public ServerContext register( SocketChannel socketChannel, ServerContext serverContext, byte[] buffer, SocketBuffer socketBuffer) throws Throwable { JiUserBase userBase = IdentityServiceLocal.getUserBase(new String(buffer)); Long id = PlayerServiceBase.ME.getPlayerId(serverContext.getServer().getId(), userBase); if (id != null) { JbPlayerContext playerContext = ContextUtils.getContext(JbPlayerContext.COMPONENT.PLAYER_CONTEXT_CLASS, id); JbPlayer player = playerContext.getPlayer(); if (player.getCard() == 0) { return null; } long serverId = player.getServerId(); if (!serverContext.getServer().getId().equals(serverId)) { if (serverContext.getServer().isMultiPort()) { serverContext = SocketServerContext.ME.getServerContext(serverId); if (serverContext == null) { return null; } } else { return null; } } synchronized (playerContext) { if (playerContext.getSocketChannel() != null) { SocketServer.close(playerContext.getSocketChannel()); } playerContext.setSocketChannel(socketChannel); } } socketBuffer.setId(id); return serverContext; }
/* * (non-Javadoc) * * @see * com.absir.server.socket.resolver.SocketAuthorResolver#doBeat(java.io. * Serializable, java.nio.channels.SocketChannel, * com.absir.server.socket.ServerContext) */ @Override public boolean doBeat(Serializable id, SocketChannel socketChannel, ServerContext serverContext) { ContextUtils.getContext(JbPlayerContext.COMPONENT.PLAYER_CONTEXT_CLASS, (Long) id); return true; }