SncpHeader优化

This commit is contained in:
redkale
2023-07-03 09:06:01 +08:00
parent 6bb91552f7
commit 319b9e04dd
4 changed files with 16 additions and 7 deletions

View File

@@ -36,7 +36,7 @@ import org.redkale.util.*;
*/ */
public abstract class Sncp { public abstract class Sncp {
private static final byte[] PING_BYTES = new SncpHeader(null, Uint128.ZERO, Uint128.ZERO) private static final byte[] PING_BYTES = new SncpHeader(null, Uint128.ZERO, "", Uint128.ZERO, "")
.writeTo(new ByteArray(SncpHeader.HEADER_SIZE).putPlaceholder(SncpHeader.HEADER_SIZE), null, 0, 0, 0) .writeTo(new ByteArray(SncpHeader.HEADER_SIZE).putPlaceholder(SncpHeader.HEADER_SIZE), null, 0, 0, 0)
.getBytes(); .getBytes();

View File

@@ -22,11 +22,15 @@ public class SncpHeader {
private Uint128 serviceid; private Uint128 serviceid;
private String serviceName;
//【预留字段】service接口版本 //【预留字段】service接口版本
private int serviceVersion; private int serviceVersion;
private Uint128 actionid; private Uint128 actionid;
private String methodName;
//SncpRequest的值是clientSncpAddressSncpResponse的值是serverSncpAddress //SncpRequest的值是clientSncpAddressSncpResponse的值是serverSncpAddress
private byte[] addrBytes; private byte[] addrBytes;
@@ -50,11 +54,13 @@ public class SncpHeader {
public SncpHeader() { public SncpHeader() {
} }
public SncpHeader(InetSocketAddress clientSncpAddress, Uint128 serviceid, Uint128 actionid) { public SncpHeader(InetSocketAddress clientSncpAddress, Uint128 serviceid, String serviceName, Uint128 actionid, String methodName) {
this.addrBytes = clientSncpAddress == null ? new byte[4] : clientSncpAddress.getAddress().getAddress(); this.addrBytes = clientSncpAddress == null ? new byte[4] : clientSncpAddress.getAddress().getAddress();
this.addrPort = clientSncpAddress == null ? 0 : clientSncpAddress.getPort(); this.addrPort = clientSncpAddress == null ? 0 : clientSncpAddress.getPort();
this.serviceid = serviceid; this.serviceid = serviceid;
this.serviceName = serviceName;
this.actionid = actionid; this.actionid = actionid;
this.methodName = methodName;
if (addrBytes.length != 4) { if (addrBytes.length != 4) {
throw new SncpException("address bytes length must be 4, but " + addrBytes.length); throw new SncpException("address bytes length must be 4, but " + addrBytes.length);
} }
@@ -149,9 +155,12 @@ public class SncpHeader {
@Override @Override
public String toString() { public String toString() {
return getClass().getSimpleName() return getClass().getSimpleName()
+ (this.seqid == null ? ("{serviceid=" + this.serviceid) : ("{seqid=" + this.seqid + ",serviceid=" + this.serviceid)) + (this.seqid == null
? ("{serviceid=" + this.serviceid + ",serviceName=" + this.serviceName)
: ("{seqid=" + this.seqid + ",serviceid=" + this.serviceid + ",serviceName=" + this.serviceName))
+ ",serviceVersion=" + this.serviceVersion + ",serviceVersion=" + this.serviceVersion
+ ",actionid=" + this.actionid + ",actionid=" + this.actionid
+ ",methodName=" + this.methodName
+ ",address=" + getAddress() + ",address=" + getAddress()
+ ",timestamp=" + this.timestamp + ",timestamp=" + this.timestamp
+ ",retcode=" + this.retcode + ",retcode=" + this.retcode

View File

@@ -88,7 +88,7 @@ public class SncpRemoteInfo<T extends Service> {
this.topic = messageAgent == null ? null : messageAgent.generateSncpReqTopic(resourceName, resourceType); this.topic = messageAgent == null ? null : messageAgent.generateSncpReqTopic(resourceName, resourceType);
for (Map.Entry<Uint128, Method> en : loadMethodActions(Sncp.getServiceType(serviceImplClass)).entrySet()) { for (Map.Entry<Uint128, Method> en : loadMethodActions(Sncp.getServiceType(serviceImplClass)).entrySet()) {
this.actions.put(en.getKey().toString(), new SncpRemoteAction(serviceImplClass, en.getValue(), serviceid, en.getKey())); this.actions.put(en.getKey().toString(), new SncpRemoteAction(serviceImplClass, resourceType, en.getValue(), serviceid, en.getKey()));
} }
} }
@@ -331,7 +331,7 @@ public class SncpRemoteInfo<T extends Service> {
protected final SncpHeader header; protected final SncpHeader header;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
SncpRemoteAction(final Class serviceImplClass, Method method, Uint128 serviceid, Uint128 actionid) { SncpRemoteAction(final Class serviceImplClass, Class resourceType, Method method, Uint128 serviceid, Uint128 actionid) {
this.actionid = actionid == null ? Sncp.actionid(method) : actionid; this.actionid = actionid == null ? Sncp.actionid(method) : actionid;
Type rt = TypeToken.getGenericType(method.getGenericReturnType(), serviceImplClass); Type rt = TypeToken.getGenericType(method.getGenericReturnType(), serviceImplClass);
this.returnObjectType = rt == void.class || rt == Void.class ? null : rt; this.returnObjectType = rt == void.class || rt == Void.class ? null : rt;
@@ -424,7 +424,7 @@ public class SncpRemoteInfo<T extends Service> {
this.paramHandlerClass = handlerFuncClass; this.paramHandlerClass = handlerFuncClass;
this.paramHandlerResultType = handlerResultType; this.paramHandlerResultType = handlerResultType;
this.paramHandlerAttachIndex = handlerAttachIndex; this.paramHandlerAttachIndex = handlerAttachIndex;
this.header = new SncpHeader(null, serviceid, actionid); this.header = new SncpHeader(null, serviceid, resourceType.getName(), actionid, method.getName());
if (this.paramHandlerIndex >= 0 && method.getReturnType() != void.class) { if (this.paramHandlerIndex >= 0 && method.getReturnType() != void.class) {
throw new SncpException(method + " have CompletionHandler type parameter but return type is not void"); throw new SncpException(method + " have CompletionHandler type parameter but return type is not void");
} }

View File

@@ -46,7 +46,7 @@ public class SncpClientCodecTest {
//---------------------------------------------- //----------------------------------------------
ByteBuffer realBuf; ByteBuffer realBuf;
{ {
SncpHeader header = new SncpHeader(sncpAddress, Uint128.ZERO, Uint128.ZERO); SncpHeader header = new SncpHeader(sncpAddress, Uint128.ZERO, "", Uint128.ZERO, "");
SncpClientRequest request = new SncpClientRequest(); SncpClientRequest request = new SncpClientRequest();
ByteArray writeArray = new ByteArray(); ByteArray writeArray = new ByteArray();
request.prepare(header, 1, "", new byte[20]); request.prepare(header, 1, "", new byte[20]);