Skip to content

Commit

Permalink
refactor cmd type
Browse files Browse the repository at this point in the history
  • Loading branch information
crossoverJie committed Oct 19, 2024
1 parent 48adb95 commit 537a765
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import com.crossoverjie.cim.client.sdk.ReConnectManager;
import com.crossoverjie.cim.client.sdk.RouteManager;
import com.crossoverjie.cim.client.sdk.io.CIMClientHandleInitializer;
import com.crossoverjie.cim.common.constant.Constants;
import com.crossoverjie.cim.common.exception.CIMException;
import com.crossoverjie.cim.common.kit.HeartBeatHandler;
import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import com.crossoverjie.cim.common.protocol.BaseCommand;
import com.crossoverjie.cim.common.protocol.Request;
import com.crossoverjie.cim.route.api.vo.req.ChatReqVO;
import com.crossoverjie.cim.route.api.vo.req.LoginReqVO;
Expand Down Expand Up @@ -82,7 +82,7 @@ public ClientImpl(ClientConfigurationData conf) {
heartBeatPacket = Request.newBuilder()
.setRequestId(this.conf.getAuth().getUserId())
.setReqMsg("ping")
.setType(Constants.CommandType.PING)
.setCmd(BaseCommand.PING)
.build();
client = this;

Expand Down Expand Up @@ -177,7 +177,7 @@ private void loginServer() {
Request login = Request.newBuilder()
.setRequestId(this.conf.getAuth().getUserId())
.setReqMsg(this.conf.getAuth().getUserName())
.setType(Constants.CommandType.LOGIN)
.setCmd(BaseCommand.LOGIN_REQUEST)
.build();
channel.writeAndFlush(login)
.addListener((ChannelFutureListener) channelFuture ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.crossoverjie.cim.client.sdk.ClientState;
import com.crossoverjie.cim.client.sdk.impl.ClientImpl;
import com.crossoverjie.cim.common.constant.Constants;
import com.crossoverjie.cim.common.protocol.BaseCommand;
import com.crossoverjie.cim.common.protocol.Response;
import com.crossoverjie.cim.common.util.NettyAttrUtil;
import io.netty.channel.ChannelFutureListener;
Expand Down Expand Up @@ -60,12 +60,12 @@ public void channelInactive(ChannelHandlerContext ctx) {
protected void channelRead0(ChannelHandlerContext ctx, Response msg) {


if (msg.getType() == Constants.CommandType.PING) {
if (msg.getCmd() == com.crossoverjie.cim.common.protocol.BaseCommand.PING) {
ClientImpl.getClient().getConf().getEvent().debug("received ping from server");
NettyAttrUtil.updateReaderTime(ctx.channel(), System.currentTimeMillis());
}

if (msg.getType() != Constants.CommandType.PING) {
if (msg.getCmd() != BaseCommand.PING) {
// callback
ClientImpl.getClient().getConf().getCallbackThreadPool().execute(() -> {
ClientImpl.getClient().getConf().getMessageListener().received(ClientImpl.getClient(), msg.getResMsg());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,4 @@ public class Constants {
public static final String COUNTER_CLIENT_PUSH_COUNT = "counter.client.push.count" ;


/**
* 自定义报文类型
*/
public static class CommandType{
/**
* 登录
*/
public static final int LOGIN = 1 ;
/**
* 业务消息
*/
public static final int MSG = 2 ;

/**
* ping
*/
public static final int PING = 3 ;
}


}
10 changes: 8 additions & 2 deletions cim-common/src/main/proto/cim.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ message Request{
// todo source user info
int64 requestId = 2;
string reqMsg = 1;
int32 type = 3;
BaseCommand cmd = 3;
}

message Response{
int64 responseId = 2;
string resMsg = 1;
int32 type = 3;
BaseCommand cmd = 3;
}

enum BaseCommand{
LOGIN_REQUEST = 0;
MESSAGE = 1;
PING = 2;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.crossoverjie.cim.common.util;

import com.crossoverjie.cim.common.protocol.BaseCommand;
import com.crossoverjie.cim.common.protocol.Request;
import com.google.protobuf.InvalidProtocolBufferException;
import org.junit.Test;
Expand All @@ -11,7 +12,7 @@ public void testProtocol() throws InvalidProtocolBufferException {
Request protocol = Request.newBuilder()
.setRequestId(123L)
.setReqMsg("你好啊")
.setType(1)
.setCmd(BaseCommand.LOGIN_REQUEST)
.build();

byte[] encode = encode(protocol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService {
@Override
public Optional<CIMUserInfo> loadUserInfoByUserId(Long userId) {
//Retrieve user information using a second-level cache.
Optional<CIMUserInfo> cimUserInfo = userInfoMap.getUnchecked(userId);
return cimUserInfo;
return userInfoMap.getUnchecked(userId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.crossoverjie.cim.server.config;

import com.crossoverjie.cim.common.constant.Constants;
import com.crossoverjie.cim.common.core.proxy.RpcProxyManager;
import com.crossoverjie.cim.common.metastore.MetaStore;
import com.crossoverjie.cim.common.metastore.ZkMetaStoreImpl;
import com.crossoverjie.cim.common.protocol.BaseCommand;
import com.crossoverjie.cim.common.protocol.Request;
import com.crossoverjie.cim.route.api.RouteApi;
import jakarta.annotation.Resource;
Expand Down Expand Up @@ -54,7 +54,7 @@ public Request heartBeat() {
return Request.newBuilder()
.setRequestId(0L)
.setReqMsg("pong")
.setType(Constants.CommandType.PING)
.setCmd(BaseCommand.PING)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.crossoverjie.cim.server.handle;

import com.crossoverjie.cim.common.constant.Constants;
import com.crossoverjie.cim.common.exception.CIMException;
import com.crossoverjie.cim.common.kit.HeartBeatHandler;
import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import com.crossoverjie.cim.common.protocol.BaseCommand;
import com.crossoverjie.cim.common.protocol.Request;
import com.crossoverjie.cim.common.util.NettyAttrUtil;
import com.crossoverjie.cim.server.kit.RouteHandler;
Expand Down Expand Up @@ -74,15 +74,15 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
protected void channelRead0(ChannelHandlerContext ctx, Request msg) throws Exception {
log.info("received msg=[{}]", msg.toString());

if (msg.getType() == Constants.CommandType.LOGIN) {
if (msg.getCmd() == BaseCommand.LOGIN_REQUEST) {
//保存客户端与 Channel 之间的关系
SessionSocketHolder.put(msg.getRequestId(), (NioSocketChannel) ctx.channel());
SessionSocketHolder.saveSession(msg.getRequestId(), msg.getReqMsg());
log.info("client [{}] online success!!", msg.getReqMsg());
}

//心跳更新时间
if (msg.getType() == Constants.CommandType.PING){
if (msg.getCmd() == BaseCommand.PING){
NettyAttrUtil.updateReaderTime(ctx.channel(),System.currentTimeMillis());
//向客户端响应 pong 消息
Request heartBeat = SpringBeanFactory.getBean("heartBeat", Request.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.crossoverjie.cim.server.server;

import com.crossoverjie.cim.common.constant.Constants;
import com.crossoverjie.cim.common.protocol.BaseCommand;
import com.crossoverjie.cim.common.protocol.Request;
import com.crossoverjie.cim.server.api.vo.req.SendMsgReqVO;
import com.crossoverjie.cim.server.init.CIMServerInitializer;
Expand Down Expand Up @@ -89,7 +89,7 @@ public void sendMsg(SendMsgReqVO sendMsgReqVO){
Request protocol = Request.newBuilder()
.setRequestId(sendMsgReqVO.getUserId())
.setReqMsg(sendMsgReqVO.getMsg())
.setType(Constants.CommandType.MSG)
.setCmd(BaseCommand.MESSAGE)
.build();

ChannelFuture future = socketChannel.writeAndFlush(protocol);
Expand Down

0 comments on commit 537a765

Please sign in to comment.