menacher / java-game-server

Jetserver is a high speed nio socket based multiplayer java game server written using Netty and Mike Rettig's Jetlang.It is specifically tuned for network based multiplayer games and supports TCP and UDP network protocols.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Specifying encoding for NettyUtils.writeString()?

nikolaykuz opened this issue · comments

Hello menacher,

I am facing an issue when trying to transmit UTF-8 encoded String with Russian characters using NettyUtils.writeString()

I have detailed description here:
http://stackoverflow.com/questions/13598921/wrong-encoding-when-running-from-jar-from-eclipse-works-perfectly

And discussion in here http://stackoverflow.com/a/13599127/1360074 of the same question.

Please, let me know if you have any comment on this.

Thanks

Looks like the underlying Netty StringEncoder takes Charset. I will add the necessary wrapper methods in NettyUtils which Charset as parameter and check it in. This way you will have fine grained control on which Charset encoding you need to use for encoding and decoding.

I have also updated the binaries. See if you can make use of these new read and write string methods which take charset parameter.

Thanks for quick update! Just updating jetserver.jar and writeString() calls would be enough?

Yes you can take the latest jetserver.jar and it will have all the changes.
If client and server both specify the same Charset while doing read-write then it should work. However I have not written any unit tests for the moment.

Following unit test I wrote is passing, so I guess it works

@Test
    public void nettyUtilVarArgsCharsetStringWriteRead()
    {
        String russianFirst = "{'response':[{'uid':123456,'first_name':'Имя',";
        String russianLast = "'last_name':'Фамилия'}]}";
        ChannelBuffer stringBuffer = NettyUtils.writeStrings(CharsetUtil.UTF_16,russianFirst,russianLast);
        String firstName = NettyUtils.readString(stringBuffer,CharsetUtil.UTF_16);
        String lastName = NettyUtils.readString(stringBuffer,CharsetUtil.US_ASCII);
        assertEquals(russianFirst,firstName);
        assertFalse(russianLast.equals(lastName));
    }

I confirm, it works now. Very tricky thing! Thanks