Java - Netty拆分消息字节

时间:2015-02-10 15:14:49

标签: java byte netty

我尝试使用Netty 3从客户端向服务器发送消息。

该消息包括:

400个字符串 210整数

消息平均字节数:3400

客户端正在成功发送消息,问题出在服务器上,即分割字节。

Client: sent 3400 bytes.    
Server: received 1024 bytes.
Server: received 2048 bytes.
Server: received 328 bytes.

编码:

public void sendFriendList(final Player player) {
    final int friendsSize = player.getFriendsIgnores().getFriends().size();
        final OutputStream stream = new OutputStream(12500);
        stream.writeSmart(83); //packet ID
        stream.writeShort(friendsSize); //number of friends
        stream.writeInt(player.getPlayerDefinition().index()); //player id
        for (int id : player.getFriendsIgnores().getFriends()) { //array of integers
            stream.writeInt(id);
            stream.writeString(PlayerLoader.getUsername(id));
            stream.writeString(PlayerLoader.getDisplayName(id));
            stream.writeInt(PlayerLoader.getWorld(id));
        }
        session.write(stream);
    }        

解码:

case 83:
        int friendsSize = stream.readUnsignedShort();
        int playerId = stream.readInt();
        player = World.get(playerId);

        if (player != null) {
            for (int i = 0; i < friendsSize; i++) {
            int id = stream.readInt();
            username = stream.readString();
            displayName = stream.readString();
            world = stream.readInt();
            player.getFriendsIgnores().addFriendDetails(id, username, displayName, world);
            }
        }
        break;    

0 个答案:

没有答案