Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 25, 2006, 1:27:00 PM (18 years ago)
Author:
rennerc
Message:

compression implemented

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/network_stream.cc

    r7809 r7851  
    3030#include "shared_network_data.h"
    3131#include "message_manager.h"
     32#include "preferences.h"
     33#include "zip.h"
     34
     35#include "src/lib/util/loading/resource_manager.h"
    3236
    3337#include "network_log.h"
     
    8892  myHostId = 0;
    8993  currentState = 0;
     94 
     95  remainingBytesToWriteToDict = Preferences::getInstance()->getInt( "compression", "writedict", 0 );
     96 
     97  assert( Zip::getInstance()->loadDictionary( "testdict" ) );
    9098}
    9199
     
    365373void NetworkStream::handleUpstream( )
    366374{
    367   byte buf[UDP_PACKET_SIZE];
    368375  int offset;
    369376  int n;
     
    430437    assert( Converter::intToByteArray( offset, buf, INTSIZE ) == INTSIZE );
    431438   
    432     assert( peer->second.socket->writePacket( buf, offset ) );
     439    int compLength = Zip::getInstance()->zip( buf, offset, compBuf, UDP_PACKET_SIZE );
     440   
     441    if ( compLength < 0 )
     442    {
     443      PRINTF(1)("compression failed!\n");
     444      continue;
     445    }
     446   
     447    assert( peer->second.socket->writePacket( compBuf, compLength ) );
     448   
     449    if ( this->remainingBytesToWriteToDict > 0 )
     450      writeToNewDict( buf, offset );
    433451   
    434452    peer->second.connectionMonitor->processUnzippedOutgoingPacket( buf, offset, currentState );
     453    peer->second.connectionMonitor->processZippedOutgoingPacket( compBuf, compLength, currentState );
    435454   
    436455    NETPRINTF(n)("send packet: %d userId = %d\n", offset, peer->second.userId);
     
    443462void NetworkStream::handleDownstream( )
    444463{
    445   byte buf[UDP_PACKET_SIZE];
    446464  int offset = 0;
    447465 
    448466  int length = 0;
    449467  int packetLength = 0;
     468  int compLength = 0;
    450469  int uniqueId = 0;
    451470  int state = 0;
     
    460479      continue;
    461480
    462     while ( 0 < (packetLength = peer->second.socket->readPacket( buf, UDP_PACKET_SIZE )) )
    463     {
     481    while ( 0 < (compLength = peer->second.socket->readPacket( compBuf, UDP_PACKET_SIZE )) )
     482    {
     483      packetLength = Zip::getInstance()->unZip( compBuf, compLength, buf, UDP_PACKET_SIZE );
     484     
    464485      if ( packetLength < 4*INTSIZE )
    465486      {
     
    468489        continue;
    469490      }
     491     
     492      if ( this->remainingBytesToWriteToDict > 0 )
     493        writeToNewDict( buf, packetLength );
    470494   
    471495      assert( Converter::byteArrayToInt( buf, &length ) == INTSIZE );
     
    586610      }
    587611     
     612      peer->second.connectionMonitor->processZippedIncomingPacket( compBuf, compLength, currentState, ackedState );
    588613      peer->second.connectionMonitor->processUnzippedIncomingPacket( buf, offset, currentState, ackedState );
    589614   
     
    629654}
    630655
    631 
    632 
    633 
    634 
    635 
     656/**
     657 * writes data to DATA/dicts/newdict
     658 * @param data pointer to data
     659 * @param length length
     660 */
     661void NetworkStream::writeToNewDict( byte * data, int length )
     662{
     663  if ( remainingBytesToWriteToDict <= 0 )
     664    return;
     665 
     666  if ( length > remainingBytesToWriteToDict )
     667    length = remainingBytesToWriteToDict;
     668 
     669  std::string fileName = ResourceManager::getInstance()->getDataDir();
     670  fileName += "/dicts/newdict";
     671 
     672  FILE * f = fopen( fileName.c_str(), "a" );
     673 
     674  if ( !f )
     675  {
     676    PRINTF(2)("could not open %s\n", fileName.c_str());
     677    remainingBytesToWriteToDict = 0;
     678    return;
     679  }
     680 
     681  if ( fwrite( data, 1, length, f ) != length )
     682  {
     683    PRINTF(2)("could not write to file\n");
     684    fclose( f );
     685    return;
     686  }
     687 
     688  fclose( f );
     689 
     690  remainingBytesToWriteToDict -= length; 
     691}
     692
     693
     694
     695
     696
     697
Note: See TracChangeset for help on using the changeset viewer.