RuneHive-Game
Loading...
Searching...
No Matches
OnDemandResponseEncoder.java
Go to the documentation of this file.
1package dev.advo.fs.net.ondemand;
2
3
4import org.jboss.netty.buffer.ChannelBuffer;
5import org.jboss.netty.buffer.ChannelBuffers;
6import org.jboss.netty.channel.Channel;
7import org.jboss.netty.channel.ChannelHandlerContext;
8import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
9
10import dev.advo.fs.fs.FileDescriptor;
11
12public final class OnDemandResponseEncoder extends OneToOneEncoder {
13
14 @Override
15 protected Object encode(ChannelHandlerContext ctx, Channel c, Object msg) throws Exception {
16 if (msg instanceof OnDemandResponse) {
18
19 FileDescriptor fileDescriptor = resp.getFileDescriptor();
20 int fileSize = resp.getFileSize();
21 ChannelBuffer chunkData = resp.getChunkData();
22
23 ChannelBuffer buf = ChannelBuffers.buffer(7 + chunkData.readableBytes());
24 buf.writeMedium(create(fileDescriptor.getType(), fileDescriptor.getFile()));
25 buf.writeInt(fileSize);
26 buf.writeBytes(chunkData);
27
28 return buf;
29 }
30 return msg;
31 }
32
33 public static int create(int index, int file) {
34 if ((index & 0xFFFFFFE0) != 0)
35 throw new IllegalArgumentException("invalid index " + index + ":" + file);
36 if ((file & 0xFFF80000) != 0)
37 throw new IllegalArgumentException("invalid file " + index + ":" + file);
38 return index & 0x1F | (file & 0x7FFFF) << 5;
39 }
40}
A class which points to a file in the cache.
int getFile()
Gets the file id.
int getType()
Gets the file type.
Object encode(ChannelHandlerContext ctx, Channel c, Object msg)