📄 teamviewerinfo.cpp
字号:
// Copyright (C) 2006 Teamviewer GmbH. All Rights Reserved.
//
// This file is part of the TeamViewer system.
//
// TeamViewer 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 program 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 program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for TeamViewer is not available from the place
// whence you received this file, check http://www.teamviewer.com
#include "stdhdrs.h"
#include "teamviewerinfo.h"
std::map<CARD16,std::string> TeamviewerInfo::localinfo;
TeamviewerInfo::TeamviewerInfo(ObjectType ot, void *o)
{
buffer = NULL;
infolen = 0;
object = o;
objecttype = ot;
partnerIsTeamviewer = false;
}
TeamviewerInfo::~TeamviewerInfo(void)
{
if(buffer)
free(buffer);
}
// since VNC-Client and VNC-Server use different functions to read/write sockets
// we check here which object we are in and call the appropriate function
bool TeamviewerInfo::readfunc(char *buf, int len)
{
switch(objecttype)
{
case TI_VNCCLIENT:
vncClientThread *vnc;
vnc = static_cast<vncClientThread*>(object);
return vnc->readSocket(buf,len);
case TI_CLIENTCONNECTION:
ClientConnection *cc;
cc = static_cast<ClientConnection*>(object);
cc->ReadExact(buf, len);
return true;
default: return false;
}
}
bool TeamviewerInfo::writefunc(char *buf, int len)
{
switch(objecttype)
{
case TI_VNCCLIENT:
vncClientThread *vnc;
vnc = static_cast<vncClientThread*>(object);
if(vnc->m_client->m_encodemgr.m_buffer)
{
omni_mutex_lock l(vnc->m_client->GetUpdateLock());
return vnc->writeSocket(buf, len);
}
else
return vnc->writeSocket(buf, len);
case TI_CLIENTCONNECTION:
ClientConnection *cc;
cc = static_cast<ClientConnection*>(object);
cc->WriteExact(buf, len);
return true;
default:
return false;
}
}
// helper function to write a tvMessage into sendbuffer
void TeamviewerInfo::sendTVMessage(CARD16 messageID, CARD32 messageLen, char* msg)
{
tvMessage tmsg;
tmsg.tvMessageID = messageID;
tmsg.tvlen = Swap32IfLE(messageLen);
buffer = reinterpret_cast<char*>(realloc(reinterpret_cast<void*>(buffer),
infolen + sizeof(tvMessage) + messageLen));
if(!buffer)
{
vnclog.Print(LL_INTERR, VNCLOG("realloc == NULL"));
return;
}
memcpy(buffer+infolen, &tmsg, sizeof(tvMessage));
infolen += sizeof(tvMessage);
if(messageLen > 0)
{
memcpy(buffer+infolen, msg, messageLen);
infolen += messageLen;
}
}
// really send all messages in buffer now
void TeamviewerInfo::FinishTeamviewerInfo()
{
if(!partnerIsTeamviewer || !infolen)
return;
char *sendbuffer = reinterpret_cast<char*>(malloc(sz_rfbTeamviewerInfoMsg + infolen));
if(!sendbuffer)
{
vnclog.Print(LL_INTERR, VNCLOG("malloc == NULL"));
return;
}
rfbTeamviewerInfoMsg *infomsg = reinterpret_cast<rfbTeamviewerInfoMsg*>(sendbuffer);
infomsg->type = rfbTeamviewerInfo;
infomsg->len = Swap32IfLE(infolen);
memcpy(sendbuffer+sz_rfbTeamviewerInfoMsg, buffer, infolen);
writefunc(sendbuffer, sz_rfbTeamviewerInfoMsg + infolen);
free(sendbuffer);
free(buffer);
buffer = NULL;
infolen = 0;
}
// send a command (don't put it in the localinfo map, so it won't be resent if
// partner request tvSendAll)
void TeamviewerInfo::SendCommand(CARD16 msgid, string value)
{
sendTVMessage(msgid, value.length(), const_cast<char*>(value.c_str()));
}
// put a value in the localinfo map and send it
void TeamviewerInfo::SendInfo(CARD16 msgid, string value)
{
TeamviewerInfo::setLocalInfo(msgid, value);
SendInfo(msgid);
}
// take a value from the localinfo map and send it
// msgid == tvSendAll -> send all available Infos
void TeamviewerInfo::SendInfo(CARD16 msgid)
{
if(msgid == tvSendAll)
{
map<CARD16,string>::iterator iter;
for(iter = localinfo.begin(); iter != localinfo.end(); iter++)
{
string s=(*iter).second;
if(s != "")
sendTVMessage((*iter).first, s.length(), const_cast<char*>(s.c_str()));
}
}
else
{
string s = getLocalInfo(msgid);
if(s != "")
sendTVMessage(msgid, s.length(), const_cast<char*>(s.c_str()));
}
}
// read all incoming messages from socket
// parameter offset decides if rfb-command byte is read again
// returns a vector which contains the ids of the messages
ResultVector TeamviewerInfo::ReadTeamviewerInfo(int offset)
{
ResultVector received;
rfbTeamviewerInfoMsg tim;
//read rfb command struct
if (readfunc(reinterpret_cast<char*>(&tim) + offset, sz_rfbTeamviewerInfoMsg - offset))
{
bool ok = true;
bool requests = false;
tim.len = Swap32IfLE(tim.len);
while(ok && tim.len>0) // repeat while there are more messages
{
tvMessage tvm;
// read teamviewer message struct
ok = readfunc(reinterpret_cast<char*>(&tvm), sz_tvMessage);
if(ok)
{
tvm.tvlen = Swap32IfLE(tvm.tvlen);
if(tvm.tvMessageID > 0x8000) // tvMessageID > 0x8000 -> request for info
{
requests = true;
SendInfo(tvm.tvMessageID - 0x8000); // prepare sending of the requested info
}
else if(tvm.tvlen > 0) // regular message: read the infos from socket
{
char *cmdbuffer = new char[tvm.tvlen+1];
cmdbuffer[tvm.tvlen] = '\0';
ok = readfunc(cmdbuffer, tvm.tvlen);
if(ok)
{
received.push_back(tvm.tvMessageID);
setPartnerInfo(tvm.tvMessageID, std::string(cmdbuffer));
}
delete[] cmdbuffer;
}
tim.len -= (sz_tvMessage + tvm.tvlen);
}
}
if(requests)
FinishTeamviewerInfo(); // really send the requested infos now
}
return received;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -