RemoteControllerCnn.cpp 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407
  1. #if (defined _WIN32 || defined _WIN64)
  2. #include <io.h>
  3. #include <winsock2.h>
  4. #include <ws2tcpip.h>
  5. #include <Psapi.h>
  6. #include "fileutil.h"
  7. #pragma comment(lib,"psapi.lib")
  8. #endif
  9. #include "RemoteControllerCnn.h"
  10. #include "RemoteControllerFSM.h"
  11. #include "Event.h"
  12. #include <stdlib.h>
  13. #include "cJSON.h"
  14. #include "base64.h"
  15. #include "digital_conv.h"
  16. #include <fstream>
  17. #include "path.h"
  18. #include "SpUtility.h"
  19. #include "../mod_healthmanager/HealthManager_client_g.h"
  20. using namespace HealthManager;
  21. #define __int64 long int
  22. #include "../mod_UpgradeMgr/UpgradeManager_client_g.h"
  23. using namespace UpgradeManager;
  24. #include "../mod_guiconsole/GUIConsole_client_g.h"
  25. using namespace GUIConsole;
  26. #include "../mod_ResourceWatcher/ResourceWatcher_client_g.h"
  27. using namespace ResourceWatcher;
  28. #include "remoteBase.h"
  29. #include "MyZip.h"
  30. namespace fs = boost::filesystem;
  31. #include <functional>
  32. #if (defined _WIN32 || defined _WIN64)
  33. #define generateFilesize(fileHandle, filePath) long long nFileSize = _filelengthi64(_fileno(fileHandle))
  34. #include "XZip.h"
  35. #else
  36. #define generateFilesize(fileHandle, filePath) long long nFileSize = fs::file_size(filePath.GetData())
  37. #include "zip.h"
  38. #include <stdlib.h>
  39. #include "XZipZilb.h"
  40. #include <sstream>
  41. #define ZeroMemory(object, size) memset(object, 0, size)
  42. #define WritePrivateProfileStringA(a, b, c, d) Base_writeIni(std::string(a), std::string(b), std::string(c), std::string(d))
  43. #if defined(RVC_OS_LINUX)
  44. void _ui64toa(unsigned long long large_unit, char* str, int hexdigith)
  45. {
  46. auto largeNumStr = std::to_string(large_unit);
  47. strcpy(str, largeNumStr.c_str());
  48. /*
  49. std::stringstream ss;
  50. ss << large_unit;
  51. std::string src = ss.str();
  52. memset(str, 0, strsize);
  53. strncpy(str, src.c_str(), strsize);
  54. */
  55. }
  56. #endif //RVC_OS_LINUX
  57. int strncpy_s(char* _Destination, size_t _SizeInBytes, char const* _Source, size_t _MaxCount)
  58. {
  59. if (_MaxCount > _SizeInBytes)
  60. return nullptr == strncpy(_Destination, _Source, _SizeInBytes);
  61. else
  62. return nullptr == strncpy(_Destination, _Source, _MaxCount);
  63. }
  64. int strncpy_s(char* _Destination, char const* _Source, size_t _MaxCount)
  65. {
  66. return nullptr == strncpy(_Destination, _Source, _MaxCount);
  67. }
  68. #endif
  69. CRemoteControllerCnn::CRemoteControllerCnn(CEntityBase *pEntity, FSMBase *pFSM)
  70. :SpSecureClient(pEntity), m_pFSM(pFSM),m_chRemoteMode('N')
  71. {
  72. }
  73. CRemoteControllerCnn::~CRemoteControllerCnn(void)
  74. {
  75. }
  76. void CRemoteControllerCnn::OnDisconnect()
  77. {
  78. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("OnDisconnect")("connection disconnected");
  79. // 连接中断,重置远程维护状态,释放所有资源
  80. m_chRemoteMode = 'N';
  81. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_ReleaseChannel));
  82. }
  83. void CRemoteControllerCnn::OnPkgAnswer(const CSmartPointer<IPackage> &pRecvPkg)
  84. {
  85. string serviceCode = pRecvPkg->GetServiceCode();
  86. if (serviceCode == "StRemote")
  87. {
  88. HandleCreateRemoteChannelRet(pRecvPkg);
  89. }
  90. else if (serviceCode == "EdRemote")
  91. {
  92. HandleEndRemoteModeReq(pRecvPkg);
  93. }
  94. else if (serviceCode == "DelRun")
  95. {
  96. HandleDelRunInfoLogReq(pRecvPkg);
  97. }
  98. else if (serviceCode == "RestartS")
  99. {
  100. HandleRestartShellReq(pRecvPkg);
  101. }
  102. else if (serviceCode == "RestartO")
  103. {
  104. HandleRestartMachineReq(pRecvPkg);
  105. }
  106. else if (serviceCode == "ViewEnti")
  107. {
  108. HandleViewEntityStateReq(pRecvPkg);
  109. }
  110. else if (serviceCode == "ViewConf")
  111. {
  112. HandleViewEntityConfigReq(pRecvPkg);
  113. }
  114. else if (serviceCode == "ViewDbg")
  115. {
  116. HandleViewDebugLogReq(pRecvPkg);
  117. }
  118. else if (serviceCode == "OprDisk")
  119. {
  120. HandleOperateDiskReq(pRecvPkg);
  121. }
  122. else if (serviceCode == "EvtLog")
  123. {
  124. HandleUploadEventLogReq(pRecvPkg);
  125. }
  126. else if(serviceCode == "ULogBD")
  127. {
  128. HandleUploadLogByDateReq(pRecvPkg);
  129. }
  130. else if(serviceCode == "ULogsBD")
  131. {
  132. HandleBatchUploadLogsByDateReq(pRecvPkg);
  133. }
  134. else if (serviceCode == "EnterMM")
  135. {
  136. HandleEnterMaintainModeReq(pRecvPkg);
  137. }
  138. else if (serviceCode == "ExitMM")
  139. {
  140. HandleExitMaintainModeReq(pRecvPkg);
  141. }
  142. else if (serviceCode == "UpgPack")
  143. {
  144. HandleUpgradePackReq(pRecvPkg);
  145. }
  146. else if (serviceCode == "Rollback")
  147. {
  148. HandleRollbackUpgradeReq(pRecvPkg);
  149. }
  150. else if (serviceCode == "GetUser")
  151. {
  152. HandleGetCurMaintainerReq(pRecvPkg);
  153. }
  154. else if (serviceCode == "Enpower")
  155. {
  156. HandleEnpowerMaintainerReq(pRecvPkg);
  157. }
  158. else if (serviceCode == "TakePwr")
  159. {
  160. HandleTakeoverPowerReq(pRecvPkg);
  161. }
  162. else if (serviceCode == "ForceQt")
  163. {
  164. HandleForceQuitMaintainReq(pRecvPkg);
  165. }
  166. else if (serviceCode == "EditCfg")
  167. {
  168. HandleEditConfigReq(pRecvPkg);
  169. }
  170. else if (serviceCode == "ViewVar")
  171. {
  172. HandleViewSysVarReq(pRecvPkg);
  173. }
  174. else if (serviceCode == "GetVer")
  175. {
  176. HandleGetVersionReq(pRecvPkg);
  177. }
  178. else if (serviceCode == "VRunCfg")
  179. {
  180. HandleViewRunCfgReq(pRecvPkg);
  181. }
  182. else if (serviceCode == "VPkgLog")
  183. {
  184. HandleViewInstallLogReq(pRecvPkg);
  185. }
  186. else if (serviceCode == "UpldLog")
  187. {
  188. HandleUploadLogReq(pRecvPkg);
  189. }
  190. else if (serviceCode == "UpldLogs")
  191. {
  192. HandleBatchUploadLogsReq(pRecvPkg);
  193. }
  194. else if (serviceCode == "ViewEnEx")
  195. {
  196. HandleViewEntityStateExReq(pRecvPkg);
  197. }
  198. else if (serviceCode == "CancUpg")
  199. {
  200. HandleCancelUpgradeReq(pRecvPkg);
  201. }
  202. else
  203. {
  204. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("OnPkgAnswer")("unknown service code: %s", serviceCode.c_str());
  205. }
  206. }
  207. bool CRemoteControllerCnn::GetHostIPAddr(BYTE addr[4])
  208. {
  209. memset(addr, 0, 4);
  210. #if (defined _WIN32 || defined _WIN64)
  211. char szHostName[64] = {};
  212. int nRet = gethostname(szHostName, sizeof(szHostName));
  213. if (nRet != 0)
  214. {
  215. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("GetHostIPAddr")("gethostname fail: %d", nRet);
  216. return false;
  217. }
  218. addrinfo hints = {};
  219. hints.ai_family = AF_INET;
  220. PADDRINFOA pResult = NULL;
  221. nRet = getaddrinfo(szHostName, NULL, &hints, &pResult);
  222. if (nRet != 0)
  223. {
  224. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("GetHostIPAddr")("getaddrinfo fail: %d", nRet);
  225. return false;
  226. }
  227. for(auto ptr=pResult; ptr != NULL ;ptr=ptr->ai_next)
  228. {
  229. assert (ptr->ai_family == AF_INET);
  230. struct in_addr *in = (struct in_addr*)ptr->ai_addr;
  231. if (in->S_un.S_un_b.s_b1 != 192 || ptr->ai_next != NULL)
  232. {
  233. memcpy(&addr[0], in, 4);
  234. break;
  235. }
  236. }
  237. freeaddrinfo(pResult);
  238. return true;
  239. #else
  240. return Base_GetHostIPAddr(addr);
  241. #endif
  242. }
  243. ErrorCodeEnum CRemoteControllerCnn::SendCreateRemoteChannelReq()
  244. {
  245. auto pEntity = m_pEntity->GetFunction();
  246. CSystemStaticInfo info;
  247. auto rc = pEntity->GetSystemStaticInfo(info);
  248. assert(rc == Error_Succeed);
  249. RvcCreateChannelReq req = {};
  250. strncpy_s(req.TerminalNo, sizeof(req.TerminalNo), info.strTerminalID, _TRUNCATE);
  251. // get ip addr
  252. assert(GetHostIPAddr(req.IPAddr));
  253. // get life id
  254. CSystemRunInfo info2 = {};
  255. rc = pEntity->GetSystemRunInfo(info2);
  256. req.LifeID = info2.tmStart;
  257. // send package
  258. auto pReqPkg = CreateNewPackage("StRemote");
  259. pReqPkg->AddStruct("RemoteR", false, false, (BYTE*)&req, sizeof(req));
  260. auto strPkgID = SendPackage(pReqPkg);
  261. assert(strPkgID != "");
  262. return Error_Succeed;
  263. }
  264. ErrorCodeEnum CRemoteControllerCnn::HandleCreateRemoteChannelRet(const CSmartPointer<IPackage> &pRecvPkg)
  265. {
  266. DWORD dwSysCode, dwUserCode;
  267. string strErrMsg;
  268. ErrorCodeEnum rc = Error_Succeed;
  269. if (pRecvPkg->GetErrMsg(dwSysCode, dwUserCode, strErrMsg))
  270. {
  271. rc = (ErrorCodeEnum)dwSysCode;
  272. LogError(Severity_Middle, rc, dwUserCode,
  273. CSimpleStringA::Format("create remote channel error: %s", strErrMsg.c_str()));
  274. return rc;
  275. }
  276. RvcCreateChannelRet ret = {};
  277. int nLen = sizeof(ret);
  278. int nArrayLen(0);
  279. if (!pRecvPkg->GetStructData("RemoteA", (BYTE*)&ret, &nLen, &nArrayLen))
  280. {
  281. LogError(Severity_Low, Error_Param, 0, "get return struct [RemoteA] fail");
  282. return Error_Param;
  283. }
  284. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleCreateRemoteChannelRet")
  285. ("create remote channel succeed, remote user: [%s], channel id: [%s]", ret.AgentID, ret.ChannelID);
  286. return EnterMonitorMode();
  287. }
  288. ErrorCodeEnum CRemoteControllerCnn::HandleEndRemoteModeReq(const CSmartPointer<IPackage> &pRecvPkg)
  289. {
  290. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleEndRemoteModeReq")("exit remote mode now");
  291. m_chRemoteMode = 'N';
  292. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_ReleaseChannel));
  293. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  294. SendPackage(pReplyPkg);
  295. return Error_Succeed;
  296. }
  297. ErrorCodeEnum CRemoteControllerCnn::HandleDelRunInfoLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  298. {
  299. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleDelRunInfoLogReq")("Del run info log req");
  300. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  301. int nLen = pRecvPkg->GetStructLen("DelRunR");
  302. if (nLen <= 0)
  303. {
  304. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [DelRunR] fail");
  305. SendPackage(pReplyPkg);
  306. return Error_Param;
  307. }
  308. assert(nLen == sizeof(RvcDelRunInfoLogReq));
  309. RvcDelRunInfoLogReq req = {};
  310. ZeroMemory(&req, sizeof(RvcDelRunInfoLogReq));
  311. int nArrayLen(0);
  312. pRecvPkg->GetStructData("DelRunR", (BYTE*)&req, &nLen, &nArrayLen);
  313. CSimpleString strLogName = req.logName;
  314. RvcDelRunInfoLogRet ret = {};
  315. ret.TaskID = req.TaskID;
  316. pReplyPkg->AddStruct("DelRunA", false, false, (BYTE*)&ret, sizeof(ret));
  317. auto pFunc = m_pEntity->GetFunction();
  318. CSystemStaticInfo sysInfo;
  319. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  320. CSimpleString runInfoPath;
  321. pFunc->GetPath("RunInfo", runInfoPath);
  322. CSimpleString delFilePaht = runInfoPath;
  323. delFilePaht.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append(strLogName).Append(".ini");
  324. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleDelRunInfoLogReq")("curTaskId:%d ,try to delete %s, strLogName = %s", req.TaskID, delFilePaht, strLogName);
  325. if (Base_Exist(delFilePaht.GetData()))
  326. {
  327. if (!Base_DeleteFile(delFilePaht.GetData()))
  328. {
  329. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件删除失败", delFilePaht).GetData()).c_str());
  330. SendPackage(pReplyPkg);
  331. return Error_Cancel;
  332. }
  333. }
  334. else
  335. {
  336. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件不存在", delFilePaht).GetData()).c_str());
  337. SendPackage(pReplyPkg);
  338. return Error_Cancel;
  339. }
  340. SendPackage(pReplyPkg);
  341. return Error_Succeed;
  342. }
  343. ErrorCodeEnum CRemoteControllerCnn::HandleTest(const CSmartPointer<IPackage> &pRecvPkg)
  344. {
  345. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  346. auto pFunc = m_pEntity->GetFunction();
  347. CSystemStaticInfo sysInfo;
  348. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  349. CSimpleString runInfoPath;
  350. pFunc->GetPath("RunInfo", runInfoPath);
  351. CSimpleString delFilePaht = runInfoPath;
  352. delFilePaht.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("MediaController").Append(".ini");
  353. Dbg("try to delete %s", delFilePaht);
  354. if (Base_Exist(delFilePaht.GetData()))
  355. {
  356. if (!Base_DeleteFile(delFilePaht.GetData()))
  357. {
  358. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件删除失败", delFilePaht).GetData()).c_str());
  359. SendPackage(pReplyPkg);
  360. return Error_Cancel;
  361. }
  362. }
  363. else
  364. {
  365. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK(CSimpleString::Format("%s文件不存在", delFilePaht).GetData()).c_str());
  366. SendPackage(pReplyPkg);
  367. return Error_Cancel;
  368. }
  369. SendPackage(pReplyPkg);
  370. return Error_Succeed;
  371. }
  372. ErrorCodeEnum CRemoteControllerCnn::HandleRestartShellReq(const CSmartPointer<IPackage> &pRecvPkg)
  373. {
  374. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleRestartShellReq")("remote restart shell req");
  375. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  376. int nLen = pRecvPkg->GetStructLen("RestSR");
  377. if (nLen <= 0)
  378. {
  379. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [RestSR] fail");
  380. SendPackage(pReplyPkg);
  381. return Error_Param;
  382. }
  383. assert(nLen == sizeof(RvcRestartShellReq));
  384. RvcRestartShellReq req = {};
  385. int nArrayLen(0);
  386. pRecvPkg->GetStructData("RestSR", (BYTE*)&req, &nLen, &nArrayLen);
  387. RvcRestartShellRet ret = {};
  388. ret.TaskID = req.TaskID;
  389. pReplyPkg->AddStruct("RestSA", false, false, (BYTE*)&ret, sizeof(ret));
  390. // 检查当前状态,是否可以重启Shell
  391. auto pFunc = m_pEntity->GetFunction();
  392. CSimpleStringA strCustomerHandleVal;
  393. pFunc->GetSysVar("CustomerHandle", strCustomerHandleVal);
  394. CSimpleStringA strCustomerBeingVal;
  395. pFunc->GetSysVar("CustomerBeing", strCustomerBeingVal);
  396. if (strCustomerHandleVal.Compare("N") != 0 || (strCustomerBeingVal.Compare("N") != 0 && strCustomerBeingVal.Compare("S") != 0))
  397. {
  398. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleRestartShellReq")("remote restart shell fail for var [CustomerHandle] == '%s' & [CustomerBeing] == '%s'"
  399. , (const char*)strCustomerHandleVal, (const char*)strCustomerBeingVal);
  400. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("当前有客户操作,无法重启").c_str());
  401. SendPackage(pReplyPkg);
  402. return Error_Cancel;
  403. }
  404. CSimpleStringA strValue = "";
  405. pFunc->GetSysVar("LocalMaintain", strValue);
  406. if (strValue.Compare("N") != 0)
  407. {
  408. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleRestartShellReq")("remote restart shell fail for var [LocalMaintain] == '%s'", (const char*)strValue);
  409. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("正在本地维护,无法重启").c_str());
  410. SendPackage(pReplyPkg);
  411. return Error_Cancel;
  412. }
  413. // 释放重启事件给健康实体
  414. LogEvent(Severity_Middle, EVENT_RESTART_SHELL, "remote restart shell");
  415. SendPackage(pReplyPkg);
  416. return Error_Succeed;
  417. }
  418. ErrorCodeEnum CRemoteControllerCnn::HandleRestartMachineReq(const CSmartPointer<IPackage> &pRecvPkg)
  419. {
  420. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleRestartMachineReq")("remote restart machine req");
  421. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  422. int nLen = pRecvPkg->GetStructLen("RestOR");
  423. if (nLen <= 0)
  424. {
  425. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [RestOR] fail");
  426. SendPackage(pReplyPkg);
  427. return Error_Param;
  428. }
  429. assert(nLen == sizeof(RvcRestartMachineReq));
  430. RvcRestartMachineReq req = {};
  431. int nArrayLen(0);
  432. pRecvPkg->GetStructData("RestOR", (BYTE*)&req, &nLen, &nArrayLen);
  433. RvcRestartMachineRet ret = {};
  434. ret.TaskID = req.TaskID;
  435. pReplyPkg->AddStruct("RestOA", false, false, (BYTE*)&ret, sizeof(ret));
  436. // 检查当前状态,是否可以重启OS
  437. auto pFunc = m_pEntity->GetFunction();
  438. CSimpleStringA strCustomerHandleVal;
  439. pFunc->GetSysVar("CustomerHandle", strCustomerHandleVal);
  440. CSimpleStringA strCustomerBeingVal;
  441. pFunc->GetSysVar("CustomerBeing", strCustomerBeingVal);
  442. if (strCustomerHandleVal.Compare("N") != 0 || (strCustomerBeingVal.Compare("N") != 0 && strCustomerBeingVal.Compare("S") != 0))
  443. {
  444. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleRestartMachineReq")("remote restart machine fail for var [CustomerHandle] == '%s' & [CustomerBeing] == '%s'"
  445. , (const char*)strCustomerHandleVal, (const char*)strCustomerBeingVal);
  446. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("当前有客户操作,无法重启").c_str());
  447. SendPackage(pReplyPkg);
  448. return Error_Cancel;
  449. }
  450. CSimpleStringA strValue = "";
  451. pFunc->GetSysVar("LocalMaintain", strValue);
  452. if (strValue.Compare("N") != 0)
  453. {
  454. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleRestartMachineReq")("remote restart machine fail for var [LocalMaintain] == '%s'", (const char*)strValue);
  455. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("正在本地维护,无法重启").c_str());
  456. SendPackage(pReplyPkg);
  457. return Error_Cancel;
  458. }
  459. // 释放重启事件给健康实体
  460. LogEvent(Severity_Middle, EVENT_RESTART_MACHINE, "remote restart machine");
  461. SendPackage(pReplyPkg);
  462. return Error_Succeed;
  463. }
  464. ErrorCodeEnum CRemoteControllerCnn::HandleViewEntityStateReq(const CSmartPointer<IPackage> &pRecvPkg)
  465. {
  466. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewEntityStateReq")("remote view entity state");
  467. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  468. int nLen = pRecvPkg->GetStructLen("ViewEntR");
  469. if (nLen <=0)
  470. {
  471. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [ViewEntR] fail");
  472. SendPackage(pReplyPkg);
  473. return Error_Param;
  474. }
  475. assert(nLen == sizeof(RvcViewEntityReq));
  476. RvcViewEntityReq req = {};
  477. int nArrayLen(0);
  478. pRecvPkg->GetStructData("ViewEntR", (BYTE*)&req, &nLen, &nArrayLen);
  479. RvcViewEntityRet1 ret1 = {};
  480. ret1.TaskID = req.TaskID;
  481. pReplyPkg->AddStruct("ViewEnA1", false, false, (BYTE*)&ret1, sizeof(ret1));
  482. auto pFunc = m_pEntity->GetFunction();
  483. CAutoArray<CSimpleStringA> strEntityNames;
  484. CAutoArray<WORD> dwEntityIDs;
  485. pFunc->GetAllRegistedEntity(strEntityNames, dwEntityIDs);
  486. int nCount = strEntityNames.GetCount();
  487. if (nCount >0)
  488. {
  489. nCount += 8; // 包括root\shell\silverlight3个虚拟实体
  490. RvcViewEntityRet2 *pRet2 = new RvcViewEntityRet2[nCount];
  491. memset(pRet2, 0, sizeof(RvcViewEntityRet2) * nCount);
  492. for(int i=0; i<nCount - 8; i++)
  493. {
  494. CEntityRunInfo info = {};
  495. CSimpleStringA strEntityName = strEntityNames[i];
  496. pFunc->GetEntityRunInfo(strEntityName, info);
  497. strncpy_s(pRet2[i].EntityName, sizeof(pRet2[i].EntityName), (const char*)strEntityName, _TRUNCATE);
  498. pRet2[i].EntityID = dwEntityIDs[i];
  499. pRet2[i].EntityState = info.eState;
  500. pRet2[i].ProcessID = info.dwProcessID;
  501. if (info.dwProcessID > 0)
  502. {
  503. #if (defined _WIN32 || defined _WIN64)
  504. auto hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, info.dwProcessID );
  505. if (hProcess != NULL)
  506. {
  507. PROCESS_MEMORY_COUNTERS pmc = {};
  508. GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
  509. pRet2[i].UsedMemory = pmc.WorkingSetSize / 1024 / 1024;
  510. }
  511. #else
  512. pRet2[i].UsedMemory = get_proc_mem(info.dwProcessID) / 1024 / 1024;
  513. #endif
  514. }
  515. }
  516. strncpy_s(pRet2[nCount - 8].EntityName, sizeof(pRet2[nCount - 8].EntityName), "guardian", _TRUNCATE);
  517. strncpy_s(pRet2[nCount - 7].EntityName, sizeof(pRet2[nCount - 7].EntityName), "DualActive", _TRUNCATE);
  518. strncpy_s(pRet2[nCount - 6].EntityName, sizeof(pRet2[nCount - 6].EntityName), "SpHost", _TRUNCATE);
  519. strncpy_s(pRet2[nCount - 5].EntityName, sizeof(pRet2[nCount - 5].EntityName), "SysInit", _TRUNCATE);
  520. strncpy_s(pRet2[nCount - 4].EntityName, sizeof(pRet2[nCount - 4].EntityName), "SpBase", _TRUNCATE);
  521. strncpy_s(pRet2[nCount - 3].EntityName, sizeof(pRet2[nCount - 3].EntityName), "Root", _TRUNCATE);
  522. strncpy_s(pRet2[nCount - 2].EntityName, sizeof(pRet2[nCount - 2].EntityName), "Shell", _TRUNCATE);
  523. strncpy_s(pRet2[nCount - 1].EntityName, sizeof(pRet2[nCount - 1].EntityName), "Silverlight", _TRUNCATE);
  524. pReplyPkg->AddStruct("ViewEnA2", false, false, (BYTE*)pRet2, sizeof(RvcViewEntityRet2) * nCount, nCount);
  525. }
  526. SendPackage(pReplyPkg);
  527. return Error_Succeed;
  528. }
  529. ErrorCodeEnum CRemoteControllerCnn::HandleViewSysVarReq(const CSmartPointer<IPackage> &pRecvPkg)
  530. {
  531. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewSysVarReq")("remote view system vars");
  532. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  533. int nLen = pRecvPkg->GetStructLen("ViewVarR");
  534. if (nLen <= 0)
  535. {
  536. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [ViewVarR] len fail");
  537. SendPackage(pReplyPkg);
  538. return Error_Param;
  539. }
  540. assert(nLen == sizeof(RvcViewSysVarReq));
  541. RvcViewSysVarReq req = {};
  542. int nArrayLen(0);
  543. if (!pRecvPkg->GetStructData("ViewVarR", (BYTE*)&req, &nLen, &nArrayLen))
  544. {
  545. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [ViewVarR] data fail");
  546. SendPackage(pReplyPkg);
  547. return Error_Param;
  548. }
  549. auto pFunc = m_pEntity->GetFunction();
  550. RvcViewSysVarRet arrRet[5] = {};
  551. char *arrVarNames[] = { "RunState", "CustomerHandle", "CallState", "EntryPermit", "LocalMaintain" };
  552. for (int i = 0; i < 5; i++)
  553. {
  554. CSimpleStringA strValue;
  555. pFunc->GetSysVar(arrVarNames[i], strValue);
  556. arrRet[i].TaskID = req.TaskID;
  557. strcpy(arrRet[i].VarName, arrVarNames[i]);
  558. arrRet[i].VarValue = strValue[0];
  559. }
  560. pRecvPkg->AddStruct("ViewVarA", false, false, (BYTE*)arrRet, sizeof(arrRet), 5);
  561. SendPackage(pRecvPkg);
  562. return Error_Succeed;
  563. }
  564. ErrorCodeEnum CRemoteControllerCnn::HandleEditConfigReq(const CSmartPointer<IPackage> &pRecvPkg)
  565. {
  566. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleEditConfigReq")("remote edit config");
  567. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  568. int nLen = pRecvPkg->GetStructLen("EditCfgR");
  569. if (nLen <= 0)
  570. {
  571. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [EditCfgR] len fail");
  572. SendPackage(pReplyPkg);
  573. return Error_Param;
  574. }
  575. char *pBuf = new char[nLen];
  576. memset(pBuf, 0, sizeof(pBuf));
  577. int nArrayLen(0);
  578. if (!pRecvPkg->GetStructData("EditCfgR", (BYTE*)pBuf, &nLen, &nArrayLen) || nArrayLen <=0)
  579. {
  580. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [EditCfgR] data fail");
  581. SendPackage(pReplyPkg);
  582. return Error_Param;
  583. }
  584. auto pFunc = m_pEntity->GetFunction();
  585. assert(nLen == nArrayLen * sizeof(RvcEditConfigReq));
  586. RvcEditConfigReq *pReq = (RvcEditConfigReq*)pBuf;
  587. RvcEditConfigRet ret = { pReq[0].TaskID };
  588. for (int i = 0; i < nArrayLen; i++)
  589. {
  590. CSimpleStringA strEntityName = pReq[i].EntityName;
  591. CSimpleStringA strCfgPath;
  592. if (iequals(strEntityName.GetData(), "Silverlight") || iequals(strEntityName.GetData(), "DualActive") || iequals(strEntityName.GetData(), "SpHost")
  593. || iequals(strEntityName.GetData(), "SysInit") || iequals(strEntityName.GetData(), "SpBase") || iequals(strEntityName.GetData(), "guardian"))
  594. {
  595. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleEditConfigReq")("entity \"%s\" has no config", strEntityName);
  596. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("实体没有配置文件").c_str());
  597. SendPackage(pReplyPkg);
  598. delete[] pBuf;
  599. return Error_NotExist;
  600. }
  601. if (iequals(strEntityName.GetData(), "Root"))
  602. {
  603. auto rc = pFunc->GetPath("HardwareCfg", strCfgPath);
  604. assert(rc == Error_Succeed);
  605. strCfgPath.Append(SPLIT_SLASH_STR).Append("Root.ini");
  606. }
  607. else
  608. {
  609. auto rc = pFunc->GetPath("Cfg", strCfgPath);
  610. assert(rc == Error_Succeed);
  611. strCfgPath.Append(SPLIT_SLASH_STR).Append((const char*)strEntityName).Append(".ini");
  612. }
  613. char cEditType = pReq[i].EditType;
  614. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleEditConfigReq")("edit config: entity = %s, type = %c, section = %s, key = %s, value = %s",
  615. (const char*)strEntityName, cEditType, pReq[i].Section, pReq[i].Key, pReq[i].Value);
  616. if (cEditType == 'A' || cEditType == 'U')
  617. WritePrivateProfileStringA(pReq[i].Section, pReq[i].Key, pReq[i].Value, strCfgPath.GetData());
  618. else if (cEditType == 'D')
  619. WritePrivateProfileStringA(pReq[i].Section, pReq[i].Key, "", strCfgPath.GetData());
  620. }
  621. delete[] pBuf;
  622. pReplyPkg->AddStruct("EditCfgA", false, false, (BYTE*)&ret, sizeof(ret));
  623. SendPackage(pRecvPkg);
  624. return Error_Succeed;
  625. }
  626. ErrorCodeEnum CRemoteControllerCnn::HandleViewEntityConfigReq(const CSmartPointer<IPackage> &pRecvPkg)
  627. {
  628. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewEntityConfigReq")("remote view entity config");
  629. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  630. int nLen = pRecvPkg->GetStructLen("ViewCfgR");
  631. if (nLen <=0)
  632. {
  633. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [ViewCfgR] fail");
  634. SendPackage(pReplyPkg);
  635. return Error_Param;
  636. }
  637. assert(nLen == sizeof(RvcViewConfigReq));
  638. RvcViewConfigReq req = {};
  639. int nArrayLen(0);
  640. pRecvPkg->GetStructData("ViewCfgR", (BYTE*)&req, &nLen, &nArrayLen);
  641. // 读取实体配置文件
  642. CSimpleStringA strEntityName = req.EntityName;
  643. auto pFunc = m_pEntity->GetFunction();
  644. CSimpleStringA strCfgPath;
  645. if (strEntityName == "Root")
  646. {
  647. auto rc = pFunc->GetPath("HardwareCfg", strCfgPath);
  648. assert(rc == Error_Succeed);
  649. strCfgPath.Append(SPLIT_SLASH_STR).Append("Root.ini");
  650. }
  651. else
  652. {
  653. auto rc = pFunc->GetPath("Cfg", strCfgPath);
  654. assert(rc == Error_Succeed);
  655. strCfgPath.Append(SPLIT_SLASH_STR).Append((const char*)strEntityName).Append(".ini");
  656. }
  657. if (!Base_Exist(strCfgPath.GetData()))
  658. {
  659. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewEntityConfigReq")("file [%s] not exist", (const char*)strCfgPath);
  660. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("配置文件不存在").c_str());
  661. SendPackage(pReplyPkg);
  662. return Error_NotExist;
  663. }
  664. auto cfgSize = Base_filesize(strCfgPath.GetData());
  665. char* tmpBuf = new char[cfgSize];
  666. memset(tmpBuf, 0, cfgSize);
  667. auto readRet = Base_readFile(strCfgPath.GetData(), 0, tmpBuf, cfgSize);
  668. if(!readRet.first)
  669. {
  670. pReplyPkg->SetErrMsg(0, 0, "open entity config file fail");
  671. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("open config [%s] fail", (const char*)strCfgPath));
  672. }
  673. else
  674. {
  675. int nRetLen = sizeof(RvcViewConfigRet) + cfgSize * 2; //需自行转换\n和\r\n
  676. char* pBuf = new char[nRetLen];
  677. memset(pBuf, 0, nRetLen);
  678. RvcViewConfigRet* pRet = (RvcViewConfigRet*)pBuf;
  679. int dstPos = 0;
  680. for (auto i = 0; i < cfgSize; i++)
  681. {
  682. if ((i == 0 && tmpBuf[i] == '\n') || (tmpBuf[i] == '\n' && tmpBuf[i - 1] != '\r'))
  683. pRet->ConfigData[dstPos++] = '\r';
  684. pRet->ConfigData[dstPos++] = tmpBuf[i];
  685. }
  686. pReplyPkg->AddStruct("ViewCfgA", false, false, (BYTE*)pBuf, dstPos + sizeof(RvcViewConfigRet));
  687. delete[] pBuf;
  688. }
  689. SendPackage(pReplyPkg);
  690. delete[] tmpBuf;
  691. return Error_Succeed;
  692. }
  693. ErrorCodeEnum CRemoteControllerCnn::HandleViewDebugLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  694. {
  695. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewDebugLogReq")("remote view entity debug log");
  696. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  697. int nLen = pRecvPkg->GetStructLen("ViewDbgR");
  698. if (nLen <=0)
  699. {
  700. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [ViewDbgR] fail");
  701. SendPackage(pReplyPkg);
  702. return Error_Param;
  703. }
  704. assert(nLen == sizeof(RvcViewDbgReq));
  705. RvcViewDbgReq req = {};
  706. int nArrayLen(0);
  707. pRecvPkg->GetStructData("ViewDbgR", (BYTE*)&req, &nLen, &nArrayLen);
  708. auto pFunc = m_pEntity->GetFunction();
  709. CSimpleStringA strEntityName = req.EntityName;
  710. if (iequals(strEntityName.GetData(), "Root"))
  711. {
  712. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("Root实体没有日志").c_str());
  713. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewDebugLogReq")("entity \"Root\" has no dbg log");
  714. SendPackage(pReplyPkg);
  715. return Error_NotExist;
  716. }
  717. CSimpleStringA strViewDate = req.ViewDate;
  718. if (strViewDate.IsNullOrEmpty())
  719. {
  720. strViewDate = getCurData().c_str();
  721. }
  722. ErrorCodeEnum rc(Error_Succeed);
  723. CSimpleStringA strLogPath;
  724. if (iequals(strEntityName.GetData(), "Shell"))
  725. {
  726. auto rc = pFunc->GetPath("Dbg", strLogPath);
  727. assert(rc == Error_Succeed);
  728. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
  729. }
  730. else if (iequals(strEntityName.GetData(), "Silverlight"))
  731. {
  732. auto rc = pFunc->GetPath("Slv", strLogPath);
  733. assert(rc == Error_Succeed);
  734. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
  735. }
  736. else if (iequals(strEntityName.GetData(), "DualActive")|| iequals(strEntityName.GetData(), "SpHost")
  737. || iequals(strEntityName.GetData(), "SysInit") || iequals(strEntityName.GetData(), "SpBase") || iequals(strEntityName.GetData(), "guardian"))
  738. {
  739. auto rc = pFunc->GetPath("Dbg", strLogPath);
  740. assert(rc == Error_Succeed);
  741. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strEntityName).Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
  742. }
  743. else
  744. {
  745. CEntityStaticInfo info = {};
  746. auto rc = pFunc->GetEntityStaticInfo(strEntityName, info);
  747. if (rc != Error_Succeed)
  748. {
  749. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewDebugLogReq")("invalid entity name [%s]", strEntityName);
  750. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体名称\"%s\"无效", strEntityName).GetData()).c_str());
  751. SendPackage(pReplyPkg);
  752. return rc;
  753. }
  754. rc = pFunc->GetPath("Dbg", strLogPath);
  755. assert(rc == Error_Succeed);
  756. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strViewDate).Append(".log");
  757. }
  758. if (!Base_Exist(strLogPath.GetData()))
  759. {
  760. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewDebugLogReq")("log [%s] not exist", (const char*)strLogPath);
  761. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
  762. SendPackage(pReplyPkg);
  763. return Error_NotExist;
  764. }
  765. long long nStartPos = req.StartPos;
  766. if (nStartPos <0)
  767. nStartPos = 0;
  768. long long nMaxBytes = req.MaxRetBytes;
  769. if (nMaxBytes <= 0)
  770. nMaxBytes = 8 * 1024;
  771. auto file = fopen(strLogPath, "rb");
  772. if (file == NULL)
  773. {
  774. auto nError = errno;
  775. pReplyPkg->SetErrMsg(Error_NotExist, 0, CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strLogPath, nError));
  776. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strLogPath, nError));
  777. SendPackage(pReplyPkg);
  778. return rc;
  779. }
  780. generateFilesize(file, strLogPath);
  781. if (nStartPos >0 && nStartPos < nFileSize)
  782. _fseeki64(file, nStartPos, SEEK_SET);
  783. else
  784. nStartPos = 0;
  785. long long nNextPos = nStartPos + nMaxBytes;
  786. if (nMaxBytes + nStartPos > nFileSize)
  787. {
  788. nMaxBytes = nFileSize - nStartPos;
  789. nNextPos = 0;
  790. }
  791. long long nRetLen = sizeof(RvcViewDbgRet) +nMaxBytes;
  792. char *pBuf = new char[nRetLen];
  793. memset(pBuf, 0, nRetLen);
  794. RvcViewDbgRet *pRet = (RvcViewDbgRet*)pBuf;
  795. pRet->TaskID = req.TaskID;
  796. strncpy_s(pRet->EntityName, sizeof(pRet->EntityName), strEntityName, _TRUNCATE);
  797. strncpy(pRet->ViewDate, strViewDate, 8);
  798. pRet->NextPos = nNextPos;
  799. pRet->FileLength = nFileSize;
  800. char *pLogData = (char*)pRet->LogData;
  801. long long nHasRead(0);
  802. DWORD nReadLen(0);
  803. BOOL bRet = TRUE;
  804. do
  805. {
  806. nReadLen = fread(pLogData+nHasRead, 1, nMaxBytes-nHasRead, file);
  807. if (nReadLen >0)
  808. nHasRead += nReadLen;
  809. }
  810. while (nReadLen >0 && nHasRead < nMaxBytes);
  811. fclose(file);
  812. if (!bRet)
  813. {
  814. delete[] pBuf;
  815. auto nError = GetLastError();
  816. pReplyPkg->SetErrMsg(Error_NotExist, 0, CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strLogPath, nError));
  817. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strLogPath, nError));
  818. SendPackage(pReplyPkg);
  819. return rc;
  820. }
  821. if (pRet->NextPos > 0)
  822. {
  823. // 整行切分处理
  824. while (nReadLen > sizeof(RvcViewDbgRet) &&
  825. (pBuf[nRetLen - 1] != '\r' && pBuf[nRetLen - 1] != '\n'))
  826. {
  827. nRetLen--;
  828. pRet->NextPos--;
  829. }
  830. }
  831. pReplyPkg->AddStruct("ViewDbgA", false, false, (BYTE*)pBuf, nRetLen);
  832. delete[] pBuf;
  833. SendPackage(pReplyPkg);
  834. return Error_Succeed;
  835. }
  836. ErrorCodeEnum CRemoteControllerCnn::HandleUploadEventLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  837. {
  838. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")("remote upload event log");
  839. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  840. int nLen = pRecvPkg->GetStructLen("EvtLogR");
  841. if (nLen <= 0)
  842. {
  843. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [EvtLogR] fail");
  844. SendPackage(pReplyPkg);
  845. return Error_Param;
  846. }
  847. if (nLen != sizeof(RvcUploadEventLogReq))
  848. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")("nLen:%d, RvcUploadEventLogReq Size:%d", nLen, sizeof(RvcUploadEventLogReq));
  849. assert(nLen == sizeof(RvcUploadEventLogReq));
  850. RvcUploadEventLogReq req;
  851. int nArrayLen(0);
  852. pRecvPkg->GetStructData("EvtLogR", (BYTE*)&req, &nLen, &nArrayLen);
  853. auto pFunc = m_pEntity->GetFunction();
  854. CSimpleStringA strZipFileName;
  855. CSimpleStringA strLogPath;
  856. CSimpleStringA strZipPath, strTodayLogName;
  857. if (req.StartPos <= 0)
  858. {
  859. ResourceWatcherService_ExtractEventLog_Req rvcReq;
  860. ZeroMemory(&rvcReq, sizeof(ResourceWatcherService_ExtractEventLog_Req));
  861. {//初始化rvcReq
  862. rvcReq.evtName = req.evtName;
  863. rvcReq.evtLevel = req.evtLevel;
  864. rvcReq.duration = req.duration;
  865. rvcReq.startTime = req.startTime;
  866. rvcReq.endTime = req.endTime;
  867. rvcReq.cusEvtFileName = CSimpleStringA(req.cusEvtFileName);
  868. rvcReq.evtSrcEventName = CSimpleStringA(req.evtSrcEventName);
  869. rvcReq.reversed1 = req.reversed1;
  870. rvcReq.reversed2 = CSimpleStringA(req.reversed2);
  871. }
  872. ResourceWatcherService_ExtractEventLog_Ans rvcAns;
  873. ZeroMemory(&rvcAns, sizeof(ResourceWatcherService_ExtractEventLog_Ans));
  874. ResourceWatcherService_ClientBase *pClient = new ResourceWatcherService_ClientBase(m_pEntity);
  875. auto rc = pClient->Connect();
  876. if (rc == Error_Succeed)
  877. {
  878. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")("evtName:%d, evtLevel:%d, duration:%d, startTime:%d, endTime:%d, cusEvtFileName:%s,evtSrcEventName:%s, reversed1:%d, reversed2:%s, startPos:%d, MaxRet:%d, UploadFile:%s",
  879. req.evtName, req.evtLevel, req.duration, req.startTime, req.endTime,
  880. req.cusEvtFileName, req.evtSrcEventName, req.reversed1, req.reversed2, req.StartPos, req.MaxRetBytes, req.UploadFile);
  881. rc = (*pClient)(EntityResource::getLink().upgradeLink())->ExtractEventLog(rvcReq, rvcAns, 60000 * 5);
  882. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")("entries:%d, information:%s, evtLogFileName:%s", rvcAns.entries, rvcAns.information.GetData(), rvcAns.evtLogFileName.GetData());
  883. pClient->GetFunction()->CloseSession();
  884. }
  885. if (rc != Error_Succeed)
  886. {
  887. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[ResourceWatcher]实体失败").c_str());
  888. LogError(Severity_Low, rc, 0, "调用[ResourceWatcher]实体失败");
  889. }
  890. else
  891. {
  892. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")("获取系统事件日志成功");
  893. //压缩位置
  894. strTodayLogName = getCurData().c_str();
  895. CSystemStaticInfo sysInfo;
  896. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  897. strZipFileName = CSimpleStringA::Format("%s_event_%s_%02X_%02X.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName, rvcAns.entries,
  898. req.evtLevel + req.duration + req.startTime + req.endTime);
  899. pFunc->GetPath("Temp", strZipPath);
  900. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  901. strLogPath = rvcAns.evtLogFileName;
  902. if (0 == rvcAns.entries)
  903. {
  904. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")("log [%s] not exist", (const char*)strLogPath);
  905. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("未能获取到相关日志").GetData()).c_str());
  906. SendPackage(pReplyPkg);
  907. return Error_NotExist;
  908. }
  909. if (!Base_Exist(strLogPath.GetData()))
  910. {
  911. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")("log [%s] not exist", (const char*)strLogPath);
  912. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
  913. SendPackage(pReplyPkg);
  914. return Error_NotExist;
  915. }
  916. // 生成ZIP
  917. rc = ZipFile(strZipPath, strLogPath);
  918. if (rc != Error_Succeed)
  919. {
  920. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  921. SendPackage(pReplyPkg);
  922. return rc;
  923. }
  924. else
  925. Base_DeleteFile(strLogPath.GetData());
  926. }
  927. }
  928. else
  929. {
  930. strZipFileName = req.UploadFile;
  931. pFunc->GetPath("Temp", strZipPath);
  932. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  933. }
  934. if (!Base_Exist(strZipPath.GetData()))
  935. {
  936. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")("zip log [%s] not exist", (const char*)strZipPath);
  937. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  938. SendPackage(pReplyPkg);
  939. return Error_NotExist;
  940. }
  941. // 开始上传
  942. long long nStartPos = req.StartPos;
  943. if (nStartPos <0)
  944. nStartPos = 0;
  945. long long nMaxBytes = req.MaxRetBytes;
  946. if (nMaxBytes <= 0)
  947. nMaxBytes = 32 * 1024;
  948. auto file = fopen(strZipPath, "rb");
  949. if (file == NULL)
  950. {
  951. auto nError = errno;
  952. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  953. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  954. SendPackage(pReplyPkg);
  955. return Error_Unexpect;
  956. }
  957. generateFilesize(file, strZipPath);
  958. if (nStartPos >0 && nStartPos < nFileSize)
  959. {
  960. _fseeki64(file, nStartPos, SEEK_SET);
  961. }
  962. else
  963. nStartPos = 0;
  964. long long nNextPos = nStartPos + nMaxBytes;
  965. if (nMaxBytes + nStartPos > nFileSize)
  966. {
  967. nMaxBytes = nFileSize - nStartPos;
  968. nNextPos = 0;
  969. }
  970. long long nRetLen = sizeof(RvcUploadEventLogRet)+nMaxBytes;
  971. char *pBuf = new char[nRetLen];
  972. memset(pBuf, 0, nRetLen);
  973. RvcUploadEventLogRet *pRet = (RvcUploadEventLogRet*)pBuf;
  974. pRet->TaskID = req.TaskID;
  975. strncpy_s(pRet->UploadFile, sizeof(pRet->UploadFile), strZipFileName, _TRUNCATE);
  976. pRet->ZipFileLen = nFileSize;
  977. pRet->StartPos = req.StartPos;
  978. pRet->NextPos = nNextPos;
  979. char *pLogData = (char*)pRet->LogData;
  980. long long nHasRead(0);
  981. DWORD nReadLen(0);
  982. BOOL bRet = TRUE;
  983. do
  984. {
  985. //Dbg("ftell: %d", ftell(file));
  986. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  987. //Dbg("ftell: %d", ftell(file));
  988. if (nReadLen >0)
  989. nHasRead += nReadLen;
  990. } while (nReadLen >0 && nHasRead < nMaxBytes);
  991. fclose(file);
  992. if (nHasRead < nMaxBytes)
  993. {
  994. delete[] pBuf;
  995. auto nError = GetLastError();
  996. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  997. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  998. SendPackage(pReplyPkg);
  999. return Error_Unexpect;
  1000. }
  1001. pReplyPkg->AddStruct("EvtLogA", false, false, (BYTE*)pBuf, nRetLen);
  1002. delete[] pBuf;
  1003. SendPackage(pReplyPkg);
  1004. if (nNextPos <= 0)
  1005. {
  1006. // 删除压缩文件
  1007. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadEventLogReq")("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  1008. Base_DeleteFile(strZipPath.GetData());
  1009. }
  1010. return Error_Succeed;
  1011. }
  1012. char *makeOperateDiskJson(RvcOperateDiskRet1 *ret1, RvcOperateDiskRet2 *ret2, int ret2Num)
  1013. {
  1014. cJSON * pJsonRoot = NULL;
  1015. pJsonRoot = cJSON_CreateObject();
  1016. if(NULL == pJsonRoot || NULL == ret1 || NULL == ret2 || ret2Num <= 0)
  1017. return NULL;
  1018. cJSON_AddNumberToObject(pJsonRoot, "TaskID", ret1->TaskID);
  1019. cJSON_AddNumberToObject(pJsonRoot, "result", ret1->result);
  1020. cJSON_AddStringToObject(pJsonRoot, "header", ret1->header);
  1021. cJSON_AddNumberToObject(pJsonRoot, "attachment1", ret1->attachment1);
  1022. cJSON *arrFileName = cJSON_CreateArray(), *arrFileSize = cJSON_CreateArray(), *arrFtCreate = cJSON_CreateArray(), *arrftModified = cJSON_CreateArray(),
  1023. *arrftAccess = cJSON_CreateArray(), *arrFileAttribute = cJSON_CreateArray(), *arrForbidAttributes = cJSON_CreateArray(),
  1024. *arrReserved1 = cJSON_CreateArray(), *arrReserved2 = cJSON_CreateArray();
  1025. if (NULL == arrFileName || NULL == arrFileSize || NULL == arrFtCreate || NULL == arrftModified || NULL == arrftAccess || NULL == arrFileAttribute || NULL == arrForbidAttributes
  1026. || NULL == arrReserved1 || NULL ==arrReserved2)
  1027. return NULL;
  1028. for (int i = 0; i < ret2Num; i++)
  1029. {
  1030. CDigitalConv62 d62;
  1031. char temp[30] = "";
  1032. string str62;
  1033. cJSON_AddStringToObject(arrFileName, "fileName", ret2[i].fileName);
  1034. _ui64toa(ret2[i].fileSize, temp, 10);
  1035. cJSON_AddStringToObject(arrFileSize, "fileSize", d62.to_x(temp, 1).data());
  1036. _ui64toa(ret2[i].ftCreate, temp, 10);
  1037. cJSON_AddStringToObject(arrFtCreate, "ftCreate", d62.to_x(temp, 1).data());
  1038. _ui64toa(ret2[i].ftModified, temp, 10);
  1039. cJSON_AddStringToObject(arrftModified, "ftModified", d62.to_x(temp, 1).data());
  1040. _ui64toa(ret2[i].ftAccess, temp, 10);
  1041. cJSON_AddStringToObject(arrftAccess, "ftAccess", d62.to_x(temp, 1).data());
  1042. cJSON_AddNumberToObject(arrFileAttribute, "fileAttribute", ret2[i].fileAttribute);
  1043. cJSON_AddNumberToObject(arrForbidAttributes, "forbidAttributes", ret2[i].forbidAttributes);
  1044. if (0 != ret2[i].reserved1)
  1045. cJSON_AddNumberToObject(arrReserved1, "reserved1", ret2[i].reserved1);
  1046. //cJSON_AddStringToObject(arrReserved2, "reserved2", ret2[i].reserved2);
  1047. }
  1048. cJSON_AddItemToObject(pJsonRoot, "arrFileName", arrFileName);
  1049. cJSON_AddItemToObject(pJsonRoot, "arrFileSize", arrFileSize);
  1050. cJSON_AddItemToObject(pJsonRoot, "arrFtCreate", arrFtCreate);
  1051. cJSON_AddItemToObject(pJsonRoot, "arrftModified", arrftModified);
  1052. cJSON_AddItemToObject(pJsonRoot, "arrftAccess", arrftAccess);
  1053. cJSON_AddItemToObject(pJsonRoot, "arrFileAttribute", arrFileAttribute);
  1054. cJSON_AddItemToObject(pJsonRoot, "arrForbidAttributes", arrForbidAttributes);
  1055. cJSON_AddItemToObject(pJsonRoot, "arrReserved1", arrReserved1);
  1056. //cJSON_AddItemToObject(pJsonRoot, "arrReserved2", arrReserved2);
  1057. char *out = cJSON_Print(pJsonRoot);
  1058. cJSON_Delete(pJsonRoot);
  1059. return out;
  1060. }
  1061. #pragma optimize( "", off )
  1062. ErrorCodeEnum CRemoteControllerCnn::HandleOperateDiskReq(const CSmartPointer<IPackage> &pRecvPkg)
  1063. {
  1064. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleOperateDiskReq")("request operate disk");
  1065. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1066. int nLen = pRecvPkg->GetStructLen("OprDiskR");
  1067. if (nLen <= 0)
  1068. {
  1069. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleOperateDiskReq")("Failed:[OprDiskR]");
  1070. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [OprDiskR] fail");
  1071. SendPackage(pReplyPkg);
  1072. return Error_Param;
  1073. }
  1074. assert(nLen == sizeof(RvcOperateDiskReq));
  1075. RvcOperateDiskReq req = {};
  1076. int nArrayLen(0);
  1077. pRecvPkg->GetStructData("OprDiskR", (BYTE*)&req, &nLen, &nArrayLen);
  1078. // 调用本地维护实体取控制信息
  1079. ResourceWatcherService_OperateFile_Req rvcReq;
  1080. ZeroMemory(&rvcReq, sizeof(ResourceWatcherService_OperateFile_Req));
  1081. {//初始化rvcReq
  1082. CSimpleStringA curTmp(req.current);
  1083. #if (defined _WIN32 || defined _WIN64)
  1084. rvcReq.current = curTmp;
  1085. #else
  1086. if (curTmp[0] == '/')
  1087. rvcReq.current = curTmp;
  1088. else
  1089. {
  1090. std::string tmpStr = curTmp.GetData();
  1091. std::replace(tmpStr.begin(), tmpStr.end(), '\\', '/');
  1092. rvcReq.current = CSimpleStringA("/") + tmpStr.c_str();
  1093. }
  1094. #endif
  1095. rvcReq.mode = req.mode;
  1096. rvcReq.attribute = req.attribute;
  1097. rvcReq.content = CSimpleStringA(req.content);
  1098. rvcReq.filter1 = req.filter1;
  1099. rvcReq.filter2 = CSimpleStringA(req.filter2);
  1100. rvcReq.attachment1 = req.attachment1;
  1101. rvcReq.attachment2 = CSimpleStringA(req.attachment2);
  1102. }
  1103. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleOperateDiskReq")("current:%s, reqMode:%d, mode:%d, attribute:%d, content:%s", rvcReq.current.GetData(), req.mode, rvcReq.mode, rvcReq.attribute, rvcReq.content.GetData());
  1104. ResourceWatcherService_OperateFile_Ans rvcAns;
  1105. ZeroMemory(&rvcAns, sizeof(ResourceWatcherService_OperateFile_Ans));
  1106. ResourceWatcherService_ClientBase *pClient = new ResourceWatcherService_ClientBase(m_pEntity);
  1107. RvcOperateDiskRet2 *pRet2 = NULL;
  1108. auto rc = pClient->Connect();
  1109. if (rc == Error_Succeed)
  1110. {
  1111. rc = (*pClient)(EntityResource::getLink().upgradeLink())->OperateFile(rvcReq, rvcAns, 60000);
  1112. pClient->GetFunction()->CloseSession();
  1113. }
  1114. if (rc != Error_Succeed)
  1115. {
  1116. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[ResourceWatcher]实体失败").c_str());
  1117. LogError(Severity_Low, rc, 0, "调用[ResourceWatcher]实体失败");
  1118. }
  1119. else
  1120. {
  1121. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleOperateDiskReq")("获取磁盘目录信息成功");
  1122. //添加首层目录信息
  1123. RvcOperateDiskRet1 ret1(req.TaskID, rvcAns.result, rvcAns.header, rvcAns.attachment1);
  1124. RvcOperateDiskRet2 current(rvcAns.attachment2, rvcAns.fileSize, rvcAns.ftCreate, rvcAns.ftModified,
  1125. rvcAns.ftAccess, rvcAns.fileAttribute, rvcAns.forbidAttribute, rvcAns.reversed1, rvcAns.reversed2);
  1126. //添加目录具体文件信息
  1127. pRet2 = new RvcOperateDiskRet2[rvcAns.count + 1];
  1128. ZeroMemory(pRet2, sizeof(RvcOperateDiskRet2) * (rvcAns.count + 1));
  1129. memcpy(&(pRet2[0]), &current, sizeof(RvcOperateDiskRet2));
  1130. //Dbg("size:%d", rvcAns.count);
  1131. for (int i = 0; i < rvcAns.count; i++)
  1132. {
  1133. RvcOperateDiskRet2 tempRet(rvcAns.fileNames[i], rvcAns.fileSizes[i], rvcAns.ftCreates[i], rvcAns.ftModifieds[i],
  1134. rvcAns.ftAccesses[i], rvcAns.fileAttributes[i], rvcAns.forbidAttributes[i],
  1135. (rvcAns.reserved1.GetCount() == rvcAns.count ? rvcAns.reserved1[i] : 0), (rvcAns.reserved2.GetCount() == rvcAns.count ? rvcAns.reserved2[i] : ""));
  1136. memcpy(&(pRet2[i + 1]), &tempRet, sizeof(RvcOperateDiskRet2));
  1137. }
  1138. char *outStr = makeOperateDiskJson(&ret1, pRet2, rvcAns.count + 1);
  1139. char *slimJsonStr = new char[strlen(outStr)];
  1140. ZeroMemory(slimJsonStr, strlen(outStr));
  1141. int pos = 0;
  1142. for (int i = 0; i < strlen(outStr); i++)
  1143. {
  1144. if ('\t' != outStr[i] && '\n' != outStr[i])
  1145. {
  1146. slimJsonStr[pos] = outStr[i];
  1147. pos++;
  1148. }
  1149. }
  1150. slimJsonStr[pos] = '\0';
  1151. long long nRetLen = sizeof(RvcOperateDiskJsonRet) + 60 * 1024;
  1152. char *pBuf = new char[nRetLen];
  1153. ZeroMemory(pBuf, nRetLen);
  1154. RvcOperateDiskJsonRet *pRet = (RvcOperateDiskJsonRet*)pBuf;
  1155. pRet->TaskID = req.TaskID;
  1156. auto pFunc = m_pEntity->GetFunction();
  1157. CSimpleStringA jsonFileName, tempDir, zipJsonName;
  1158. pFunc->GetPath("Temp", tempDir);
  1159. jsonFileName.Append(tempDir).Append(SPLIT_SLASH_STR).Append("transfer.txt");
  1160. zipJsonName.Append(tempDir).Append(SPLIT_SLASH_STR).Append("zipJson.zip");
  1161. Dbg("jsonFileName:%s", jsonFileName.GetData());
  1162. try {
  1163. FILE* fp = NULL;
  1164. fp = fopen(jsonFileName.GetData(), "w+");
  1165. if (fp) {
  1166. Dbg("jsonLength:%d", pRet->jsonLength);
  1167. auto gbkStr = SP::Utility::UTF8ToGBK(slimJsonStr);
  1168. fputs(gbkStr.c_str(), fp);
  1169. fclose(fp);
  1170. }
  1171. /*
  1172. std::fstream jsonFile;
  1173. Dbg("1");
  1174. jsonFile.open(jsonFileName.GetData(), ios::out | ios::trunc);
  1175. Dbg("2");
  1176. if (jsonFile && jsonFile.is_open())
  1177. {
  1178. Dbg("jsonLength:%d", pRet->jsonLength);
  1179. auto gbkStr = SP::Utility::UTF8ToGBK(slimJsonStr);
  1180. jsonFile << gbkStr << endl;
  1181. jsonFile.close();
  1182. }
  1183. */
  1184. }
  1185. catch (std::exception& e)
  1186. {
  1187. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI("HandleOperateDiskReq")("%s write err, %s", jsonFileName.GetData(), e.what());
  1188. }
  1189. if (Error_Succeed != (rc = ZipFile(zipJsonName, jsonFileName)))
  1190. {
  1191. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)zipJsonName).GetData()).c_str());
  1192. SendPackage(pReplyPkg);
  1193. return rc;
  1194. }
  1195. FILE* fp;
  1196. FILE* infile = fopen(zipJsonName.GetData(), "rt");
  1197. pRet->jsonLength = fread(pRet->jsonData, sizeof(char), 20000, infile);
  1198. fclose(infile);
  1199. /*
  1200. std::ifstream t;
  1201. t.open(zipJsonName, std::ios_base::binary | std::ios_base::in | std::ios_base::out); // open input file
  1202. t.seekg(0, std::ios::end); // go to the end
  1203. pRet->jsonLength = t.tellg(); // report location (this is the length)
  1204. t.seekg(0, std::ios::beg); // go back to the beginning
  1205. t.read(pRet->jsonData, pRet->jsonLength); // read the whole file into the buffer
  1206. t.close(); // close file handle
  1207. */
  1208. pReplyPkg->AddStruct("OprDkJA", false, false, (BYTE*)pRet, nRetLen);
  1209. delete []slimJsonStr;
  1210. Base_DeleteFile(jsonFileName.GetData());
  1211. Base_DeleteFile(zipJsonName.GetData());
  1212. }
  1213. SendPackage(pReplyPkg);
  1214. return Error_Succeed;
  1215. }
  1216. ErrorCodeEnum CRemoteControllerCnn::HandleEnterMaintainModeReq(const CSmartPointer<IPackage> &pRecvPkg)
  1217. {
  1218. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleEnterMaintainModeReq")("request enter maintain mode");
  1219. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1220. int nLen = pRecvPkg->GetStructLen("EnterMMR");
  1221. if (nLen <=0)
  1222. {
  1223. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [EnterMMR] fail");
  1224. SendPackage(pReplyPkg);
  1225. return Error_Param;
  1226. }
  1227. assert(nLen == sizeof(RvcEnterMaintainModeReq));
  1228. RvcEnterMaintainModeReq req = {};
  1229. int nArrayLen(0);
  1230. pRecvPkg->GetStructData("EnterMMR", (BYTE*)&req, &nLen, &nArrayLen);
  1231. RvcEnterMaintainModeRet ret = {};
  1232. ret.TaskID = req.TaskID;
  1233. pReplyPkg->AddStruct("EnterMMA", false, false, (BYTE*)&ret, sizeof(ret));
  1234. // 检查当前状态
  1235. auto pFunc = m_pEntity->GetFunction();
  1236. CSimpleStringA strCustomerHandleVal;
  1237. pFunc->GetSysVar("CustomerHandle", strCustomerHandleVal);
  1238. CSimpleStringA strCustomerBeingVal;
  1239. pFunc->GetSysVar("CustomerBeing", strCustomerBeingVal);
  1240. if (strCustomerHandleVal.Compare("N") != 0 || (strCustomerBeingVal.Compare("N") != 0 && strCustomerBeingVal.Compare("S") != 0))
  1241. {
  1242. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleEnterMaintainModeReq")("remote enter maintain mode fail for var [CustomerHandle] == '%s' & [CustomerBeing] == '%s'"
  1243. , (const char*)strCustomerHandleVal, (const char*)strCustomerBeingVal);
  1244. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("当前有客户操作,无法暂停服务").c_str());
  1245. SendPackage(pReplyPkg);
  1246. return Error_Cancel;
  1247. }
  1248. CSimpleStringA strValue = "";
  1249. pFunc->GetSysVar("LocalMaintain", strValue);
  1250. if (strValue.Compare("N") != 0)
  1251. {
  1252. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleEnterMaintainModeReq")("remote enter maintain mode fail for var [LocalMaintain] == '%s'", (const char*)strValue);
  1253. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("正在本地维护,无法暂停服务").c_str());
  1254. SendPackage(pReplyPkg);
  1255. return Error_Cancel;
  1256. }
  1257. // 调用健康实体进入维护模式
  1258. HealthManagerService_ClientBase *pClient = new HealthManagerService_ClientBase(m_pEntity);
  1259. auto rc = pClient->Connect();
  1260. if (rc == Error_Succeed)
  1261. {
  1262. HealthManagerService_EnterState_Req req = {};
  1263. req.state = "M";
  1264. HealthManagerService_EnterState_Ans ans = {};
  1265. rc = (*pClient)(EntityResource::getLink().upgradeLink())->EnterState(req, ans, 10000);
  1266. pClient->GetFunction()->CloseSession();
  1267. }
  1268. else {
  1269. pClient->SafeDelete();
  1270. }
  1271. if (rc != Error_Succeed)
  1272. {
  1273. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用健康实体进入维护模式失败").c_str());
  1274. LogError(Severity_Low, rc, 0, "调用健康失败进入维护模式失败");
  1275. }
  1276. SendPackage(pReplyPkg);
  1277. return Error_Succeed;
  1278. }
  1279. ErrorCodeEnum CRemoteControllerCnn::HandleExitMaintainModeReq(const CSmartPointer<IPackage> &pRecvPkg)
  1280. {
  1281. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleExitMaintainModeReq")("request exit maintain mode");
  1282. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1283. int nLen = pRecvPkg->GetStructLen("ExitMMR");
  1284. if (nLen <=0)
  1285. {
  1286. pReplyPkg->SetErrMsg(Error_Param, 0, SP::Utility::UTF8ToGBK("get struct [ExitMMR] fail").c_str());
  1287. SendPackage(pReplyPkg);
  1288. return Error_Param;
  1289. }
  1290. assert(nLen == sizeof(RvcExitMaintainModeReq));
  1291. RvcExitMaintainModeReq req = {};
  1292. int nArrayLen(0);
  1293. pRecvPkg->GetStructData("ExitMMR", (BYTE*)&req, &nLen, &nArrayLen);
  1294. RvcExitMaintainModeRet ret = {};
  1295. ret.TaskID = req.TaskID;
  1296. pReplyPkg->AddStruct("ExitMMA", false, false, (BYTE*)&ret, sizeof(ret));
  1297. HealthManagerService_ClientBase *pClient = new HealthManagerService_ClientBase(m_pEntity);
  1298. auto rc = pClient->Connect();
  1299. if (rc == Error_Succeed)
  1300. {
  1301. HealthManagerService_ExitState_Req req = {};
  1302. req.state = "M";
  1303. HealthManagerService_ExitState_Ans ans = {};
  1304. rc = (*pClient)(EntityResource::getLink().upgradeLink())->ExitState(req, ans, 10000);
  1305. pClient->GetFunction()->CloseSession();
  1306. } else {
  1307. pClient->SafeDelete();
  1308. }
  1309. if (rc != Error_Succeed)
  1310. {
  1311. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用健康实体退出维护模式失败").c_str());
  1312. LogError(Severity_Low, rc, 0, "调用健康失败退出维护模式失败");
  1313. }
  1314. SendPackage(pReplyPkg);
  1315. return Error_Succeed;
  1316. }
  1317. ErrorCodeEnum CRemoteControllerCnn::HandleUpgradePackReq(const CSmartPointer<IPackage> &pRecvPkg)
  1318. {
  1319. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUpgradePackReq")("request remote upgarde pack");
  1320. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1321. int nLen = pRecvPkg->GetStructLen("UpgPackR");
  1322. if (nLen <=0)
  1323. {
  1324. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [UpgPackR] fail");
  1325. SendPackage(pReplyPkg);
  1326. return Error_Param;
  1327. }
  1328. assert(nLen == sizeof(RvcUpgradePackReq));
  1329. RvcUpgradePackReq req = {};
  1330. int nArrayLen(0);
  1331. pRecvPkg->GetStructData("UpgPackR", (BYTE*)&req, &nLen, &nArrayLen);
  1332. RvcUpgradePackRet ret = {};
  1333. ret.TaskID = req.TaskID;
  1334. pReplyPkg->AddStruct("UpgPackA", false, false, (BYTE*)&ret, sizeof(ret));
  1335. CSimpleStringA strPack = req.PackName;
  1336. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUpgradePackReq")("begin upgrade pack: %s", (const char*)strPack);
  1337. // 调用升级实体接口升级
  1338. UpgradeMgrService_ClientBase *pClient = new UpgradeMgrService_ClientBase(m_pEntity);
  1339. auto rc = pClient->Connect();
  1340. if (rc == Error_Succeed)
  1341. {
  1342. UpgradeMgrService_RegistLocalPack_Req req = {};
  1343. req.strPackFile = strPack;
  1344. UpgradeMgrService_RegistLocalPack_Ans ans = {};
  1345. rc = (*pClient)(EntityResource::getLink().upgradeLink())->RegistLocalPack(req, ans, 10000);
  1346. pClient->GetFunction()->CloseSession();
  1347. } else {
  1348. pClient->SafeDelete();
  1349. }
  1350. if (rc != Error_Succeed)
  1351. {
  1352. if (rc == Error_NotExist)
  1353. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("升级包文件不存在").c_str());
  1354. else if (rc == Error_AlreadyExist)
  1355. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("升级包已安装").c_str());
  1356. else if (rc == Error_Cancel)
  1357. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("升级包已被取消安装").c_str());
  1358. else
  1359. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("远程执行升级失败").c_str());
  1360. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUpgradePackReq")("remote regist upgrade pack fail: 0x%X", rc);
  1361. }
  1362. else
  1363. {
  1364. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUpgradePackReq")("remote regist upgrade pack succeed!");
  1365. }
  1366. SendPackage(pReplyPkg);
  1367. return Error_Succeed;
  1368. }
  1369. ErrorCodeEnum CRemoteControllerCnn::HandleRollbackUpgradeReq(const CSmartPointer<IPackage> &pRecvPkg)
  1370. {
  1371. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleRollbackUpgradeReq")("request remote rollback upgarde");
  1372. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1373. int nLen = pRecvPkg->GetStructLen("RollbakR");
  1374. if (nLen <= 0)
  1375. {
  1376. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [RollbakR] fail");
  1377. SendPackage(pReplyPkg);
  1378. return Error_Param;
  1379. }
  1380. assert(nLen == sizeof(RvcRollbackReq));
  1381. RvcRollbackReq req = {};
  1382. int nArrayLen(0);
  1383. pRecvPkg->GetStructData("RollbakR", (BYTE*)&req, &nLen, &nArrayLen);
  1384. RvcRollbackRet ret = {};
  1385. ret.TaskID = req.TaskID;
  1386. pReplyPkg->AddStruct("RollbakA", false, false, (BYTE*)&ret, sizeof(ret));
  1387. // 调用升级实体接口回滚
  1388. UpgradeMgrService_ClientBase *pClient = new UpgradeMgrService_ClientBase(m_pEntity);
  1389. auto rc = pClient->Connect();
  1390. if (rc == Error_Succeed)
  1391. {
  1392. UpgradeMgrService_RollbackUpdate_Req req = {};
  1393. UpgradeMgrService_RollbackUpdate_Ans ans = {};
  1394. rc = (*pClient)(EntityResource::getLink().upgradeLink())->RollbackUpdate(req, ans, 10000);
  1395. pClient->GetFunction()->CloseSession();
  1396. } else {
  1397. pClient->SafeDelete();
  1398. }
  1399. if (rc != Error_Succeed)
  1400. {
  1401. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用升级实体回滚升级失败").c_str());
  1402. LogError(Severity_Low, rc, 0, "调用升级实体回滚升级失败");
  1403. }
  1404. else
  1405. {
  1406. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleRollbackUpgradeReq")("remote rollback upgrade succeed!");
  1407. }
  1408. SendPackage(pReplyPkg);
  1409. return Error_Succeed;
  1410. }
  1411. ErrorCodeEnum CRemoteControllerCnn::HandleGetCurMaintainerReq(const CSmartPointer<IPackage> &pRecvPkg)
  1412. {
  1413. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleGetCurMaintainerReq")("request get maintainer info");
  1414. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1415. int nLen = pRecvPkg->GetStructLen("GetUserR");
  1416. if (nLen <=0)
  1417. {
  1418. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [GetUserR] fail");
  1419. SendPackage(pReplyPkg);
  1420. return Error_Param;
  1421. }
  1422. assert(nLen == sizeof(RvcGetUserReq));
  1423. RvcGetUserReq req = {};
  1424. int nArrayLen(0);
  1425. pRecvPkg->GetStructData("GetUserR", (BYTE*)&req, &nLen, &nArrayLen);
  1426. RvcGetUserRet ret = {};
  1427. ret.TaskID = req.TaskID;
  1428. // 调用本地维护实体取控制信息
  1429. GUIConsoleService_GetCurrentMaintainer_Req req1 = {};
  1430. GUIConsoleService_GetCurrentMaintainer_Ans ans1 = {};
  1431. GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
  1432. auto rc = pClient->Connect();
  1433. if (rc == Error_Succeed)
  1434. {
  1435. rc = (*pClient)(EntityResource::getLink().upgradeLink())->GetCurrentMaintainer(req1, ans1, 10000);
  1436. pClient->GetFunction()->CloseSession();
  1437. } else {
  1438. pClient->SafeDelete();
  1439. }
  1440. if (rc != Error_Succeed)
  1441. {
  1442. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
  1443. LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
  1444. }
  1445. else
  1446. {
  1447. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleGetCurMaintainerReq")("获取运维人员信息成功");
  1448. strncpy_s(ret.UserID, sizeof(ret.UserID), ans1.strUserID, _TRUNCATE);
  1449. strncpy_s(ret.UserName, sizeof(ret.UserName), ans1.strUserName, _TRUNCATE);
  1450. strncpy_s(ret.CurRight, sizeof(ret.CurRight), ans1.strCurRight, _TRUNCATE);
  1451. strncpy_s(ret.Authorizer, sizeof(ret.Authorizer), ans1.strAuthorizer, _TRUNCATE);
  1452. ret.BeginTime = ans1.dwBeginTime;
  1453. pReplyPkg->AddStruct("GetUserA", false, false, (BYTE*)&ret, sizeof(ret));
  1454. }
  1455. SendPackage(pReplyPkg);
  1456. return Error_Succeed;
  1457. }
  1458. ErrorCodeEnum CRemoteControllerCnn::HandleEnpowerMaintainerReq(const CSmartPointer<IPackage> &pRecvPkg)
  1459. {
  1460. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleEnpowerMaintainerReq")("request enpower maintainer");
  1461. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1462. int nLen = pRecvPkg->GetStructLen("EnpowerR");
  1463. if (nLen <=0)
  1464. {
  1465. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [EnpowerR] fail");
  1466. SendPackage(pReplyPkg);
  1467. return Error_Param;
  1468. }
  1469. assert(nLen == sizeof(RvcEnpowerUserReq));
  1470. RvcEnpowerUserReq req = {};
  1471. int nArrayLen(0);
  1472. pRecvPkg->GetStructData("EnpowerR", (BYTE*)&req, &nLen, &nArrayLen);
  1473. RvcEnpowerUserRet ret = {};
  1474. ret.TaskID = req.TaskID;
  1475. // 调用本地维护实体取控制信息
  1476. GUIConsoleService_Empower_Req req1 = {};
  1477. GUIConsoleService_Empower_Ans ans1 = {};
  1478. GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
  1479. auto rc = pClient->Connect();
  1480. if (rc == Error_Succeed)
  1481. {
  1482. req1.strUserID = req.UserID;
  1483. rc = (*pClient)(EntityResource::getLink().upgradeLink())->Empower(req1, ans1, 10000);
  1484. pClient->GetFunction()->CloseSession();
  1485. } else {
  1486. pClient->SafeDelete();
  1487. }
  1488. if (rc != Error_Succeed)
  1489. {
  1490. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
  1491. LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
  1492. }
  1493. else
  1494. {
  1495. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleEnpowerMaintainerReq")("远程维护授权成功");
  1496. pReplyPkg->AddStruct("EnpowerA", false, false, (BYTE*)&ret, sizeof(ret));
  1497. }
  1498. SendPackage(pReplyPkg);
  1499. return Error_Succeed;
  1500. }
  1501. ErrorCodeEnum CRemoteControllerCnn::HandleTakeoverPowerReq(const CSmartPointer<IPackage> &pRecvPkg)
  1502. {
  1503. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleTakeoverPowerReq")("request takeover maintainer power");
  1504. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1505. int nLen = pRecvPkg->GetStructLen("TakeovrR");
  1506. if (nLen <=0)
  1507. {
  1508. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [TakeovrR] fail");
  1509. SendPackage(pReplyPkg);
  1510. return Error_Param;
  1511. }
  1512. assert(nLen == sizeof(RvcTakeoverPowerReq));
  1513. RvcTakeoverPowerReq req = {};
  1514. int nArrayLen(0);
  1515. pRecvPkg->GetStructData("TakeovrR", (BYTE*)&req, &nLen, &nArrayLen);
  1516. RvcTakeoverPowerRet ret = {};
  1517. ret.TaskID = req.TaskID;
  1518. // 调用本地维护实体取控制信息
  1519. GUIConsoleService_Takeover_Req req1 = {};
  1520. GUIConsoleService_Takeover_Ans ans1 = {};
  1521. GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
  1522. auto rc = pClient->Connect();
  1523. if (rc == Error_Succeed)
  1524. {
  1525. req1.strUserID = req.UserID;
  1526. rc = (*pClient)(EntityResource::getLink().upgradeLink())->Takeover(req1, ans1, 10000);
  1527. pClient->GetFunction()->CloseSession();
  1528. } else {
  1529. pClient->SafeDelete();
  1530. }
  1531. if (rc != Error_Succeed)
  1532. {
  1533. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
  1534. LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
  1535. }
  1536. else
  1537. {
  1538. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleTakeoverPowerReq")("远程解除高级维护权限成功");
  1539. pReplyPkg->AddStruct("TakeovrA", false, false, (BYTE*)&ret, sizeof(ret));
  1540. }
  1541. SendPackage(pReplyPkg);
  1542. return Error_Succeed;
  1543. }
  1544. ErrorCodeEnum CRemoteControllerCnn::HandleForceQuitMaintainReq(const CSmartPointer<IPackage> &pRecvPkg)
  1545. {
  1546. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleForceQuitMaintainReq")("request force quit maintain");
  1547. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1548. int nLen = pRecvPkg->GetStructLen("ForceQtR");
  1549. if (nLen <=0)
  1550. {
  1551. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [ForceQtR] fail");
  1552. SendPackage(pReplyPkg);
  1553. return Error_Param;
  1554. }
  1555. assert(nLen == sizeof(RvcForceQuitReq));
  1556. RvcForceQuitReq req = {};
  1557. int nArrayLen(0);
  1558. pRecvPkg->GetStructData("ForceQtR", (BYTE*)&req, &nLen, &nArrayLen);
  1559. RvcForceQuitRet ret = {};
  1560. ret.TaskID = req.TaskID;
  1561. // 调用本地维护实体
  1562. GUIConsoleService_ForceQuit_Req req1 = {};
  1563. GUIConsoleService_ForceQuit_Ans ans1 = {};
  1564. GUIConsoleService_ClientBase *pClient = new GUIConsoleService_ClientBase(m_pEntity);
  1565. auto rc = pClient->Connect();
  1566. if (rc == Error_Succeed)
  1567. {
  1568. rc = (*pClient)(EntityResource::getLink().upgradeLink())->ForceQuit(req1, ans1, 10000);
  1569. pClient->GetFunction()->CloseSession();
  1570. } else {
  1571. pClient->SafeDelete();
  1572. }
  1573. if (rc != Error_Succeed)
  1574. {
  1575. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("调用[GuiConsole]实体失败").c_str());
  1576. LogError(Severity_Low, rc, 0, "调用[GuiConsole]实体失败");
  1577. }
  1578. else
  1579. {
  1580. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleForceQuitMaintainReq")("远程强制退出维护状态成功");
  1581. pReplyPkg->AddStruct("ForceQtA", false, false, (BYTE*)&ret, sizeof(ret));
  1582. }
  1583. SendPackage(pReplyPkg);
  1584. return Error_Succeed;
  1585. }
  1586. ErrorCodeEnum CRemoteControllerCnn::EnterMonitorMode()
  1587. {
  1588. m_chRemoteMode = 'O';
  1589. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_EnterMonitor));
  1590. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("EnterMonitorMode")("enter monitor mode succeed");
  1591. return Error_Succeed;
  1592. }
  1593. ErrorCodeEnum CRemoteControllerCnn::EnterMaintainMode(bool bLocalMaintain)
  1594. {
  1595. // 是否远程维护
  1596. if (bLocalMaintain)
  1597. {
  1598. // 弹出本地控制台
  1599. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("EnterMaintainMode")
  1600. ("show local maintain console");
  1601. }
  1602. else
  1603. {
  1604. // 远程维护 显示维护图片 遮挡前端操作界面
  1605. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("EnterMaintainMode")
  1606. ("show remote maintain picture");
  1607. }
  1608. m_chRemoteMode = bLocalMaintain ? 'L' : 'R';
  1609. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_EnterMaintain));
  1610. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("EnterMaintainMode")
  1611. ("enter maintain mode succeed");
  1612. return Error_Succeed;
  1613. }
  1614. ErrorCodeEnum CRemoteControllerCnn::EnterTerminalMode(int nRemotePort)
  1615. {
  1616. // 检测本机远程桌面是否打开
  1617. // 检测远程用户是否创建
  1618. // 连接分行桥接服务端口
  1619. // 重定向此连接至3389端口
  1620. m_chRemoteMode = 'T';
  1621. m_pFSM->PostEventFIFO(new FSMEvent(CRemoteControllerFSM::Event_EnterTerminal));
  1622. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("EnterTerminalMode")("enter terminal mode succeed");
  1623. return Error_Succeed;
  1624. }
  1625. ErrorCodeEnum CRemoteControllerCnn::HandleGetVersionReq(const CSmartPointer<IPackage> &pRecvPkg)
  1626. {
  1627. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleGetVersionReq")("request get active version");
  1628. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1629. int nLen = pRecvPkg->GetStructLen("GetVerR");
  1630. if (nLen <= 0)
  1631. {
  1632. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [GetVerR] fail");
  1633. SendPackage(pReplyPkg);
  1634. return Error_Param;
  1635. }
  1636. assert(nLen == sizeof(RvcGetActiveVerReq));
  1637. RvcGetActiveVerReq req = {};
  1638. int nArrayLen(0);
  1639. pRecvPkg->GetStructData("GetVerR", (BYTE*)&req, &nLen, &nArrayLen);
  1640. RvcGetActiveVerRet ret = {};
  1641. ret.TaskID = req.TaskID;
  1642. CSimpleStringA strPath;
  1643. auto rc = m_pEntity->GetFunction()->GetPath("RootVer", strPath);
  1644. if (rc == Error_Succeed)
  1645. {
  1646. strPath.Append(SPLIT_SLASH_STR).Append("active.txt");
  1647. FILE *pFile = fopen(strPath, "r");
  1648. if (pFile != NULL)
  1649. {
  1650. char szTemp[256] = {};
  1651. int n = fread(szTemp, 1, 256, pFile);
  1652. strncpy_s(ret.Version, sizeof(ret.Version), szTemp, _TRUNCATE);
  1653. fclose(pFile);
  1654. pReplyPkg->AddStruct("GetVerA", false, false, (BYTE*)&ret, sizeof(ret));
  1655. }
  1656. else
  1657. {
  1658. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleGetVersionReq")("open active.txt fail");
  1659. pReplyPkg->SetErrMsg(Error_NotExist, 0, "open active.txt fail");
  1660. }
  1661. }
  1662. else
  1663. {
  1664. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleGetVersionReq")("get root ver path fail");
  1665. pReplyPkg->SetErrMsg(Error_Unexpect, 0, "get root ver path fail");
  1666. }
  1667. SendPackage(pReplyPkg);
  1668. return Error_Succeed;
  1669. }
  1670. ErrorCodeEnum CRemoteControllerCnn::HandleViewRunCfgReq(const CSmartPointer<IPackage> &pRecvPkg)
  1671. {
  1672. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewRunCfgReq")("remote view runcfg");
  1673. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1674. int nLen = pRecvPkg->GetStructLen("VRunCfgR");
  1675. if (nLen <= 0)
  1676. {
  1677. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [VRunCfgR] fail");
  1678. SendPackage(pReplyPkg);
  1679. return Error_Param;
  1680. }
  1681. assert(nLen == sizeof(RvcViewRunCfgReq));
  1682. RvcViewRunCfgReq req = {};
  1683. int nArrayLen(0);
  1684. pRecvPkg->GetStructData("VRunCfgR", (BYTE*)&req, &nLen, &nArrayLen);
  1685. // 读取实体配置文件
  1686. CSimpleStringA strConfigName = req.ConfigName;
  1687. CSimpleStringA strCfgPath;
  1688. if (Error_Succeed != m_pEntity->GetFunction()->GetPath("RunInfo", strCfgPath)) {
  1689. Dbg("Get RunInfo path failed");
  1690. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("配置不存在").c_str());
  1691. SendPackage(pReplyPkg);
  1692. return Error_NotExist;
  1693. }
  1694. strCfgPath.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append(strConfigName);
  1695. if (!Base_Exist(strCfgPath.GetData()))
  1696. {
  1697. Dbg("runcfg [%s] not exist", (const char*)strCfgPath);
  1698. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("配置不存在").c_str());
  1699. SendPackage(pReplyPkg);
  1700. return Error_NotExist;
  1701. }
  1702. auto cfgSize = Base_filesize(strCfgPath.GetData());
  1703. char* tmpBuf = new char[cfgSize];
  1704. memset(tmpBuf, 0, cfgSize);
  1705. auto readRet = Base_readFile(strCfgPath.GetData(), 0, tmpBuf, cfgSize);
  1706. if (!readRet.first)
  1707. {
  1708. pReplyPkg->SetErrMsg(0, 0, "open runcfg fail");
  1709. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewRunCfgReq")("open file [%s] fail", (const char*)strCfgPath);
  1710. }
  1711. else
  1712. {
  1713. int nRetLen = sizeof(RvcViewRunCfgRet) + cfgSize * 2;
  1714. char* pBuf = new char[nRetLen];
  1715. memset(pBuf, 0, nRetLen);
  1716. RvcViewRunCfgRet* pRet = (RvcViewRunCfgRet*)pBuf;
  1717. int dstPos = 0;
  1718. for (auto i = 0; i < cfgSize; i++)
  1719. {
  1720. if ((i == 0 && tmpBuf[i] == '\n') || (tmpBuf[i] == '\n' && tmpBuf[i - 1] != '\r'))
  1721. pRet->ConfigData[dstPos++] = '\r';
  1722. pRet->ConfigData[dstPos++] = tmpBuf[i];
  1723. }
  1724. pReplyPkg->AddStruct("VRunCfgA", false, false, (BYTE*)pBuf, dstPos + sizeof(RvcViewConfigRet));
  1725. delete[] pBuf;
  1726. }
  1727. delete[] tmpBuf;
  1728. SendPackage(pReplyPkg);
  1729. return Error_Succeed;
  1730. }
  1731. ErrorCodeEnum CRemoteControllerCnn::HandleViewInstallLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  1732. {
  1733. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewInstallLogReq")("remote view install log");
  1734. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1735. int nLen = pRecvPkg->GetStructLen("VPkgLogR");
  1736. if (nLen <= 0)
  1737. {
  1738. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [VPkgLogR] fail");
  1739. SendPackage(pReplyPkg);
  1740. return Error_Param;
  1741. }
  1742. assert(nLen == sizeof(RvcViewInstallLogReq));
  1743. RvcViewInstallLogReq req = {};
  1744. int nArrayLen(0);
  1745. pRecvPkg->GetStructData("VPkgLogR", (BYTE*)&req, &nLen, &nArrayLen);
  1746. CSimpleStringA strEntityName = req.InstallLog;
  1747. // 读取升级日志文件
  1748. CSimpleStringA strLogFile = req.InstallLog;
  1749. CSimpleStringA strLogPath;
  1750. auto rc = m_pEntity->GetFunction()->GetPath("RunInfo", strLogPath);
  1751. assert(rc == Error_Succeed);
  1752. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)strLogFile);
  1753. if (!Base_Exist(strLogPath.GetData()))
  1754. {
  1755. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewInstallLogReq")("install log [%s] not exist", (const char*)strLogPath);
  1756. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("日志文件不存在").c_str());
  1757. SendPackage(pReplyPkg);
  1758. return Error_NotExist;
  1759. }
  1760. auto file = fopen(strLogPath, "rb");
  1761. if (file == NULL)
  1762. {
  1763. auto nError = errno;
  1764. pReplyPkg->SetErrMsg(Error_NotExist, 0, CSimpleStringA::Format("open install log [%s] fail(%d)", (const char*)strLogPath, nError));
  1765. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("open install log [%s] fail(%d)", (const char*)strLogPath, nError));
  1766. SendPackage(pReplyPkg);
  1767. return rc;
  1768. }
  1769. generateFilesize(file, strLogPath);
  1770. long long nRetLen = sizeof(RvcViewInstallLogRet)+nFileSize;
  1771. char *pBuf = new char[nRetLen];
  1772. memset(pBuf, 0, nRetLen);
  1773. RvcViewInstallLogRet *pRet = (RvcViewInstallLogRet*)pBuf;
  1774. pRet->TaskID = req.TaskID;
  1775. char *pLogData = (char*)pRet->LogData;
  1776. long long nHasRead(0);
  1777. DWORD nReadLen(0);
  1778. do
  1779. {
  1780. nReadLen = fread(pLogData + nHasRead, 1, nFileSize - nHasRead, file);
  1781. if (nReadLen >0)
  1782. nHasRead += nReadLen;
  1783. } while (nReadLen >0 && nHasRead < nFileSize);
  1784. fclose(file);
  1785. std::string gbkLogStr = SP::Utility::UTF8ToGBK(pLogData);
  1786. int gbkLen = sizeof(RvcViewInstallLogRet) + gbkLogStr.length();
  1787. char* gbkBuf = new char[gbkLen];
  1788. memset(gbkBuf, 0, gbkLen);
  1789. RvcViewInstallLogRet* gbkRet = (RvcViewInstallLogRet*)gbkBuf;
  1790. gbkRet->TaskID = req.TaskID;
  1791. memcpy(gbkRet->LogData, gbkLogStr.c_str(), gbkLogStr.length());
  1792. pReplyPkg->AddStruct("VPkgLogA", false, false, (BYTE*)gbkRet, gbkLen);
  1793. delete[] pBuf;
  1794. delete[] gbkBuf;
  1795. SendPackage(pReplyPkg);
  1796. return Error_Succeed;
  1797. }
  1798. ErrorCodeEnum CRemoteControllerCnn::ZipFile(LPCTSTR lpszZipArchive, LPCTSTR lpszSrcFile)
  1799. {
  1800. BOOL bResult = TRUE;
  1801. assert(lpszZipArchive);
  1802. assert(lpszZipArchive[0] != ('\0'));
  1803. if (!lpszZipArchive || lpszZipArchive[0] == ('\0'))
  1804. return Error_Param;
  1805. assert(lpszSrcFile);
  1806. assert(lpszSrcFile[0] != ('\0'));
  1807. if (!lpszSrcFile || lpszSrcFile[0] == ('\0'))
  1808. return Error_Param;
  1809. // does zip source file exist?
  1810. if (!Base_Exist(lpszSrcFile))
  1811. {
  1812. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("ZipFile")("source file '%s' cannot be found", lpszSrcFile);
  1813. return Error_NotExist;
  1814. }
  1815. // does zip file exist?
  1816. if (Base_Exist(lpszZipArchive))
  1817. {
  1818. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("ZipFile")("dest zip file '%s' exists, delete it!", lpszZipArchive);
  1819. Base_DeleteFile(lpszZipArchive);
  1820. }
  1821. // 先拷贝源文件(防止文件被占用导致压缩失败)
  1822. CSimpleStringA strCopyFile = lpszSrcFile;
  1823. strCopyFile += ".bak";
  1824. Base_CopyFile(lpszSrcFile, strCopyFile.GetData());
  1825. // use only the file name for zip file entry
  1826. TCHAR * cp = (TCHAR *)strrchr(lpszSrcFile, SPLIT_SLASH);
  1827. if (cp == NULL)
  1828. cp = (TCHAR *)lpszSrcFile;
  1829. else
  1830. cp++;
  1831. #if (defined _WIN32 || defined _WIN64)
  1832. HZIP hz = CreateZip((void *)lpszZipArchive, 0, ZIP_FILENAME);
  1833. #else
  1834. zipFile hz = zipOpen(lpszZipArchive, APPEND_STATUS_CREATE); //创建zip文件
  1835. #endif
  1836. if (hz)
  1837. {
  1838. #if (defined _WIN32 || defined _WIN64)
  1839. ZRESULT zr = ZipAdd(hz, cp, (void *)(const char*)strCopyFile, 0, ZIP_FILENAME);
  1840. CloseZip(hz);
  1841. if (zr == ZR_OK)
  1842. #else
  1843. auto zr = AddFileToZip(hz, cp, strCopyFile.GetData());
  1844. zipClose(hz, NULL);
  1845. if (zr)
  1846. #endif
  1847. {
  1848. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("ZipFile")("added '%s' to zip file '%s' succeed", lpszSrcFile, lpszZipArchive);
  1849. bResult = TRUE;
  1850. }
  1851. else
  1852. {
  1853. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("ZipFile")("failed to add zip source file '%s'", lpszSrcFile);
  1854. bResult = FALSE;
  1855. }
  1856. }
  1857. else
  1858. {
  1859. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("ZipFile")("failed to create zip file '%s'", lpszZipArchive);
  1860. bResult = FALSE;
  1861. }
  1862. if (Base_Exist(strCopyFile.GetData()))
  1863. Base_DeleteFile(strCopyFile.GetData());
  1864. if (!bResult && Base_Exist(lpszZipArchive))
  1865. Base_DeleteFile(lpszZipArchive);
  1866. return bResult ? Error_Succeed : Error_Unexpect;
  1867. }
  1868. ErrorCodeEnum CRemoteControllerCnn::BatchZipFiles(LPCTSTR lpszZipArchive, const list<string> &listSrcFiles)
  1869. {
  1870. BOOL bResult = TRUE;
  1871. if (!lpszZipArchive || lpszZipArchive[0] == ('\0'))
  1872. return Error_Param;
  1873. // does zip file exist?
  1874. if (Base_Exist(lpszZipArchive))
  1875. {
  1876. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("BatchZipFiles")("dest zip file '%s' exists, delete it!", lpszZipArchive);
  1877. Base_DeleteFile(lpszZipArchive);
  1878. }
  1879. #if (defined _WIN32 || defined _WIN64)
  1880. HZIP hz = CreateZip((void*)lpszZipArchive, 0, ZIP_FILENAME);
  1881. #else
  1882. zipFile hz = zipOpen(lpszZipArchive, APPEND_STATUS_CREATE); //创建zip文件
  1883. #endif
  1884. if (!hz)
  1885. {
  1886. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("BatchZipFiles")("failed to create zip file '%s'", lpszZipArchive);
  1887. return Error_Unexpect;
  1888. }
  1889. // does zip source file exist?
  1890. for (auto it = listSrcFiles.begin(); it != listSrcFiles.end(); it++)
  1891. {
  1892. if (!Base_Exist(it->c_str()))
  1893. {
  1894. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("BatchZipFiles")("source file '%s' cannot be found", it->c_str());
  1895. bResult = FALSE;
  1896. break;
  1897. }
  1898. // 先拷贝源文件(防止文件被占用导致压缩失败)
  1899. CSimpleStringA strSrcFile = it->c_str();
  1900. CSimpleStringA strCopyFile = strSrcFile;
  1901. strCopyFile += ".bak";
  1902. Base_CopyFile(strSrcFile.GetData(), strCopyFile.GetData());
  1903. // use only the file name for zip file entry
  1904. CSimpleStringA strTmp = strSrcFile;
  1905. const TCHAR * cp = strrchr(strTmp, SPLIT_SLASH);
  1906. if (cp != NULL)
  1907. {
  1908. *((char*)cp) = '_';
  1909. cp = (TCHAR *)strrchr(strTmp, SPLIT_SLASH);
  1910. }
  1911. if (cp != NULL)
  1912. cp++;
  1913. else
  1914. cp = strTmp;
  1915. #if (defined _WIN32 || defined _WIN64)
  1916. ZRESULT zr = ZipAdd(hz, cp, (void*)(const char*)strCopyFile, 0, ZIP_FILENAME);
  1917. CloseZip(hz);
  1918. #else
  1919. auto zr = AddFileToZip(hz, cp, strCopyFile.GetData());
  1920. #endif
  1921. // delete .bak file
  1922. if (Base_Exist(strCopyFile.GetData()))
  1923. Base_DeleteFile(strCopyFile.GetData());
  1924. // did add work?
  1925. #if (defined _WIN32 || defined _WIN64)
  1926. if (zr == ZR_OK)
  1927. #else
  1928. if(zr)
  1929. #endif
  1930. {
  1931. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("BatchZipFiles")("added '%s' to zip file '%s' succeed", cp, lpszZipArchive);
  1932. }
  1933. else
  1934. {
  1935. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("BatchZipFiles")("failed to add zip source file '%s'", cp);
  1936. bResult = FALSE;
  1937. break;
  1938. }
  1939. }
  1940. if (!bResult && Base_Exist(lpszZipArchive))
  1941. {
  1942. Base_DeleteFile(lpszZipArchive);
  1943. }
  1944. #if (defined _WIN32 || defined _WIN64)
  1945. if (hz)
  1946. CloseZip(hz);
  1947. #else
  1948. zipClose(hz, NULL);
  1949. #endif
  1950. return bResult ? Error_Succeed : Error_Unexpect;
  1951. }
  1952. ErrorCodeEnum CRemoteControllerCnn::HandleUploadLogByDateReq(const CSmartPointer<IPackage> &pRecvPkg)
  1953. {
  1954. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogByDateReq")("remote upload log");
  1955. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  1956. int nLen = pRecvPkg->GetStructLen("ULogBDR");
  1957. if (nLen <= 0)
  1958. {
  1959. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [ULogBDR] fail");
  1960. SendPackage(pReplyPkg);
  1961. return Error_Param;
  1962. }
  1963. assert(nLen == sizeof(RvcUploadLogByDateReq));
  1964. RvcUploadLogByDateReq req = {};
  1965. int nArrayLen(0);
  1966. pRecvPkg->GetStructData("ULogBDR", (BYTE*)&req, &nLen, &nArrayLen);
  1967. auto pFunc = m_pEntity->GetFunction();
  1968. CSimpleStringA strZipFileName;
  1969. CSimpleStringA strLogPath;
  1970. CSimpleStringA strZipPath;
  1971. if (req.StartPos <= 0)
  1972. {
  1973. auto strTodayLogName = 0 == strlen(req.ViewDate) ? CSimpleStringA(getCurData().c_str()) : CSimpleStringA(req.ViewDate);
  1974. CSystemStaticInfo sysInfo;
  1975. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  1976. assert(rc == Error_Succeed);
  1977. CSimpleStringA strReqLogName = req.UploadFile;
  1978. // 第一次上传,需生成对应ZIP文件
  1979. if (strReqLogName.Compare("Silverlight", true) == 0) // slv日志
  1980. {
  1981. strZipFileName = CSimpleStringA::Format("%s_slv_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
  1982. pFunc->GetPath("Slv", strLogPath);
  1983. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1984. }
  1985. else if (strReqLogName.IsEndWith(".zip", true)) // 安装包日志
  1986. {
  1987. strZipFileName = CSimpleStringA::Format("%s_pkg_%s", (const char*)sysInfo.strTerminalID, (const char*)strReqLogName);
  1988. pFunc->GetPath("RunInfo", strLogPath);
  1989. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)strReqLogName).Append(".log");
  1990. }
  1991. else
  1992. {
  1993. // 实体Dbg日志
  1994. strZipFileName = CSimpleStringA::Format("%s_dbg_%s_%s.zip",
  1995. (const char*)sysInfo.strTerminalID, (const char*)strReqLogName, (const char*)strTodayLogName);
  1996. pFunc->GetPath("Dbg", strLogPath);
  1997. if (strReqLogName.Compare("Shell", true) == 0)
  1998. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  1999. else if (strReqLogName.Compare("DualActive", true) == 0)
  2000. strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2001. else if (strReqLogName.Compare("guardian", true) == 0)
  2002. strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2003. else if (strReqLogName.Compare("SpHost", true) == 0)
  2004. strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2005. else if (strReqLogName.Compare("SysInit", true) == 0)
  2006. strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2007. else if (strReqLogName.Compare("SpBase", true) == 0)
  2008. strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2009. else
  2010. {
  2011. CEntityStaticInfo info = {};
  2012. auto nRet = pFunc->GetEntityStaticInfo(strReqLogName, info);
  2013. if (nRet != Error_Succeed)
  2014. {
  2015. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogByDateReq")("entity [%s] not exist", (const char*)strReqLogName);
  2016. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)strReqLogName).GetData()).c_str());
  2017. SendPackage(pReplyPkg);
  2018. return Error_NotExist;
  2019. }
  2020. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2021. }
  2022. }
  2023. pFunc->GetPath("Temp", strZipPath);
  2024. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  2025. if (!Base_Exist(strLogPath.GetData()))
  2026. {
  2027. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogByDateReq")("log [%s] not exist", (const char*)strLogPath);
  2028. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
  2029. SendPackage(pReplyPkg);
  2030. return Error_NotExist;
  2031. }
  2032. // 生成ZIP
  2033. rc = ZipFile(strZipPath, strLogPath);
  2034. if (rc != Error_Succeed)
  2035. {
  2036. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  2037. SendPackage(pReplyPkg);
  2038. return rc;
  2039. }
  2040. }
  2041. else
  2042. {
  2043. strZipFileName = req.UploadFile;
  2044. pFunc->GetPath("Temp", strZipPath);
  2045. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  2046. }
  2047. if (!Base_Exist(strZipPath.GetData()))
  2048. {
  2049. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogByDateReq")("zip log [%s] not exist", (const char*)strZipPath);
  2050. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  2051. SendPackage(pReplyPkg);
  2052. return Error_NotExist;
  2053. }
  2054. // 开始上传
  2055. long long nStartPos = req.StartPos;
  2056. if (nStartPos <0)
  2057. nStartPos = 0;
  2058. long long nMaxBytes = req.MaxRetBytes;
  2059. if (nMaxBytes <= 0)
  2060. nMaxBytes = 32 * 1024;
  2061. auto file = fopen(strZipPath, "rb");
  2062. if (file == NULL)
  2063. {
  2064. auto nError = errno;
  2065. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2066. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogByDateReq")(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  2067. SendPackage(pReplyPkg);
  2068. return Error_Unexpect;
  2069. }
  2070. generateFilesize(file, strZipPath);
  2071. if (nStartPos >0 && nStartPos < nFileSize)
  2072. {
  2073. _fseeki64(file, nStartPos, SEEK_SET);
  2074. }
  2075. else
  2076. nStartPos = 0;
  2077. long long nNextPos = nStartPos + nMaxBytes;
  2078. if (nMaxBytes + nStartPos > nFileSize)
  2079. {
  2080. nMaxBytes = nFileSize - nStartPos;
  2081. nNextPos = 0;
  2082. }
  2083. long long nRetLen = sizeof(RvcUploadLogByDateRet)+nMaxBytes;
  2084. char *pBuf = new char[nRetLen];
  2085. memset(pBuf, 0, nRetLen);
  2086. RvcUploadLogByDateRet *pRet = (RvcUploadLogByDateRet*)pBuf;
  2087. pRet->TaskID = req.TaskID;
  2088. strncpy_s(pRet->UploadFile, sizeof(pRet->UploadFile), strZipFileName, _TRUNCATE);
  2089. pRet->ZipFileLen = nFileSize;
  2090. pRet->StartPos = req.StartPos;
  2091. pRet->NextPos = nNextPos;
  2092. char *pLogData = (char*)pRet->LogData;
  2093. long long nHasRead(0);
  2094. DWORD nReadLen(0);
  2095. do
  2096. {
  2097. //Dbg("ftell: %d", ftell(file));
  2098. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  2099. //Dbg("ftell: %d", ftell(file));
  2100. if (nReadLen >0)
  2101. nHasRead += nReadLen;
  2102. } while (nReadLen >0 && nHasRead < nMaxBytes);
  2103. fclose(file);
  2104. if (nHasRead < nMaxBytes)
  2105. {
  2106. delete[] pBuf;
  2107. auto nError = GetLastError();
  2108. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2109. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogByDateReq")(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  2110. SendPackage(pReplyPkg);
  2111. return Error_Unexpect;
  2112. }
  2113. pReplyPkg->AddStruct("ULogBDA", false, false, (BYTE*)pBuf, nRetLen);
  2114. delete[] pBuf;
  2115. SendPackage(pReplyPkg);
  2116. if (nNextPos <= 0)
  2117. {
  2118. // 删除压缩文件
  2119. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogByDateReq")("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  2120. Base_DeleteFile(strZipPath.GetData());
  2121. }
  2122. return Error_Succeed;
  2123. }
  2124. ErrorCodeEnum CRemoteControllerCnn::HandleUploadLogReq(const CSmartPointer<IPackage> &pRecvPkg)
  2125. {
  2126. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")("remote upload log");
  2127. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2128. int nLen = pRecvPkg->GetStructLen("UpldLogR");
  2129. if (nLen <= 0)
  2130. {
  2131. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [UpldLogR] fail");
  2132. SendPackage(pReplyPkg);
  2133. return Error_Param;
  2134. }
  2135. assert(nLen == sizeof(RvcUploadLogReq));
  2136. RvcUploadLogReq req = {};
  2137. int nArrayLen(0);
  2138. pRecvPkg->GetStructData("UpldLogR", (BYTE*)&req, &nLen, &nArrayLen);
  2139. auto pFunc = m_pEntity->GetFunction();
  2140. CSimpleStringA strZipFileName;
  2141. CSimpleStringA strLogPath;
  2142. CSimpleStringA strZipPath;
  2143. if (req.StartPos <= 0)
  2144. {
  2145. auto strTodayLogName = getCurData().c_str();
  2146. CSystemStaticInfo sysInfo;
  2147. auto rc = pFunc->GetSystemStaticInfo(sysInfo);
  2148. assert(rc == Error_Succeed);
  2149. CSimpleStringA strReqLogName = req.UploadFile;
  2150. // 第一次上传,需生成对应ZIP文件
  2151. if (strReqLogName.Compare("Silverlight", true) == 0) // slv日志
  2152. {
  2153. strZipFileName = CSimpleStringA::Format("%s_slv_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
  2154. pFunc->GetPath("Slv", strLogPath);
  2155. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2156. }
  2157. else if (strReqLogName.Compare("AllDbg", true) == 0)
  2158. {
  2159. strZipFileName = CSimpleStringA::Format("%s_AllDbg_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
  2160. pFunc->GetPath("Dbg", strLogPath);
  2161. pFunc->GetPath("Temp", strZipPath);
  2162. strZipPath.Append(SPLIT_SLASH_STR).Append((const char*)strZipFileName);
  2163. try
  2164. {
  2165. std::pair<bool, std::string> result = MyZip::ZipDir(strZipPath.GetData(), strLogPath.GetData());
  2166. if (result.first)
  2167. goto BeginUpload;
  2168. else
  2169. {
  2170. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")("%s zip failed", strZipFileName);
  2171. pReplyPkg->SetErrMsg(Error_NotExist, 0, CSimpleStringA::Format("%s zip failed", strZipFileName));
  2172. SendPackage(pReplyPkg);
  2173. return Error_NotExist;
  2174. }
  2175. }
  2176. catch (exception &e)
  2177. {
  2178. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")("error:%s", e.what());
  2179. }
  2180. }
  2181. else if (strReqLogName.Compare("RunInfomation", true) == 0)
  2182. {
  2183. strZipFileName = CSimpleStringA::Format("%s_runInformation_%s.zip", (const char*)sysInfo.strTerminalID, (const char*)strTodayLogName);
  2184. pFunc->GetPath("Temp", strZipPath);
  2185. strZipPath.Append(SPLIT_SLASH_STR).Append((const char*)strZipFileName);
  2186. vector<std::string> zipArr;
  2187. pFunc->GetPath("RunInfo", strLogPath);
  2188. zipArr.push_back(strLogPath.GetData());
  2189. pFunc->GetPath("HardwareCfg", strLogPath);
  2190. strLogPath.Append(SPLIT_SLASH_STR).Append("root.ini");
  2191. zipArr.push_back(strLogPath.GetData());
  2192. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")("%s,%s,%s", zipArr[0].c_str(), zipArr[1].c_str(), strZipPath);
  2193. std::pair<bool, std::string> result = MyZip::ZipVector(strZipPath.GetData(), zipArr);
  2194. if (result.first)
  2195. goto BeginUpload;
  2196. else
  2197. {
  2198. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")("%s zip failed", strZipFileName);
  2199. pReplyPkg->SetErrMsg(Error_NotExist, 0, CSimpleStringA::Format("%s zip failed", strZipFileName));
  2200. SendPackage(pReplyPkg);
  2201. return Error_NotExist;
  2202. }
  2203. }
  2204. else if (strReqLogName.IsEndWith(".zip", true)) // 安装包日志
  2205. {
  2206. strZipFileName = CSimpleStringA::Format("%s_pkg_%s", (const char*)sysInfo.strTerminalID, (const char*)strReqLogName);
  2207. pFunc->GetPath("RunInfo", strLogPath);
  2208. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)strReqLogName).Append(".log");
  2209. }
  2210. else
  2211. {
  2212. // 实体Dbg日志
  2213. strZipFileName = CSimpleStringA::Format("%s_dbg_%s_%s.zip",
  2214. (const char*)sysInfo.strTerminalID, (const char*)strReqLogName, (const char*)strTodayLogName);
  2215. pFunc->GetPath("Dbg", strLogPath);
  2216. if (strReqLogName.Compare("Shell", true) == 0)
  2217. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2218. else if (strReqLogName.Compare("DualActive", true) == 0)
  2219. strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2220. else if (strReqLogName.Compare("guardian", true) == 0)
  2221. strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2222. else if (strReqLogName.Compare("SpHost", true) == 0)
  2223. strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2224. else if (strReqLogName.Compare("SysInit", true) == 0)
  2225. strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2226. else if (strReqLogName.Compare("SpBase", true) == 0)
  2227. strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2228. else
  2229. {
  2230. CEntityStaticInfo info = {};
  2231. auto nRet = pFunc->GetEntityStaticInfo(strReqLogName, info);
  2232. if (nRet != Error_Succeed)
  2233. {
  2234. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")("entity [%s] not exist", (const char*)strReqLogName);
  2235. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)strReqLogName).GetData()).c_str());
  2236. SendPackage(pReplyPkg);
  2237. return Error_NotExist;
  2238. }
  2239. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2240. }
  2241. }
  2242. pFunc->GetPath("Temp", strZipPath);
  2243. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  2244. if (!Base_Exist(strLogPath.GetData()))
  2245. {
  2246. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")("log [%s] not exist", (const char*)strLogPath);
  2247. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志文件[%s]不存在", (const char*)strLogPath).GetData()).c_str());
  2248. SendPackage(pReplyPkg);
  2249. return Error_NotExist;
  2250. }
  2251. // 生成ZIP
  2252. rc = ZipFile(strZipPath, strLogPath);
  2253. if (rc != Error_Succeed)
  2254. {
  2255. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  2256. SendPackage(pReplyPkg);
  2257. return rc;
  2258. }
  2259. }
  2260. else
  2261. {
  2262. strZipFileName = req.UploadFile;
  2263. pFunc->GetPath("Temp", strZipPath);
  2264. strZipPath.Append(SPLIT_SLASH_STR).Append(strZipFileName);
  2265. }
  2266. if (!Base_Exist(strZipPath.GetData()))
  2267. {
  2268. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")("zip log [%s] not exist", (const char*)strZipPath);
  2269. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  2270. SendPackage(pReplyPkg);
  2271. return Error_NotExist;
  2272. }
  2273. BeginUpload:
  2274. // 开始上传
  2275. long long nStartPos = req.StartPos;
  2276. if (nStartPos <0)
  2277. nStartPos = 0;
  2278. long long nMaxBytes = req.MaxRetBytes;
  2279. if (nMaxBytes <= 0)
  2280. nMaxBytes = 32 * 1024;
  2281. auto file = fopen(strZipPath, "rb");
  2282. if (file == NULL)
  2283. {
  2284. auto nError = errno;
  2285. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2286. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  2287. SendPackage(pReplyPkg);
  2288. return Error_Unexpect;
  2289. }
  2290. generateFilesize(file, strZipPath);
  2291. if (nStartPos >0 && nStartPos < nFileSize)
  2292. {
  2293. _fseeki64(file, nStartPos, SEEK_SET);
  2294. }
  2295. else
  2296. nStartPos = 0;
  2297. long long nNextPos = nStartPos + nMaxBytes;
  2298. if (nMaxBytes + nStartPos > nFileSize)
  2299. {
  2300. nMaxBytes = nFileSize - nStartPos;
  2301. nNextPos = 0;
  2302. }
  2303. long long nRetLen = sizeof(RvcUploadLogRet)+nMaxBytes;
  2304. char *pBuf = new char[nRetLen];
  2305. memset(pBuf, 0, nRetLen);
  2306. RvcUploadLogRet *pRet = (RvcUploadLogRet*)pBuf;
  2307. pRet->TaskID = req.TaskID;
  2308. strncpy_s(pRet->UploadFile, sizeof(pRet->UploadFile), strZipFileName, _TRUNCATE);
  2309. pRet->ZipFileLen = nFileSize;
  2310. pRet->StartPos = req.StartPos;
  2311. pRet->NextPos = nNextPos;
  2312. char *pLogData = (char*)pRet->LogData;
  2313. long long nHasRead(0);
  2314. DWORD nReadLen(0);
  2315. do
  2316. {
  2317. //Dbg("ftell: %d", ftell(file));
  2318. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  2319. //Dbg("ftell: %d", ftell(file));
  2320. if (nReadLen >0)
  2321. nHasRead += nReadLen;
  2322. } while (nReadLen >0 && nHasRead < nMaxBytes);
  2323. fclose(file);
  2324. if (nHasRead < nMaxBytes)
  2325. {
  2326. delete[] pBuf;
  2327. auto nError = GetLastError();
  2328. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2329. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  2330. SendPackage(pReplyPkg);
  2331. return Error_Unexpect;
  2332. }
  2333. pReplyPkg->AddStruct("UpldLogA", false, false, (BYTE*)pBuf, nRetLen);
  2334. delete[] pBuf;
  2335. SendPackage(pReplyPkg);
  2336. if (nNextPos <= 0)
  2337. {
  2338. // 删除压缩文件
  2339. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleUploadLogReq")("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  2340. Base_DeleteFile(strZipPath.GetData());
  2341. }
  2342. return Error_Succeed;
  2343. }
  2344. ErrorCodeEnum CRemoteControllerCnn::HandleViewEntityStateExReq(const CSmartPointer<IPackage> &pRecvPkg)
  2345. {
  2346. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleViewEntityStateExReq")("remote view entity state ex");
  2347. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2348. int nLen = pRecvPkg->GetStructLen("ViEnExR");
  2349. if (nLen <= 0)
  2350. {
  2351. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [ViEnExR] fail");
  2352. SendPackage(pReplyPkg);
  2353. return Error_Param;
  2354. }
  2355. assert(nLen == sizeof(RvcViewEntityExReq));
  2356. RvcViewEntityExReq req = {};
  2357. int nArrayLen(0);
  2358. pRecvPkg->GetStructData("ViEnExR", (BYTE*)&req, &nLen, &nArrayLen);
  2359. RvcViewEntityExRet1 ret1 = {};
  2360. ret1.TaskID = req.TaskID;
  2361. pReplyPkg->AddStruct("ViEnExA1", false, false, (BYTE*)&ret1, sizeof(ret1));
  2362. auto pFunc = m_pEntity->GetFunction();
  2363. CAutoArray<CSimpleStringA> strEntityNames;
  2364. CAutoArray<WORD> dwEntityIDs;
  2365. pFunc->GetAllRegistedEntity(strEntityNames, dwEntityIDs);
  2366. int nCount = strEntityNames.GetCount();
  2367. if (nCount >0)
  2368. {
  2369. nCount += 8; // 包括root\shell\silverlight3个虚拟实体
  2370. RvcViewEntityExRet2 *pRet2 = new RvcViewEntityExRet2[nCount];
  2371. memset(pRet2, 0, sizeof(RvcViewEntityExRet2)* nCount);
  2372. CSimpleStringA strEntCfgDir;
  2373. CSimpleStringA strRunCfgDir;
  2374. pFunc->GetPath("Cfg", strEntCfgDir);
  2375. strEntCfgDir.Append(SPLIT_SLASH_STR);
  2376. pFunc->GetPath("RunInfo", strRunCfgDir);
  2377. strRunCfgDir.Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR);
  2378. for (int i = 0; i<nCount - 8; i++)
  2379. {
  2380. CEntityRunInfo info = {};
  2381. CSimpleStringA strEntityName = strEntityNames[i];
  2382. pFunc->GetEntityRunInfo(strEntityName, info);
  2383. strncpy_s(pRet2[i].EntityName, sizeof(pRet2[i].EntityName), (const char*)strEntityName, _TRUNCATE);
  2384. pRet2[i].EntityID = dwEntityIDs[i];
  2385. pRet2[i].EntityState = info.eState;
  2386. pRet2[i].ProcessID = info.dwProcessID;
  2387. if (info.dwProcessID > 0)
  2388. {
  2389. #if (defined _WIN32 || defined _WIN64)
  2390. auto hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, info.dwProcessID);
  2391. if (hProcess != NULL)
  2392. {
  2393. PROCESS_MEMORY_COUNTERS pmc = {};
  2394. GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
  2395. pRet2[i].UsedMemory = pmc.WorkingSetSize / 1024 / 1024;
  2396. CloseHandle(hProcess);
  2397. }
  2398. #else
  2399. pRet2[i].UsedMemory = get_proc_mem(info.dwProcessID) / 1024 / 1024;
  2400. #endif
  2401. }
  2402. CSimpleStringA strEntCfgPath = strEntCfgDir + strEntityName + ".ini";
  2403. CSimpleStringA strRunCfgPath = strRunCfgDir + strEntityName + ".ini";
  2404. pRet2[i].HasConfig = Base_Exist(strEntCfgPath.GetData());
  2405. pRet2[i].HasRunCfg = Base_Exist(strRunCfgPath.GetData());
  2406. }
  2407. strncpy_s(pRet2[nCount - 8].EntityName, sizeof(pRet2[nCount - 8].EntityName), "guardian", _TRUNCATE);
  2408. pRet2[nCount - 8].HasConfig = false;
  2409. strncpy_s(pRet2[nCount - 7].EntityName, sizeof(pRet2[nCount - 7].EntityName), "DualActive", _TRUNCATE);
  2410. pRet2[nCount - 7].HasConfig = false;
  2411. strncpy_s(pRet2[nCount - 6].EntityName, sizeof(pRet2[nCount - 6].EntityName), "SpHost", _TRUNCATE);
  2412. pRet2[nCount - 6].HasConfig = false;
  2413. strncpy_s(pRet2[nCount - 5].EntityName, sizeof(pRet2[nCount - 5].EntityName), "SysInit", _TRUNCATE);
  2414. pRet2[nCount - 5].HasConfig = false;
  2415. strncpy_s(pRet2[nCount - 4].EntityName, sizeof(pRet2[nCount - 4].EntityName), "SpBase", _TRUNCATE);
  2416. pRet2[nCount - 4].HasConfig = false;
  2417. // root\shell\silverlight3个虚拟实体
  2418. strncpy_s(pRet2[nCount - 3].EntityName, sizeof(pRet2[nCount - 3].EntityName), "Root", _TRUNCATE);
  2419. pRet2[nCount - 3].HasConfig = true;
  2420. strncpy_s(pRet2[nCount - 2].EntityName, sizeof(pRet2[nCount - 2].EntityName), "Shell", _TRUNCATE);
  2421. pRet2[nCount - 2].HasConfig = true;
  2422. strncpy_s(pRet2[nCount - 1].EntityName, sizeof(pRet2[nCount - 1].EntityName), "Silverlight", _TRUNCATE);
  2423. pReplyPkg->AddStruct("ViEnExA2", false, false, (BYTE*)pRet2, sizeof(RvcViewEntityExRet2)* nCount, nCount);
  2424. }
  2425. SendPackage(pReplyPkg);
  2426. return Error_Succeed;
  2427. }
  2428. ErrorCodeEnum CRemoteControllerCnn::HandleBatchUploadLogsByDateReq(const CSmartPointer<IPackage> &pRecvPkg)
  2429. {
  2430. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")("remote batch upload logs by date");
  2431. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2432. int nLen = pRecvPkg->GetStructLen("ULogsBDR");
  2433. if (nLen <= 0)
  2434. {
  2435. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [ULogsBDR] fail");
  2436. SendPackage(pReplyPkg);
  2437. return Error_Param;
  2438. }
  2439. assert(nLen == sizeof(RvcBatchUploadLogsByDateReq) || nLen == sizeof(RvcBatchUploadLogsByDateReq2));
  2440. RvcBatchUploadLogsByDateReq req = {};
  2441. RvcBatchUploadLogsByDateReq2 req2 = {};
  2442. int nArrayLen(0);
  2443. if (nLen == sizeof(RvcBatchUploadLogsByDateReq))
  2444. pRecvPkg->GetStructData("ULogsBDR", (BYTE*)&req, &nLen, &nArrayLen);
  2445. else
  2446. {
  2447. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")("RvcBatchUploadLogsByDateReq2");
  2448. pRecvPkg->GetStructData("ULogsBDR", (BYTE*)&req2, &nLen, &nArrayLen);
  2449. req.TaskID = req2.TaskID;
  2450. strncpy_s(req.ViewDate, req2.ViewDate, sizeof(req2.ViewDate));
  2451. strncpy_s(req.UploadFile, req2.UploadFile, sizeof(req2.UploadFile));
  2452. strncpy_s(req.ZipFileName, req2.ZipFileName, sizeof(req2.ZipFileName));
  2453. req.StartPos = req2.StartPos;
  2454. req.MaxRetBytes = req2.MaxRetBytes;
  2455. }
  2456. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")("UploadFile:%s, ZipFileName:%s", req.UploadFile, req.ZipFileName);
  2457. CSimpleStringA strZipPath;
  2458. auto pFunc = m_pEntity->GetFunction();
  2459. pFunc->GetPath("Temp", strZipPath);
  2460. strZipPath.Append(SPLIT_SLASH_STR);
  2461. CSimpleStringA strZipFileName = req.ZipFileName;
  2462. strZipPath += strZipFileName;
  2463. if (req.StartPos <= 0)
  2464. {
  2465. CSimpleStringA strLogPath;
  2466. auto strTodayLogName = (0 == strlen(req.ViewDate) ? CSimpleStringA(getCurData().c_str()) : CSimpleStringA(req.ViewDate));
  2467. CSimpleStringA strReqLogNames = req.UploadFile;
  2468. auto arrFiles = strReqLogNames.Split(';');
  2469. list<string> listSrcFiles;
  2470. for (int i = 0; i < arrFiles.GetCount(); i++)
  2471. {
  2472. // 第一次上传,需生成对应ZIP文件
  2473. if (arrFiles[i].Compare("Silverlight", true) == 0) // slv日志
  2474. {
  2475. pFunc->GetPath("Slv", strLogPath);
  2476. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2477. }
  2478. else if (arrFiles[i].IsEndWith(".zip", true)) // 安装包日志
  2479. {
  2480. pFunc->GetPath("RunInfo", strLogPath);
  2481. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)arrFiles[i]).Append(".log");
  2482. }
  2483. else
  2484. {
  2485. // 实体Dbg日志
  2486. pFunc->GetPath("Dbg", strLogPath);
  2487. if (arrFiles[i].Compare("Shell", true) == 0)
  2488. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2489. else if (arrFiles[i].Compare("DualActive", true) == 0)
  2490. strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2491. else if (arrFiles[i].Compare("guardian", true) == 0)
  2492. strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2493. else if (arrFiles[i].Compare("SpHost", true) == 0)
  2494. strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2495. else if (arrFiles[i].Compare("SysInit", true) == 0)
  2496. strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2497. else if (arrFiles[i].Compare("SpBase", true) == 0)
  2498. strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2499. else if (arrFiles[i].Compare("Root", true) == 0)
  2500. strLogPath.Append(SPLIT_SLASH_STR).Append("Root").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2501. else
  2502. {
  2503. CEntityStaticInfo info = {};
  2504. auto nRet = pFunc->GetEntityStaticInfo(arrFiles[i], info);
  2505. if (nRet != Error_Succeed)
  2506. {
  2507. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")("entity [%s] not exist", (const char*)arrFiles[i]);
  2508. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)arrFiles[i]).GetData()).c_str());
  2509. SendPackage(pReplyPkg);
  2510. return Error_NotExist;
  2511. }
  2512. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2513. }
  2514. }
  2515. if (!Base_Exist(strLogPath.GetData()))
  2516. {
  2517. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")("log [%s] not exist", (const char*)strLogPath);
  2518. continue;
  2519. }
  2520. listSrcFiles.push_back((const char*)strLogPath);
  2521. }
  2522. if (listSrcFiles.size() == 0)
  2523. {
  2524. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("日志文件不存在").c_str());
  2525. SendPackage(pReplyPkg);
  2526. return Error_NotExist;
  2527. }
  2528. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")("strZipPath:%s", strZipPath);
  2529. // 生成ZIP
  2530. auto rc = BatchZipFiles(strZipPath, listSrcFiles);
  2531. if (rc != Error_Succeed)
  2532. {
  2533. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  2534. SendPackage(pReplyPkg);
  2535. return rc;
  2536. }
  2537. }
  2538. if (!Base_Exist(strZipPath.GetData()))
  2539. {
  2540. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")("zip log [%s] not exist", (const char*)strZipPath);
  2541. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  2542. SendPackage(pReplyPkg);
  2543. return Error_NotExist;
  2544. }
  2545. // 开始上传
  2546. long long nStartPos = req.StartPos;
  2547. if (nStartPos <0)
  2548. nStartPos = 0;
  2549. long long nMaxBytes = req.MaxRetBytes;
  2550. if (nMaxBytes <= 0)
  2551. nMaxBytes = 32 * 1024;
  2552. auto file = fopen(strZipPath, "rb");
  2553. if (file == NULL)
  2554. {
  2555. auto nError = errno;
  2556. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2557. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  2558. SendPackage(pReplyPkg);
  2559. return Error_Unexpect;
  2560. }
  2561. generateFilesize(file, strZipPath);
  2562. if (nStartPos >0 && nStartPos < nFileSize)
  2563. {
  2564. _fseeki64(file, nStartPos, SEEK_SET);
  2565. }
  2566. else
  2567. nStartPos = 0;
  2568. long long nNextPos = nStartPos + nMaxBytes;
  2569. if (nMaxBytes + nStartPos > nFileSize)
  2570. {
  2571. nMaxBytes = nFileSize - nStartPos;
  2572. nNextPos = 0;
  2573. }
  2574. long long nRetLen = sizeof(RvcBatchUploadLogsByDateRet) + nMaxBytes;
  2575. char *pBuf = new char[nRetLen];
  2576. memset(pBuf, 0, nRetLen);
  2577. RvcBatchUploadLogsByDateRet *pRet = (RvcBatchUploadLogsByDateRet*)pBuf;
  2578. pRet->TaskID = req.TaskID;
  2579. strncpy_s(pRet->ZipFileName, sizeof(pRet->ZipFileName), strZipFileName, _TRUNCATE);
  2580. pRet->ZipFileLen = nFileSize;
  2581. pRet->StartPos = req.StartPos;
  2582. pRet->NextPos = nNextPos;
  2583. char *pLogData = (char*)pRet->LogData;
  2584. long long nHasRead(0);
  2585. DWORD nReadLen(0);
  2586. do
  2587. {
  2588. //Dbg("ftell: %d", ftell(file));
  2589. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  2590. //Dbg("ftell: %d", ftell(file));
  2591. if (nReadLen >0)
  2592. nHasRead += nReadLen;
  2593. } while (nReadLen >0 && nHasRead < nMaxBytes);
  2594. fclose(file);
  2595. if (nHasRead < nMaxBytes)
  2596. {
  2597. delete[] pBuf;
  2598. auto nError = GetLastError();
  2599. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2600. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  2601. SendPackage(pReplyPkg);
  2602. return Error_Unexpect;
  2603. }
  2604. pReplyPkg->AddStruct("ULogsBDA", false, false, (BYTE*)pBuf, nRetLen);
  2605. delete[] pBuf;
  2606. SendPackage(pReplyPkg);
  2607. if (nNextPos <= 0)
  2608. {
  2609. // 删除压缩文件
  2610. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsByDateReq")("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  2611. Base_DeleteFile(strZipPath.GetData());
  2612. }
  2613. return Error_Succeed;
  2614. }
  2615. ErrorCodeEnum CRemoteControllerCnn::HandleBatchUploadLogsReq(const CSmartPointer<IPackage> &pRecvPkg)
  2616. {
  2617. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsReq")("remote batch upload logs");
  2618. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2619. int nLen = pRecvPkg->GetStructLen("UpdLogsR");
  2620. if (nLen <= 0)
  2621. {
  2622. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [UpdLogsR] fail");
  2623. SendPackage(pReplyPkg);
  2624. return Error_Param;
  2625. }
  2626. assert(nLen == sizeof(RvcBatchUploadLogsReq) || nLen == sizeof(RvcBatchUploadLogsReq2));
  2627. RvcBatchUploadLogsReq req = {};
  2628. RvcBatchUploadLogsReq2 req2 = {};
  2629. int nArrayLen(0);
  2630. if (nLen == sizeof(RvcBatchUploadLogsReq))
  2631. pRecvPkg->GetStructData("UpdLogsR", (BYTE*)&req, &nLen, &nArrayLen);
  2632. else
  2633. {
  2634. pRecvPkg->GetStructData("UpdLogsR", (BYTE*)&req2, &nLen, &nArrayLen);
  2635. req.TaskID = req2.TaskID;
  2636. req.StartPos = req2.StartPos;
  2637. req.MaxRetBytes = req2.MaxRetBytes;
  2638. strncpy_s(req.UploadFile, req2.UploadFile, sizeof(req2.UploadFile));
  2639. strncpy_s(req.ZipFileName, req2.ZipFileName, sizeof(req2.ZipFileName));
  2640. }
  2641. CSimpleStringA strZipPath;
  2642. auto pFunc = m_pEntity->GetFunction();
  2643. pFunc->GetPath("Temp", strZipPath);
  2644. strZipPath.Append(SPLIT_SLASH_STR);
  2645. CSimpleStringA strZipFileName = req.ZipFileName;
  2646. strZipPath += strZipFileName;
  2647. if (req.StartPos <= 0)
  2648. {
  2649. CSimpleStringA strLogPath;
  2650. auto strTodayLogName = CSimpleStringA(getCurData().c_str());
  2651. CSimpleStringA strReqLogNames = req.UploadFile;
  2652. auto arrFiles = strReqLogNames.Split(';');
  2653. list<string> listSrcFiles;
  2654. for (int i = 0; i < arrFiles.GetCount(); i++)
  2655. {
  2656. // 第一次上传,需生成对应ZIP文件
  2657. if (arrFiles[i].Compare("Silverlight", true) == 0) // slv日志
  2658. {
  2659. pFunc->GetPath("Slv", strLogPath);
  2660. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2661. }
  2662. else if (arrFiles[i].IsEndWith(".zip", true)) // 安装包日志
  2663. {
  2664. pFunc->GetPath("RunInfo", strLogPath);
  2665. strLogPath.Append(SPLIT_SLASH_STR).Append("InstallLog").Append(SPLIT_SLASH_STR).Append((const char*)arrFiles[i]).Append(".log");
  2666. }
  2667. else
  2668. {
  2669. // 实体Dbg日志
  2670. pFunc->GetPath("Dbg", strLogPath);
  2671. if (arrFiles[i].Compare("Shell", true) == 0)
  2672. strLogPath.Append(SPLIT_SLASH_STR).Append("SpShell").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2673. else if (arrFiles[i].Compare("DualActive", true) == 0)
  2674. strLogPath.Append(SPLIT_SLASH_STR).Append("DualActive").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2675. else if (arrFiles[i].Compare("guardian", true) == 0)
  2676. strLogPath.Append(SPLIT_SLASH_STR).Append("guardian").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2677. else if (arrFiles[i].Compare("SpHost", true) == 0)
  2678. strLogPath.Append(SPLIT_SLASH_STR).Append("SpHost").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2679. else if (arrFiles[i].Compare("SysInit", true) == 0)
  2680. strLogPath.Append(SPLIT_SLASH_STR).Append("SysInit").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2681. else if (arrFiles[i].Compare("SpBase", true) == 0)
  2682. strLogPath.Append(SPLIT_SLASH_STR).Append("SpBase").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2683. else if (arrFiles[i].Compare("Root", true) == 0)
  2684. strLogPath.Append(SPLIT_SLASH_STR).Append("Root").Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2685. else
  2686. {
  2687. CEntityStaticInfo info = {};
  2688. auto nRet = pFunc->GetEntityStaticInfo(arrFiles[i], info);
  2689. if (nRet != Error_Succeed)
  2690. {
  2691. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsReq")("entity [%s] not exist", (const char*)arrFiles[i]);
  2692. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("实体[%s]不存在", (const char*)arrFiles[i]).GetData()).c_str());
  2693. SendPackage(pReplyPkg);
  2694. return Error_NotExist;
  2695. }
  2696. strLogPath.Append(SPLIT_SLASH_STR).Append((const char*)info.strSpFileName).Append(SPLIT_SLASH_STR).Append((const char*)strTodayLogName).Append(".log");
  2697. }
  2698. }
  2699. if (!Base_Exist(strLogPath.GetData()))
  2700. {
  2701. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsReq")("log [%s] not exist", (const char*)strLogPath);
  2702. continue;
  2703. }
  2704. listSrcFiles.push_back((const char*)strLogPath);
  2705. }
  2706. if (listSrcFiles.size() == 0)
  2707. {
  2708. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK("日志文件不存在").c_str());
  2709. SendPackage(pReplyPkg);
  2710. return Error_NotExist;
  2711. }
  2712. // 生成ZIP
  2713. auto rc = BatchZipFiles(strZipPath, listSrcFiles);
  2714. if (rc != Error_Succeed)
  2715. {
  2716. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("生成压缩文件[%s]失败", (const char*)strZipFileName).GetData()).c_str());
  2717. SendPackage(pReplyPkg);
  2718. return rc;
  2719. }
  2720. }
  2721. if (!Base_Exist(strZipPath.GetData()))
  2722. {
  2723. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsReq")("zip log [%s] not exist", (const char*)strZipPath);
  2724. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("日志压缩文件[%s]不存在", (const char*)strZipPath).GetData()).c_str());
  2725. SendPackage(pReplyPkg);
  2726. return Error_NotExist;
  2727. }
  2728. // 开始上传
  2729. long long nStartPos = req.StartPos;
  2730. if (nStartPos <0)
  2731. nStartPos = 0;
  2732. long long nMaxBytes = req.MaxRetBytes;
  2733. if (nMaxBytes <= 0)
  2734. nMaxBytes = 32 * 1024;
  2735. auto file = fopen(strZipPath, "rb");
  2736. if (file == NULL)
  2737. {
  2738. auto nError = errno;
  2739. pReplyPkg->SetErrMsg(Error_Unexpect, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取日志文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2740. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsReq")(CSimpleStringA::Format("open debug log [%s] fail(%d)", (const char*)strZipPath, nError));
  2741. SendPackage(pReplyPkg);
  2742. return Error_Unexpect;
  2743. }
  2744. generateFilesize(file, strZipPath);
  2745. if (nStartPos >0 && nStartPos < nFileSize)
  2746. {
  2747. _fseeki64(file, nStartPos, SEEK_SET);
  2748. }
  2749. else
  2750. nStartPos = 0;
  2751. long long nNextPos = nStartPos + nMaxBytes;
  2752. if (nMaxBytes + nStartPos > nFileSize)
  2753. {
  2754. nMaxBytes = nFileSize - nStartPos;
  2755. nNextPos = 0;
  2756. }
  2757. long long nRetLen = sizeof(RvcBatchUploadLogsRet) + nMaxBytes;
  2758. char *pBuf = new char[nRetLen];
  2759. memset(pBuf, 0, nRetLen);
  2760. RvcBatchUploadLogsRet *pRet = (RvcBatchUploadLogsRet*)pBuf;
  2761. pRet->TaskID = req.TaskID;
  2762. strncpy_s(pRet->ZipFileName, sizeof(pRet->ZipFileName), strZipFileName, _TRUNCATE);
  2763. pRet->ZipFileLen = nFileSize;
  2764. pRet->StartPos = req.StartPos;
  2765. pRet->NextPos = nNextPos;
  2766. char *pLogData = (char*)pRet->LogData;
  2767. long long nHasRead(0);
  2768. DWORD nReadLen(0);
  2769. do
  2770. {
  2771. nReadLen = fread(pLogData + nHasRead, 1, nMaxBytes - nHasRead, file);
  2772. if (nReadLen >0)
  2773. nHasRead += nReadLen;
  2774. } while (nReadLen >0 && nHasRead < nMaxBytes);
  2775. fclose(file);
  2776. if (nHasRead < nMaxBytes)
  2777. {
  2778. delete[] pBuf;
  2779. auto nError = GetLastError();
  2780. pReplyPkg->SetErrMsg(Error_NotExist, 0, SP::Utility::UTF8ToGBK(CSimpleStringA::Format("读取文件[%s]失败(%d)", (const char*)strZipFileName, nError).GetData()).c_str());
  2781. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsReq")(CSimpleStringA::Format("read log [%s] fail(%d)", (const char*)strZipPath, nError));
  2782. SendPackage(pReplyPkg);
  2783. return Error_Unexpect;
  2784. }
  2785. pReplyPkg->AddStruct("UpdLogsA", false, false, (BYTE*)pBuf, nRetLen);
  2786. delete[] pBuf;
  2787. SendPackage(pReplyPkg);
  2788. if (nNextPos <= 0)
  2789. {
  2790. // 删除压缩文件
  2791. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleBatchUploadLogsReq")("finish upload log zip '%s', delete it!", (const char*)strZipFileName);
  2792. Base_DeleteFile(strZipPath.GetData());
  2793. }
  2794. return Error_Succeed;
  2795. }
  2796. ErrorCodeEnum CRemoteControllerCnn::HandleCancelUpgradeReq(const CSmartPointer<IPackage> &pRecvPkg)
  2797. {
  2798. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleCancelUpgradeReq")("remote cancel upgarde");
  2799. auto pReplyPkg = CreateReplyPackage(pRecvPkg);
  2800. int nLen = pRecvPkg->GetStructLen("CancUpgR");
  2801. if (nLen <= 0)
  2802. {
  2803. pReplyPkg->SetErrMsg(Error_Param, 0, "get struct [CancUpgR] fail");
  2804. SendPackage(pReplyPkg);
  2805. return Error_Param;
  2806. }
  2807. assert(nLen == sizeof(RvcCancelUpgradeReq));
  2808. RvcCancelUpgradeReq req = {};
  2809. int nArrayLen(0);
  2810. pRecvPkg->GetStructData("CancUpgR", (BYTE*)&req, &nLen, &nArrayLen);
  2811. RvcCancelUpgradeRet ret = {};
  2812. ret.TaskID = req.TaskID;
  2813. pReplyPkg->AddStruct("CancUpgA", false, false, (BYTE*)&ret, sizeof(ret));
  2814. CSimpleStringA strPack = req.PackName;
  2815. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleCancelUpgradeReq")("begin cancel upgrade: %s", (const char*)strPack);
  2816. // 调用升级实体接口升级
  2817. UpgradeMgrService_ClientBase *pClient = new UpgradeMgrService_ClientBase(m_pEntity);
  2818. auto rc = pClient->Connect();
  2819. if (rc == Error_Succeed)
  2820. {
  2821. UpgradeMgrService_CancelUpdate_Req req = {};
  2822. req.strPackFile = strPack;
  2823. UpgradeMgrService_CancelUpdate_Ans ans = {};
  2824. rc = (*pClient)(EntityResource::getLink().upgradeLink())->CancelUpdate(req, ans, 10000);
  2825. pClient->GetFunction()->CloseSession();
  2826. } else {
  2827. pClient->SafeDelete();
  2828. }
  2829. if (rc != Error_Succeed)
  2830. {
  2831. pReplyPkg->SetErrMsg(rc, 0, SP::Utility::UTF8ToGBK("远程取消升级失败").c_str());
  2832. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleCancelUpgradeReq")("remote cancel upgrade fail: 0x%X", rc);
  2833. }
  2834. else
  2835. {
  2836. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleCancelUpgradeReq")("remote cancel upgrade succeed!");
  2837. }
  2838. SendPackage(pReplyPkg);
  2839. return Error_Succeed;
  2840. }