RemoteControllerCnn.cpp 113 KB

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