Skip to content

Commit

Permalink
Fix crash when the tested node has transferred too many bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
tindy2013 committed Nov 8, 2020
1 parent 29a6cc1 commit 5311d2b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 48 deletions.
17 changes: 10 additions & 7 deletions src/confbuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ int explodeLog(const std::string &log, std::vector<nodeInfo> &nodes)
node.remarks = vArray[1];
node.avgPing = ini.Get("AvgPing");
node.avgSpeed = ini.Get("AvgSpeed");
node.groupID = ini.GetInt("GroupID");
node.id = ini.GetInt("ID");
node.groupID = ini.GetNumber<int>("GroupID");
node.id = ini.GetNumber<int>("ID");
node.maxSpeed = ini.Get("MaxSpeed");
node.online = ini.GetBool("Online");
node.pkLoss = ini.Get("PkLoss");
ini.GetIntArray("RawPing", ",", node.rawPing);
ini.GetIntArray("RawSitePing", ",", node.rawSitePing);
ini.GetIntArray("RawSpeed", ",", node.rawSpeed);
ini.GetNumberArray<int>("RawPing", ",", node.rawPing);
ini.GetNumberArray<int>("RawSitePing", ",", node.rawSitePing);
ini.GetNumberArray<unsigned long long>("RawSpeed", ",", node.rawSpeed);
node.sitePing = ini.Get("SitePing");
node.totalRecvBytes = ini.GetInt("UsedTraffic");
node.totalRecvBytes = ini.GetNumber<unsigned long long>("UsedTraffic");
node.ulSpeed = ini.Get("ULSpeed");
nodes.push_back(node);
}
Expand Down Expand Up @@ -160,7 +160,10 @@ std::string ssrConstruct(const std::string &group, const std::string &remarks, c
config = config_libev;
config = replace_first(config, "?group?", group);
config = replace_first(config, "?remarks?", remarks);
config = replace_first(config, "?remarks_base64?", remarks_base64);
if(remarks_base64.empty())
config = replace_first(config, "?remarks_base64?", base64_encode(remarks));
else
config = replace_first(config, "?remarks_base64?", remarks_base64);
config = replace_first(config, "?server?", isIPv6(server) ? "[" + server + "]" : server);
config = replace_first(config, "?port?", port);
config = replace_first(config, "?protocol?", protocol);
Expand Down
50 changes: 17 additions & 33 deletions src/ini_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,19 +649,19 @@ class INIReader
}

/**
* @brief Retrieve one integer item value with the exact same name in the given section.
* @brief Retrieve a number item value with the exact same name in the given section.
*/
int GetInt(const std::string &section, const std::string &itemName)
template <typename T> T GetNumber(const std::string &section, const std::string &itemName, T def_value = 0)
{
return to_int(Get(section, itemName), 0);
return to_number<T>(Get(section, itemName), def_value);
}

/**
* @brief Retrieve one integer item value with the exact same name in current section.
* @brief Retrieve a number item value with the exact same name in current section.
*/
int GetInt(const std::string &itemName)
template <typename T> T GetNumber(const std::string &itemName, T def_value = 0)
{
return GetInt(current_section, itemName);
return GetNumber<T>(current_section, itemName, def_value);
}

/**
Expand All @@ -687,26 +687,26 @@ class INIReader
}

/**
* @brief Retrieve a string style array with specific separator and write into integer array.
* @brief Retrieve a string style array with specific separator and write into number array.
*/
template <typename T> void GetIntArray(const std::string &section, const std::string &itemName, const std::string &separator, T &Array)
template <typename T, typename U> void GetNumberArray(const std::string &section, const std::string &itemName, const std::string &separator, U &Array)
{
string_array vArray;
unsigned int index, UBound = sizeof(Array) / sizeof(Array[0]);
vArray = split(Get(section, itemName), separator);
for(index = 0; index < vArray.size() && index < UBound; index++)
Array[index] = stoi(vArray[index]);
Array[index] = to_number<T>(vArray[index]);
for(; index < UBound; index++)
Array[index] = 0;
}

/**
* @brief Retrieve a string style array with specific separator and write into integer array.
* @brief Retrieve a string style array with specific separator and write into number array.
*/
template <typename T> void GetIntArray(const std::string &itemName, const std::string &separator, T &Array)
template <typename T, typename U> void GetNumberArray(const std::string &itemName, const std::string &separator, U &Array)
{
if(current_section.size())
GetIntArray(current_section, itemName, separator, Array);
GetNumberArray<T>(current_section, itemName, separator, Array);
}

/**
Expand Down Expand Up @@ -763,35 +763,19 @@ class INIReader
}

/**
* @brief Add a double value with given values.
*/
int SetDouble(const std::string &section, std::string itemName, double itemVal)
{
return Set(section, std::move(itemName), std::to_string(itemVal));
}

/**
* @brief Add a double value with given values.
*/
int SetDouble(std::string itemName, double itemVal)
{
return SetDouble(current_section, std::move(itemName), itemVal);
}

/**
* @brief Add a long value with given values.
* @brief Add a number value with given values.
*/
int SetLong(const std::string &section, std::string itemName, long itemVal)
template <typename T> int SetNumber(const std::string &section, std::string itemName, T itemVal)
{
return Set(section, std::move(itemName), std::to_string(itemVal));
}

/**
* @brief Add a long value with given values.
* @brief Add a number value with given values.
*/
int SetLong(std::string itemName, long itemVal)
template <typename T> int SetNumber(std::string itemName, T itemVal)
{
return SetLong(current_section, std::move(itemName), itemVal);
return SetNumber<T>(current_section, std::move(itemName), itemVal);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,9 @@ void saveResult(std::vector<nodeInfo> &nodes)
ini.Set("AvgSpeed", x.avgSpeed);
ini.Set("MaxSpeed", x.maxSpeed);
ini.Set("ULSpeed", x.ulSpeed);
ini.SetLong("UsedTraffic", x.totalRecvBytes);
ini.SetLong("GroupID", x.groupID);
ini.SetLong("ID", x.id);
ini.SetNumber<unsigned long long>("UsedTraffic", x.totalRecvBytes);
ini.SetNumber<int>("GroupID", x.groupID);
ini.SetNumber<int>("ID", x.id);
ini.SetBool("Online", x.online);
ini.SetArray("RawPing", ",", x.rawPing);
ini.SetArray("RawSitePing", ",", x.rawSitePing);
Expand Down
9 changes: 6 additions & 3 deletions src/multithread_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const int times_to_ping = 10, fail_limit = 2;
//for use of multi-thread socket test
typedef std::lock_guard<std::mutex> guarded_mutex;
std::mutex opened_socket_mutex;
std::atomic_int received_bytes = 0;
std::atomic_ullong received_bytes = 0;
std::atomic_int launched = 0, still_running = 0;
std::atomic_bool EXIT_FLAG = false;

Expand Down Expand Up @@ -361,7 +361,7 @@ int perform_test(nodeInfo &node, std::string localaddr, int localport, std::stri

writeLog(LOG_TYPE_FILEDL, "All threads launched. Start accumulating data.");
auto start = steady_clock::now();
int transferred_bytes = 0, last_bytes = 0, this_bytes = 0, cur_recv_bytes = 0, max_speed = 0;
unsigned long long transferred_bytes = 0, last_bytes = 0, this_bytes = 0, cur_recv_bytes = 0, max_speed = 0;
for(i = 1; i < 21; i++)
{
sleep(500); //accumulate data
Expand Down Expand Up @@ -421,6 +421,7 @@ int perform_test(nodeInfo &node, std::string localaddr, int localport, std::stri
pthread_kill(threads[i], SIGUSR1);
#endif
*/
pthread_cancel(threads[i]);
pthread_join(threads[i], NULL);
writeLog(LOG_TYPE_FILEDL, "Thread #" + std::to_string(i + 1) + " has exited.");
}
Expand Down Expand Up @@ -466,7 +467,7 @@ int upload_test(nodeInfo &node, std::string localaddr, int localport, std::strin

writeLog(LOG_TYPE_FILEUL, "Worker threads launched. Start accumulating data.");
auto start = steady_clock::now();
int transferred_bytes = 0, this_bytes = 0, cur_sent_bytes = 0;
unsigned long long transferred_bytes = 0, this_bytes = 0, cur_sent_bytes = 0;
for(i = 1; i < 11; i++)
{
sleep(1000); //accumulate data
Expand Down Expand Up @@ -516,7 +517,9 @@ int upload_test(nodeInfo &node, std::string localaddr, int localport, std::strin
pthread_kill(workers[i], SIGUSR1);
#endif // _WIN32
*/
pthread_cancel(workers[i]);
pthread_join(workers[i], NULL);
writeLog(LOG_TYPE_FILEUL, "Thread #" + std::to_string(i + 1) + " has exited.");
}
writeLog(LOG_TYPE_FILEUL, "Upload test completed.");
return 0;
Expand Down
4 changes: 2 additions & 2 deletions src/nodeinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ struct nodeInfo
std::string server;
int port = 0;
std::string proxyStr;
long long rawSpeed[20] = {};
long long totalRecvBytes = 0;
unsigned long long rawSpeed[20] = {};
unsigned long long totalRecvBytes = 0;
int duration = 0;
std::string avgSpeed = "N/A";
std::string maxSpeed = "N/A";
Expand Down

0 comments on commit 5311d2b

Please sign in to comment.