I'm having a strange problem and I hope that I'm not trying to do anything stupid here.
What I am trying to do is just send a String to the SGS and then echo the ByteBuffer back out on a channel to all the connected clients (just one at the moment).
What I am finding is if once the data is recieved at the server, I get all the bytes out of the ByteBuffer and then send the ByteBuffer on, the information is sent back to the client correctly. If I just send the ByteBuffer on, the information dosen't get sent correctly to the clients (the ByteBuffer that is recieved dosn't contain any data, capacity = 0).
To send the data I am doing this:
1 2 3
| ByteBuffer buffer = ByteBuffer.allocate(message.length()); buffer.put(message.getBytes()); toServer.sendData(buffer); |
toServer.sendData does this:
1 2 3
| public void sendData(ByteBuffer bytes) { mgr.sendToServer(bytes, true); } |
at the recieving end I am doing this:
1 2 3 4 5 6 7 8 9 10
| public void dataArrived(byte[] from, ByteBuffer data, boolean reliable) { int remaining = data.remaining(); System.out.println("remaining: " + remaining + " capacity: " + data.capacity()); byte [] message = new byte[remaining]; data.get(message); System.out.println("raw message is: " + new String(message)); } |
and in the server I am doing this:
1 2 3 4 5 6 7 8 9 10
| public void userDataReceived(UserID from, ByteBuffer data) { SimTask task = SimTask.getCurrent(); byte[] dummy = new byte[data.remaining()]; data.get(dummy); for(UserID id : users) { task.sendData(gameChannel, id, data, true); } } |
If I don't have the two lines mentioned in the commentsthere I get a capacity 0 ByteBuffer at the client. I did try just doing a data.get(); and then sending the buffer on but that way I only got the first charceter of my message.
users is a list that I add clients too as they connect
1 2 3 4 5 6 7 8 9
| public void userJoined(UserID uid, Subject subject) { Set<Principal> principals = subject.getPrincipals(); Principal principal = principals.iterator().next(); System.err.println("User Joined server (P): " + uid + " (" + principal.getName() + ")"); users.add(uid); SimTask.getCurrent().addUserDataListener(uid, thisobj); } |
Is there something really obvious that I'm missing here?
Thanks in advance,
Dan.