From 7486919dbe697b2c718737fd96fcc923965945db Mon Sep 17 00:00:00 2001 From: windows11 Date: Wed, 13 Oct 2021 18:45:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=90=AF=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=AB=AF=EF=BC=8C=E6=9C=88=E6=B5=81=E9=87=8F=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E4=B8=A2=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/main.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/server/src/main.cpp b/server/src/main.cpp index a0acdf8..8962417 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -285,8 +285,10 @@ void CMain::JSONUpdateThread(void *pUser) } else { - str_format(pBuf, sizeof(aFileBuf) - (pBuf - aFileBuf), "{ \"name\": \"%s\", \"type\": \"%s\", \"host\": \"%s\", \"location\": \"%s\", \"online4\": false, \"online6\": false },\n", - pClients[i].m_aName, pClients[i].m_aType, pClients[i].m_aHost, pClients[i].m_aLocation); + // sava network traffic record to json when close client + // last_network_in == last network in record, last_network_out == last network out record + str_format(pBuf, sizeof(aFileBuf) - (pBuf - aFileBuf), "{ \"name\": \"%s\", \"type\": \"%s\", \"host\": \"%s\", \"location\": \"%s\", \"online4\": false, \"online6\": false, \"last_network_in\": %" PRId64 ", \"last_network_out\": %" PRId64 " },\n", + pClients[i].m_aName, pClients[i].m_aType, pClients[i].m_aHost, pClients[i].m_aLocation, pClients[i].m_LastNetworkIN, pClients[i].m_LastNetworkOUT); pBuf += strlen(pBuf); } } @@ -397,6 +399,42 @@ int CMain::ReadConfig() } } + // if file exists, read last network traffic record,reset m_LastNetworkIN and m_LastNetworkOUT + IOHANDLE nFile = io_open(m_Config.m_aJSONFile, IOFLAG_READ); + if(nFile) + { + int nFileSize = (int)io_length(nFile); + char *pNFileData = (char *)mem_alloc(nFileSize + 1, 1); + + io_read(nFile, pNFileData, nFileSize); + pNFileData[nFileSize] = 0; + io_close(nFile); + + json_settings nJsonSettings; + mem_zero(&nJsonSettings, sizeof(nJsonSettings)); + json_value *pNJsonData = json_parse_ex(&nJsonSettings, pNFileData, strlen(pNFileData), aError); + if(pNJsonData) + { + const json_value &rStart = (*pNJsonData)["servers"]; + if(rStart.type == json_array) + { + int ID = 0; + for(unsigned i = 0; i < rStart.u.array.length; i++) + { + if(ID < 0 || ID >= NET_MAX_CLIENTS) + continue; + + Client(ID)->m_LastNetworkIN = rStart[i]["last_network_in"].u.integer; + Client(ID)->m_LastNetworkOUT = rStart[i]["last_network_out"].u.integer; + + ID++; + } + } + json_value_free(pNJsonData); + } + mem_free(pNFileData); + } + // clean up json_value_free(pJsonData); mem_free(pFileData);