1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Squash: Fix random bug

This commit is contained in:
winlin 2021-07-04 16:04:51 +08:00
parent db3e11152e
commit 13d015b8fd
14 changed files with 28 additions and 34 deletions

View file

@ -135,39 +135,38 @@ void srs_parse_query_string(string q, map<string,string>& query)
}
}
static bool _random_initialized = false;
void srs_random_generate(char* bytes, int size)
{
if (!_random_initialized) {
_random_initialized = true;
::srandom((unsigned long)(srs_update_system_time() | (::getpid()<<13)));
}
for (int i = 0; i < size; i++) {
// the common value in [0x0f, 0xf0]
bytes[i] = 0x0f + (random() % (256 - 0x0f - 0x0f));
bytes[i] = 0x0f + (srs_random() % (256 - 0x0f - 0x0f));
}
}
std::string srs_random_str(int len)
{
if (!_random_initialized) {
_random_initialized = true;
::srandom((unsigned long)(srs_update_system_time() | (::getpid()<<13)));
}
static string random_table = "01234567890123456789012345678901234567890123456789abcdefghijklmnopqrstuvwxyz";
string ret;
ret.reserve(len);
for (int i = 0; i < len; ++i) {
ret.append(1, random_table[random() % random_table.size()]);
ret.append(1, random_table[srs_random() % random_table.size()]);
}
return ret;
}
long srs_random()
{
static bool _random_initialized = false;
if (!_random_initialized) {
_random_initialized = true;
::srandom((unsigned long)(srs_update_system_time() | (::getpid()<<13)));
}
return random();
}
string srs_generate_tc_url(string host, string vhost, string app, int port)
{
string tcUrl = "rtmp://";