Hi, thanks for the answer, i will try to be more clear about my protocol:
The protocol is simple like a Chat protocol.
Client and Server send and receive XML text data under Text Data, in other
words they send and receive Strings that is XML.
For both Client and Server i am using Mina 2.0 and the same Coder/Decoder.
So the client decoder is:
public class *FlashDecoder *extends *CumulativeProtocolDecoder *{
private Map<Long, String> buffer = Collections.synchronizedMap(new
HashMap<Long, String>());
private static final Logger log =
LoggerFactory.getLogger(FlashCrossdomainDecoder.class);
@Override
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
StringBuffer msg = new StringBuffer();
while (in.hasRemaining()) {
byte[] data = new byte[in.remaining()];
in.get(data);
msg.append(new String(data));
}
if (msg.toString().codePointAt(msg.toString().length()-1) == '\0') {
if (buffer.containsKey(session.getId())) {
String res = buffer.get(session.getId()) + msg.toString();
res = res.trim();
out.write(res);
buffer.remove(session.getId());
} else {
String res = msg.toString();
res = res.trim();
out.write(res);
log.debug("[doDecode] out=" + res.trim());
return true;
}
} else {
if (buffer.containsKey(session.getId())) {
buffer.put(session.getId(), buffer.get(session.getId()) +
msg.toString());
} else {
buffer.put(session.getId(), msg.toString());
}
}
return false;
}
}
The server encorder is:
public class *FlashEncoder *extends ProtocolEncoderAdapter {
private static final Logger log = LoggerFactory.getLogger(
FlashCrossdomainEncoder.class);
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
try {
out.write(IoBuffer.wrap(message.toString().getBytes()));
log.debug("[encode] message=" + message.toString().trim());
} catch (Throwable e) {
e.printStackTrace();
}
}
}
As attach there is the complete code.
"*do you deal with the fact that you may have more than one message to deal
with in the decoder ?* "
I thought yes, for each packet of a single message, client or server
receive, i use a Map (Map<Long, String> buffer => <sessionId, packet> ) in
order to store the single message.
And it is working 9 times on 10.
I don't understand where the client get this message <game g="4931" d="2" *
s="1"* a="0" h="turn" lr="" ia="0"/> that i can think only it is an old
message !
Server Sent
17:48:23,790 DEBUG {pool-4-thread-2} a:? - [encode] >> *tob *message=<game
g="4931" d="2" s="2" a="0" h="turn" lr="check" ia="0"/>
17:48:23,891 DEBUG {pool-4-thread-4} a:? - [encode] >> *antares *message=<game
g="4931" d="2" s="2" a="0" h="turn" lr="check" ia="0"/>
Client Received
17:48:23,792 DEBUG {pool-6-thread-1} Handler:543 - << *tob *RECEIVED=<game
g="4931" d="2" s="2" a="0" h="turn" lr="check" ia="0"/>
here i don't see any other received data from server by antares only this:
17:48:24,174 DEBUG {pool-3-thread-1} Handler:543 - << *antares *RECEIVED=<game
g="4931" d="2" *s="1"* a="0" h="turn" lr="" ia="0"/>
Do you have a codec that has the right way to manipulate string ?
Please let me know what i can send to you in order to help you to help me.
Thanks
F.
2010/9/6 Emmanuel Lécharny <[email protected]>
> On 9/6/10 6:13 PM, FLV wrote:
>
>> Hi all,
>>
>> i need your help.
>>
>
> A few questions :
>
> - do you deal with the fact that you may have more than one message to deal
> with in the decoder ? (ie, the bytes you received may be the contatenation
> of more of one message)
> - what is the protocol you are dealing with ? Its hard to know what your
> codec is supposed to produce without this input
>
>
>
> --
> Regards,
> Cordialement,
> Emmanuel Lécharny
> www.iktek.com
>
>
Hi all,
i need your help.
I am using *apache mina 2.0 RC1* for my card game and
I am using OrderedThreadPoolExecutor for read and write server side.
chain.addLast("readExecutor", new ExecutorFilter(new
OrderedThreadPoolExecutor(), IoEventType.MESSAGE_RECEIVED))
;
chain.addLast("codec", new ProtocolCodecFilter(new
FlashCrossdomainCodec()));
chain.addLast("writeExecutor", new ExecutorFilter(new
OrderedThreadPoolExecutor(), IoEventType.WRITE));
Also my codec is a CumulativeProtocolDecoder with the following code:
public class *FlashDecoder *extends *CumulativeProtocolDecoder *{
private Map<Long, String> buffer = Collections.synchronizedMap(new
HashMap<Long, String>());
private static final Logger log =
LoggerFactory.getLogger(FlashCrossdomainDecoder.class);
@Override
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
StringBuffer msg = new StringBuffer();
while (in.hasRemaining()) {
byte[] data = new byte[in.remaining()];
in.get(data);
msg.append(new String(data));
}
if (msg.toString().codePointAt(msg.toString().length()-1) == '\0') {
if (buffer.containsKey(session.getId())) {
String res = buffer.get(session.getId()) + msg.toString();
res = res.trim();
out.write(res);
buffer.remove(session.getId());
} else {
String res = msg.toString();
res = res.trim();
out.write(res);
log.debug("[doDecode] out=" + res.trim());
return true;
}
} else {
if (buffer.containsKey(session.getId())) {
buffer.put(session.getId(), buffer.get(session.getId()) +
msg.toString());
} else {
buffer.put(session.getId(), msg.toString());
}
}
return false;
}
}
public class *FlashEncoder *extends ProtocolEncoderAdapter {
private static final Logger log =
LoggerFactory.getLogger(FlashCrossdomainEncoder.class);
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
try {
out.write(IoBuffer.wrap(message.toString().getBytes()));
log.debug("[encode] message=" + message.toString().trim());
} catch (Throwable e) {
e.printStackTrace();
}
}
}
PROBLEM:
Sometimes the server seems to send a string but the client receives other or
(old) one !
Server Sent
17:48:23,790 DEBUG {pool-4-thread-2} a:? - [encode] message=<game g="4931"
d="2" s="2" a="0" h="turn" lr="check" ia="0"/>
17:48:23,891 DEBUG {pool-4-thread-4} a:? - [encode] message=<game g="4931"
d="2" s="2" a="0" h="turn" lr="check" ia="0"/>
Client Received
17:48:23,792 DEBUG {pool-6-thread-1} Handler:543 - << tob RECEIVED=<game
g="4931" d="2" s="2" a="0" h="turn" lr="check" ia="0"/>
here i don't see any other received data from server by antares only this:
17:48:24,174 DEBUG {pool-3-thread-1} Handler:543 - << antares RECEIVED=<game
g="4931" d="2" *s="1"* a="0" h="turn" lr="" ia="0"/>
So in order i receive:
<game g="4931" d="2" s="1" a="0" h="burningandturn" lr="check" ia="0"/>
<game g="4931" d="2" s="1" a="0" h="burningandturn" lr="check" ia="0"/>
<game g="4931" d="2" s="1" a="0" h="turn" lr="" ia="0"/>
<game g="4931" d="2" s="2" a="0" h="turn" lr="check" ia="0"/>
<game g="4931" d="2" *s="1"* a="0" h="turn" lr="" ia="0"/>
Please what is wrong ? I need to do something to the codec ?
Thanks! and i like so much Mina
Francesco