public void messageSent(Message message) {
          if (message.getID().equals(BTMessage.ID_BT_PIECE)) {
            try {
              lock_mon.enter();

              // due to timing issues we can get in here with a message already removed

              queued_messages.remove(message);

            } finally {
              lock_mon.exit();
            }

            /*
            if ( peer.getIp().equals( "64.71.5.2" )){

            	outgoing_message_queue.setTrace( true );

            	// BTPiece p = (BTPiece)message;

            	// TimeFormatter.milliTrace( "obt sent: " + p.getPieceNumber() + "/" + p.getPieceOffset());
            }
            */

            doReadAheadLoads();
          }
        }
        public void messageQueued(Message message) {
          // if another message is going to be sent anyway, add our haves as well

          String message_id = message.getID();

          if (!(message_id.equals(BTMessage.ID_BT_HAVE)
              || message_id.equals(AZMessage.ID_AZ_HAVE))) {
            sendPendingHaves();
          }
        }
Ejemplo n.º 3
0
 @Override
 public boolean messageReceived(Message message) {
   logger.finest("incoming speed packet: " + message.getDescription());
   lastMessageTime = System.currentTimeMillis();
   if (incomingHandshake == null) {
     if (message instanceof OSF2FHandshake) {
       incomingHandshake = (OSF2FHandshake) message;
     } else {
       logger.warning("incoming connection not started with handshake");
       close();
     }
   } else if (message instanceof OSF2FChannelReset) {
     OSF2FChannelReset m = (OSF2FChannelReset) message;
     if (remoteLocalEstimate == -1) {
       remoteLocalEstimate = m.getChannelId();
     } else {
       remoteRemoteEstimate = m.getChannelId();
       logger.finer(
           "estimate of "
               + getRemoteIp()
               + " remote_local_estimate="
               + remoteLocalEstimate
               + " remote_remote="
               + remoteRemoteEstimate);
       close();
     }
   } else if ((message instanceof OSF2FChannelMsg)) {
     OSF2FChannelMsg m = (OSF2FChannelMsg) message;
     if (packetSize == 0) {
       packetSize = m.getMessageSize();
     }
     if (m.getMessageSize() != packetSize) {
       logger.warning("got different size payload packet");
       close();
       return true;
     }
     /*
      * ok all is good, send back a timestamp to the other side
      */
     int relTime = (int) (System.currentTimeMillis() - startTime);
     messageTimes.add(relTime);
     /*
      * we only need to send an int over, channel reset is the
      * smallest message (5+4 bytes), a bit of a hack but wth...
      */
     connection
         .getOutgoingMessageQueue()
         .addMessage(new OSF2FChannelReset(OSF2FMessage.CURRENT_VERSION, relTime), false);
   } else {
     logger.warning("incoming speed check connection unknown packet");
     close();
     return true;
   }
   return true;
 }
  public ByteBuffer destroy() {
    if (destroyed) {
      Debug.out("Trying to redestroy message decoder, stack trace follows: " + this);
      Debug.outStackTrace();
    }

    is_paused = true;
    destroyed = true;

    // there's a concurrency issue with the decoder whereby it can be destroyed while will being
    // messed with. Don't
    // have the energy to look into it properly atm so just try to ensure that it doesn't bork too
    // badly (parg: 29/04/2012)
    // only occasional but does have potential to generate direct buffer mem leak ;(

    int lbuff_read = 0;
    int pbuff_read = 0;
    length_buffer.limit(SS, 4);

    DirectByteBuffer plb = payload_buffer;

    if (reading_length_mode) {
      lbuff_read = length_buffer.position(SS);
    } else { // reading payload
      length_buffer.position(SS, 4);
      lbuff_read = 4;
      pbuff_read = plb == null ? 0 : plb.position(SS);
    }

    ByteBuffer unused = ByteBuffer.allocate(lbuff_read + pbuff_read); // TODO convert to direct?

    length_buffer.flip(SS);
    unused.put(length_buffer.getBuffer(SS));

    try {
      if (plb != null) {
        plb.flip(SS);
        unused.put(
            plb.getBuffer(
                SS)); // Got a buffer overflow exception here in the past - related to PEX?
      }
    } catch (RuntimeException e) {
      Debug.out("hit known threading issue");
    }

    unused.flip();

    length_buffer.returnToPool();

    if (plb != null) {
      plb.returnToPool();
      payload_buffer = null;
    }

    try {
      for (int i = 0; i < messages_last_read.size(); i++) {
        Message msg = (Message) messages_last_read.get(i);
        msg.destroy();
      }
    } catch (RuntimeException e) {
      // happens if messages modified by alt thread...
      Debug.out("hit known threading issue");
    }
    messages_last_read.clear();

    return unused;
  }