add CUdpSocket::Open() to address family selection

Open(uint16 uiPort) -> Open(uint16 uiPort, int af = AF_UNSPEC)
pull/189/head
SASANO Takayoshi 5 years ago
parent a90b01549e
commit 690d234351

@ -50,7 +50,7 @@ CUdpSocket::~CUdpSocket()
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
// open & close // open & close
bool CUdpSocket::Open(uint16 uiPort) bool CUdpSocket::Open(uint16 uiPort, int af)
{ {
bool open = false; bool open = false;
struct sockaddr_storage *ss; struct sockaddr_storage *ss;
@ -61,9 +61,11 @@ bool CUdpSocket::Open(uint16 uiPort)
m_Ip[i] = CIp(g_Reflector.GetListenIp(i), uiPort); m_Ip[i] = CIp(g_Reflector.GetListenIp(i), uiPort);
ss = m_Ip[i].GetSockAddr(ss_len); ss = m_Ip[i].GetSockAddr(ss_len);
// create socket // create socket, avoid two cases:
// (avoid INADDR_ANY on secondary and later IP address) // 1. address family is specified and not matched
m_Socket[i] = ( i != 0 && g_Reflector.GetListenIp(i) == CIp() ) ? // 2. INADDR_ANY (0.0.0.0) on secondary and later IP address
m_Socket[i] = ( ( af != AF_UNSPEC && af != ss->ss_family ) ||
( i != 0 && g_Reflector.GetListenIp(i) == CIp() ) ) ?
-1 : socket(ss->ss_family, SOCK_DGRAM, 0); -1 : socket(ss->ss_family, SOCK_DGRAM, 0);
if ( m_Socket[i] != -1 ) if ( m_Socket[i] != -1 )
{ {

@ -57,7 +57,7 @@ public:
~CUdpSocket(); ~CUdpSocket();
// open & close // open & close
bool Open(uint16); bool Open(uint16, int = AF_UNSPEC);
void Close(void); void Close(void);
int GetSocket(const CIp &); int GetSocket(const CIp &);

Loading…
Cancel
Save

Powered by TurnKey Linux.