Search in sources :

Example 1 with ChannelSession

use of com.akaxin.common.channel.ChannelSession in project openzaly by akaxincom.

the class ImSiteAuthHandler method hello.

public CommandResponse hello(Command command) {
    ChannelSession channelSession = command.getChannelSession();
    try {
        if (channelSession != null) {
            ImSiteHelloProto.ImSiteHelloRequest request = ImSiteHelloProto.ImSiteHelloRequest.parseFrom(command.getParams());
            String clientVersion = request.getClientVersion();
            logger.debug("{} client={} siteUserId={} action={} clientVersion={}", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), RequestAction.IM_SITE_HELLO, clientVersion);
            return helloResponse(channelSession.getChannel());
        }
    } catch (Exception e) {
        logger.error(StringHelper.format("{} client={} siteUserId={} action={} error.", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), RequestAction.IM_SITE_HELLO), e);
    }
    return defaultErrorResponse();
}
Also used : ChannelSession(com.akaxin.common.channel.ChannelSession) ImSiteHelloProto(com.akaxin.proto.site.ImSiteHelloProto)

Example 2 with ChannelSession

use of com.akaxin.common.channel.ChannelSession in project openzaly by akaxincom.

the class NettyServerHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, RedisCommand redisCmd) throws Exception {
    InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
    String clientIp = socketAddress.getAddress().getHostAddress();
    ChannelSession channelSession = ctx.channel().attr(ParserConst.CHANNELSESSION).get();
    // disconnect tcp connection as channel is unavailable
    if (channelSession.getChannel() == null || !channelSession.getChannel().isActive()) {
        // 断开连接事件(与对方的连接断开)
        ctx.disconnect();
        logger.warn("{} close client={} as its channel is not active ", AkxProject.PLN, clientIp);
    }
    String version = redisCmd.getParameterByIndex(0);
    String action = redisCmd.getParameterByIndex(1);
    byte[] params = redisCmd.getBytesParamByIndex(2);
    CoreProto.TransportPackageData packageData = CoreProto.TransportPackageData.parseFrom(params);
    Command command = new Command();
    command.setClientIp(clientIp);
    command.setSiteUserId(channelSession.getUserId());
    command.setDeviceId(channelSession.getDeviceId());
    command.setAction(action);
    command.setHeader(packageData.getHeaderMap());
    command.setParams(packageData.getData().toByteArray());
    command.setChannelSession(channelSession);
    command.setStartTime(System.currentTimeMillis());
    if (!RequestAction.IM_CTS_PING.getName().equalsIgnoreCase(command.getAction())) {
        logger.debug("{} client={} -> site version={} action={} params-length={}", AkxProject.PLN, clientIp, version, action, params.length);
    } else {
        logger.trace("{} client={} ping -> site", AkxProject.PLN, clientIp);
    }
    if (RequestAction.IM.getName().equals(command.getRety())) {
        // 如果是syncFinish,则这里需要修改channel中的syncFinTime
        channelSession.setActionForPsn(action);
        // 单独处理im.site.hello && im.site.auth
        if (RequestAction.SITE.getName().equalsIgnoreCase(command.getService())) {
            String anoRequest = command.getRety() + "." + command.getService();
            command.setRety(anoRequest);
        }
        CommandResponse response = this.executor.execute(command.getRety(), command);
        if (!RequestAction.IM_CTS_PING.getName().equals(command.getAction())) {
            // 输出IM请求结果
            LogUtils.requestResultLog(logger, command, response);
        }
    } else if (RequestAction.API.getName().equalsIgnoreCase(command.getRety())) {
        CommandResponse response = this.executor.execute(command.getRety(), command);
        // 输出API请求结果
        LogUtils.requestResultLog(logger, command, response);
    } else {
        /**
         * <pre>
         *
         * pipeline.addLast("A", new AHandler());
         * pipeline.addLast("B", new BHandler());
         * pipeline.addLast("C", new CHandler());
         *
         * ctx.close() && channel().close();
         * 		ctx.close(): close the channel in current handler,will start from the tail of the ChannelPipeline
         * 				do A.close() ,B.close(),C.close();
         * 		channel().close():close channel in all handler from pointer
         * 				do C.close() , B.close(), A.close();
         * </pre>
         */
        // 关闭channel事件(关闭自己的连接)
        ctx.channel().close();
        logger.error("{} client={} siteUserId={} action={} unknow request method", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), command.getAction());
        return;
    }
}
Also used : CoreProto(com.akaxin.proto.core.CoreProto) ChannelSession(com.akaxin.common.channel.ChannelSession) Command(com.akaxin.common.command.Command) RedisCommand(com.akaxin.common.command.RedisCommand) InetSocketAddress(java.net.InetSocketAddress) CommandResponse(com.akaxin.common.command.CommandResponse)

Example 3 with ChannelSession

use of com.akaxin.common.channel.ChannelSession in project openzaly by akaxincom.

the class U2MessageImageSecretHandler method handle.

public Boolean handle(Command command) {
    ChannelSession channelSession = command.getChannelSession();
    try {
        ImCtsMessageProto.ImCtsMessageRequest request = ImCtsMessageProto.ImCtsMessageRequest.parseFrom(command.getParams());
        int type = request.getType().getNumber();
        if (CoreProto.MsgType.SECRET_IMAGE_VALUE == type) {
            String siteUserId = request.getSecretImage().getSiteUserId();
            String siteFriendId = request.getSecretImage().getSiteFriendId();
            String msgId = request.getSecretImage().getMsgId();
            String tsKey = request.getSecretImage().getTsKey();
            String ts_device_id = request.getSecretImage().getSiteDeviceId();
            String imageId = request.getSecretImage().getImageId();
            command.setSiteUserId(siteUserId);
            command.setSiteFriendId(siteFriendId);
            U2MessageBean u2Bean = new U2MessageBean();
            u2Bean.setMsgId(msgId);
            u2Bean.setMsgType(type);
            u2Bean.setSendUserId(siteUserId);
            u2Bean.setSiteUserId(siteFriendId);
            u2Bean.setContent(imageId);
            u2Bean.setTsKey(tsKey);
            u2Bean.setDeviceId(ts_device_id);
            long msgTime = System.currentTimeMillis();
            u2Bean.setMsgTime(System.currentTimeMillis());
            LogUtils.requestDebugLog(logger, command, u2Bean.toString());
            boolean success = messageDao.saveU2Message(u2Bean);
            msgStatusResponse(command, msgId, msgTime, success);
            return success;
        }
        return true;
    } catch (Exception e) {
        LogUtils.requestErrorLog(logger, command, this.getClass(), e);
    }
    return false;
}
Also used : U2MessageBean(com.akaxin.site.storage.bean.U2MessageBean) ChannelSession(com.akaxin.common.channel.ChannelSession) ImCtsMessageProto(com.akaxin.proto.site.ImCtsMessageProto)

Example 4 with ChannelSession

use of com.akaxin.common.channel.ChannelSession in project openzaly by akaxincom.

the class U2MessageTextSecretHandler method handle.

public Boolean handle(Command command) {
    ChannelSession channelSession = command.getChannelSession();
    try {
        ImCtsMessageProto.ImCtsMessageRequest request = ImCtsMessageProto.ImCtsMessageRequest.parseFrom(command.getParams());
        int type = request.getType().getNumber();
        if (CoreProto.MsgType.SECRET_TEXT_VALUE == type) {
            String siteUserId = command.getSiteUserId();
            String siteFriendId = command.getSiteFriendId();
            String msgId = request.getSecretText().getMsgId();
            String tsKey = request.getSecretText().getTsKey();
            String tsDeviceId = request.getSecretText().getSiteDeviceId();
            ByteString byteStr = request.getSecretText().getText();
            String msgText = Base64.getEncoder().encodeToString(byteStr.toByteArray());
            long msgTime = System.currentTimeMillis();
            U2MessageBean u2Bean = new U2MessageBean();
            u2Bean.setMsgId(msgId);
            u2Bean.setMsgType(type);
            u2Bean.setSendUserId(siteUserId);
            u2Bean.setSiteUserId(siteFriendId);
            u2Bean.setContent(msgText);
            u2Bean.setTsKey(tsKey);
            u2Bean.setDeviceId(tsDeviceId);
            u2Bean.setMsgTime(msgTime);
            LogUtils.requestDebugLog(logger, command, u2Bean.toString());
            boolean success = messageDao.saveU2Message(u2Bean);
            msgStatusResponse(command, msgId, msgTime, success);
            return success;
        }
        return true;
    } catch (Exception e) {
        LogUtils.requestErrorLog(logger, command, this.getClass(), e);
    }
    return false;
}
Also used : U2MessageBean(com.akaxin.site.storage.bean.U2MessageBean) ChannelSession(com.akaxin.common.channel.ChannelSession) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ImCtsMessageProto(com.akaxin.proto.site.ImCtsMessageProto)

Example 5 with ChannelSession

use of com.akaxin.common.channel.ChannelSession in project openzaly by akaxincom.

the class SyncGroupMessageHandler method handle.

public Boolean handle(Command command) {
    ChannelSession channelSession = command.getChannelSession();
    try {
        ImSyncMessageProto.ImSyncMessageRequest syncRequest = ImSyncMessageProto.ImSyncMessageRequest.parseFrom(command.getParams());
        String siteUserId = command.getSiteUserId();
        String deviceId = command.getDeviceId();
        Map<String, Long> groupPointerMap = syncRequest.getGroupsPointerMap();
        LogUtils.requestDebugLog(logger, command, syncRequest.toString());
        int syncTotalCount = 0;
        // 查找个人有多少群
        List<String> userGroups = userGroupDao.getUserGroupsId(siteUserId);
        if (userGroups != null) {
            for (String groupId : userGroups) {
                long clientGroupPointer = 0;
                if (groupPointerMap != null && groupPointerMap.get(groupId) != null) {
                    clientGroupPointer = groupPointerMap.get(groupId);
                }
                // 校验群消息游标
                long maxGroupMessagePointer = syncDao.queryGroupMessagePointer(groupId, siteUserId, deviceId);
                long startPointer = NumUtils.getMax(maxGroupMessagePointer, clientGroupPointer);
                while (true) {
                    List<GroupMessageBean> msgList = syncDao.queryGroupMessage(groupId, siteUserId, deviceId, startPointer, SYNC_MAX_MESSAGE_COUNT);
                    if (msgList != null && msgList.size() > 0) {
                        startPointer = groupMessageToClient(channelSession.getChannel(), siteUserId, msgList);
                        syncTotalCount += msgList.size();
                    }
                    if (msgList == null || msgList.size() < SYNC_MAX_MESSAGE_COUNT) {
                        break;
                    }
                }
            }
        }
        logger.debug("client={} siteUserId={} deviceId={} sync group-msg count={}", command.getClientIp(), siteUserId, deviceId, syncTotalCount);
        msgFinishToClient(channelSession.getChannel(), siteUserId, deviceId);
    } catch (Exception e) {
        logger.error("sync group message error.command=" + command, e);
    }
    return true;
}
Also used : ChannelSession(com.akaxin.common.channel.ChannelSession) ImSyncMessageProto(com.akaxin.proto.site.ImSyncMessageProto) ByteString(com.google.protobuf.ByteString) GroupMessageBean(com.akaxin.site.storage.bean.GroupMessageBean)

Aggregations

ChannelSession (com.akaxin.common.channel.ChannelSession)16 ImCtsMessageProto (com.akaxin.proto.site.ImCtsMessageProto)8 GroupMessageBean (com.akaxin.site.storage.bean.GroupMessageBean)6 InetSocketAddress (java.net.InetSocketAddress)4 U2MessageBean (com.akaxin.site.storage.bean.U2MessageBean)3 ByteString (com.google.protobuf.ByteString)3 RedisCommand (com.akaxin.common.command.RedisCommand)2 ImSyncMessageProto (com.akaxin.proto.site.ImSyncMessageProto)2 Command (com.akaxin.common.command.Command)1 CommandResponse (com.akaxin.common.command.CommandResponse)1 CoreProto (com.akaxin.proto.core.CoreProto)1 ImSiteHelloProto (com.akaxin.proto.site.ImSiteHelloProto)1 ImMessageService (com.akaxin.site.message.service.ImMessageService)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1