Changeset 2025
- Timestamp:
- 12/18/07 08:30:57 (3 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/twcore/src/twcore/core/net/GamePacketGenerator.java
r1676 r2025 16 16 public class GamePacketGenerator { 17 17 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 27 29 28 30 /** … … 37 39 m_ssEncryption = ssEncryption; 38 40 m_outboundQueue = outboundQueue; 39 m_messageList = Collections.synchronizedList(new LinkedList<ByteArray>());41 m_messageList = new DelayedPacketList<ByteArray>( DEFAULT_PACKET_CAP ); 40 42 41 43 m_timerTask = new TimerTask(){ 44 int size; 42 45 public void run(){ 43 int size;44 46 45 47 synchronized (m_messageList) { 46 48 size = m_messageList.size(); 47 49 if( size == 1 ){ 48 sendReliableMessage( m_messageList. remove(0) );50 sendReliableMessage( m_messageList.getNextPacket() ); 49 51 } else if( size > 1 ){ 50 52 sendClusteredPacket(); … … 75 77 76 78 /** 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 /** 77 88 * Adds a packet to the standard outgoing queue. 78 89 * @param array Packet to add … … 80 91 public void composePacket( int[] array ){ 81 92 82 m_messageList.add ( new ByteArray( array ) );93 m_messageList.addNormalPacket( new ByteArray( array ) ); 83 94 } 84 95 … … 89 100 public void composePacket( byte[] array ){ 90 101 91 m_messageList.add ( new ByteArray( array ) );102 m_messageList.addNormalPacket( new ByteArray( array ) ); 92 103 } 93 104 … … 98 109 public void composePacket( ByteArray bytearray ){ 99 110 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 ) ); 101 148 } 102 149 … … 104 151 * Adds a packet to the immediate outgoing queue, ignoring any other packets 105 152 * in the standard queue it could be clustered with. Fast but not as efficient. 153 * Packets sent in this manner are not sent reliably. 106 154 * @param array Packet to add 107 155 */ … … 114 162 * Adds a packet to the immediate outgoing queue, ignoring any other packets 115 163 * in the standard queue it could be clustered with. Fast but not as efficient. 164 * Packets sent in this manner are not sent reliably. 116 165 * @param array Packet to add 117 166 */ … … 124 173 * Adds a packet to the immediate outgoing queue, ignoring any other packets 125 174 * in the standard queue it could be clustered with. Fast but not as efficient. 175 * Packets sent in this manner are not sent reliably. 126 176 * @param bytearray Packet to add 127 177 * @param size Size of packet … … 135 185 * Adds a packet to the immediate high-priority outgoing queue, ignoring any 136 186 * 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. 138 189 * @param array Packet to add 139 190 */ … … 146 197 * Adds a packet to the immediate high-priority outgoing queue, ignoring any 147 198 * 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. 149 201 * @param array Packet to add 150 202 */ … … 157 209 * Adds a packet to the immediate high-priority outgoing queue, ignoring any 158 210 * 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. 160 213 * @param bytearray Packet to add 161 214 * @param size Size of packet … … 285 338 sendReliableMessage( bytearray ); 286 339 } 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 290 343 * from the properties of setup.cfg ([registration] tag). The registry variables are replaced by "TWCore" 291 344 * since we want to keep TWCore cross-platform. 292 * 345 * 293 346 * @param realname Real name 294 347 * @param email E-mail address … … 333 386 726 40 System\CurrentControlSet\Services\Class\MEDIA\0004 334 387 */ 335 388 336 389 ByteArray bytearray = new ByteArray(766); 337 390 338 391 bytearray.addByte( 0x17 ); // Type 339 392 bytearray.addPaddedString(realname, 32); // Real name … … 364 417 bytearray.addPaddedString("TWCore", 40); // ...\MEDIA\0003 365 418 bytearray.addPaddedString("TWCore", 40); // ...\MEDIA\0004 366 419 367 420 this.sendMassiveChunkPacket( bytearray ); 368 421 } … … 473 526 bytearray.addByte( 0x00 ); // Terminator 474 527 475 compose Packet( bytearray );528 composeLowPriorityPacket( bytearray ); 476 529 } 477 530 … … 495 548 // We might need to send more than one packet if the total of all messages 496 549 // is longer than 500 bytes. 550 m_messageList.resetCap(); 551 497 552 while( done == false ){ 498 553 499 554 // 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(); 502 557 nextSize = tempMessage.size(); 503 558 } else { … … 511 566 bytearray.addByteArray( tempMessage ); 512 567 513 if( m_messageList. size() > 0 ){514 tempMessage = m_messageList. remove(0);568 if( m_messageList.cappedSize() > 0 ){ 569 tempMessage = m_messageList.getNextPacket(); 515 570 sizeLeft -= nextSize + 1; 516 571 nextSize = tempMessage.size(); … … 717 772 } 718 773 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 } 719 827 }
