sthread
1.0.0
[sthread]是一個基於協程的高效能網路庫,目前提供支援TCP/UDP等協定的非阻塞式的用戶端和服務端程式庫
除此之外,基於該庫之上正在開發各個客戶端庫:memcache,redis,wrk等
編譯.a或.so,到目前目錄下執行:
make
編譯測試程式碼,到tests目錄下執行:
make
或只執行某個單元測試:
make event
void func ( void *args)
{
std::string *str = (std::string*)args;
time_t ttl = 0 ;
std::vector< int32_t > vc;
int ret = Instance<DNS>()-> dns_lookup (str-> c_str (), vc, &ttl);
if (ret < 0 )
{
LOG_ERROR ( " %s, make dns query failed " , str-> c_str ());
return ;
}
for (std::vector< int32_t >::iterator iter = vc. begin ();
iter != vc. end (); iter++)
{
struct in_addr addr;
memcpy (&addr, &(*iter), sizeof ( struct in_addr ));
LOG_DEBUG ( " %s, %d, %s " , str-> c_str (), *iter, inet_ntoa (addr));
}
LOG_DEBUG ( " ttl : %ld " , ttl);
}
int main ( int argc, char * argv[])
{
int ret = mt_init_frame ();
LOG_DEBUG ( " init ret : %d " , ret);
mt_set_hook_flag ();
mt_set_timeout ( 200 );
Frame *frame = Instance<Frame>();
// 测试 : 使用协程请求耗时
LOG_DEBUG ( " --- start time : %ld " , Util::system_ms ());
for ( int i = 2000 ; i < 2005 ; i++)
{
std::stringstream ss;
ss << " www. " << i << " .com " ;
std::string *s = new std::string (ss. str ());
Frame::CreateThread (func, s);
}
Frame::Loop ( true );
LOG_DEBUG ( " --- end time : %ld " , Util::system_ms ());
return 0 ;
}
說明其中dns內部修改呼叫接口
...
addr.sin_family = AF_INET;
if (m_dns_svr_ == NULL )
{
addr. sin_addr . s_addr = inet_addr (PUBLIC_DNS_DEFAULT_SERVER); // /服务器ip
}
else
{
addr. sin_addr . s_addr = inet_addr (m_dns_svr_); // /服务器ip
}
addr.sin_port = htons(( uint16_t )PUBLIC_DNS_DEFAULT_PORT);
int query_len = make_dns_query_format();
if (query_len <= 0 )
{
return - 1 ;
}
int recv_len = sizeof (m_recv_buf_);
ret = udp_sendrecv(&addr, m_send_buf_, query_len, m_recv_buf_, recv_len, m_timeout_);
LOG_DEBUG ( " ret : %d, recv_buf : %s, recv_len : %d " , ret, m_recv_buf_, recv_len);
...