Skip to content

Commit

Permalink
fix client write request
Browse files Browse the repository at this point in the history
  • Loading branch information
chao.yu committed Aug 26, 2018
1 parent fe2d876 commit 745b5f1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 26 deletions.
7 changes: 1 addition & 6 deletions src/main/java/the/flash/client/NettyClient.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package the.flash.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
Expand All @@ -13,7 +12,6 @@
import the.flash.client.handler.MessageResponseHandler;
import the.flash.codec.PacketDecoder;
import the.flash.codec.PacketEncoder;
import the.flash.protocol.PacketCodeC;
import the.flash.protocol.request.MessageRequestPacket;
import the.flash.util.LoginUtil;

Expand Down Expand Up @@ -81,10 +79,7 @@ private static void startConsoleThread(Channel channel) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();

MessageRequestPacket packet = new MessageRequestPacket();
packet.setMessage(line);
ByteBuf byteBuf = PacketCodeC.INSTANCE.encode(channel.alloc(), packet);
channel.writeAndFlush(byteBuf);
channel.writeAndFlush(new MessageRequestPacket(line));
}
}
}).start();
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/the/flash/protocol/PacketCodeC.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package the.flash.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import the.flash.protocol.request.LoginRequestPacket;
import the.flash.protocol.request.MessageRequestPacket;
import the.flash.protocol.response.LoginResponsePacket;
Expand Down Expand Up @@ -35,24 +34,6 @@ private PacketCodeC() {
serializerMap.put(serializer.getSerializerAlogrithm(), serializer);
}


public ByteBuf encode(ByteBufAllocator byteBufAllocator, Packet packet) {
// 1. 创建 ByteBuf 对象
ByteBuf byteBuf = byteBufAllocator.ioBuffer();
// 2. 序列化 java 对象
byte[] bytes = Serializer.DEFAULT.serialize(packet);

// 2. 实际编码过程
byteBuf.writeInt(MAGIC_NUMBER);
byteBuf.writeByte(packet.getVersion());
byteBuf.writeByte(Serializer.DEFAULT.getSerializerAlogrithm());
byteBuf.writeByte(packet.getCommand());
byteBuf.writeInt(bytes.length);
byteBuf.writeBytes(bytes);

return byteBuf;
}

public void encode(ByteBuf byteBuf, Packet packet) {
// 1. 序列化 java 对象
byte[] bytes = Serializer.DEFAULT.serialize(packet);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package the.flash.protocol.request;

import lombok.Data;
import lombok.NoArgsConstructor;
import the.flash.protocol.Packet;

import static the.flash.protocol.command.Command.MESSAGE_REQUEST;

@Data
@NoArgsConstructor
public class MessageRequestPacket extends Packet {

private String message;

public MessageRequestPacket(String message) {
this.message = message;
}

@Override
public Byte getCommand() {
return MESSAGE_REQUEST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public void encode() {
loginRequestPacket.setPassword("password");

PacketCodeC packetCodeC = PacketCodeC.INSTANCE;
ByteBuf byteBuf = packetCodeC.encode(ByteBufAllocator.DEFAULT, loginRequestPacket);
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer();

packetCodeC.encode(byteBuf, loginRequestPacket);
Packet decodedPacket = packetCodeC.decode(byteBuf);

Assert.assertArrayEquals(serializer.serialize(loginRequestPacket), serializer.serialize(decodedPacket));
Expand Down

0 comments on commit 745b5f1

Please sign in to comment.