ref[attachment]: refactor attachment of net

This commit is contained in:
godotg
2023-04-14 23:08:18 +08:00
parent 4f51f8b103
commit f763246231
9 changed files with 130 additions and 36 deletions
@@ -26,26 +26,28 @@ public enum AttachmentType {
*/
SIGNAL_PACKET((byte) 0, SignalAttachment.class),
SIGNAL_ONLY_PACKET((byte) 1, SignalOnlyAttachment.class),
/**
* gateway attachment
*/
GATEWAY_PACKET((byte) 1, GatewayAttachment.class),
GATEWAY_PACKET((byte) 2, GatewayAttachment.class),
/**
* udp attachment
*/
UDP_PACKET((byte) 2, UdpAttachment.class),
UDP_PACKET((byte) 3, UdpAttachment.class),
/**
* http attachment
*/
HTTP_PACKET((byte) 3, HttpAttachment.class),
HTTP_PACKET((byte) 4, HttpAttachment.class),
/**
* not used attachment
*/
NO_ANSWER_PACKET((byte) 4, NoAnswerAttachment.class),
NO_ANSWER_PACKET((byte) 5, NoAnswerAttachment.class),
;
@@ -21,7 +21,7 @@ import org.springframework.lang.Nullable;
*/
public class GatewayAttachment implements IAttachment {
public static final short PROTOCOL_ID = 1;
public static final short PROTOCOL_ID = 2;
/**
* session id
@@ -73,7 +73,10 @@ public class GatewayAttachment implements IAttachment {
return AttachmentType.GATEWAY_PACKET;
}
@Override
/**
* EN:Used to determine which thread the message is processed on
* CN:用来确定这条消息在哪一个线程处理
*/
public int taskExecutorHash() {
return useTaskExecutorHashParam ? taskExecutorHashParam : (int) uid;
}
@@ -21,7 +21,7 @@ import io.netty.handler.codec.http.HttpResponseStatus;
*/
public class HttpAttachment implements IAttachment {
public static final short PROTOCOL_ID = 3;
public static final short PROTOCOL_ID = 4;
private long uid;
@@ -45,7 +45,10 @@ public class HttpAttachment implements IAttachment {
return AttachmentType.HTTP_PACKET;
}
@Override
/**
* EN:Used to determine which thread the message is processed on
* CN:用来确定这条消息在哪一个线程处理
*/
public int taskExecutorHash() {
return useTaskExecutorHashParam ? taskExecutorHashParam : (int) uid;
}
@@ -22,10 +22,4 @@ public interface IAttachment extends IPacket {
AttachmentType packetType();
/**
* EN:Used to determine which thread the message is processed on
* CN:用来确定这条消息在哪一个线程处理
*/
int taskExecutorHash();
}
@@ -20,7 +20,7 @@ package com.zfoo.net.router.attachment;
*/
public class NoAnswerAttachment implements IAttachment {
public static final short PROTOCOL_ID = 4;
public static final short PROTOCOL_ID = 5;
private int taskExecutorHash;
@@ -35,11 +35,6 @@ public class NoAnswerAttachment implements IAttachment {
return AttachmentType.NO_ANSWER_PACKET;
}
@Override
public int taskExecutorHash() {
return taskExecutorHash;
}
@Override
public short protocolId() {
return PROTOCOL_ID;
@@ -71,7 +71,10 @@ public class SignalAttachment implements IAttachment {
return AttachmentType.SIGNAL_PACKET;
}
@Override
/**
* EN:Used to determine which thread the message is processed on
* CN:用来确定这条消息在哪一个线程处理
*/
public int taskExecutorHash() {
return taskExecutorHash;
}
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.net.router.attachment;
/**
* 主要用来支持godot,unity,ts这种异步语法的使用,做为async/await语法的支撑
*
* @author godotg
* @version 3.0
*/
public class SignalOnlyAttachment implements IAttachment {
public static final short PROTOCOL_ID = 1;
private int signalId;
private long timestamp;
@Override
public AttachmentType packetType() {
return AttachmentType.SIGNAL_ONLY_PACKET;
}
@Override
public short protocolId() {
return PROTOCOL_ID;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SignalOnlyAttachment that = (SignalOnlyAttachment) o;
return signalId == that.signalId;
}
@Override
public int hashCode() {
return signalId;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public int getSignalId() {
return signalId;
}
public void setSignalId(int signalId) {
this.signalId = signalId;
}
}
@@ -20,7 +20,7 @@ import com.zfoo.util.math.RandomUtils;
*/
public class UdpAttachment implements IAttachment {
public static final short PROTOCOL_ID = 2;
public static final short PROTOCOL_ID = 3;
private String host;
private int port;
@@ -37,11 +37,6 @@ public class UdpAttachment implements IAttachment {
return AttachmentType.UDP_PACKET;
}
@Override
public int taskExecutorHash() {
return RandomUtils.randomInt();
}
@Override
public short protocolId() {
return PROTOCOL_ID;
@@ -15,6 +15,11 @@ package com.zfoo.net.task;
import com.zfoo.event.manager.EventBus;
import com.zfoo.net.NetContext;
import com.zfoo.net.router.attachment.GatewayAttachment;
import com.zfoo.net.router.attachment.HttpAttachment;
import com.zfoo.net.router.attachment.IAttachment;
import com.zfoo.net.router.attachment.SignalAttachment;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.collection.concurrent.CopyOnWriteHashMapLongObject;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.StringUtils;
@@ -114,17 +119,39 @@ public final class TaskBus {
*/
public static void dispatch(PacketReceiverTask task) {
var attachment = task.getAttachment();
if (attachment == null) {
var session = task.getSession();
var uid = session.getUid();
if (uid > 0) {
execute((int) uid, task);
} else {
execute((int) session.getSid(), task);
}
dispatchBySession(task.getSession(), task);
} else {
execute(attachment.taskExecutorHash(), task);
dispatchByAttachment(attachment, task);
}
}
private static void dispatchBySession(Session session, PacketReceiverTask task) {
var uid = session.getUid();
if (uid > 0) {
execute((int) uid, task);
} else {
execute((int) session.getSid(), task);
}
}
private static void dispatchByAttachment(IAttachment attachment, PacketReceiverTask task) {
switch (attachment.packetType()) {
case SIGNAL_PACKET:
execute(((SignalAttachment) attachment).taskExecutorHash(), task);
break;
case GATEWAY_PACKET:
execute(((GatewayAttachment) attachment).taskExecutorHash(), task);
break;
case HTTP_PACKET:
execute(((HttpAttachment) attachment).taskExecutorHash(), task);
break;
case SIGNAL_ONLY_PACKET:
case NO_ANSWER_PACKET:
case UDP_PACKET:
dispatchBySession(task.getSession(), task);
break;
default:
}
}