On 04/25/2012 05:00 AM,
kana...@gmail.com wrote:
> Merci NotMe, mais j'aimerais le faire soit en C avec JNI soit en Java mais sans JNA ... bref, je crois que je suis bien partis pour tout faire avec JNI à la main ...
Ca n'est pas tres complique a faire en pure Java, en utilisant java.nio.* et
java.nio.channels.*
Les classes dans java.nio.channels sont utilises pour recevoir les donnes dans
un objet de type ByteBuffer. Ensuite c'est simple de parser le contenu d'un
ByteBuffer. Voila un exemple partiel pour RELOAD[1] (code under AGPL3):
void parse(ByteBuffer buffer)
throws IOException {
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
long reloToken = buffer.getInt() & 0xFFFFFFFFl;
if (reloToken != 0xd2454c4fl) {
throw new IOException("relo_token invalid");
}
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
byte[] overlay = new byte[4];
buffer.get(overlay);
if (buffer.remaining() < 2) {
throw new IOException("Buffer too small");
}
int sequence = buffer.getShort() & 0xFFFF;
if (buffer.remaining() < 1) {
throw new IOException("Buffer too small");
}
int version = buffer.get() & 0xFF;
if (buffer.remaining() < 1) {
throw new IOException("Buffer too small");
}
short ttl = (short)(buffer.get() & 0xFF);
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
long fragment = buffer.getInt() & 0xFFFFFFFFl;
boolean last = (fragment & 0x40000000) == 0x40000000;
int offset = (int)(fragment & 0xFFFFFF);
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
long length = buffer.getInt() & 0xFFFFFFFFl;
if (buffer.remaining() < length - 20) {
throw new IOException("Buffer too small");
}
if (buffer.remaining() < 8) {
throw new IOException("Buffer too small");
}
long transactionId = buffer.getLong();
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
long maxResponseLength = buffer.getInt() & 0xFFFFFFFFl;
if (buffer.remaining() < 2) {
throw new IOException("Buffer too small");
}
int viaListLength = buffer.getShort() & 0xFFFF;
if (buffer.remaining() < 2) {
throw new IOException("Buffer too small");
}
int destinationListLength = buffer.getShort() & 0xFFFF;
if (buffer.remaining() < 2) {
throw new IOException("Buffer too small");
}
int optionsLength = buffer.getShort() & 0xFFFF;
if (buffer.remaining() < viaListLength) {
throw new IOException("Buffer too small");
}
int limit = buffer.limit();
buffer.limit(buffer.position() + viaListLength);
List<Address> vias = viaListLength > 0 ? Address.getAllByName(buffer) : new
ArrayList<Address>();
buffer.limit(limit);
if (buffer.remaining() < destinationListLength) {
throw new IOException("Buffer too small");
}
limit = buffer.limit();
buffer.limit(buffer.position() + destinationListLength);
List<Address> destinations = destinationListLength > 0 ?
Address.getAllByName(buffer) : new ArrayList<Address>();
buffer.limit(limit);
if (buffer.remaining() < optionsLength) {
throw new IOException("Buffer too small");
}
buffer.position(buffer.position() + optionsLength);
}
[1]
https://www.ietf.org/id/draft-ietf-p2psip-base-21.txt