Changeset 2025

Show
Ignore:
Timestamp:
12/18/07 08:30:57 (3 years ago)
Author:
dugwyler
Message:

Low-priority packet limiting functionality. Chat packets are always sent last in a cluster, and the number that can be sent per cluster can now be limited. This should prevent server recycles when the cap is set up properly, at the cost of a small delay in messages sent when there are a great deal being sent out. Note that the cap is by default extremely high (100 chat packets allowed every 75ms) so it should not affect normal bot operations in any way. Bots needing to utilize the functionality to its fullest should call BotAction's setLowPriorityPacketCap(int).

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/twcore/src/twcore/core/net/GamePacketGenerator.java

    r1676 r2025  
    1616public class GamePacketGenerator { 
    1717 
    18     private Timer                 m_timer;          // Schedules clustered packets 
    19     private TimerTask             m_timerTask;      // Clustered packet send task 
    20     private List<ByteArray>       m_messageList;    // Msgs waiting for clustered send 
    21     private SSEncryption          m_ssEncryption;   // Encryption class 
    22     private Sender                m_outboundQueue;  // Outgoing packet queue 
    23     private int                   m_serverTimeDifference;   // Diff (*tinfo) 
    24     private ReliablePacketHandler m_reliablePacketHandler;  // Handles reliable sends 
    25  
    26     private long                  m_sendDelay = 75; // Delay between cluster sends 
     18    private Timer                  m_timer;          // Schedules clustered packets 
     19    private TimerTask              m_timerTask;      // Clustered packet send task 
     20    private DelayedPacketList<ByteArray> m_messageList;  // Packets waiting to be sent in clusters 
     21    private SSEncryption           m_ssEncryption;   // Encryption class 
     22    private Sender                 m_outboundQueue;  // Outgoing packet queue 
     23    private int                    m_serverTimeDifference;   // Diff (*tinfo) 
     24    private ReliablePacketHandler  m_reliablePacketHandler;  // Handles reliable sends 
     25 
     26    private long                   m_sendDelay = 75;         // Delay between packet sends 
     27    private final static int       DEFAULT_PACKET_CAP = 100; // Default # low-priority packets 
     28                                                             // allowed per clustered send 
    2729 
    2830    /** 
     
    3739        m_ssEncryption = ssEncryption; 
    3840        m_outboundQueue = outboundQueue; 
    39         m_messageList = Collections.synchronizedList(new LinkedList<ByteArray>()); 
     41        m_messageList = new DelayedPacketList<ByteArray>( DEFAULT_PACKET_CAP ); 
    4042 
    4143        m_timerTask = new TimerTask(){ 
     44            int size; 
    4245            public void run(){ 
    43                 int         size; 
    4446 
    4547                synchronized (m_messageList) { 
    4648                    size = m_messageList.size(); 
    4749                    if( size == 1 ){ 
    48                         sendReliableMessage( m_messageList.remove(0) ); 
     50                        sendReliableMessage( m_messageList.getNextPacket() ); 
    4951                    } else if( size > 1 ){ 
    5052                        sendClusteredPacket(); 
     
    7577 
    7678    /** 
     79     * Sets the max number of lower-priority (aka chat) packets that can 
     80     * be sent per clustered send. 
     81     * @param cap Max # lower-priority packets allowed to be sent per clustered send 
     82     */ 
     83    public void setPacketCap( int cap ){ 
     84        m_messageList.setPacketCap( cap ); 
     85    } 
     86 
     87    /** 
    7788     * Adds a packet to the standard outgoing queue. 
    7889     * @param array Packet to add 
     
    8091    public void composePacket( int[] array ){ 
    8192 
    82         m_messageList.add( new ByteArray( array ) ); 
     93        m_messageList.addNormalPacket( new ByteArray( array ) ); 
    8394    } 
    8495 
     
    89100    public void composePacket( byte[] array ){ 
    90101 
    91         m_messageList.add( new ByteArray( array ) ); 
     102        m_messageList.addNormalPacket( new ByteArray( array ) ); 
    92103    } 
    93104 
     
    98109    public void composePacket( ByteArray bytearray ){ 
    99110 
    100         m_messageList.add( bytearray ); 
     111        m_messageList.addNormalPacket( bytearray ); 
     112    } 
     113 
     114    /** 
     115     * Adds a packet to the outgoing queue with low priority.  The number of 
     116     * such packets allowed to be transmitted out per clustered send is capped, 
     117     * and can be set with setPacketCap(int). 
     118     * @param bytearray Packet to add 
     119     * @see #setPacketCap(int) 
     120     */ 
     121    public void composeLowPriorityPacket( ByteArray bytearray ){ 
     122 
     123        m_messageList.addCappedPacket( bytearray ); 
     124    } 
     125 
     126    /** 
     127     * Adds a packet to the outgoing queue with low priority.  The number of 
     128     * such packets allowed to be transmitted out per clustered send is capped, 
     129     * and can be set with setPacketCap(int). 
     130     * @param array Packet to add 
     131     * @see #setPacketCap(int) 
     132     */ 
     133    public void composeLowPriorityPacket( int[] array ){ 
     134 
     135        m_messageList.addCappedPacket( new ByteArray( array ) ); 
     136    } 
     137 
     138    /** 
     139     * Adds a packet to the outgoing queue with low priority.  The number of 
     140     * such packets allowed to be transmitted out per clustered send is capped, 
     141     * and can be set with setPacketCap(int). 
     142     * @param array Packet to add 
     143     * @see #setPacketCap(int) 
     144     */ 
     145    public void composeLowPriorityPacket( byte[] array ){ 
     146 
     147        m_messageList.addCappedPacket( new ByteArray( array ) ); 
    101148    } 
    102149 
     
    104151     * Adds a packet to the immediate outgoing queue, ignoring any other packets 
    105152     * in the standard queue it could be clustered with.  Fast but not as efficient. 
     153     * Packets sent in this manner are not sent reliably. 
    106154     * @param array Packet to add 
    107155     */ 
     
    114162     * Adds a packet to the immediate outgoing queue, ignoring any other packets 
    115163     * in the standard queue it could be clustered with.  Fast but not as efficient. 
     164     * Packets sent in this manner are not sent reliably. 
    116165     * @param array Packet to add 
    117166     */ 
     
    124173     * Adds a packet to the immediate outgoing queue, ignoring any other packets 
    125174     * in the standard queue it could be clustered with.  Fast but not as efficient. 
     175     * Packets sent in this manner are not sent reliably. 
    126176     * @param bytearray Packet to add 
    127177     * @param size Size of packet 
     
    135185     * Adds a packet to the immediate high-priority outgoing queue, ignoring any 
    136186     * other packets in the standard queue it could be clustered with and placing 
    137      * it at the head of the immediate queue. 
     187     * it at the head of the immediate queue.  Packets sent in this manner are not 
     188     * sent reliably. 
    138189     * @param array Packet to add 
    139190     */ 
     
    146197     * Adds a packet to the immediate high-priority outgoing queue, ignoring any 
    147198     * other packets in the standard queue it could be clustered with and placing 
    148      * it at the head of the immediate queue. 
     199     * it at the head of the immediate queue.  Packets sent in this manner are not 
     200     * sent reliably. 
    149201     * @param array Packet to add 
    150202     */ 
     
    157209     * Adds a packet to the immediate high-priority outgoing queue, ignoring any 
    158210     * other packets in the standard queue it could be clustered with and placing 
    159      * it at the head of the immediate queue. 
     211     * it at the head of the immediate queue.  Packets sent in this manner are not 
     212     * sent reliably. 
    160213     * @param bytearray Packet to add 
    161214     * @param size Size of packet 
     
    285338        sendReliableMessage( bytearray ); 
    286339    } 
    287      
    288     /** 
    289      * Sends the registration data if the server asks for it. This information is mostly build up  
     340 
     341    /** 
     342     * Sends the registration data if the server asks for it. This information is mostly build up 
    290343     * from the properties of setup.cfg ([registration] tag). The registry variables are replaced by "TWCore" 
    291344     * since we want to keep TWCore cross-platform. 
    292      *  
     345     * 
    293346     * @param realname Real name 
    294347     * @param email E-mail address 
     
    333386                        726             40              System\CurrentControlSet\Services\Class\MEDIA\0004 
    334387                */ 
    335          
     388 
    336389        ByteArray bytearray = new ByteArray(766); 
    337          
     390 
    338391        bytearray.addByte( 0x17 );                                              // Type 
    339392        bytearray.addPaddedString(realname, 32);                // Real name 
     
    364417        bytearray.addPaddedString("TWCore", 40);                //                              ...\MEDIA\0003 
    365418        bytearray.addPaddedString("TWCore", 40);                //                              ...\MEDIA\0004 
    366          
     419 
    367420        this.sendMassiveChunkPacket( bytearray ); 
    368421    } 
     
    473526        bytearray.addByte( 0x00 );          // Terminator 
    474527 
    475         composePacket( bytearray ); 
     528        composeLowPriorityPacket( bytearray ); 
    476529    } 
    477530 
     
    495548            // We might need to send more than one packet if the total of all messages 
    496549            // is longer than 500 bytes. 
     550            m_messageList.resetCap(); 
     551 
    497552            while( done == false ){ 
    498553 
    499554                // We now need to add as many as we can before we fill up the 500 bytes. 
    500                 if( m_messageList.size() > 0 ){ 
    501                     tempMessage = m_messageList.remove(0); 
     555                if( m_messageList.cappedSize() > 0 ){ 
     556                    tempMessage = m_messageList.getNextPacket(); 
    502557                    nextSize = tempMessage.size(); 
    503558                } else { 
     
    511566                    bytearray.addByteArray( tempMessage ); 
    512567 
    513                     if( m_messageList.size() > 0 ){ 
    514                         tempMessage = m_messageList.remove(0); 
     568                    if( m_messageList.cappedSize() > 0 ){ 
     569                        tempMessage = m_messageList.getNextPacket(); 
    515570                        sizeLeft -= nextSize + 1; 
    516571                        nextSize = tempMessage.size(); 
     
    717772    } 
    718773 
     774    /** 
     775     * Implementation to send out most packets as normal, while placing a cap 
     776     * on the number of less-important packets (generally messages) that are 
     777     * sent out per run of the cluster packet composition timer. 
     778     * @author dugwyler 
     779     * @param <E> 
     780     */ 
     781    private class DelayedPacketList<E> { 
     782        private List<E> m_normalPacketList; 
     783        private List<E> m_cappedPacketList; 
     784        private int m_packetCap; 
     785        private int m_remainingCap; 
     786 
     787        public DelayedPacketList( int packetCap ) { 
     788            m_normalPacketList = Collections.synchronizedList(new LinkedList<E>()); 
     789            m_cappedPacketList = Collections.synchronizedList(new LinkedList<E>()); 
     790            m_packetCap = packetCap; 
     791            m_remainingCap = packetCap; 
     792        } 
     793 
     794        public void setPacketCap( int packetCap ) { 
     795            m_packetCap = packetCap; 
     796        } 
     797 
     798        public void resetCap() { 
     799            m_remainingCap = m_packetCap; 
     800        } 
     801 
     802        public void addNormalPacket( E packet ) { 
     803            m_normalPacketList.add( packet ); 
     804        } 
     805 
     806        public void addCappedPacket( E packet ) { 
     807            m_cappedPacketList.add( packet ); 
     808        } 
     809 
     810        public E getNextPacket( ) { 
     811            if( m_normalPacketList.isEmpty() == false ) 
     812                return m_normalPacketList.remove( 0 ); 
     813            else { 
     814                m_remainingCap--; 
     815                return m_cappedPacketList.remove( 0 ); 
     816            } 
     817        } 
     818 
     819        public int size() { 
     820            return m_normalPacketList.size() + m_cappedPacketList.size(); 
     821        } 
     822 
     823        public int cappedSize() { 
     824            return m_normalPacketList.size() + Math.min( m_cappedPacketList.size(), m_packetCap ); 
     825        } 
     826    } 
    719827}