nio.Buffer.position

70 views
Skip to first unread message

PaulRuss

unread,
Nov 27, 2014, 8:22:30 AM11/27/14
to kryone...@googlegroups.com
HI Kryo Users!

I've been using Kryonet for a while with this program that I'm involved with - all is well. But I recently stopped using JavaSerializer to serialize all the classes, and let Kryo take over. Now I'm getting this error:


Exception in thread "Client" java.lang.IllegalArgumentException
 at java.nio.Buffer.position(Buffer.java:244)
 at com.esotericsoftware.kryonet.TcpConnection.send(TcpConnection.java:188)
 at com.esotericsoftware.kryonet.Connection.sendTCP(Connection.java:59)
 at com.esotericsoftware.kryonet.Client.keepAlive(Client.java:329)
 at com.esotericsoftware.kryonet.Client.update(Client.java:321)
 at com.esotericsoftware.kryonet.Client.run(Client.java:338)
 at java.lang.Thread.run(Thread.java:745)


Occurs here in the KyroNet source:

/** This method is thread safe. */
 public int send (Connection connection, Object object) throws IOException {
  SocketChannel socketChannel = this.socketChannel;
  if (socketChannel == null) throw new SocketException("Connection is closed.");
  synchronized (writeLock) {
   // Leave room for length.
   int start = writeBuffer.position();
   int lengthLength = serialization.getLengthLength();
   writeBuffer.position(writeBuffer.position() + lengthLength); <----- HERE

I've listed my register class below, in case that helps. Any help appreciated,

Best,
Paul



/**
 * Created by Paul on 23/09/2014. :)
 */
public class RegisterClasses {
    public void register (Kryo kryo) {
        //java
        kryo.register(ArrayList.class);
        kryo.register(HashSet.class);
        kryo.register(Set.class);
        kryo.register(Timestamp.class);
        kryo.register(BigDecimal.class);
        //enum
        kryo.register(EntityStatusType.class);
        kryo.register(PrivilegeType.class);
        kryo.register(DanPrefix.class);
        kryo.register(LogonResponseType.class);
        //DTO
        kryo.register(CompanyDto.class);
        kryo.register(DefermentAccountDto.class);
        kryo.register(DivisionDto.class);
        kryo.register(PrivilegeDto.class);
        kryo.register(RoleDto.class);
        kryo.register(UserDto.class);
        //client side
        kryo.register(ClientLogonNetMsg.class);
        kryo.register(ClientRequestAllUsersNetMsg.class);
        kryo.register(ClientRequestAllCompaniesAndRolesNetMsg.class);
        kryo.register(ClientCreateUserNetMsg.class);
        kryo.register(ClientRequestRetireUserNetMsg.class);
        kryo.register(ClientRequestOneUserWithAllRolesAndCompaniesNetMsg.class);
        kryo.register(ClientRequestAllDefermentAccountNetMsg.class);
        kryo.register(ClientDefermentAccountTransactionNetMsg.class);
        kryo.register(ClientGenericEntityStatusChangeRequestNetMsg.class);
        //server side
        kryo.register(ServerLogonResponseNetMsg.class);
        kryo.register(ServerUserListResponseNetMsg.class);
        kryo.register(ServerAllCompaniesAndRolesResponseNetMsg.class);
        kryo.register(ServerCreateUserResultNetMsg.class);
        kryo.register(ServerRetireUserResponseNetMsg.class);
        kryo.register(ServerUnknownMessageReceivedResponseNetMsg.class);
        kryo.register(ServerOneUserAllCompaniesAndRolesResponseNetMsg.class);
        kryo.register(ServerDefermentAccountListResponseNetMsg.class);
        kryo.register(ServerGenericOperationResultNetMsg.class);
    }
}



PaulRuss

unread,
Nov 27, 2014, 8:26:10 AM11/27/14
to kryone...@googlegroups.com
Only happens periodically, when I pull back a large data load. I've tried increasing buffer size, to no avail - thanks!

PaulRuss

unread,
Nov 27, 2014, 8:38:35 AM11/27/14
to kryone...@googlegroups.com
I should also mention, I'm using version 2.24 from Maven.

PaulRuss

unread,
Nov 27, 2014, 11:00:15 AM11/27/14
to kryone...@googlegroups.com
I've tracked the error down to one DTO, which is creating the problems. If I replace this with a new JavaSerializer(), in my register classes code, this works fine. Can you see any problem with this class?

/**
 * Created by Paul on 30/09/2014. :)
 */
public class CompanyDto implements Serializable {
    private static final long serialVersionUID = 3908337574314923665L;
    private String companyCode;
    private String companyName;
    private Set<DivisionDto> divisionDtoSet;
    private long companyID;
    public CompanyDto() {}
    public String getCompanyCode() {
        return companyCode;
    }
    public void setCompanyCode(String companyCode) {
        this.companyCode = companyCode;
    }
    public String getCompanyName() {
        return companyName;
    }
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    public Set<DivisionDto> getDivisionDtoSet() {
        return divisionDtoSet;
    }
    public void setDivisionDtoSet(Set<DivisionDto> divisionDtoSet) {
        this.divisionDtoSet = divisionDtoSet;
    }
    public long getCompanyID() {
        return companyID;
    }
    public void setCompanyID(long companyID) {
        this.companyID = companyID;
    }
}

PaulRuss

unread,
Nov 27, 2014, 11:49:13 AM11/27/14
to kryone...@googlegroups.com
I've tried adding a Custom serializer,

   @Override
    public void write(Kryo kryo, Output output) {
        output.writeString(companyCode);
        output.writeString(companyName);
        CollectionSerializer cs = new CollectionSerializer();
        cs.write(kryo, output, divisionDtoSet);
        output.writeLong(companyID);
    }
    @Override
    public void read(Kryo kryo, Input input) {
        companyCode = input.readString();
        companyName = input.readString();
        CollectionSerializer cs = new CollectionSerializer();
        divisionDtoSet = (Set)cs.read(kryo, input, java.util.Collection.class);
    }

Compiles, but fails with a no default constructor argument. If I try to user the java Externizable:

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(companyCode);
        out.writeObject(companyName);
        out.writeObject(divisionDtoSet);
        out.writeLong(companyID);
    }
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        companyCode= (String) in.readObject();
        companyName = (String) in.readObject();
        divisionDtoSet = (Set<DivisionDto>) in.readObject();
        companyID = in.readLong();
    }

this works but only using the JavaSerializer, not the new ExternalizableSerializer()

Best,
Paul



PaulRuss

unread,
Dec 10, 2014, 4:22:43 AM12/10/14
to kryone...@googlegroups.com
BUMP!


On Thursday, 27 November 2014 13:22:30 UTC, PaulRuss wrote:

Joachim Durchholz

unread,
Dec 10, 2014, 5:10:09 AM12/10/14
to kryone...@googlegroups.com
Am 10.12.2014 um 10:22 schrieb PaulRuss:
> BUMP!

Seems like nobody has enough data to see the issue.
Do you have an SSCCE (see http://sscce.org ) ?

PaulRuss

unread,
Dec 10, 2014, 5:32:29 AM12/10/14
to kryone...@googlegroups.com
Thanks for the response. I'll put something together today, and post it. Best,
Paul

PaulRuss

unread,
Jan 13, 2015, 12:10:22 PM1/13/15
to kryone...@googlegroups.com
Hi Nate, et Al.

Sorry for the long delay - I've been so busy. In trying to replicate this, I've made a small Kryo test server and client. I've loaded in some dummy data. The first issue is that Kryo is getting stuck in a loop.

In my object tree Object A contains a Set of Object B, which contains a Set of Object A. The quick solution has been to add new JavaSerializer() into the class registration.

I'd appreciate any thoughts you have about how I can move this forwards.

Here is test rig.
https://drive.google.com/file/d/0B_YEgxccklmHanVzWlVEajhCcTA/view?usp=sharing

Please run up the KryoServer, then the KryoClient, which will send the looping message.

Thank you, Guys!

Stacktrace:
at com.esotericsoftware.kryo.io.ByteBufferOutput.require(ByteBufferOutput.java:189)
    at com.esotericsoftware.kryo.io.ByteBufferOutput.writeAscii_slow(ByteBufferOutput.java:552)
    at com.esotericsoftware.kryo.io.ByteBufferOutput.writeString(ByteBufferOutput.java:404)
    at com.esotericsoftware.kryo.serializers.UnsafeCacheFields$UnsafeStringField.write(UnsafeCacheFields.java:194)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:495)
    at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:599)
    at com.esotericsoftware.kryo.serializers.CollectionSerializer.write(CollectionSerializer.java:82)
    at com.esotericsoftware.kryo.serializers.CollectionSerializer.write(CollectionSerializer.java:22)

Nate

unread,
Jan 13, 2015, 12:12:47 PM1/13/15
to kryone...@googlegroups.com
Call "kryo.setReferences(true);" if you have circular dependencies.


--
You received this message because you are subscribed to the "kryonet-users" group.
http://groups.google.com/group/kryonet-users
---
You received this message because you are subscribed to the Google Groups "kryonet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kryonet-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

PaulRuss

unread,
Jan 13, 2015, 12:24:17 PM1/13/15
to kryone...@googlegroups.com
That did it! Thanks, Nate. Whole system is otherwise working like a charm!

Best,
Paul
Reply all
Reply to author
Forward
0 new messages