RemoteControllerCnn.cpp 103 KB

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