⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vncinstream.cpp

📁 vncviewer_source_1.0
💻 CPP
字号:
/* Copyright (C) 2006 Lucas Gomez  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// INCLUDE FILES
#include "VncInStream.h"
#include "VncViewer.pan"

//LITERALS

//CONSTANTS

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// VncInStream::VncInStream()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
VncInStream::VncInStream(MStreamBuf* aIS,TInt aBufSize)
{
    iIS=aIS;
    iBufSize = aBufSize;
    iBuf.CreateMaxL(iBufSize);
    iPtr=iEnd=iPtrOffset=0;
    iTimeWaitedIn100us=5;
    iTimedKBits=0;
}

// ---------------------------------------------------------------------------
// VncInStream::~VncInStream()
// Destructor.
// ---------------------------------------------------------------------------
//
VncInStream::~VncInStream()
{

}

void VncInStream::ReadBytes(RBuf8& aData,TInt aOffset,TInt aLength)
{
	if(aLength<VncInConstants::KMinBulkSize) 
	{
      	InStream::ReadBytes(aData,aOffset,aLength);
      	return;
    }

    TInt n=iEnd-iPtr;
    if(n>aLength) n=aLength;

    aData.MidTPtr(aOffset).Copy(iBuf.Mid(iPtr,n));
    aOffset+=n;
    aLength-=n;
    iPtr+=n;

    while(aLength>0) 
    {
      	n=Read(aData,aOffset,aLength);
      	aOffset+=n;
      	aLength-=n;
      	iPtrOffset+=n;
    }
}
	
TInt VncInStream::Pos()
{
	return iPtrOffset+iPtr;
}

void VncInStream::StartTiming()
{
	iTiming=ETrue;

    // Carry over up to 1s worth of previous rate for smoothing.

    if(iTimeWaitedIn100us>10000) 
    {
      	iTimedKBits=iTimedKBits*10000/iTimeWaitedIn100us;
      	iTimeWaitedIn100us=10000;
    }
}

void VncInStream::StopTiming()
{
	iTiming=EFalse; 
    if(iTimeWaitedIn100us<iTimedKBits/2)
      	iTimeWaitedIn100us=iTimedKBits/2; // upper limit 20Mbit/s
}

TInt64 VncInStream::KBitsPerSecond()
{
	return iTimedKBits*10000/iTimeWaitedIn100us;
}

TInt64 VncInStream::TimeWaited()
{
	return iTimeWaitedIn100us;
}

TInt VncInStream::Overrun(TInt aItemSize,TInt aNItems)
{
	if(aItemSize>iBufSize)
      	User::Leave(EVncViewerVncInStreamOverrun);

    if(iEnd-iPtr!=0)
    {
    	iBuf.Copy(iBuf.Mid(iPtr,iEnd-iPtr));
    	iBuf.SetMax();
    }

    iPtrOffset+=iPtr;
    iEnd-=iPtr;
    iPtr=0;

    while(iEnd<aItemSize) 
    {
      	TInt n=Read(iBuf,iEnd,iBufSize-iEnd);
      	iEnd+=n;
    }

    if (aItemSize*aNItems>iEnd)
      	aNItems=iEnd/aItemSize;

    return aNItems;
}

TInt VncInStream::Read(RBuf8& aBuf,TInt aOffset,TInt aLength)
{
	TTime before;
	if(iTiming)
		before.HomeTime();
	
	TPtr8 temp(NULL,0);
	temp.Set(aBuf.MidTPtr(aOffset,aLength));
	TRequestStatus status;
	TInt n=iIS->ReadL(temp,aLength,status);
	if(n==0) User::Leave(EVncViewerEndOfStream);

	if(iTiming)
	{
		TTime after;
		after.HomeTime();
		TInt64 newTimeWaited=after.MicroSecondsFrom(before).Int64()/100;
		int newKBits=n*8/1000;

		// limit rate to between 10kbit/s and 40Mbit/s

		if(newTimeWaited>newKBits*1000) newTimeWaited=newKBits*1000;
		if(newTimeWaited<newKBits/4)    newTimeWaited=newKBits/4;

		iTimeWaitedIn100us+=newTimeWaited;
		iTimedKBits+=newKBits;
	}

	return n;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -