📄 multipeeruploader.java
字号:
return 0; //not allowed to write
}
HashMap connections_to_notify_of_exception = new HashMap();
ArrayList manual_notifications = new ArrayList();
int num_bytes_remaining = num_bytes_to_write;
try {
lists_lock.enter();
int num_unusable_connections = 0;
while( num_bytes_remaining > 0 && num_unusable_connections < ready_connections.size() ) {
NetworkConnectionBase conn = (NetworkConnectionBase)ready_connections.removeFirst();
if( !conn.getTransportBase().isReadyForWrite( waiter ) ) { //not yet ready for writing
ready_connections.addLast( conn ); //re-add to end as currently unusable
num_unusable_connections++;
continue; //move on to the next connection
}
int total_size = conn.getOutgoingMessageQueue().getTotalSize();
if( total_size < 1 ) { //oops, all messages have been removed
addToWaitingList( conn );
continue; //move on to the next connection
}
int mss_size = conn.getMssSize();
int num_bytes_allowed = num_bytes_remaining > mss_size ? mss_size : num_bytes_remaining; //allow a single full packet at most
int num_bytes_available = total_size > mss_size ? mss_size : total_size; //allow a single full packet at most
if( num_bytes_allowed >= num_bytes_available ) { //we're allowed enough (for either a full packet or to drain any remaining data)
int written = 0;
try {
written = conn.getOutgoingMessageQueue().deliverToTransport( num_bytes_available, true );
if( written > 0 ) {
manual_notifications.add( conn ); //register it for manual listener notification
}
boolean has_urgent_data = conn.getOutgoingMessageQueue().hasUrgentMessage();
int remaining = conn.getOutgoingMessageQueue().getTotalSize();
if( remaining >= mss_size || has_urgent_data ) { //still has a full packet's worth, or has urgent data
ready_connections.addLast( conn ); //re-add to end for further writing
num_unusable_connections = 0; //reset the unusable count so that it has a chance to try this connection again in the loop
}
else { //connection does not have enough for a full packet, so remove and place into waiting list
addToWaitingList( conn );
}
}
catch( Throwable e ) { //write exception, so move to waiting list while it waits for removal
if( AEDiagnostics.TRACE_CONNECTION_DROPS ) {
if( e.getMessage() == null ) {
Debug.out( "null write exception message: ", e );
}
else {
if( e.getMessage().indexOf( "An existing connection was forcibly closed by the remote host" ) == -1 &&
e.getMessage().indexOf( "Connection reset by peer" ) == -1 &&
e.getMessage().indexOf( "Broken pipe" ) == -1 &&
e.getMessage().indexOf( "An established connection was aborted by the software in your host machine" ) == -1 ) {
System.out.println( "MP: write exception [" +conn.getTransportBase().getDescription()+ "]: " +e.getMessage() );
}
}
}
connections_to_notify_of_exception.put( conn, e ); //do exception notification outside of sync'd block
addToWaitingList( conn );
}
num_bytes_remaining -= written;
}
else { //we're not allowed enough to maximize the packet payload
ready_connections.addLast( conn ); //re-add to end as currently unusable
num_unusable_connections++;
continue; //move on to the next connection
}
}
}
finally {
lists_lock.exit();
}
//manual queue listener notifications
for( int i=0; i < manual_notifications.size(); i++ ) {
NetworkConnectionBase conn = (NetworkConnectionBase)manual_notifications.get( i );
conn.getOutgoingMessageQueue().doListenerNotifications();
}
//exception notifications
for( Iterator i = connections_to_notify_of_exception.entrySet().iterator(); i.hasNext(); ) {
Map.Entry entry = (Map.Entry)i.next();
NetworkConnectionBase conn = (NetworkConnectionBase)entry.getKey();
Throwable exception = (Throwable)entry.getValue();
conn.notifyOfException( exception );
}
int num_bytes_written = num_bytes_to_write - num_bytes_remaining;
if( num_bytes_written > 0 ) {
rate_handler.bytesProcessed( num_bytes_written );
}
return num_bytes_written;
}
/**
* Does this entity have data ready for writing.
* @return true if it has data to send, false if empty
*/
/*
public boolean hasWriteDataAvailable() {
if( ready_connections.isEmpty() ) return false;
return true;
}
*/
private static class PeerData {
private OutgoingMessageQueue.MessageQueueListener queue_listener;
private long last_message_added_time;
}
public long
getBytesReadyToWrite()
{
long total = 0;
try {
lists_lock.enter();
for( Iterator i = waiting_connections.keySet().iterator(); i.hasNext(); ) {
NetworkConnectionBase conn = (NetworkConnectionBase)i.next();
total += conn.getOutgoingMessageQueue().getTotalSize();
}
for( Iterator i = ready_connections.iterator(); i.hasNext(); ) {
NetworkConnectionBase conn = (NetworkConnectionBase)i.next();
total += conn.getOutgoingMessageQueue().getTotalSize();
}
}finally{
lists_lock.exit();
}
return( total );
}
public int
getConnectionCount()
{
return( waiting_connections.size() + ready_connections.size());
}
public int
getReadyConnectionCount(
EventWaiter waiter )
{
int total = 0;
try {
lists_lock.enter();
for( Iterator i = waiting_connections.keySet().iterator(); i.hasNext(); ) {
NetworkConnectionBase conn = (NetworkConnectionBase)i.next();
if ( conn.getTransportBase().isReadyForWrite(waiter)){
total++;
}
}
for( Iterator i = ready_connections.iterator(); i.hasNext(); ) {
NetworkConnectionBase conn = (NetworkConnectionBase)i.next();
if ( conn.getTransportBase().isReadyForWrite(waiter)){
total++;
}
}
}finally{
lists_lock.exit();
}
return( total );
}
//////////////// RateControlledWriteEntity implementation ////////////////////
public boolean canProcess( EventWaiter waiter ) {
flushCheck(); //since this method is called repeatedly from a loop, we can use it to check flushes
if( ready_connections.isEmpty() ) return false; //no data to send
if( rate_handler.getCurrentNumBytesAllowed() < 1/*NetworkManager.getTcpMssSize()*/ ) return false;
return true;
}
public boolean doProcessing( EventWaiter waiter ) {
int num_bytes_allowed = rate_handler.getCurrentNumBytesAllowed();
if( num_bytes_allowed < 1 ) return false;
return write( waiter, num_bytes_allowed ) > 0 ? true : false;
}
public int getPriority() {
return RateControlledEntity.PRIORITY_HIGH;
}
public String
getString()
{
String str = "can_process=" + canProcess(null) + ",bytes_allowed=" + rate_handler.getCurrentNumBytesAllowed() + ", waiting ";
try {
lists_lock.enter();
for( Iterator i = waiting_connections.keySet().iterator(); i.hasNext(); ) {
NetworkConnectionBase conn = (NetworkConnectionBase)i.next();
str += "," + conn.getString();
}
str += ": ready ";
for( Iterator i = ready_connections.iterator(); i.hasNext(); ) {
NetworkConnectionBase conn = (NetworkConnectionBase)i.next();
str += "," + conn.getString();
}
}finally{
lists_lock.exit();
}
return( "MPU: " + str );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -