1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 21:23:55 +08:00
This commit is contained in:
Him188moe 2019-08-16 00:30:38 +08:00
parent 14ffda6ae2
commit bbc1f717a6
2 changed files with 53 additions and 50 deletions
mirai-core/src/main/java/net/mamoe/mirai

View File

@ -5,6 +5,7 @@ import lombok.extern.log4j.Log4j2;
import net.mamoe.mirai.event.MiraiEventManager;
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
import net.mamoe.mirai.event.events.server.ServerEnableEvent;
import net.mamoe.mirai.network.Protocol;
import net.mamoe.mirai.network.Robot;
import net.mamoe.mirai.task.MiraiTaskManager;
import net.mamoe.mirai.utils.LoggerTextFormat;
@ -94,7 +95,13 @@ public class MiraiServer {
});
*/
new Robot(1994701021L);
Robot robot = new Robot(1994701021L);
try {
robot.connect(Protocol.SERVER_IP.get(2), 8000);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
/*
System.out.println("network test");
try {

View File

@ -9,7 +9,6 @@ import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioSocketChannel
import io.netty.handler.codec.bytes.ByteArrayDecoder
import io.netty.handler.codec.bytes.ByteArrayEncoder
import lombok.extern.log4j.Log4j2
import net.mamoe.mirai.MiraiServer
import net.mamoe.mirai.network.packet.Packet
import net.mamoe.mirai.network.packet.client.Client0825ResponsePacket
@ -53,7 +52,29 @@ class Robot(val number: Long) {
println("connected server...")
ch.pipeline().addLast(ByteArrayEncoder())
ch.pipeline().addLast(ByteArrayDecoder())
ch.pipeline().addLast(ClientHandler(this@Robot))
ch.pipeline().addLast(object : SimpleChannelInboundHandler<ByteArray>() {
override fun channelRead0(ctx: ChannelHandlerContext?, bytes: ByteArray) {
try {
/*val remaining = Reader.read(bytes);
if (Reader.isPacketAvailable()) {
robot.onPacketReceived(Reader.toServerPacket())
Reader.init()
remaining
}*/
this@Robot.onPacketReceived(ServerPacket.ofByteArray(bytes))
} catch (e: Exception) {
MiraiServer.getLogger().catching(e)
}
}
override fun channelActive(ctx: ChannelHandlerContext) {
println("Successfully connected to server")
}
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
MiraiServer.getLogger().catching(cause)
}
})
}
})
@ -65,60 +86,35 @@ class Robot(val number: Long) {
}
}
@Log4j2
private class ClientHandler(val robot: Robot) : SimpleChannelInboundHandler<ByteArray>() {
private object Reader {
private var length: Int? = null
private lateinit var bytes: ByteArray
private object Reader {
private var length: Int? = null
private lateinit var bytes: ByteArray
fun init(bytes: ByteArray) {
this.length = bytes.size
this.bytes = bytes
}
/**
* Reads bytes, combining them to create a packet, returning remaining bytes.
*/
fun read(bytes: ByteArray): ByteArray? {
checkNotNull(this.length)
val needSize = length!! - this.bytes.size;//How many bytes we need
if (needSize == bytes.size || needSize > bytes.size) {
this.bytes += bytes
return null;
}
//We got more than we need
this.bytes += bytes.copyOfRange(0, needSize)
return bytes.copyOfRange(needSize, bytes.size - needSize)//We got remaining bytes, that is of another packet
}
fun isPacketAvailable() = this.length == this.bytes.size
fun toServerPacket(): ServerPacket {
return ServerPacket.ofByteArray(this.bytes)
}
fun init(bytes: ByteArray) {
this.length = bytes.size
this.bytes = bytes
}
override fun channelRead0(ctx: ChannelHandlerContext?, bytes: ByteArray) {
try {
/*val remaining = Reader.read(bytes);
if (Reader.isPacketAvailable()) {
robot.onPacketReceived(Reader.toServerPacket())
Reader.init()
remaining
}*/
robot.onPacketReceived(ServerPacket.ofByteArray(bytes))
} catch (e: Exception) {
MiraiServer.getLogger().catching(e)
/**
* Reads bytes, combining them to create a packet, returning remaining bytes.
*/
fun read(bytes: ByteArray): ByteArray? {
checkNotNull(this.length)
val needSize = length!! - this.bytes.size;//How many bytes we need
if (needSize == bytes.size || needSize > bytes.size) {
this.bytes += bytes
return null;
}
//We got more than we need
this.bytes += bytes.copyOfRange(0, needSize)
return bytes.copyOfRange(needSize, bytes.size - needSize)//We got remaining bytes, that is of another packet
}
override fun channelActive(ctx: ChannelHandlerContext) {
println("Successfully connected to server")
}
fun isPacketAvailable() = this.length == this.bytes.size
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
MiraiServer.getLogger().catching(cause)
fun toServerPacket(): ServerPacket {
return ServerPacket.ofByteArray(this.bytes)
}
}
}