You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by "maxbox@directbox.com" <ma...@directbox.com> on 2009/05/23 18:55:33 UTC
Need help with my MessageDecoder
Hi everybody,
i am new to mina and first i want to say that the framework is awesome, great work.
I use mina v2.0.0-M5 to build a little game with client-server architecture. I have read the examples, specially the SumUp server and based on that build my own protocol.
Now i have a problem with my login MessageDecoder, first here is the code:
public class LoginRequestDecoder extends AbstractDecoder<LoginRequest> {
private final String CHARSET_NAME = "UTF8";
public LoginRequestDecoder() {
super(ProtocolContants.LOGINREQUEST);
}
@Override
protected LoginRequest decodeBody(IoSession session, IoBuffer in) {
if (in.remaining() < ProtocolContants.LOGINREQUEST_BODY) {
return null;
}
LoginRequest m = new LoginRequest();
m.setGameTyp(in.getInt());
try {
m.setName(in.getString(Charset.forName(CHARSET_NAME).newDecoder()));
} catch (CharacterCodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return m;
}
@Override
public void finishDecode(IoSession session, ProtocolDecoderOutput out)
throws Exception {
// no resources to dispose
}
}
public abstract class AbstractDecoder<T> implements MessageDecoder {
private final int m_nType;
private boolean m_bReadHeader;
protected AbstractDecoder(int type) {
this.m_nType = type;
}
public MessageDecoderResult decodable(IoSession session, IoBuffer in) {
// Return NEED_DATA if the whole header is not read yet
if (in.remaining() < ProtocolContants.HEADER_LEN) {
return MessageDecoderResult.NEED_DATA;
}
// Return OK if type and bodyLength matches
if (m_nType == in.getShort()) {
return MessageDecoderResult.OK;
}
// Return NOT_OK if not matches
return MessageDecoderResult.NOT_OK;
}
public MessageDecoderResult decode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
// Try to skip header if not read
if (!m_bReadHeader) {
in.getShort(); // Skip 'type'
m_bReadHeader = true;
}
// Try to decode body
T m = decodeBody(session, in);
// Return NEED_DATA if the body is not fully read
if (m == null) {
return MessageDecoderResult.NEED_DATA;
} else {
m_bReadHeader = false; // reset readHeader for the next decode
}
out.write(m);
return MessageDecoderResult.OK;
}
protected abstract T decodeBody(IoSession session, IoBuffer in);
}
As you can see i use fixed message size, my loginrequest contain 3 attributes:
1: int message type (inherited from AbstractDecoder)
2: int game typ
3: String login name
The message type will be decoded by AbstractDecoder and the rest (loginrequest body) from my LoginRequestDecoder but here is the problem. It does not decode my message because of the decodeBody methode returns everytime null:
if (in.remaining() < ProtocolContants.LOGINREQUEST_BODY) {
return null;
}
ProtocolContants.LOGINREQUEST_BODY = 8 (4 byte int + 4 byte String, i hope this is correct)
in.remaining() returns everytime 7 i don't know why ???
Maybe i miss something, could somebody help me with that?
Max
Re: Need help with my MessageDecoder
Posted by maxbox <ma...@directbox.com>.
Sorry guys, that was so stupid.
The SumUp example contains the solution already, i should have read the
example more carefully. Shame on me!
Yeah please the forget the question ;)
Max
--
View this message in context: http://www.nabble.com/Need-help-with-my-MessageDecoder-tp23685248p23699110.html
Sent from the Apache MINA User Forum mailing list archive at Nabble.com.