diff --git a/trunk/src/app/srs_app_listener.cpp b/trunk/src/app/srs_app_listener.cpp index 25573561e..affc5f2fe 100755 --- a/trunk/src/app/srs_app_listener.cpp +++ b/trunk/src/app/srs_app_listener.cpp @@ -296,6 +296,7 @@ SrsUdpMuxSocket::SrsUdpMuxSocket(srs_netfd_t fd) peer_port = 0; fast_id_ = 0; + address_changed_ = false; cache_buffer_ = new SrsBuffer(buf, nb_buf); } @@ -323,39 +324,13 @@ int SrsUdpMuxSocket::recvfrom(srs_utime_t timeout) } // Parse address from cache. - bool parsed = false; if (from.ss_family == AF_INET) { sockaddr_in* addr = (sockaddr_in*)&from; - - // Load from fast cache, previous ip. - std::map::iterator it = cache_.find(addr->sin_addr.s_addr); - if (it == cache_.end()) { - peer_ip = inet_ntoa(addr->sin_addr); - cache_[addr->sin_addr.s_addr] = peer_ip; - } else { - peer_ip = it->second; - } - - peer_id_ = ""; - peer_port = ntohs(addr->sin_port); - fast_id_ = uint64_t(peer_port)<<48 | uint64_t(addr->sin_addr.s_addr); - parsed = true; + fast_id_ = uint64_t(addr->sin_port)<<48 | uint64_t(addr->sin_addr.s_addr); } - if (!parsed && nread > 0) { - // TODO: FIXME: Maybe we should not covert to string for each packet. - char address_string[64]; - char port_string[16]; - if (getnameinfo((sockaddr*)&from, fromlen, - (char*)&address_string, sizeof(address_string), - (char*)&port_string, sizeof(port_string), - NI_NUMERICHOST|NI_NUMERICSERV)) { - return -1; - } - - peer_ip = std::string(address_string); - peer_port = atoi(port_string); - } + // We will regenerate the peer_ip, peer_port and peer_id. + address_changed_ = true; return nread; } @@ -414,11 +389,48 @@ int SrsUdpMuxSocket::get_peer_port() const std::string SrsUdpMuxSocket::peer_id() { - if (peer_id_.empty()) { + if (address_changed_) { + address_changed_ = false; + + // Parse address from cache. + bool parsed = false; + if (from.ss_family == AF_INET) { + sockaddr_in* addr = (sockaddr_in*)&from; + + // Load from fast cache, previous ip. + std::map::iterator it = cache_.find(addr->sin_addr.s_addr); + if (it == cache_.end()) { + peer_ip = inet_ntoa(addr->sin_addr); + cache_[addr->sin_addr.s_addr] = peer_ip; + } else { + peer_ip = it->second; + } + + peer_port = ntohs(addr->sin_port); + parsed = true; + } + + if (!parsed) { + // TODO: FIXME: Maybe we should not covert to string for each packet. + char address_string[64]; + char port_string[16]; + if (getnameinfo((sockaddr*)&from, fromlen, + (char*)&address_string, sizeof(address_string), + (char*)&port_string, sizeof(port_string), + NI_NUMERICHOST|NI_NUMERICSERV)) { + return ""; + } + + peer_ip = std::string(address_string); + peer_port = atoi(port_string); + } + + // Build the peer id. static char id_buf[128]; int len = snprintf(id_buf, sizeof(id_buf), "%s:%d", peer_ip.c_str(), peer_port); peer_id_ = string(id_buf, len); } + return peer_id_; } diff --git a/trunk/src/app/srs_app_listener.hpp b/trunk/src/app/srs_app_listener.hpp index 66ad2dbad..53d0f92c0 100644 --- a/trunk/src/app/srs_app_listener.hpp +++ b/trunk/src/app/srs_app_listener.hpp @@ -153,6 +153,8 @@ private: private: // Cache for peer id. std::string peer_id_; + // If the address changed, we should generate the peer_id. + bool address_changed_; // For IPv4 client, we use 8 bytes int id to find it fastly. uint64_t fast_id_; public: