1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364 |
- #if (defined _WIN32 || defined _WIN64)
- #include <io.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <Psapi.h>
- #include "fileutil.h"
- #pragma comment(lib,"psapi.lib")
- #endif
- #include "RemoteControllerCnn.h"
- #include "RemoteControllerFSM.h"
- #include "Event.h"
- #include "cJSON.h"
- #include "base64.h"
- #include "digital_conv.h"
- #include <fstream>
- #include "path.h"
- #include "SpUtility.h"
- #include "../mod_healthmanager/HealthManager_client_g.h"
- using namespace HealthManager;
- #define __int64 long int
- #include "../mod_UpgradeMgr/UpgradeManager_client_g.h"
- using namespace UpgradeManager;
- #include "../mod_guiconsole/GUIConsole_client_g.h"
- using namespace GUIConsole;
- #include "../mod_ResourceWatcher/ResourceWatcher_client_g.h"
- using namespace ResourceWatcher;
- #include "remoteBase.h"
- #include "MyZip.h"
- namespace fs = boost::filesystem;
- #include <functional>
- #if (defined _WIN32 || defined _WIN64)
- #define generateFilesize(fileHandle, filePath) long long nFileSize = _filelengthi64(_fileno(fileHandle))
- #include "XZip.h"
- #else
- #define generateFilesize(fileHandle, filePath) long long nFileSize = fs::file_size(filePath.GetData())
- #include "zip.h"
- #include <stdlib.h>
- #include "XZipZilb.h"
- #define ZeroMemory(object, size) memset(object, 0, size)
- #define WritePrivateProfileStringA(a, b, c, d) Base_writeIni(std::string(a), std::string(b), std::string(c), std::string(d))
- #include <sstream>
- void _ui64toa(unsigned long long large_unit, char *str)
- {
- auto largeNumStr = std::to_string(large_unit);
- strcpy(str, largeNumStr.c_str());
- /*
- std::stringstream ss;
- ss << large_unit;
- std::string src = ss.str();
- memset(str, 0, strsize);
- strncpy(str, src.c_str(), strsize);
- */
- }
- int strncpy_s(char* _Destination, size_t _SizeInBytes, char const* _Source, size_t _MaxCount)
- {
- if (_MaxCount > _SizeInBytes)
- return nullptr == strncpy(_Destination, _Source, _SizeInBytes);
- else
- return nullptr == strncpy(_Destination, _Source, _MaxCount);
- }
- int strncpy_s(char* _Destination, char const* _Source, size_t _MaxCount)
- {
- return nullptr == strncpy(_Destination, _Source, _MaxCount);
- }
- #endif
- CRemoteControllerCnn::CRemoteControllerCnn(CEntityBase *pEntity, FSMBase *pFSM)
- :SpSecureClient(pEntity), m_pFSM(pFSM),m_chRemoteMode('N')
- {
- }
- CRemoteControllerCnn::~CRemoteControllerCnn(void)
- {
- }
- void CRemoteControllerCnn::OnDisconnect()
- {
- Dbg("connection disconnected");
- // 连接中断,重置远程维护状态,释放所有资源
- m_chRemoteMode = 'N';
- m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_ReleaseChannel));
- }
- void CRemoteControllerCnn::OnPkgAnswer(const CSmartPointer<IPackage> &pRecvPkg)
- {
- string serviceCode = pRecvPkg->GetServiceCode();
- if (serviceCode == "StRemote")
- {
- HandleCreateRemoteChannelRet(pRecvPkg);
- }
- else if (serviceCode == "EdRemote")
- {
- HandleEndRemoteModeReq(pRecvPkg);
- }
- else if (serviceCode == "DelRun")
- {
- HandleDelRunInfoLogReq(pRecvPkg);
- }
- else if (serviceCode == "RestartS")
- {
- HandleRestartShellReq(pRecvPkg);
- }
- else if (serviceCode == "RestartO")
- {
- HandleRestartMachineReq(pRecvPkg);
- }
- else if (serviceCode == "ViewEnti")
- {
- HandleViewEntityStateReq(pRecvPkg);
- }
- else if (serviceCode == "ViewConf")
- {
- HandleViewEntityConfigReq(pRecvPkg);
- }
- else if (serviceCode == "ViewDbg")
- {
- HandleViewDebugLogReq(pRecvPkg);
- }
- else if (serviceCode == "OprDisk")
- {
- HandleOperateDiskReq(pRecvPkg);
- }
- else if (serviceCode == "EvtLog")
- {
- HandleUploadEventLogReq(pRecvPkg);
- }
- else if(serviceCode == "ULogBD")
- {
- HandleUploadLogByDateReq(pRecvPkg);
- }
- else if(serviceCode == "ULogsBD")
- {
- HandleBatchUploadLogsByDateReq(pRecvPkg);
- }
- else if (serviceCode == "EnterMM")
- {
- HandleEnterMaintainModeReq(pRecvPkg);
- }
- else if (serviceCode == "ExitMM")
- {
- HandleExitMaintainModeReq(pRecvPkg);
- }
- else if (serviceCode == "UpgPack")
- {
- HandleUpgradePackReq(pRecvPkg);
- }
- else if (serviceCode == "Rollback")
- {
- HandleRollbackUpgradeReq(pRecvPkg);
- }
- else if (serviceCode == "GetUser")
- {
- HandleGetCurMaintainerReq(pRecvPkg);
- }
- else if (serviceCode == "Enpower")
- {
- HandleEnpowerMaintainerReq(pRecvPkg);
- }
- else if (serviceCode == "TakePwr")
- {
- HandleTakeoverPowerReq(pRecvPkg);
- }
- else if (serviceCode == "ForceQt")
- {
- HandleForceQuitMaintainReq(pRecvPkg);
- }
- else if (serviceCode == "EditCfg")
- {
- HandleEditConfigReq(pRecvPkg);
- }
- else if (serviceCode == "ViewVar")
- {
- HandleViewSysVarReq(pRecvPkg);
- }
- else if (serviceCode == "GetVer")
- {
- HandleGetVersionReq(pRecvPkg);
- }
- else if (serviceCode == "VRunCfg")
- {
- HandleViewRunCfgReq(pRecvPkg);
- }
- else if (serviceCode == "VPkgLog")
- {
- HandleViewInstallLogReq(pRecvPkg);
- }
- else if (serviceCode == "UpldLog")
- {
- HandleUploadLogReq(pRecvPkg);
- }
- else if (serviceCode == "UpldLogs")
- {
- HandleBatchUploadLogsReq(pRecvPkg);
- }
- else if (serviceCode == "ViewEnEx")
- {
- HandleViewEntityStateExReq(pRecvPkg);
- }
- else if (serviceCode == "CancUpg")
- {
- HandleCancelUpgradeReq(pRecvPkg);
- }
- else
- {
- Dbg("unknown service code: %s", serviceCode.c_str());
- }
- }
- bool CRemoteControllerCnn::GetHostIPAddr(BYTE addr[4])
- {
- memset(addr, 0, 4);
- #if (defined _WIN32 || defined _WIN64)
- char szHostName[64] = {};
- int nRet = gethostname(szHostName, sizeof(szHostName));
- if (nRet != 0)
- {
- Dbg("gethostname fail: %d", nRet);
- return false;
- }
- addrinfo hints = {};
- hints.ai_family = AF_INET;
- PADDRINFOA pResult = NULL;
- nRet = getaddrinfo(szHostName, NULL, &hints, &pResult);
- if (nRet != 0)
- {
- Dbg("getaddrinfo fail: %d", nRet);
- return false;
- }
- for(auto ptr=pResult; ptr != NULL ;ptr=ptr->ai_next)
- {
- assert (ptr->ai_family == AF_INET);
-
- struct in_addr *in = (struct in_addr*)ptr->ai_addr;
- if (in->S_un.S_un_b.s_b1 != 192 || ptr->ai_next != NULL)
- {
- memcpy(&addr[0], in, 4);
- break;
- }
- }
- freeaddrinfo(pResult);
- return true;
- #else
- return Base_GetHostIPAddr(addr);
- #endif
-
- }
- ErrorCodeEnum CRemoteControllerCnn::SendCreateRemoteChannelReq()
- {
- auto pEntity = m_pEntity->GetFunction();
- CSystemStaticInfo info;
- auto rc = pEntity->GetSystemStaticInfo(info);
- assert(rc == Error_Succeed);
- RvcCreateChannelReq req = {};
- strncpy_s(req.TerminalNo, sizeof(req.TerminalNo), info.strTerminalID, _TRUNCATE);
- // get ip addr
- assert(GetHostIPAddr(req.IPAddr));
- // get life id
- CSystemRunInfo info2 = {};
- rc = pEntity->GetSystemRunInfo(info2);
- req.LifeID = info2.tmStart;
- // send package
- auto pReqPkg = CreateNewPackage("StRemote");
- pReqPkg->AddStruct("RemoteR", false, false, (BYTE*)&req, sizeof(req));
- auto strPkgID = SendPackage(pReqPkg);
- assert(strPkgID != "");
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleCreateRemoteChannelRet(const CSmartPointer<IPackage> &pRecvPkg)
- {
- DWORD dwSysCode, dwUserCode;
- string strErrMsg;
- ErrorCodeEnum rc = Error_Succeed;
- if (pRecvPkg->GetErrMsg(dwSysCode, dwUserCode, strErrMsg))
- {
- rc = (ErrorCodeEnum)dwSysCode;
- LogError(Severity_Middle, rc, dwUserCode,
- CSimpleStringA::Format("create remote channel error: %s", strErrMsg.c_str()));
- return rc;
- }
- RvcCreateChannelRet ret = {};
- int nLen = sizeof(ret);
- int nArrayLen(0);
- if (!pRecvPkg->GetStructData("RemoteA", (BYTE*)&ret, &nLen, &nArrayLen))
- {
- LogError(Severity_Low, Error_Param, 0, "get return struct [RemoteA] fail");
- return Error_Param;
- }
- Dbg("create remote channel succeed, remote user: [%s], channel id: [%s]", ret.AgentID, ret.ChannelID);
- return EnterMonitorMode();
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleEndRemoteModeReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("exit remote mode now");
- m_chRemoteMode = 'N';
- m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_ReleaseChannel));
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleDelRunInfoLogReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("Del run info log req");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("DelRunR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [DelRunR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcDelRunInfoLogReq));
- RvcDelRunInfoLogReq req = {};
- ZeroMemory(&req, sizeof(RvcDelRunInfoLogReq));
- int nArrayLen(0);
- pRecvPkg->GetStructData("DelRunR", (BYTE*)&req, &nLen, &nArrayLen);
- CSimpleString strLogName = req.logName;
- RvcDelRunInfoLogRet ret = {};
- ret.TaskID = req.TaskID;
- pReplyPkg->AddStruct("DelRunA", false, false, (BYTE*)&ret, sizeof(ret));
- auto pFunc = m_pEntity->GetFunction();
- CSystemStaticInfo sysInfo;
- auto rc = pFunc->GetSystemStaticInfo(sysInfo);
- CSimpleString runInfoPath;
- pFunc->GetPath("RunInfo", runInfoPath);
- CSimpleString delFilePaht = runInfoPath;
- delFilePaht.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append(strLogName).Append(".ini");
- Dbg("curTaskId:%d ,try to delete %s, strLogName = %s", req.TaskID, delFilePaht, strLogName);
- if (Base_Exist(delFilePaht.GetData()))
- {
- if (!Base_DeleteFile(delFilePaht.GetData()))
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件删除失败", delFilePaht).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
- }
- else
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件不存在", delFilePaht).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleTest(const CSmartPointer<IPackage> &pRecvPkg)
- {
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- auto pFunc = m_pEntity->GetFunction();
- CSystemStaticInfo sysInfo;
- auto rc = pFunc->GetSystemStaticInfo(sysInfo);
- CSimpleString runInfoPath;
- pFunc->GetPath("RunInfo", runInfoPath);
- CSimpleString delFilePaht = runInfoPath;
- delFilePaht.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("MediaController").Append(".ini");
- Dbg("try to delete %s", delFilePaht);
- if (Base_Exist(delFilePaht.GetData()))
- {
- if (!Base_DeleteFile(delFilePaht.GetData()))
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件删除失败", delFilePaht).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
- }
- else
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件不存在", delFilePaht).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleRestartShellReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote restart shell req");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("RestSR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [RestSR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcRestartShellReq));
- RvcRestartShellReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("RestSR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcRestartShellRet ret = {};
- ret.TaskID = req.TaskID;
- pReplyPkg->AddStruct("RestSA", false, false, (BYTE*)&ret, sizeof(ret));
- // 检查当前状态,是否可以重启Shell
- auto pFunc = m_pEntity->GetFunction();
- CSimpleStringA strCustomerHandleVal;
- pFunc->GetSysVar("CustomerHandle", strCustomerHandleVal);
- CSimpleStringA strCustomerBeingVal;
- pFunc->GetSysVar("CustomerBeing", strCustomerBeingVal);
- if (strCustomerHandleVal.Compare("N") != 0 || (strCustomerBeingVal.Compare("N") != 0 && strCustomerBeingVal.Compare("S") != 0))
- {
- Dbg("remote restart shell fail for var [CustomerHandle] == '%s' & [CustomerBeing] == '%s'"
- , (const char*)strCustomerHandleVal, (const char*)strCustomerBeingVal);
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("当前有客户操作,无法重启").c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
- CSimpleStringA strValue = "";
- pFunc->GetSysVar("LocalMaintain", strValue);
- if (strValue.Compare("N") != 0)
- {
- Dbg("remote restart shell fail for var [LocalMaintain] == '%s'", (const char*)strValue);
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("正在本地维护,无法重启").c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
- // 释放重启事件给健康实体
- LogEvent(Severity_Middle, EVENT_RESTART_SHELL, "remote restart shell");
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleRestartMachineReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote restart machine req");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("RestOR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [RestOR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcRestartMachineReq));
- RvcRestartMachineReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("RestOR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcRestartMachineRet ret = {};
- ret.TaskID = req.TaskID;
- pReplyPkg->AddStruct("RestOA", false, false, (BYTE*)&ret, sizeof(ret));
- // 检查当前状态,是否可以重启OS
- auto pFunc = m_pEntity->GetFunction();
- CSimpleStringA strCustomerHandleVal;
- pFunc->GetSysVar("CustomerHandle", strCustomerHandleVal);
- CSimpleStringA strCustomerBeingVal;
- pFunc->GetSysVar("CustomerBeing", strCustomerBeingVal);
- if (strCustomerHandleVal.Compare("N") != 0 || (strCustomerBeingVal.Compare("N") != 0 && strCustomerBeingVal.Compare("S") != 0))
- {
- Dbg("remote restart machine fail for var [CustomerHandle] == '%s' & [CustomerBeing] == '%s'"
- , (const char*)strCustomerHandleVal, (const char*)strCustomerBeingVal);
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("当前有客户操作,无法重启").c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
- CSimpleStringA strValue = "";
- pFunc->GetSysVar("LocalMaintain", strValue);
- if (strValue.Compare("N") != 0)
- {
- Dbg("remote restart machine fail for var [LocalMaintain] == '%s'", (const char*)strValue);
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("正在本地维护,无法重启").c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
- // 释放重启事件给健康实体
- LogEvent(Severity_Middle, EVENT_RESTART_MACHINE, "remote restart machine");
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleViewEntityStateReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote view entity state");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("ViewEntR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewEntR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcViewEntityReq));
- RvcViewEntityReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("ViewEntR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcViewEntityRet1 ret1 = {};
- ret1.TaskID = req.TaskID;
- pReplyPkg->AddStruct("ViewEnA1", false, false, (BYTE*)&ret1, sizeof(ret1));
- auto pFunc = m_pEntity->GetFunction();
-
- CAutoArray<CSimpleStringA> strEntityNames;
- CAutoArray<WORD> dwEntityIDs;
- pFunc->GetAllRegistedEntity(strEntityNames, dwEntityIDs);
- int nCount = strEntityNames.GetCount();
- if (nCount >0)
- {
- nCount += 8; // 包括root\shell\silverlight3个虚拟实体
- RvcViewEntityRet2 *pRet2 = new RvcViewEntityRet2[nCount];
- memset(pRet2, 0, sizeof(RvcViewEntityRet2) * nCount);
- for(int i=0; i<nCount - 8; i++)
- {
- CEntityRunInfo info = {};
- CSimpleStringA strEntityName = strEntityNames[i];
- pFunc->GetEntityRunInfo(strEntityName, info);
- strncpy_s(pRet2[i].EntityName, sizeof(pRet2[i].EntityName), (const char*)strEntityName, _TRUNCATE);
- pRet2[i].EntityID = dwEntityIDs[i];
- pRet2[i].EntityState = info.eState;
- pRet2[i].ProcessID = info.dwProcessID;
- if (info.dwProcessID > 0)
- {
- #if (defined _WIN32 || defined _WIN64)
- auto hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, info.dwProcessID );
- if (hProcess != NULL)
- {
- PROCESS_MEMORY_COUNTERS pmc = {};
- GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
- pRet2[i].UsedMemory = pmc.WorkingSetSize / 1024 / 1024;
- }
- #else
- pRet2[i].UsedMemory = get_proc_mem(info.dwProcessID) / 1024 / 1024;
-
- #endif
- }
- }
- strncpy_s(pRet2[nCount - 8].EntityName, sizeof(pRet2[nCount - 8].EntityName), "guardian", _TRUNCATE);
- strncpy_s(pRet2[nCount - 7].EntityName, sizeof(pRet2[nCount - 7].EntityName), "DualActive", _TRUNCATE);
- strncpy_s(pRet2[nCount - 6].EntityName, sizeof(pRet2[nCount - 6].EntityName), "SpHost", _TRUNCATE);
- strncpy_s(pRet2[nCount - 5].EntityName, sizeof(pRet2[nCount - 5].EntityName), "SysInit", _TRUNCATE);
- strncpy_s(pRet2[nCount - 4].EntityName, sizeof(pRet2[nCount - 4].EntityName), "SpBase", _TRUNCATE);
- strncpy_s(pRet2[nCount - 3].EntityName, sizeof(pRet2[nCount - 3].EntityName), "Root", _TRUNCATE);
- strncpy_s(pRet2[nCount - 2].EntityName, sizeof(pRet2[nCount - 2].EntityName), "Shell", _TRUNCATE);
- strncpy_s(pRet2[nCount - 1].EntityName, sizeof(pRet2[nCount - 1].EntityName), "Silverlight", _TRUNCATE);
- pReplyPkg->AddStruct("ViewEnA2", false, false, (BYTE*)pRet2, sizeof(RvcViewEntityRet2) * nCount, nCount);
- }
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleViewSysVarReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote view system vars");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("ViewVarR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewVarR] len fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcViewSysVarReq));
- RvcViewSysVarReq req = {};
- int nArrayLen(0);
- if (!pRecvPkg->GetStructData("ViewVarR", (BYTE*)&req, &nLen, &nArrayLen))
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewVarR] data fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- auto pFunc = m_pEntity->GetFunction();
- RvcViewSysVarRet arrRet[5] = {};
- char *arrVarNames[] = { "RunState", "CustomerHandle", "CallState", "EntryPermit", "LocalMaintain" };
- for (int i = 0; i < 5; i++)
- {
- CSimpleStringA strValue;
- pFunc->GetSysVar(arrVarNames[i], strValue);
-
- arrRet[i].TaskID = req.TaskID;
- strcpy(arrRet[i].VarName, arrVarNames[i]);
- arrRet[i].VarValue = strValue[0];
- }
- pRecvPkg->AddStruct("ViewVarA", false, false, (BYTE*)arrRet, sizeof(arrRet), 5);
- SendPackage(pRecvPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleEditConfigReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote edit config");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("EditCfgR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EditCfgR] len fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- char *pBuf = new char[nLen];
- memset(pBuf, 0, sizeof(pBuf));
-
- int nArrayLen(0);
- if (!pRecvPkg->GetStructData("EditCfgR", (BYTE*)pBuf, &nLen, &nArrayLen) || nArrayLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EditCfgR] data fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- auto pFunc = m_pEntity->GetFunction();
- assert(nLen == nArrayLen * sizeof(RvcEditConfigReq));
- RvcEditConfigReq *pReq = (RvcEditConfigReq*)pBuf;
- RvcEditConfigRet ret = { pReq[0].TaskID };
- for (int i = 0; i < nArrayLen; i++)
- {
- CSimpleStringA strEntityName = pReq[i].EntityName;
- CSimpleStringA strCfgPath;
- if (iequals(strEntityName.GetData(), "Silverlight") || iequals(strEntityName.GetData(), "DualActive") || iequals(strEntityName.GetData(), "SpHost")
- || iequals(strEntityName.GetData(), "SysInit") || iequals(strEntityName.GetData(), "SpBase") || iequals(strEntityName.GetData(), "guardian"))
- {
- Dbg("entity \"%s\" has no config", strEntityName);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("实体没有配置文件").c_str());
- SendPackage(pReplyPkg);
- delete[] pBuf;
- return Error_NotExist;
- }
- if (iequals(strEntityName.GetData(), "Root"))
- {
- auto rc = pFunc->GetPath("HardwareCfg", strCfgPath);
- assert(rc == Error_Succeed);
- strCfgPath.Append(SPLIT_SLASH_STR).Append("Root.ini");
- }
- else
- {
- auto rc = pFunc->GetPath("Cfg", strCfgPath);
- assert(rc == Error_Succeed);
- strCfgPath.Append(SPLIT_SLASH_STR).Append((const char*)strEntityName).Append(".ini");
- }
- char cEditType = pReq[i].EditType;
- Dbg("edit config: entity = %s, type = %c, section = %s, key = %s, value = %s", (const char*)strEntityName, cEditType, pReq[i].Section, pReq[i].Key, pReq[i].Value);
- if (cEditType == 'A' || cEditType == 'U')
- WritePrivateProfileStringA(pReq[i].Section, pReq[i].Key, pReq[i].Value, strCfgPath.GetData());
- else if (cEditType == 'D')
- WritePrivateProfileStringA(pReq[i].Section, pReq[i].Key, "", strCfgPath.GetData());
- }
- delete[] pBuf;
- pReplyPkg->AddStruct("EditCfgA", false, false, (BYTE*)&ret, sizeof(ret));
- SendPackage(pRecvPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleViewEntityConfigReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote view entity config");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("ViewCfgR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewCfgR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcViewConfigReq));
- RvcViewConfigReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("ViewCfgR", (BYTE*)&req, &nLen, &nArrayLen);
- // 读取实体配置文件
- CSimpleStringA strEntityName = req.EntityName;
- auto pFunc = m_pEntity->GetFunction();
- CSimpleStringA strCfgPath;
- if (strEntityName == "Root")
- {
- auto rc = pFunc->GetPath("HardwareCfg", strCfgPath);
- assert(rc == Error_Succeed);
- strCfgPath.Append(SPLIT_SLASH_STR).Append("Root.ini");
- }
- else
- {
- auto rc = pFunc->GetPath("Cfg", strCfgPath);
- assert(rc == Error_Succeed);
- strCfgPath.Append(SPLIT_SLASH_STR).Append((const char*)strEntityName).Append(".ini");
- }
-
- if (!Base_Exist(strCfgPath.GetData()))
- {
- Dbg("file [%s] not exist", (const char*)strCfgPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("配置文件不存在").c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- auto cfgSize = Base_filesize(strCfgPath.GetData());
-
- char* tmpBuf = new char[cfgSize];
- memset(tmpBuf, 0, cfgSize);
-
- auto readRet = Base_readFile(strCfgPath.GetData(), 0, tmpBuf, cfgSize);
- if(!readRet.first)
- {
- pReplyPkg->SetErrMsg(0, 0, SP::Utility::UTF8ToGBK("open entity config file fail").c_str());
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("open config [%s] fail", (const char*)strCfgPath));
- }
- else
- {
- int nRetLen = sizeof(RvcViewConfigRet) + cfgSize * 2; //需自行转换\n和\r\n
- char* pBuf = new char[nRetLen];
- memset(pBuf, 0, nRetLen);
- RvcViewConfigRet* pRet = (RvcViewConfigRet*)pBuf;
- int dstPos = 0;
- for (auto i = 0; i < cfgSize; i++)
- {
- if ((i == 0 && tmpBuf[i] == '\n') || (tmpBuf[i] == '\n' && tmpBuf[i - 1] != '\r'))
- pRet->ConfigData[dstPos++] = '\r';
- pRet->ConfigData[dstPos++] = tmpBuf[i];
- }
- pReplyPkg->AddStruct("ViewCfgA", false, false, (BYTE*)pBuf, dstPos + sizeof(RvcViewConfigRet));
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleViewDebugLogReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote view entity debug log");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("ViewDbgR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViewDbgR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcViewDbgReq));
- RvcViewDbgReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("ViewDbgR", (BYTE*)&req, &nLen, &nArrayLen);
- auto pFunc = m_pEntity->GetFunction();
- CSimpleStringA strEntityName = req.EntityName;
-
- if (iequals(strEntityName.GetData(), "Root"))
- {
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("Root实体没有日志").c_str());
- Dbg("entity \"Root\" has no dbg log");
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- CSimpleStringA strViewDate = req.ViewDate;
- if (strViewDate.IsNullOrEmpty())
- {
- strViewDate = getCurData().c_str();
- }
- ErrorCodeEnum rc(Error_Succeed);
- CSimpleStringA strLogPath;
- if (iequals(strEntityName.GetData(), "Shell"))
- {
- auto rc = pFunc->GetPath("Dbg", strLogPath);
- assert(rc == Error_Succeed);
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
- }
- else if (iequals(strEntityName.GetData(), "Silverlight"))
- {
- auto rc = pFunc->GetPath("Slv", strLogPath);
- assert(rc == Error_Succeed);
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
- }
- else if (iequals(strEntityName.GetData(), "DualActive")|| iequals(strEntityName.GetData(), "SpHost")
- || iequals(strEntityName.GetData(), "SysInit") || iequals(strEntityName.GetData(), "SpBase") || iequals(strEntityName.GetData(), "guardian"))
- {
- auto rc = pFunc->GetPath("Dbg", strLogPath);
- assert(rc == Error_Succeed);
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strEntityName).Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
- }
- else
- {
- CEntityStaticInfo info = {};
- auto rc = pFunc->GetEntityStaticInfo(strEntityName, info);
- if (rc != Error_Succeed)
- {
- Dbg("invalid entity name [%s]", strEntityName);
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体名称\"%s\"无效", strEntityName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return rc;
- }
- rc = pFunc->GetPath("Dbg", strLogPath);
- assert(rc == Error_Succeed);
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
- }
- if (!Base_Exist(strLogPath.GetData()))
- {
- Dbg("log [%s] not exist", (const char*)strLogPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- long long nStartPos = req.StartPos;
- if (nStartPos <0)
- nStartPos = 0;
-
- long long nMaxBytes = req.MaxRetBytes;
- if (nMaxBytes <= 0)
- nMaxBytes = 8 * 1024;
- auto file = fopen(strLogPath, "rb");
- if (file == NULL)
- {
- auto nError = errno;
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strLogPath, nError).GetData()).c_str());
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strLogPath, nError));
- SendPackage(pReplyPkg);
- return rc;
- }
- generateFilesize(file, strLogPath);
- if (nStartPos >0 && nStartPos < nFileSize)
- _fseeki64(file, nStartPos, SEEK_SET);
- else
- nStartPos = 0;
-
- long long nNextPos = nStartPos + nMaxBytes;
- if (nMaxBytes + nStartPos > nFileSize)
- {
- nMaxBytes = nFileSize - nStartPos;
- nNextPos = 0;
- }
-
- long long nRetLen = sizeof(RvcViewDbgRet) +nMaxBytes;
- char *pBuf = new char[nRetLen];
- memset(pBuf, 0, nRetLen);
-
- RvcViewDbgRet *pRet = (RvcViewDbgRet*)pBuf;
- pRet->TaskID = req.TaskID;
- strncpy_s(pRet->EntityName, sizeof(pRet->EntityName), strEntityName, _TRUNCATE);
- strncpy(pRet->ViewDate, strViewDate, 8);
- pRet->NextPos = nNextPos;
- pRet->FileLength = nFileSize;
- char *pLogData = (char*)pRet->LogData;
- long long nHasRead(0);
- DWORD nReadLen(0);
-
- BOOL bRet = TRUE;
- do
- {
- nReadLen = fread(pLogData+nHasRead, 1, nMaxBytes-nHasRead, file);
-
- if (nReadLen >0)
- nHasRead += nReadLen;
- }
- while (nReadLen >0 && nHasRead < nMaxBytes);
- fclose(file);
- if (!bRet)
- {
- delete[] pBuf;
- auto nError = GetLastError();
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strLogPath, nError).GetData()).c_str());
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strLogPath, nError));
- SendPackage(pReplyPkg);
- return rc;
- }
- if (pRet->NextPos > 0)
- {
- // 整行切分处理
- while (nReadLen > sizeof(RvcViewDbgRet) &&
- (pBuf[nRetLen - 1] != '\r' && pBuf[nRetLen - 1] != '\n'))
- {
- nRetLen--;
- pRet->NextPos--;
- }
- }
- pReplyPkg->AddStruct("ViewDbgA", false, false, (BYTE*)pBuf, nRetLen);
- delete[] pBuf;
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleUploadEventLogReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote upload event log");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("EvtLogR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EvtLogR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- if (nLen != sizeof(RvcUploadEventLogReq))
- Dbg("nLen:%d, RvcUploadEventLogReq Size:%d", nLen, sizeof(RvcUploadEventLogReq));
- assert(nLen == sizeof(RvcUploadEventLogReq));
- RvcUploadEventLogReq req;
- int nArrayLen(0);
- pRecvPkg->GetStructData("EvtLogR", (BYTE*)&req, &nLen, &nArrayLen);
- auto pFunc = m_pEntity->GetFunction();
- CSimpleStringA strZipFileName;
- CSimpleStringA strLogPath;
- CSimpleStringA strZipPath, strTodayLogName;
- if (req.StartPos <= 0)
- {
- ResourceWatcherService_ExtractEventLog_Req rvcReq;
- ZeroMemory(&rvcReq, sizeof(ResourceWatcherService_ExtractEventLog_Req));
- {//初始化rvcReq
- rvcReq.evtName = req.evtName;
- rvcReq.evtLevel = req.evtLevel;
- rvcReq.duration = req.duration;
- rvcReq.startTime = req.startTime;
- rvcReq.endTime = req.endTime;
- rvcReq.cusEvtFileName = CSimpleStringA(req.cusEvtFileName);
- rvcReq.evtSrcEventName = CSimpleStringA(req.evtSrcEventName);
- rvcReq.reversed1 = req.reversed1;
- rvcReq.reversed2 = CSimpleStringA(req.reversed2);
- }
- ResourceWatcherService_ExtractEventLog_Ans rvcAns;
- ZeroMemory(&rvcAns, sizeof(ResourceWatcherService_ExtractEventLog_Ans));
- ResourceWatcherService_ClientBase *pClient = new ResourceWatcherService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- Dbg("evtName:%d, evtLevel:%d, duration:%d, startTime:%d, endTime:%d, cusEvtFileName:%s,evtSrcEventName:%s, reversed1:%d, reversed2:%s, startPos:%d, MaxRet:%d, UploadFile:%s",
- req.evtName, req.evtLevel, req.duration, req.startTime, req.endTime,
- req.cusEvtFileName, req.evtSrcEventName, req.reversed1, req.reversed2, req.StartPos, req.MaxRetBytes, req.UploadFile);
- rc = pClient->ExtractEventLog(rvcReq, rvcAns, 60000 * 5);
- Dbg("entries:%d, information:%s, evtLogFileName:%s", rvcAns.entries, rvcAns.information.GetData(), rvcAns.evtLogFileName.GetData());
- pClient->GetFunction()->CloseSession();
- }
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[ResourceWatcher]实体失败").c_str());
- LogError(Severity_Low, rc, 0, "调用[ResourceWatcher]实体失败");
- }
- else
- {
- Dbg("获取系统事件日志成功");
- //压缩位置
- strTodayLogName = getCurData().c_str();
- CSystemStaticInfo sysInfo;
- auto rc = pFunc->GetSystemStaticInfo(sysInfo);
- strZipFileName = CSimpleStringA::Format("%s_event_%s_%02X_%02X.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName, rvcAns.entries,
- req.evtLevel + req.duration + req.startTime + req.endTime);
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
- strLogPath = rvcAns.evtLogFileName;
- if (0 == rvcAns.entries)
- {
- Dbg("log [%s] not exist", (const char*)strLogPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("未能获取到相关日志").GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- if (!Base_Exist(strLogPath.GetData()))
- {
- Dbg("log [%s] not exist", (const char*)strLogPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- // 生成ZIP
- rc = ZipFile(strZipPath, strLogPath);
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return rc;
- }
- else
- Base_DeleteFile(strLogPath.GetData());
- }
- }
- else
- {
- strZipFileName = req.UploadFile;
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
- }
- if (!Base_Exist(strZipPath.GetData()))
- {
- Dbg("zip log [%s] not exist", (const char*)strZipPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- // 开始上传
- long long nStartPos = req.StartPos;
- if (nStartPos <0)
- nStartPos = 0;
- long long nMaxBytes = req.MaxRetBytes;
- if (nMaxBytes <= 0)
- nMaxBytes = 32 * 1024;
- auto file = fopen(strZipPath, "rb");
- if (file == NULL)
- {
- auto nError = errno;
- pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- generateFilesize(file, strZipPath);
- if (nStartPos >0 && nStartPos < nFileSize)
- {
- _fseeki64(file, nStartPos, SEEK_SET);
- }
- else
- nStartPos = 0;
- long long nNextPos = nStartPos + nMaxBytes;
- if (nMaxBytes + nStartPos > nFileSize)
- {
- nMaxBytes = nFileSize - nStartPos;
- nNextPos = 0;
- }
- long long nRetLen = sizeof(RvcUploadEventLogRet)+nMaxBytes;
- char *pBuf = new char[nRetLen];
- memset(pBuf, 0, nRetLen);
- RvcUploadEventLogRet *pRet = (RvcUploadEventLogRet*)pBuf;
- pRet->TaskID = req.TaskID;
- strncpy_s(pRet->UploadFile, sizeof(pRet->UploadFile), strZipFileName, _TRUNCATE);
- pRet->ZipFileLen = nFileSize;
- pRet->StartPos = req.StartPos;
- pRet->NextPos = nNextPos;
- char *pLogData = (char*)pRet->LogData;
- long long nHasRead(0);
- DWORD nReadLen(0);
- BOOL bRet = TRUE;
- do
- {
- //Dbg("ftell: %d", ftell(file));
- nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
- //Dbg("ftell: %d", ftell(file));
- if (nReadLen >0)
- nHasRead += nReadLen;
- } while (nReadLen >0 && nHasRead < nMaxBytes);
- fclose(file);
- if (nHasRead < nMaxBytes)
- {
- delete[] pBuf;
- auto nError = GetLastError();
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- pReplyPkg->AddStruct("EvtLogA", false, false, (BYTE*)pBuf, nRetLen);
- delete[] pBuf;
- SendPackage(pReplyPkg);
- if (nNextPos <= 0)
- {
- // 删除压缩文件
- Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
- Base_DeleteFile(strZipPath.GetData());
- }
- return Error_Succeed;
- }
- char *makeOperateDiskJson(RvcOperateDiskRet1 *ret1, RvcOperateDiskRet2 *ret2, int ret2Num)
- {
- cJSON * pJsonRoot = NULL;
- pJsonRoot = cJSON_CreateObject();
- if(NULL == pJsonRoot || NULL == ret1 || NULL == ret2 || ret2Num <= 0)
- return NULL;
- cJSON_AddNumberToObject(pJsonRoot, "TaskID", ret1->TaskID);
- cJSON_AddNumberToObject(pJsonRoot, "result", ret1->result);
- cJSON_AddStringToObject(pJsonRoot, "header", ret1->header);
- cJSON_AddNumberToObject(pJsonRoot, "attachment1", ret1->attachment1);
- cJSON *arrFileName = cJSON_CreateArray(), *arrFileSize = cJSON_CreateArray(), *arrFtCreate = cJSON_CreateArray(), *arrftModified = cJSON_CreateArray(),
- *arrftAccess = cJSON_CreateArray(), *arrFileAttribute = cJSON_CreateArray(), *arrForbidAttributes = cJSON_CreateArray(),
- *arrReserved1 = cJSON_CreateArray(), *arrReserved2 = cJSON_CreateArray();
- if (NULL == arrFileName || NULL == arrFileSize || NULL == arrFtCreate || NULL == arrftModified || NULL == arrftAccess || NULL == arrFileAttribute || NULL == arrForbidAttributes
- || NULL == arrReserved1 || NULL ==arrReserved2)
- return NULL;
- for (int i = 0; i < ret2Num; i++)
- {
- CDigitalConv62 d62;
- char temp[30] = "";
- string str62;
- cJSON_AddStringToObject(arrFileName, "fileName", ret2[i].fileName);
- _ui64toa(ret2[i].fileSize, temp);
- cJSON_AddStringToObject(arrFileSize, "fileSize", d62.to_x(temp, 1).data());
- _ui64toa(ret2[i].ftCreate, temp);
- cJSON_AddStringToObject(arrFtCreate, "ftCreate", d62.to_x(temp, 1).data());
- _ui64toa(ret2[i].ftModified, temp);
- cJSON_AddStringToObject(arrftModified, "ftModified", d62.to_x(temp, 1).data());
- _ui64toa(ret2[i].ftAccess, temp);
- cJSON_AddStringToObject(arrftAccess, "ftAccess", d62.to_x(temp, 1).data());
- cJSON_AddNumberToObject(arrFileAttribute, "fileAttribute", ret2[i].fileAttribute);
- cJSON_AddNumberToObject(arrForbidAttributes, "forbidAttributes", ret2[i].forbidAttributes);
- if (0 != ret2[i].reserved1)
- cJSON_AddNumberToObject(arrReserved1, "reserved1", ret2[i].reserved1);
-
- //cJSON_AddStringToObject(arrReserved2, "reserved2", ret2[i].reserved2);
- }
- cJSON_AddItemToObject(pJsonRoot, "arrFileName", arrFileName);
- cJSON_AddItemToObject(pJsonRoot, "arrFileSize", arrFileSize);
- cJSON_AddItemToObject(pJsonRoot, "arrFtCreate", arrFtCreate);
- cJSON_AddItemToObject(pJsonRoot, "arrftModified", arrftModified);
- cJSON_AddItemToObject(pJsonRoot, "arrftAccess", arrftAccess);
- cJSON_AddItemToObject(pJsonRoot, "arrFileAttribute", arrFileAttribute);
- cJSON_AddItemToObject(pJsonRoot, "arrForbidAttributes", arrForbidAttributes);
- cJSON_AddItemToObject(pJsonRoot, "arrReserved1", arrReserved1);
- //cJSON_AddItemToObject(pJsonRoot, "arrReserved2", arrReserved2);
- char *out = cJSON_Print(pJsonRoot);
- cJSON_Delete(pJsonRoot);
- return out;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleOperateDiskReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request operate disk");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("OprDiskR");
- if (nLen <= 0)
- {
- Dbg("Failed:[OprDiskR]");
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [OprDiskR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcOperateDiskReq));
- RvcOperateDiskReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("OprDiskR", (BYTE*)&req, &nLen, &nArrayLen);
-
- // 调用本地维护实体取控制信息
- ResourceWatcherService_OperateFile_Req rvcReq;
- ZeroMemory(&rvcReq, sizeof(ResourceWatcherService_OperateFile_Req));
- {//初始化rvcReq
- CSimpleStringA curTmp(req.current);
- #if (defined _WIN32 || defined _WIN64)
- rvcReq.current = curTmp;
- #else
- if (curTmp[0] == '/')
- rvcReq.current = curTmp;
- else
- {
- std::string tmpStr = curTmp.GetData();
- std::replace(tmpStr.begin(), tmpStr.end(), '\\', '/');
- rvcReq.current = CSimpleStringA("/") + tmpStr.c_str();
- }
-
-
- #endif
- rvcReq.mode = req.mode;
- rvcReq.attribute = req.attribute;
- rvcReq.content = CSimpleStringA(req.content);
- rvcReq.filter1 = req.filter1;
- rvcReq.filter2 = CSimpleStringA(req.filter2);
- rvcReq.attachment1 = req.attachment1;
- rvcReq.attachment2 = CSimpleStringA(req.attachment2);
- }
- Dbg("current:%s, reqMode:%d, mode:%d, attribute:%d, content:%s", rvcReq.current.GetData(), req.mode, rvcReq.mode, rvcReq.attribute, rvcReq.content.GetData());
- ResourceWatcherService_OperateFile_Ans rvcAns;
- ZeroMemory(&rvcAns, sizeof(ResourceWatcherService_OperateFile_Ans));
- ResourceWatcherService_ClientBase *pClient = new ResourceWatcherService_ClientBase(m_pEntity);
- RvcOperateDiskRet2 *pRet2 = NULL;
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- rc = pClient->OperateFile(rvcReq, rvcAns, 60000);
- pClient->GetFunction()->CloseSession();
-
- }
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[ResourceWatcher]实体失败").c_str());
- LogError(Severity_Low, rc, 0, "调用[ResourceWatcher]实体失败");
- }
- else
- {
- Dbg("获取磁盘目录信息成功");
- //添加首层目录信息
- RvcOperateDiskRet1 ret1(req.TaskID, rvcAns.result, rvcAns.header, rvcAns.attachment1);
- RvcOperateDiskRet2 current(rvcAns.attachment2, rvcAns.fileSize, rvcAns.ftCreate, rvcAns.ftModified,
- rvcAns.ftAccess, rvcAns.fileAttribute, rvcAns.forbidAttribute, rvcAns.reversed1, rvcAns.reversed2);
- //添加目录具体文件信息
- pRet2 = new RvcOperateDiskRet2[rvcAns.count + 1];
- ZeroMemory(pRet2, sizeof(RvcOperateDiskRet2) * (rvcAns.count + 1));
- memcpy(&(pRet2[0]), ¤t, sizeof(RvcOperateDiskRet2));
- //Dbg("size:%d", rvcAns.count);
- for (int i = 0; i < rvcAns.count; i++)
- {
-
- RvcOperateDiskRet2 tempRet(rvcAns.fileNames[i], rvcAns.fileSizes[i], rvcAns.ftCreates[i], rvcAns.ftModifieds[i],
- rvcAns.ftAccesses[i], rvcAns.fileAttributes[i], rvcAns.forbidAttributes[i],
- (rvcAns.reserved1.GetCount() == rvcAns.count ? rvcAns.reserved1[i] : 0), (rvcAns.reserved2.GetCount() == rvcAns.count ? rvcAns.reserved2[i] : ""));
- memcpy(&(pRet2[i + 1]), &tempRet, sizeof(RvcOperateDiskRet2));
- }
- char *outStr = makeOperateDiskJson(&ret1, pRet2, rvcAns.count + 1);
- char *slimJsonStr = new char[strlen(outStr)];
- ZeroMemory(slimJsonStr, strlen(outStr));
- int pos = 0;
- for (int i = 0; i < strlen(outStr); i++)
- {
- if ('\t' != outStr[i] && '\n' != outStr[i])
- {
- slimJsonStr[pos] = outStr[i];
- pos++;
- }
- }
- slimJsonStr[pos] = '\0';
- long long nRetLen = sizeof(RvcOperateDiskJsonRet) + 60 * 1024;
- char *pBuf = new char[nRetLen];
- ZeroMemory(pBuf, nRetLen);
- RvcOperateDiskJsonRet *pRet = (RvcOperateDiskJsonRet*)pBuf;
- pRet->TaskID = req.TaskID;
- auto pFunc = m_pEntity->GetFunction();
- CSimpleStringA jsonFileName, tempDir, zipJsonName;
- pFunc->GetPath("Temp", tempDir);
- jsonFileName.Append(tempDir).Append(SPLIT_SLASH_STR).Append("transfer.txt");
- zipJsonName.Append(tempDir).Append(SPLIT_SLASH_STR).Append("zipJson.zip");
- ofstream jsonFile(jsonFileName.GetData());
- if (jsonFile.is_open())
- {
- Dbg("jsonLength:%d", pRet->jsonLength);
- auto gbkStr = SP::Utility::UTF8ToGBK(slimJsonStr);
- jsonFile << gbkStr << endl;
- jsonFile.close();
- }
- if (Error_Succeed != (rc = ZipFile(zipJsonName, jsonFileName)))
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)zipJsonName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return rc;
- }
- std::ifstream t;
- t.open(zipJsonName, std::ios_base::binary | std::ios_base::in | std::ios_base::out); // open input file
- t.seekg(0, std::ios::end); // go to the end
- pRet->jsonLength = t.tellg(); // report location (this is the length)
- t.seekg(0, std::ios::beg); // go back to the beginning
- t.read(pRet->jsonData, pRet->jsonLength); // read the whole file into the buffer
- t.close(); // close file handle
- pReplyPkg->AddStruct("OprDkJA", false, false, (BYTE*)pRet, nRetLen);
- delete []slimJsonStr;
- Base_DeleteFile(jsonFileName.GetData());
- Base_DeleteFile(zipJsonName.GetData());
-
- }
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleEnterMaintainModeReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request enter maintain mode");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("EnterMMR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EnterMMR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcEnterMaintainModeReq));
- RvcEnterMaintainModeReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("EnterMMR", (BYTE*)&req, &nLen, &nArrayLen);
-
- RvcEnterMaintainModeRet ret = {};
- ret.TaskID = req.TaskID;
- pReplyPkg->AddStruct("EnterMMA", false, false, (BYTE*)&ret, sizeof(ret));
- // 检查当前状态
- auto pFunc = m_pEntity->GetFunction();
- CSimpleStringA strCustomerHandleVal;
- pFunc->GetSysVar("CustomerHandle", strCustomerHandleVal);
- CSimpleStringA strCustomerBeingVal;
- pFunc->GetSysVar("CustomerBeing", strCustomerBeingVal);
- if (strCustomerHandleVal.Compare("N") != 0 || (strCustomerBeingVal.Compare("N") != 0 && strCustomerBeingVal.Compare("S") != 0))
- {
- Dbg("remote enter maintain mode fail for var [CustomerHandle] == '%s' & [CustomerBeing] == '%s'"
- , (const char*)strCustomerHandleVal, (const char*)strCustomerBeingVal);
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("当前有客户操作,无法暂停服务").c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
-
- CSimpleStringA strValue = "";
- pFunc->GetSysVar("LocalMaintain", strValue);
- if (strValue.Compare("N") != 0)
- {
- Dbg("remote enter maintain mode fail for var [LocalMaintain] == '%s'", (const char*)strValue);
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("正在本地维护,无法暂停服务").c_str());
- SendPackage(pReplyPkg);
- return Error_Cancel;
- }
- // 调用健康实体进入维护模式
- HealthManagerService_ClientBase *pClient = new HealthManagerService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- HealthManagerService_EnterState_Req req = {};
- req.state = "M";
- HealthManagerService_EnterState_Ans ans = {};
- rc = pClient->EnterState(req, ans, 10000);
- pClient->GetFunction()->CloseSession();
- }
- else {
- pClient->SafeDelete();
- }
-
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用健康实体进入维护模式失败").c_str());
- LogError(Severity_Low, rc, 0, "调用健康失败进入维护模式失败");
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleExitMaintainModeReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request exit maintain mode");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("ExitMMR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ExitMMR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcExitMaintainModeReq));
- RvcExitMaintainModeReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("ExitMMR", (BYTE*)&req, &nLen, &nArrayLen);
-
- RvcExitMaintainModeRet ret = {};
- ret.TaskID = req.TaskID;
- pReplyPkg->AddStruct("ExitMMA", false, false, (BYTE*)&ret, sizeof(ret));
-
- HealthManagerService_ClientBase *pClient = new HealthManagerService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- HealthManagerService_ExitState_Req req = {};
- req.state = "M";
- HealthManagerService_ExitState_Ans ans = {};
- rc = pClient->ExitState(req, ans, 10000);
- pClient->GetFunction()->CloseSession();
- } else {
- pClient->SafeDelete();
- }
-
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用健康实体退出维护模式失败").c_str());
- LogError(Severity_Low, rc, 0, "调用健康失败退出维护模式失败");
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleUpgradePackReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request remote upgarde pack");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("UpgPackR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [UpgPackR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcUpgradePackReq));
- RvcUpgradePackReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("UpgPackR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcUpgradePackRet ret = {};
- ret.TaskID = req.TaskID;
- pReplyPkg->AddStruct("UpgPackA", false, false, (BYTE*)&ret, sizeof(ret));
- CSimpleStringA strPack = req.PackName;
- Dbg("begin upgrade pack: %s", (const char*)strPack);
-
- // 调用升级实体接口升级
- UpgradeMgrService_ClientBase *pClient = new UpgradeMgrService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- UpgradeMgrService_RegistLocalPack_Req req = {};
- req.strPackFile = strPack;
- UpgradeMgrService_RegistLocalPack_Ans ans = {};
- rc = pClient->RegistLocalPack(req, ans, 10000);
- pClient->GetFunction()->CloseSession();
- } else {
- pClient->SafeDelete();
- }
- if (rc != Error_Succeed)
- {
- if (rc == Error_NotExist)
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("升级包文件不存在").c_str());
- else if (rc == Error_AlreadyExist)
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("升级包已安装").c_str());
- else if (rc == Error_Cancel)
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("升级包已被取消安装").c_str());
- else
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("远程执行升级失败").c_str());
- Dbg("remote regist upgrade pack fail: 0x%X", rc);
- }
- else
- {
- Dbg("remote regist upgrade pack succeed!");
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleRollbackUpgradeReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request remote rollback upgarde");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("RollbakR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [RollbakR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcRollbackReq));
- RvcRollbackReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("RollbakR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcRollbackRet ret = {};
- ret.TaskID = req.TaskID;
- pReplyPkg->AddStruct("RollbakA", false, false, (BYTE*)&ret, sizeof(ret));
- // 调用升级实体接口回滚
- UpgradeMgrService_ClientBase *pClient = new UpgradeMgrService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- UpgradeMgrService_RollbackUpdate_Req req = {};
- UpgradeMgrService_RollbackUpdate_Ans ans = {};
- rc = pClient->RollbackUpdate(req, ans, 10000);
- pClient->GetFunction()->CloseSession();
- } else {
- pClient->SafeDelete();
- }
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用升级实体回滚升级失败").c_str());
- LogError(Severity_Low, rc, 0, "调用升级实体回滚升级失败");
- }
- else
- {
- Dbg("remote rollback upgrade succeed!");
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleGetCurMaintainerReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request get maintainer info");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("GetUserR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [GetUserR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcGetUserReq));
- RvcGetUserReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("GetUserR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcGetUserRet ret = {};
- ret.TaskID = req.TaskID;
- // 调用本地维护实体取控制信息
- GUIConsoleService_GetCurrentMaintainer_Req req1 = {};
- GUIConsoleService_GetCurrentMaintainer_Ans ans1 = {};
-
- GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- rc = pClient->GetCurrentMaintainer(req1, ans1, 10000);
- pClient->GetFunction()->CloseSession();
- } else {
- pClient->SafeDelete();
- }
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
- LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
- }
- else
- {
- Dbg("获取运维人员信息成功");
- strncpy_s(ret.UserID, sizeof(ret.UserID), ans1.strUserID, _TRUNCATE);
- strncpy_s(ret.UserName, sizeof(ret.UserName), ans1.strUserName, _TRUNCATE);
- strncpy_s(ret.CurRight, sizeof(ret.CurRight), ans1.strCurRight, _TRUNCATE);
- strncpy_s(ret.Authorizer, sizeof(ret.Authorizer), ans1.strAuthorizer, _TRUNCATE);
- ret.BeginTime = ans1.dwBeginTime;
- pReplyPkg->AddStruct("GetUserA", false, false, (BYTE*)&ret, sizeof(ret));
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleEnpowerMaintainerReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request enpower maintainer");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("EnpowerR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [EnpowerR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcEnpowerUserReq));
- RvcEnpowerUserReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("EnpowerR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcEnpowerUserRet ret = {};
- ret.TaskID = req.TaskID;
- // 调用本地维护实体取控制信息
- GUIConsoleService_Empower_Req req1 = {};
- GUIConsoleService_Empower_Ans ans1 = {};
-
- GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- req1.strUserID = req.UserID;
- rc = pClient->Empower(req1, ans1, 10000);
- pClient->GetFunction()->CloseSession();
- } else {
- pClient->SafeDelete();
- }
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
- LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
- }
- else
- {
- Dbg("远程维护授权成功");
- pReplyPkg->AddStruct("EnpowerA", false, false, (BYTE*)&ret, sizeof(ret));
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleTakeoverPowerReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request takeover maintainer power");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("TakeovrR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [TakeovrR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcTakeoverPowerReq));
- RvcTakeoverPowerReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("TakeovrR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcTakeoverPowerRet ret = {};
- ret.TaskID = req.TaskID;
- // 调用本地维护实体取控制信息
- GUIConsoleService_Takeover_Req req1 = {};
- GUIConsoleService_Takeover_Ans ans1 = {};
-
- GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- req1.strUserID = req.UserID;
- rc = pClient->Takeover(req1, ans1, 10000);
- pClient->GetFunction()->CloseSession();
- } else {
- pClient->SafeDelete();
- }
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
- LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
- }
- else
- {
- Dbg("远程解除高级维护权限成功");
- pReplyPkg->AddStruct("TakeovrA", false, false, (BYTE*)&ret, sizeof(ret));
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleForceQuitMaintainReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request force quit maintain");
-
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("ForceQtR");
- if (nLen <=0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ForceQtR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcForceQuitReq));
- RvcForceQuitReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("ForceQtR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcForceQuitRet ret = {};
- ret.TaskID = req.TaskID;
- // 调用本地维护实体
- GUIConsoleService_ForceQuit_Req req1 = {};
- GUIConsoleService_ForceQuit_Ans ans1 = {};
-
- GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- rc = pClient->ForceQuit(req1, ans1, 10000);
- pClient->GetFunction()->CloseSession();
- } else {
- pClient->SafeDelete();
- }
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
- LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
- }
- else
- {
- Dbg("远程强制退出维护状态成功");
- pReplyPkg->AddStruct("ForceQtA", false, false, (BYTE*)&ret, sizeof(ret));
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::EnterMonitorMode()
- {
- m_chRemoteMode = 'O';
- m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_EnterMonitor));
- Dbg("enter monitor mode succeed");
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::EnterMaintainMode(bool bLocalMaintain)
- {
- // 是否远程维护
- if (bLocalMaintain)
- {
- // 弹出本地控制台
- Dbg("show local maintain console");
- }
- else
- {
- // 远程维护 显示维护图片 遮挡前端操作界面
- Dbg("show remote maintain picture");
- }
- m_chRemoteMode = bLocalMaintain ? 'L' : 'R';
- m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_EnterMaintain));
- Dbg("enter maintain mode succeed");
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::EnterTerminalMode(int nRemotePort)
- {
- // 检测本机远程桌面是否打开
- // 检测远程用户是否创建
- // 连接分行桥接服务端口
- // 重定向此连接至3389端口
- m_chRemoteMode = 'T';
- m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_EnterTerminal));
- Dbg("enter terminal mode succeed");
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleGetVersionReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("request get active version");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("GetVerR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [GetVerR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcGetActiveVerReq));
- RvcGetActiveVerReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("GetVerR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcGetActiveVerRet ret = {};
- ret.TaskID = req.TaskID;
- CSimpleStringA strPath;
- auto rc = m_pEntity->GetFunction()->GetPath("RootVer", strPath);
- if (rc == Error_Succeed)
- {
- strPath.Append(SPLIT_SLASH_STR).Append("active.txt");
- FILE *pFile = fopen(strPath, "r");
- if (pFile != NULL)
- {
- char szTemp[256] = {};
- int n = fread(szTemp, 1, 256, pFile);
-
- strncpy_s(ret.Version, sizeof(ret.Version), szTemp, _TRUNCATE);
- fclose(pFile);
- pReplyPkg->AddStruct("GetVerA", false, false, (BYTE*)&ret, sizeof(ret));
- }
- else
- {
- Dbg("open active.txt fail");
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("open active.txt fail").c_str());
- }
- }
- else
- {
- Dbg("get root ver path fail");
- pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK("get root ver path fail").c_str());
- }
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleViewRunCfgReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote view runcfg");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("VRunCfgR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [VRunCfgR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcViewRunCfgReq));
- RvcViewRunCfgReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("VRunCfgR", (BYTE*)&req, &nLen, &nArrayLen);
- // 读取实体配置文件
- CSimpleStringA strConfigName = req.ConfigName;
- CSimpleStringA strCfgPath;
- if (Error_Succeed != m_pEntity->GetFunction()->GetPath("RunInfo", strCfgPath)) {
- Dbg("Get RunInfo path failed");
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("配置不存在").c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- strCfgPath.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append(strConfigName);
- if (!Base_Exist(strCfgPath.GetData()))
- {
- Dbg("runcfg [%s] not exist", (const char*)strCfgPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("配置不存在").c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- auto cfgSize = Base_filesize(strCfgPath.GetData());
- char* tmpBuf = new char[cfgSize];
- memset(tmpBuf, 0, cfgSize);
- auto readRet = Base_readFile(strCfgPath.GetData(), 0, tmpBuf, cfgSize);
- if (!readRet.first)
- {
- pReplyPkg->SetErrMsg(0, 0, SP::Utility::UTF8ToGBK("open runcfg fail").c_str());
- Dbg("open file [%s] fail", (const char*)strCfgPath);
- }
- else
- {
- int nRetLen = sizeof(RvcViewRunCfgRet) + cfgSize * 2;
- char* pBuf = new char[nRetLen];
- memset(pBuf, 0, nRetLen);
- RvcViewRunCfgRet* pRet = (RvcViewRunCfgRet*)pBuf;
- int dstPos = 0;
- for (auto i = 0; i < cfgSize; i++)
- {
- if ((i == 0 && tmpBuf[i] == '\n') || (tmpBuf[i] == '\n' && tmpBuf[i - 1] != '\r'))
- pRet->ConfigData[dstPos++] = '\r';
- pRet->ConfigData[dstPos++] = tmpBuf[i];
- }
- pReplyPkg->AddStruct("VRunCfgA", false, false, (BYTE*)pBuf, dstPos + sizeof(RvcViewConfigRet));
- }
-
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleViewInstallLogReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote view install log");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("VPkgLogR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [VPkgLogR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcViewInstallLogReq));
- RvcViewInstallLogReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("VPkgLogR", (BYTE*)&req, &nLen, &nArrayLen);
- CSimpleStringA strEntityName = req.InstallLog;
- // 读取升级日志文件
- CSimpleStringA strLogFile = req.InstallLog;
- CSimpleStringA strLogPath;
- auto rc = m_pEntity->GetFunction()->GetPath("RunInfo", strLogPath);
- assert(rc == Error_Succeed);
- strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)strLogFile);
- if (!Base_Exist(strLogPath.GetData()))
- {
- Dbg("install log [%s] not exist", (const char*)strLogPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("日志文件不存在").c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
-
- auto file = fopen(strLogPath, "rb");
- if (file == NULL)
- {
- auto nError = errno;
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("open install log [%s] fail(%d)", (const char*)strLogPath, nError).GetData()).c_str());
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("open install log [%s] fail(%d)", (const char*)strLogPath, nError));
- SendPackage(pReplyPkg);
- return rc;
- }
- generateFilesize(file, strLogPath);
- long long nRetLen = sizeof(RvcViewInstallLogRet)+nFileSize;
- char *pBuf = new char[nRetLen];
- memset(pBuf, 0, nRetLen);
- RvcViewInstallLogRet *pRet = (RvcViewInstallLogRet*)pBuf;
- pRet->TaskID = req.TaskID;
- char *pLogData = (char*)pRet->LogData;
- long long nHasRead(0);
- DWORD nReadLen(0);
- do
- {
- //Dbg("ftell: %d", ftell(file));
- nReadLen = fread(pLogData + nHasRead, 1, nFileSize - nHasRead, file);
- //Dbg("ftell: %d", ftell(file));
- if (nReadLen >0)
- nHasRead += nReadLen;
- } while (nReadLen >0 && nHasRead < nFileSize);
- fclose(file);
- std::string gbkLogStr = SP::Utility::UTF8ToGBK(pLogData);
- int gbkLen = sizeof(RvcViewInstallLogRet) + gbkLogStr.length();
- char* gbkBuf = new char[gbkLen];
- memset(gbkBuf, 0, gbkLen);
- RvcViewInstallLogRet* gbkRet = (RvcViewInstallLogRet*)gbkBuf;
- gbkRet->TaskID = req.TaskID;
- memcpy(gbkRet->LogData, gbkLogStr.c_str(), gbkLogStr.length());
- pReplyPkg->AddStruct("VPkgLogA", false, false, (BYTE*)gbkRet, gbkLen);
- delete[] pBuf;
- delete[] gbkBuf;
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::ZipFile(LPCTSTR lpszZipArchive, LPCTSTR lpszSrcFile)
- {
- BOOL bResult = TRUE;
- assert(lpszZipArchive);
- assert(lpszZipArchive[0] != ('\0'));
- if (!lpszZipArchive || lpszZipArchive[0] == ('\0'))
- return Error_Param;
- assert(lpszSrcFile);
- assert(lpszSrcFile[0] != ('\0'));
- if (!lpszSrcFile || lpszSrcFile[0] == ('\0'))
- return Error_Param;
- // does zip source file exist?
- if (!Base_Exist(lpszSrcFile))
- {
- Dbg("source file '%s' cannot be found", lpszSrcFile);
- return Error_NotExist;
- }
- // does zip file exist?
- if (Base_Exist(lpszZipArchive))
- {
- Dbg("dest zip file '%s' exists, delete it!", lpszZipArchive);
- Base_DeleteFile(lpszZipArchive);
- }
- // 先拷贝源文件(防止文件被占用导致压缩失败)
- CSimpleStringA strCopyFile = lpszSrcFile;
- strCopyFile += ".bak";
- Base_CopyFile(lpszSrcFile, strCopyFile.GetData());
- // use only the file name for zip file entry
- TCHAR * cp = (TCHAR *)strrchr(lpszSrcFile, SPLIT_SLASH);
- if (cp == NULL)
- cp = (TCHAR *)lpszSrcFile;
- else
- cp++;
- #if (defined _WIN32 || defined _WIN64)
- HZIP hz = CreateZip((void *)lpszZipArchive, 0, ZIP_FILENAME);
- #else
- zipFile hz = zipOpen(lpszZipArchive, APPEND_STATUS_CREATE); //创建zip文件
- #endif
- if (hz)
- {
- #if (defined _WIN32 || defined _WIN64)
- ZRESULT zr = ZipAdd(hz, cp, (void *)(const char*)strCopyFile, 0, ZIP_FILENAME);
- CloseZip(hz);
- if (zr == ZR_OK)
- #else
- auto zr = AddFileToZip(hz, cp, strCopyFile.GetData());
- zipClose(hz, NULL);
- if (zr)
- #endif
-
- {
- Dbg("added '%s' to zip file '%s' succeed", lpszSrcFile, lpszZipArchive);
- bResult = TRUE;
- }
- else
- {
- Dbg("failed to add zip source file '%s'", lpszSrcFile);
- bResult = FALSE;
- }
- }
- else
- {
- Dbg("failed to create zip file '%s'", lpszZipArchive);
- bResult = FALSE;
- }
- if (Base_Exist(strCopyFile.GetData()))
- Base_DeleteFile(strCopyFile.GetData());
- if (!bResult && Base_Exist(lpszZipArchive))
- Base_DeleteFile(lpszZipArchive);
- return bResult ? Error_Succeed : Error_Unexpect;
- }
- ErrorCodeEnum CRemoteControllerCnn::BatchZipFiles(LPCTSTR lpszZipArchive, const list<string> &listSrcFiles)
- {
- BOOL bResult = TRUE;
- if (!lpszZipArchive || lpszZipArchive[0] == ('\0'))
- return Error_Param;
- // does zip file exist?
- if (Base_Exist(lpszZipArchive))
- {
- Dbg("dest zip file '%s' exists, delete it!", lpszZipArchive);
- Base_DeleteFile(lpszZipArchive);
- }
- #if (defined _WIN32 || defined _WIN64)
- HZIP hz = CreateZip((void*)lpszZipArchive, 0, ZIP_FILENAME);
- #else
- zipFile hz = zipOpen(lpszZipArchive, APPEND_STATUS_CREATE); //创建zip文件
- #endif
- if (!hz)
- {
- Dbg("failed to create zip file '%s'", lpszZipArchive);
- return Error_Unexpect;
- }
-
- // does zip source file exist?
- for (auto it = listSrcFiles.begin(); it != listSrcFiles.end(); it++)
- {
- if (!Base_Exist(it->c_str()))
- {
- Dbg("source file '%s' cannot be found", it->c_str());
- bResult = FALSE;
- break;
- }
- // 先拷贝源文件(防止文件被占用导致压缩失败)
- CSimpleStringA strSrcFile = it->c_str();
- CSimpleStringA strCopyFile = strSrcFile;
- strCopyFile += ".bak";
- Base_CopyFile(strSrcFile.GetData(), strCopyFile.GetData());
- // use only the file name for zip file entry
- CSimpleStringA strTmp = strSrcFile;
- const TCHAR * cp = strrchr(strTmp, SPLIT_SLASH);
- if (cp != NULL)
- {
- *((char*)cp) = '_';
- cp = (TCHAR *)strrchr(strTmp, SPLIT_SLASH);
- }
- if (cp != NULL)
- cp++;
- else
- cp = strTmp;
- #if (defined _WIN32 || defined _WIN64)
- ZRESULT zr = ZipAdd(hz, cp, (void*)(const char*)strCopyFile, 0, ZIP_FILENAME);
- CloseZip(hz);
- #else
- auto zr = AddFileToZip(hz, cp, strCopyFile.GetData());
- #endif
- // delete .bak file
- if (Base_Exist(strCopyFile.GetData()))
- Base_DeleteFile(strCopyFile.GetData());
- // did add work?
- #if (defined _WIN32 || defined _WIN64)
- if (zr == ZR_OK)
- #else
- if(zr)
- #endif
- {
- Dbg("added '%s' to zip file '%s' succeed", cp, lpszZipArchive);
- }
- else
- {
- Dbg("failed to add zip source file '%s'", cp);
- bResult = FALSE;
-
- break;
- }
- }
- if (!bResult && Base_Exist(lpszZipArchive))
- {
- Base_DeleteFile(lpszZipArchive);
- }
- zipClose(hz, NULL);
- return bResult ? Error_Succeed : Error_Unexpect;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleUploadLogByDateReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote upload log");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("ULogBDR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ULogBDR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcUploadLogByDateReq));
- RvcUploadLogByDateReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("ULogBDR", (BYTE*)&req, &nLen, &nArrayLen);
- auto pFunc = m_pEntity->GetFunction();
- CSimpleStringA strZipFileName;
- CSimpleStringA strLogPath;
- CSimpleStringA strZipPath;
- if (req.StartPos <= 0)
- {
- auto strTodayLogName = 0 == strlen(req.ViewDate) ? CSimpleStringA(getCurData().c_str()) : CSimpleStringA(req.ViewDate);
- CSystemStaticInfo sysInfo;
- auto rc = pFunc->GetSystemStaticInfo(sysInfo);
- assert(rc == Error_Succeed);
- CSimpleStringA strReqLogName = req.UploadFile;
-
- // 第一次上传,需生成对应ZIP文件
- if (strReqLogName.Compare("Silverlight", true) == 0) // slv日志
- {
- strZipFileName = CSimpleStringA::Format("%s_slv_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
-
- pFunc->GetPath("Slv", strLogPath);
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- }
- else if (strReqLogName.IsEndWith(".zip", true)) // 安装包日志
- {
- strZipFileName = CSimpleStringA::Format("%s_pkg_%s", (const char*)sysInfo.strTerminalID, (const char*)strReqLogName);
- pFunc->GetPath("RunInfo", strLogPath);
- strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)strReqLogName).Append(".log");
- }
- else
- {
- // 实体Dbg日志
- strZipFileName = CSimpleStringA::Format("%s_dbg_%s_%s.zip",
- (const char*)sysInfo.strTerminalID, (const char*)strReqLogName, (const char*)strTodayLogName);
- pFunc->GetPath("Dbg", strLogPath);
- if (strReqLogName.Compare("Shell", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("DualActive", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("guardian", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("SpHost", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("SysInit", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("SpBase", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else
- {
- CEntityStaticInfo info = {};
- auto nRet = pFunc->GetEntityStaticInfo(strReqLogName, info);
- if (nRet != Error_Succeed)
- {
- Dbg("entity [%s] not exist", (const char*)strReqLogName);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)strReqLogName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- }
- }
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
-
- if (!Base_Exist(strLogPath.GetData()))
- {
- Dbg("log [%s] not exist", (const char*)strLogPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- // 生成ZIP
- rc = ZipFile(strZipPath, strLogPath);
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return rc;
- }
- }
- else
- {
- strZipFileName = req.UploadFile;
-
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
- }
-
- if (!Base_Exist(strZipPath.GetData()))
- {
- Dbg("zip log [%s] not exist", (const char*)strZipPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- // 开始上传
- long long nStartPos = req.StartPos;
- if (nStartPos <0)
- nStartPos = 0;
- long long nMaxBytes = req.MaxRetBytes;
- if (nMaxBytes <= 0)
- nMaxBytes = 32 * 1024;
- auto file = fopen(strZipPath, "rb");
- if (file == NULL)
- {
- auto nError = errno;
- pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- generateFilesize(file, strZipPath);
-
- if (nStartPos >0 && nStartPos < nFileSize)
- {
- _fseeki64(file, nStartPos, SEEK_SET);
- }
- else
- nStartPos = 0;
- long long nNextPos = nStartPos + nMaxBytes;
- if (nMaxBytes + nStartPos > nFileSize)
- {
- nMaxBytes = nFileSize - nStartPos;
- nNextPos = 0;
- }
- long long nRetLen = sizeof(RvcUploadLogByDateRet)+nMaxBytes;
- char *pBuf = new char[nRetLen];
- memset(pBuf, 0, nRetLen);
- RvcUploadLogByDateRet *pRet = (RvcUploadLogByDateRet*)pBuf;
- pRet->TaskID = req.TaskID;
- strncpy_s(pRet->UploadFile, sizeof(pRet->UploadFile), strZipFileName, _TRUNCATE);
- pRet->ZipFileLen = nFileSize;
- pRet->StartPos = req.StartPos;
- pRet->NextPos = nNextPos;
- char *pLogData = (char*)pRet->LogData;
- long long nHasRead(0);
- DWORD nReadLen(0);
- do
- {
- //Dbg("ftell: %d", ftell(file));
- nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
- //Dbg("ftell: %d", ftell(file));
- if (nReadLen >0)
- nHasRead += nReadLen;
- } while (nReadLen >0 && nHasRead < nMaxBytes);
- fclose(file);
- if (nHasRead < nMaxBytes)
- {
- delete[] pBuf;
- auto nError = GetLastError();
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- pReplyPkg->AddStruct("ULogBDA", false, false, (BYTE*)pBuf, nRetLen);
- delete[] pBuf;
- SendPackage(pReplyPkg);
- if (nNextPos <= 0)
- {
- // 删除压缩文件
- Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
- Base_DeleteFile(strZipPath.GetData());
- }
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleUploadLogReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote upload log");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("UpldLogR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [UpldLogR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcUploadLogReq));
- RvcUploadLogReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("UpldLogR", (BYTE*)&req, &nLen, &nArrayLen);
- auto pFunc = m_pEntity->GetFunction();
- CSimpleStringA strZipFileName;
- CSimpleStringA strLogPath;
- CSimpleStringA strZipPath;
- if (req.StartPos <= 0)
- {
- auto strTodayLogName = getCurData().c_str();
- CSystemStaticInfo sysInfo;
- auto rc = pFunc->GetSystemStaticInfo(sysInfo);
- assert(rc == Error_Succeed);
- CSimpleStringA strReqLogName = req.UploadFile;
-
- // 第一次上传,需生成对应ZIP文件
- if (strReqLogName.Compare("Silverlight", true) == 0) // slv日志
- {
- strZipFileName = CSimpleStringA::Format("%s_slv_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
-
- pFunc->GetPath("Slv", strLogPath);
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
-
- }
- else if (strReqLogName.Compare("AllDbg", true) == 0)
- {
- strZipFileName = CSimpleStringA::Format("%s_AllDbg_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
- pFunc->GetPath("Dbg", strLogPath);
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR).Append((const char*)strZipFileName);
- try
- {
- std::pair<bool, std::string> result = MyZip::ZipDir(strZipPath.GetData(), strLogPath.GetData());
- if (result.first)
- goto BeginUpload;
- else
- {
- Dbg("%s zip failed", strZipFileName);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("%s zip failed", strZipFileName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- }
- catch (exception &e)
- {
- Dbg("error:%s", e.what());
- }
- }
- else if (strReqLogName.Compare("RunInfomation", true) == 0)
- {
- strZipFileName = CSimpleStringA::Format("%s_runInformation_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR).Append((const char*)strZipFileName);
- vector<std::string> zipArr;
- pFunc->GetPath("RunInfo", strLogPath);
- zipArr.push_back(strLogPath.GetData());
- pFunc->GetPath("HardwareCfg", strLogPath);
- strLogPath.Append(SPLIT_SLASH_STR).Append("root.ini");
- zipArr.push_back(strLogPath.GetData());
- Dbg("%s,%s,%s", zipArr[0].c_str(), zipArr[1].c_str(), strZipPath);
- std::pair<bool, std::string> result = MyZip::ZipVector(strZipPath.GetData(), zipArr);
- if (result.first)
- goto BeginUpload;
- else
- {
- Dbg("%s zip failed", strZipFileName);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("%s zip failed", strZipFileName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- }
- else if (strReqLogName.IsEndWith(".zip", true)) // 安装包日志
- {
- strZipFileName = CSimpleStringA::Format("%s_pkg_%s", (const char*)sysInfo.strTerminalID, (const char*)strReqLogName);
- pFunc->GetPath("RunInfo", strLogPath);
- strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)strReqLogName).Append(".log");
- }
- else
- {
- // 实体Dbg日志
- strZipFileName = CSimpleStringA::Format("%s_dbg_%s_%s.zip",
- (const char*)sysInfo.strTerminalID, (const char*)strReqLogName, (const char*)strTodayLogName);
- pFunc->GetPath("Dbg", strLogPath);
- if (strReqLogName.Compare("Shell", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("DualActive", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("guardian", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("SpHost", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("SysInit", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (strReqLogName.Compare("SpBase", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else
- {
- CEntityStaticInfo info = {};
- auto nRet = pFunc->GetEntityStaticInfo(strReqLogName, info);
- if (nRet != Error_Succeed)
- {
- Dbg("entity [%s] not exist", (const char*)strReqLogName);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)strReqLogName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- }
- }
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
-
- if (!Base_Exist(strLogPath.GetData()))
- {
- Dbg("log [%s] not exist", (const char*)strLogPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- // 生成ZIP
- rc = ZipFile(strZipPath, strLogPath);
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return rc;
- }
- }
- else
- {
- strZipFileName = req.UploadFile;
-
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
- }
-
- if (!Base_Exist(strZipPath.GetData()))
- {
- Dbg("zip log [%s] not exist", (const char*)strZipPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- BeginUpload:
- // 开始上传
- long long nStartPos = req.StartPos;
- if (nStartPos <0)
- nStartPos = 0;
- long long nMaxBytes = req.MaxRetBytes;
- if (nMaxBytes <= 0)
- nMaxBytes = 32 * 1024;
- auto file = fopen(strZipPath, "rb");
- if (file == NULL)
- {
- auto nError = errno;
- pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- generateFilesize(file, strZipPath);
- if (nStartPos >0 && nStartPos < nFileSize)
- {
- _fseeki64(file, nStartPos, SEEK_SET);
- }
- else
- nStartPos = 0;
- long long nNextPos = nStartPos + nMaxBytes;
- if (nMaxBytes + nStartPos > nFileSize)
- {
- nMaxBytes = nFileSize - nStartPos;
- nNextPos = 0;
- }
- long long nRetLen = sizeof(RvcUploadLogRet)+nMaxBytes;
- char *pBuf = new char[nRetLen];
- memset(pBuf, 0, nRetLen);
- RvcUploadLogRet *pRet = (RvcUploadLogRet*)pBuf;
- pRet->TaskID = req.TaskID;
- strncpy_s(pRet->UploadFile, sizeof(pRet->UploadFile), strZipFileName, _TRUNCATE);
- pRet->ZipFileLen = nFileSize;
- pRet->StartPos = req.StartPos;
- pRet->NextPos = nNextPos;
- char *pLogData = (char*)pRet->LogData;
- long long nHasRead(0);
- DWORD nReadLen(0);
- do
- {
- //Dbg("ftell: %d", ftell(file));
- nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
- //Dbg("ftell: %d", ftell(file));
- if (nReadLen >0)
- nHasRead += nReadLen;
- } while (nReadLen >0 && nHasRead < nMaxBytes);
- fclose(file);
- if (nHasRead < nMaxBytes)
- {
- delete[] pBuf;
- auto nError = GetLastError();
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- pReplyPkg->AddStruct("UpldLogA", false, false, (BYTE*)pBuf, nRetLen);
- delete[] pBuf;
- SendPackage(pReplyPkg);
- if (nNextPos <= 0)
- {
- // 删除压缩文件
- Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
- Base_DeleteFile(strZipPath.GetData());
- }
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleViewEntityStateExReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote view entity state ex");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("ViEnExR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ViEnExR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcViewEntityExReq));
- RvcViewEntityExReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("ViEnExR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcViewEntityExRet1 ret1 = {};
- ret1.TaskID = req.TaskID;
- pReplyPkg->AddStruct("ViEnExA1", false, false, (BYTE*)&ret1, sizeof(ret1));
- auto pFunc = m_pEntity->GetFunction();
- CAutoArray<CSimpleStringA> strEntityNames;
- CAutoArray<WORD> dwEntityIDs;
- pFunc->GetAllRegistedEntity(strEntityNames, dwEntityIDs);
- int nCount = strEntityNames.GetCount();
- if (nCount >0)
- {
- nCount += 8; // 包括root\shell\silverlight3个虚拟实体
- RvcViewEntityExRet2 *pRet2 = new RvcViewEntityExRet2[nCount];
- memset(pRet2, 0, sizeof(RvcViewEntityExRet2)* nCount);
- CSimpleStringA strEntCfgDir;
- CSimpleStringA strRunCfgDir;
- pFunc->GetPath("Cfg", strEntCfgDir);
- strEntCfgDir.Append(SPLIT_SLASH_STR);
- pFunc->GetPath("RunInfo", strRunCfgDir);
- strRunCfgDir.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR);
- for (int i = 0; i<nCount - 8; i++)
- {
- CEntityRunInfo info = {};
- CSimpleStringA strEntityName = strEntityNames[i];
- pFunc->GetEntityRunInfo(strEntityName, info);
- strncpy_s(pRet2[i].EntityName, sizeof(pRet2[i].EntityName), (const char*)strEntityName, _TRUNCATE);
- pRet2[i].EntityID = dwEntityIDs[i];
- pRet2[i].EntityState = info.eState;
- pRet2[i].ProcessID = info.dwProcessID;
- if (info.dwProcessID > 0)
- {
- #if (defined _WIN32 || defined _WIN64)
- auto hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, info.dwProcessID);
- if (hProcess != NULL)
- {
- PROCESS_MEMORY_COUNTERS pmc = {};
- GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
- pRet2[i].UsedMemory = pmc.WorkingSetSize / 1024 / 1024;
- }
- #else
- pRet2[i].UsedMemory = get_proc_mem(info.dwProcessID) / 1024 / 1024;
- #endif
- }
- CSimpleStringA strEntCfgPath = strEntCfgDir + strEntityName + ".ini";
- CSimpleStringA strRunCfgPath = strRunCfgDir + strEntityName + ".ini";
- pRet2[i].HasConfig = Base_Exist(strEntCfgPath.GetData());
- pRet2[i].HasRunCfg = Base_Exist(strRunCfgPath.GetData());
- }
- strncpy_s(pRet2[nCount - 8].EntityName, sizeof(pRet2[nCount - 8].EntityName), "guardian", _TRUNCATE);
- pRet2[nCount - 8].HasConfig = false;
- strncpy_s(pRet2[nCount - 7].EntityName, sizeof(pRet2[nCount - 7].EntityName), "DualActive", _TRUNCATE);
- pRet2[nCount - 7].HasConfig = false;
- strncpy_s(pRet2[nCount - 6].EntityName, sizeof(pRet2[nCount - 6].EntityName), "SpHost", _TRUNCATE);
- pRet2[nCount - 6].HasConfig = false;
- strncpy_s(pRet2[nCount - 5].EntityName, sizeof(pRet2[nCount - 5].EntityName), "SysInit", _TRUNCATE);
- pRet2[nCount - 5].HasConfig = false;
- strncpy_s(pRet2[nCount - 4].EntityName, sizeof(pRet2[nCount - 4].EntityName), "SpBase", _TRUNCATE);
- pRet2[nCount - 4].HasConfig = false;
- // root\shell\silverlight3个虚拟实体
- strncpy_s(pRet2[nCount - 3].EntityName, sizeof(pRet2[nCount - 3].EntityName), "Root", _TRUNCATE);
- pRet2[nCount - 3].HasConfig = true;
- strncpy_s(pRet2[nCount - 2].EntityName, sizeof(pRet2[nCount - 2].EntityName), "Shell", _TRUNCATE);
- pRet2[nCount - 2].HasConfig = true;
- strncpy_s(pRet2[nCount - 1].EntityName, sizeof(pRet2[nCount - 1].EntityName), "Silverlight", _TRUNCATE);
- pReplyPkg->AddStruct("ViEnExA2", false, false, (BYTE*)pRet2, sizeof(RvcViewEntityExRet2)* nCount, nCount);
- }
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleBatchUploadLogsByDateReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote batch upload logs by date");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("ULogsBDR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ULogsBDR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcBatchUploadLogsByDateReq) || nLen == sizeof(RvcBatchUploadLogsByDateReq2));
- RvcBatchUploadLogsByDateReq req = {};
- RvcBatchUploadLogsByDateReq2 req2 = {};
- int nArrayLen(0);
- if (nLen == sizeof(RvcBatchUploadLogsByDateReq))
- pRecvPkg->GetStructData("ULogsBDR", (BYTE*)&req, &nLen, &nArrayLen);
- else
- {
- Dbg("RvcBatchUploadLogsByDateReq2");
- pRecvPkg->GetStructData("ULogsBDR", (BYTE*)&req2, &nLen, &nArrayLen);
- req.TaskID = req2.TaskID;
- strncpy_s(req.ViewDate, req2.ViewDate, sizeof(req2.ViewDate));
- strncpy_s(req.UploadFile, req2.UploadFile, sizeof(req2.UploadFile));
- strncpy_s(req.ZipFileName, req2.ZipFileName, sizeof(req2.ZipFileName));
- req.StartPos = req2.StartPos;
- req.MaxRetBytes = req2.MaxRetBytes;
- }
- Dbg("UploadFile:%s, ZipFileName:%s", req.UploadFile, req.ZipFileName);
-
-
-
- CSimpleStringA strZipPath;
- auto pFunc = m_pEntity->GetFunction();
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR);
- CSimpleStringA strZipFileName = req.ZipFileName;
- strZipPath += strZipFileName;
- if (req.StartPos <= 0)
- {
- CSimpleStringA strLogPath;
- auto strTodayLogName = (0 == strlen(req.ViewDate) ? CSimpleStringA(getCurData().c_str()) : CSimpleStringA(req.ViewDate));
- CSimpleStringA strReqLogNames = req.UploadFile;
- auto arrFiles = strReqLogNames.Split(';');
- list<string> listSrcFiles;
- for (int i = 0; i < arrFiles.GetCount(); i++)
- {
- // 第一次上传,需生成对应ZIP文件
- if (arrFiles[i].Compare("Silverlight", true) == 0) // slv日志
- {
- pFunc->GetPath("Slv", strLogPath);
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- }
- else if (arrFiles[i].IsEndWith(".zip", true)) // 安装包日志
- {
- pFunc->GetPath("RunInfo", strLogPath);
- strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)arrFiles[i]).Append(".log");
- }
- else
- {
- // 实体Dbg日志
- pFunc->GetPath("Dbg", strLogPath);
- if (arrFiles[i].Compare("Shell", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("DualActive", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("guardian", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("SpHost", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("SysInit", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("SpBase", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("Root", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("Root").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else
- {
- CEntityStaticInfo info = {};
- auto nRet = pFunc->GetEntityStaticInfo(arrFiles[i], info);
- if (nRet != Error_Succeed)
- {
- Dbg("entity [%s] not exist", (const char*)arrFiles[i]);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)arrFiles[i]).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- }
- }
- if (!Base_Exist(strLogPath.GetData()))
- {
- Dbg("log [%s] not exist", (const char*)strLogPath);
- continue;
- }
- listSrcFiles.push_back((const char*)strLogPath);
- }
- if (listSrcFiles.size() == 0)
- {
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("日志文件不存在").c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- Dbg("strZipPath:%s", strZipPath.GetData());
- // 生成ZIP
- auto rc = BatchZipFiles(strZipPath, listSrcFiles);
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return rc;
- }
- }
- if (!Base_Exist(strZipPath.GetData()))
- {
- Dbg("zip log [%s] not exist", (const char*)strZipPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- // 开始上传
- long long nStartPos = req.StartPos;
- if (nStartPos <0)
- nStartPos = 0;
- long long nMaxBytes = req.MaxRetBytes;
- if (nMaxBytes <= 0)
- nMaxBytes = 32 * 1024;
- auto file = fopen(strZipPath, "rb");
- if (file == NULL)
- {
- auto nError = errno;
- pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- generateFilesize(file, strZipPath);
- if (nStartPos >0 && nStartPos < nFileSize)
- {
- _fseeki64(file, nStartPos, SEEK_SET);
- }
- else
- nStartPos = 0;
- long long nNextPos = nStartPos + nMaxBytes;
- if (nMaxBytes + nStartPos > nFileSize)
- {
- nMaxBytes = nFileSize - nStartPos;
- nNextPos = 0;
- }
- long long nRetLen = sizeof(RvcBatchUploadLogsByDateRet) + nMaxBytes;
- char *pBuf = new char[nRetLen];
- memset(pBuf, 0, nRetLen);
- RvcBatchUploadLogsByDateRet *pRet = (RvcBatchUploadLogsByDateRet*)pBuf;
- pRet->TaskID = req.TaskID;
- strncpy_s(pRet->ZipFileName, sizeof(pRet->ZipFileName), strZipFileName, _TRUNCATE);
- pRet->ZipFileLen = nFileSize;
- pRet->StartPos = req.StartPos;
- pRet->NextPos = nNextPos;
- char *pLogData = (char*)pRet->LogData;
- long long nHasRead(0);
- DWORD nReadLen(0);
- do
- {
- //Dbg("ftell: %d", ftell(file));
- nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
- //Dbg("ftell: %d", ftell(file));
- if (nReadLen >0)
- nHasRead += nReadLen;
- } while (nReadLen >0 && nHasRead < nMaxBytes);
- fclose(file);
- if (nHasRead < nMaxBytes)
- {
- delete[] pBuf;
- auto nError = GetLastError();
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- pReplyPkg->AddStruct("ULogsBDA", false, false, (BYTE*)pBuf, nRetLen);
- delete[] pBuf;
- SendPackage(pReplyPkg);
- if (nNextPos <= 0)
- {
- // 删除压缩文件
- Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
- Base_DeleteFile(strZipPath.GetData());
- }
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleBatchUploadLogsReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote batch upload logs");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("UpdLogsR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [UpdLogsR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcBatchUploadLogsReq) || nLen == sizeof(RvcBatchUploadLogsReq2));
- RvcBatchUploadLogsReq req = {};
- RvcBatchUploadLogsReq2 req2 = {};
- int nArrayLen(0);
- if (nLen == sizeof(RvcBatchUploadLogsReq))
- pRecvPkg->GetStructData("UpdLogsR", (BYTE*)&req, &nLen, &nArrayLen);
- else
- {
- pRecvPkg->GetStructData("UpdLogsR", (BYTE*)&req2, &nLen, &nArrayLen);
- req.TaskID = req2.TaskID;
- req.StartPos = req2.StartPos;
- req.MaxRetBytes = req2.MaxRetBytes;
- strncpy_s(req.UploadFile, req2.UploadFile, sizeof(req2.UploadFile));
- strncpy_s(req.ZipFileName, req2.ZipFileName, sizeof(req2.ZipFileName));
- }
-
-
- CSimpleStringA strZipPath;
- auto pFunc = m_pEntity->GetFunction();
- pFunc->GetPath("Temp", strZipPath);
- strZipPath.Append(SPLIT_SLASH_STR);
- CSimpleStringA strZipFileName = req.ZipFileName;
- strZipPath += strZipFileName;
- if (req.StartPos <= 0)
- {
- CSimpleStringA strLogPath;
- auto strTodayLogName = CSimpleStringA(getCurData().c_str());
- CSimpleStringA strReqLogNames = req.UploadFile;
- auto arrFiles = strReqLogNames.Split(';');
- list<string> listSrcFiles;
- for (int i = 0; i < arrFiles.GetCount(); i++)
- {
- // 第一次上传,需生成对应ZIP文件
- if (arrFiles[i].Compare("Silverlight", true) == 0) // slv日志
- {
- pFunc->GetPath("Slv", strLogPath);
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- }
- else if (arrFiles[i].IsEndWith(".zip", true)) // 安装包日志
- {
- pFunc->GetPath("RunInfo", strLogPath);
- strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)arrFiles[i]).Append(".log");
- }
- else
- {
- // 实体Dbg日志
- pFunc->GetPath("Dbg", strLogPath);
- if (arrFiles[i].Compare("Shell", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("DualActive", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("guardian", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("SpHost", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("SysInit", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("SpBase", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else if (arrFiles[i].Compare("Root", true) == 0)
- strLogPath.Append(SPLIT_SLASH_STR).Append("Root").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- else
- {
- CEntityStaticInfo info = {};
- auto nRet = pFunc->GetEntityStaticInfo(arrFiles[i], info);
- if (nRet != Error_Succeed)
- {
- Dbg("entity [%s] not exist", (const char*)arrFiles[i]);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)arrFiles[i]).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
- }
- }
- if (!Base_Exist(strLogPath.GetData()))
- {
- Dbg("log [%s] not exist", (const char*)strLogPath);
- continue;
- }
- listSrcFiles.push_back((const char*)strLogPath);
- }
- if (listSrcFiles.size() == 0)
- {
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("日志文件不存在").c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- // 生成ZIP
- auto rc = BatchZipFiles(strZipPath, listSrcFiles);
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
- SendPackage(pReplyPkg);
- return rc;
- }
- }
- if (!Base_Exist(strZipPath.GetData()))
- {
- Dbg("zip log [%s] not exist", (const char*)strZipPath);
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
- SendPackage(pReplyPkg);
- return Error_NotExist;
- }
- // 开始上传
- long long nStartPos = req.StartPos;
- if (nStartPos <0)
- nStartPos = 0;
- long long nMaxBytes = req.MaxRetBytes;
- if (nMaxBytes <= 0)
- nMaxBytes = 32 * 1024;
- auto file = fopen(strZipPath, "rb");
- if (file == NULL)
- {
- auto nError = errno;
- pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- generateFilesize(file, strZipPath);
- if (nStartPos >0 && nStartPos < nFileSize)
- {
- _fseeki64(file, nStartPos, SEEK_SET);
- }
- else
- nStartPos = 0;
- long long nNextPos = nStartPos + nMaxBytes;
- if (nMaxBytes + nStartPos > nFileSize)
- {
- nMaxBytes = nFileSize - nStartPos;
- nNextPos = 0;
- }
- long long nRetLen = sizeof(RvcBatchUploadLogsRet) + nMaxBytes;
- char *pBuf = new char[nRetLen];
- memset(pBuf, 0, nRetLen);
- RvcBatchUploadLogsRet *pRet = (RvcBatchUploadLogsRet*)pBuf;
- pRet->TaskID = req.TaskID;
- strncpy_s(pRet->ZipFileName, sizeof(pRet->ZipFileName), strZipFileName, _TRUNCATE);
- pRet->ZipFileLen = nFileSize;
- pRet->StartPos = req.StartPos;
- pRet->NextPos = nNextPos;
- char *pLogData = (char*)pRet->LogData;
- long long nHasRead(0);
- DWORD nReadLen(0);
- do
- {
- nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
- if (nReadLen >0)
- nHasRead += nReadLen;
- } while (nReadLen >0 && nHasRead < nMaxBytes);
- fclose(file);
- if (nHasRead < nMaxBytes)
- {
- delete[] pBuf;
- auto nError = GetLastError();
- pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
- Dbg(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
- SendPackage(pReplyPkg);
- return Error_Unexpect;
- }
- pReplyPkg->AddStruct("UpdLogsA", false, false, (BYTE*)pBuf, nRetLen);
- delete[] pBuf;
- SendPackage(pReplyPkg);
- if (nNextPos <= 0)
- {
- // 删除压缩文件
- Dbg("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
- Base_DeleteFile(strZipPath.GetData());
- }
- return Error_Succeed;
- }
- ErrorCodeEnum CRemoteControllerCnn::HandleCancelUpgradeReq(const CSmartPointer<IPackage> &pRecvPkg)
- {
- Dbg("remote cancel upgarde");
- auto pReplyPkg = CreateReplyPackage(pRecvPkg);
- int nLen = pRecvPkg->GetStructLen("CancUpgR");
- if (nLen <= 0)
- {
- pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [CancUpgR] fail").c_str());
- SendPackage(pReplyPkg);
- return Error_Param;
- }
- assert(nLen == sizeof(RvcCancelUpgradeReq));
- RvcCancelUpgradeReq req = {};
- int nArrayLen(0);
- pRecvPkg->GetStructData("CancUpgR", (BYTE*)&req, &nLen, &nArrayLen);
- RvcCancelUpgradeRet ret = {};
- ret.TaskID = req.TaskID;
- pReplyPkg->AddStruct("CancUpgA", false, false, (BYTE*)&ret, sizeof(ret));
- CSimpleStringA strPack = req.PackName;
- Dbg("begin cancel upgrade: %s", (const char*)strPack);
- // 调用升级实体接口升级
- UpgradeMgrService_ClientBase *pClient = new UpgradeMgrService_ClientBase(m_pEntity);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- UpgradeMgrService_CancelUpdate_Req req = {};
- req.strPackFile = strPack;
- UpgradeMgrService_CancelUpdate_Ans ans = {};
- rc = pClient->CancelUpdate(req, ans, 10000);
- pClient->GetFunction()->CloseSession();
- } else {
- pClient->SafeDelete();
- }
- if (rc != Error_Succeed)
- {
- pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("远程取消升级失败").c_str());
- Dbg("remote cancel upgrade fail: 0x%X", rc);
- }
- else
- {
- Dbg("remote cancel upgrade succeed!");
- }
- SendPackage(pReplyPkg);
- return Error_Succeed;
- }
|