mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
use kernel utility int2str and float2str
This commit is contained in:
parent
6efd2dd27e
commit
9ead08725d
14 changed files with 104 additions and 75 deletions
|
@ -23,6 +23,8 @@
|
|||
|
||||
#include <srs_kernel_balance.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
SrsLbRoundRobin::SrsLbRoundRobin()
|
||||
{
|
||||
index = -1;
|
||||
|
@ -38,3 +40,18 @@ u_int32_t SrsLbRoundRobin::current()
|
|||
return index;
|
||||
}
|
||||
|
||||
string SrsLbRoundRobin::selected()
|
||||
{
|
||||
return elem;
|
||||
}
|
||||
|
||||
string SrsLbRoundRobin::select(const vector<string>& servers)
|
||||
{
|
||||
srs_assert(!servers.empty());
|
||||
|
||||
index = (int)(count++ % servers.size());
|
||||
elem = servers.at(index);
|
||||
|
||||
return elem;
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#include <srs_core.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* the round-robin load balance algorithm,
|
||||
|
@ -42,21 +43,15 @@ private:
|
|||
int index;
|
||||
// total scheduled count.
|
||||
u_int32_t count;
|
||||
// current selected server.
|
||||
std::string elem;
|
||||
public:
|
||||
SrsLbRoundRobin();
|
||||
virtual ~SrsLbRoundRobin();
|
||||
public:
|
||||
virtual u_int32_t current();
|
||||
public:
|
||||
template<typename T>
|
||||
const T& select(const std::vector<T>& servers)
|
||||
{
|
||||
srs_assert(!servers.empty());
|
||||
|
||||
index = (int)(count++ % servers.size());
|
||||
|
||||
return servers.at(index);
|
||||
}
|
||||
virtual std::string selected();
|
||||
virtual std::string select(const std::vector<std::string>& servers);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -169,14 +169,43 @@ string srs_dns_resolve(string host)
|
|||
|
||||
char ipv4[16];
|
||||
memset(ipv4, 0, sizeof(ipv4));
|
||||
for (int i = 0; i < answer->h_length; i++) {
|
||||
inet_ntop(AF_INET, answer->h_addr_list[i], ipv4, sizeof(ipv4));
|
||||
break;
|
||||
|
||||
// covert the first entry to ip.
|
||||
if (answer->h_length > 0) {
|
||||
inet_ntop(AF_INET, answer->h_addr_list[0], ipv4, sizeof(ipv4));
|
||||
}
|
||||
|
||||
return ipv4;
|
||||
}
|
||||
|
||||
void srs_parse_hostport(const string& hostport, string& host, int& port)
|
||||
{
|
||||
host = hostport;
|
||||
|
||||
size_t pos = hostport.find(":");
|
||||
if (pos != std::string::npos) {
|
||||
string p = hostport.substr(pos + 1);
|
||||
host = hostport.substr(0, pos);
|
||||
port = ::atoi(p.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
string srs_int2str(int64_t value)
|
||||
{
|
||||
// len(max int64_t) is 20, plus one "+-."
|
||||
char tmp[22];
|
||||
snprintf(tmp, 22, "%"PRId64, value);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
string srs_float2str(double value)
|
||||
{
|
||||
// len(max int64_t) is 20, plus one "+-."
|
||||
char tmp[22];
|
||||
snprintf(tmp, 22, "%.2f", value);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool srs_is_little_endian()
|
||||
{
|
||||
// convert to network(big-endian) order, if not equals,
|
||||
|
|
|
@ -52,6 +52,13 @@ extern int64_t srs_update_system_time_ms();
|
|||
|
||||
// dns resolve utility, return the resolved ip address.
|
||||
extern std::string srs_dns_resolve(std::string host);
|
||||
// split the host:port to host and port.
|
||||
extern void srs_parse_hostport(const std::string& hostport, std::string& host, int& port);
|
||||
|
||||
// parse the int64 value to string.
|
||||
extern std::string srs_int2str(int64_t value);
|
||||
// parse the float value to string, precise is 2.
|
||||
extern std::string srs_float2str(double value);
|
||||
|
||||
// whether system is little endian
|
||||
extern bool srs_is_little_endian();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue