Binding to a port
This is the only way to start the network from the framework
Code:
HandShakeCoder defaultCoder = ...
RSFramework.bind(43594, defaultCoder);
The network requires some kind of default coder to handle the encoding and decoding of the network messages. From that default coder, you can assign new coders using
session.setCoder(RSCoder) This is also shown in the example below...
Code:
public class HandshakeCoder implements RSSessionCoder {
private static final byte JS5 = 15;
private static final byte LOGIN = 14;
@Override
public void decode(RSNetworkSession session, RSStream stream, List<Object> out) {
int request = stream.readUnsignedByte();
if (request == JS5) {
... // handling js5 variables and responses
session.setCoder(new CacheUpdateCoder());
} else if (request == LOGIN) {
... // handling login variables and responses
session.setCoder(new LoginCoder());
}
}
@Override
public RSStream encode(RSNetworkSession session, RSStream in) {
return in;
}
}
Implementing an RSSessionCoder
Coders will handle the encoding and decoding of messages or frames. If there is no encoding done, you can just return the
in parameter of the
encode(RSNetworkSession, RSStream) method, otherwise return the encoded message as an RSStream. If you are sending any outgoing messages, they are to be added to the
out list in the
decode(RSNetworkSession, RSStream, List<Object>) method
RSFrame and RSStream
An RSFrame is just an extension of the RSStream class, but with an opcode and header. Below is shown an example of creating different RSFrames...
Code:
RSFrame varShortFrame = RSFrame.varShort(0); // header type is var_short with opcode of 0
RSFrame varByteFrame = RSFrame.varByte(0); // header type is var_short with opcode of 0
RSFrame standardFrame = RSFrame.standard(0); // header type is var_short with opcode of 0
By usual definition, what makes a frame is a message with a corresponding operation code, so that is why it is called RSFrame.
RSNetworkSession
The RSNetworkSession holds the Channel, the RSSessionCoder, and other attributes for the network. As shown above, you can set the coder from the session variable passed through other coders, and this originates from the default coder that is usually set on application startup. The Channel of the session is just from the io.netty.channel package of netty 4.1.36 and will hold various methods to handle what happens with the channel. Attributes can be set to the session as well, which connects directly to the channel. These attributes are used for setting any values to the connection while the session is active. Below is an example of using the attributes and also writing to the session...
Code:
public class LoginCoder implements RSSessionCoder {
@Override
public void decode(RSNetworkSession session, RSStream in, List<Object> out) {
... // login variables and functionality
Player player = new Player();
session.set("Player", player);
}
... // other methods and functionality
}
/**
* This may be a class you use for world request packet handling in the lobby
*/
public class WorldRequest {
// This is an example class not part of the framework
public void handleFrame(RSNetworkSession session, RSFrame frame) {
Player player = session.get("Player", Player.class); // getting the player object from the session for some reason
RSFrame worldResponse = RSFrame.varShort(80);
worldResponse.writeByte(1);
worldResponse.writeShort(2);
worldResponse.writeInt(3);
...
session.write(worldResponse);
}
}