RemoteControllerCnn.cpp 112 KB

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