PEPeerTransportDebuggerpublic class PEPeerTransportDebugger extends Object
Fields Summary |
---|
protected int | piece_length | protected static final int | BT_READING_LENGTH_AND_TYPE | protected int | state | protected byte[] | data_read | protected int | data_read_pos |
Constructors Summary |
---|
protected PEPeerTransportDebugger(PEPeerTransportProtocol transport)
piece_length = transport.getControl().getPieceLength(0);
|
Methods Summary |
---|
protected void | analyse(java.nio.ByteBuffer buffer, int length)
byte[] data = new byte[length];
buffer.get(data);
for (int i=0;i<data.length;i++){
if ( data_read_pos == data_read.length ){
if ( state == BT_READING_LENGTH_AND_TYPE ){
ByteBuffer bb = ByteBuffer.wrap( data_read );
int len = bb.getInt();
state = bb.get();
//System.out.println( "Header: len = " + len + ", state = " + state );
if ( len == 1 ){
// messages with no body
//System.out.println( "msg:" + state );
state = BT_READING_LENGTH_AND_TYPE;
data_read = new byte[5];
}else{
data_read = new byte[len-1];
}
}else{
// messages with body
//System.out.println( "msg:" + state );
if ( state == 7 ){ //bt piece
ByteBuffer bb = ByteBuffer.wrap( data_read );
int piece_number = bb.getInt();
int piece_offset = bb.getInt();
long overall_offset = ((long)piece_number)*piece_length + piece_offset;
while(bb.hasRemaining()){
byte v = bb.get();
if ((byte)overall_offset != v ){
System.out.println( "piece: write is bad at " + overall_offset +
": expected = " + (byte)overall_offset + ", actual = " + v );
break;
}
overall_offset++;
}
}
state = BT_READING_LENGTH_AND_TYPE;
data_read = new byte[5];
}
data_read_pos = 0;
}
data_read[data_read_pos++] = data[i];
}
| public int | write(java.nio.channels.SocketChannel chan, java.nio.ByteBuffer buffer)
int pos = buffer.position();
int len = chan.write( buffer );
if ( len > 0 ){
buffer.position( pos );
analyse( buffer, len );
}
return( len );
| public long | write(java.nio.channels.SocketChannel chan, java.nio.ByteBuffer[] buffers, int array_offset, int array_length)
int[] pos = new int[buffers.length];
for (int i=array_offset;i<array_offset+array_length;i++){
pos[i] = buffers[i].position();
}
long len = chan.write( buffers, array_offset, array_length );
for (int i=array_offset;i<array_offset+array_length;i++){
ByteBuffer buffer = buffers[i];
int written = buffer.position() - pos[i];
if ( written > 0 ){
buffer.position( pos[i] );
analyse( buffer, written );
}
}
return( len );
|
|