Saves TCP sockets in NetAccessManager on the basis of both the local and remote address, in order to allow multiple Sockets/Connectors for a single local address.
This commit is contained in:
parent
3c34615d2b
commit
026bfff995
@ -62,6 +62,12 @@ class Connector
|
|||||||
*/
|
*/
|
||||||
private final TransportAddress listenAddress;
|
private final TransportAddress listenAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The remote address of the socket of this <tt>Connector</tt> if it is
|
||||||
|
* a TCP socket, or <tt>null</tt> if it is UDP.
|
||||||
|
*/
|
||||||
|
private final TransportAddress remoteAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a network access point.
|
* Creates a network access point.
|
||||||
* @param socket the socket that this access point is supposed to use for
|
* @param socket the socket that this access point is supposed to use for
|
||||||
@ -76,9 +82,19 @@ class Connector
|
|||||||
this.sock = socket;
|
this.sock = socket;
|
||||||
this.messageQueue = messageQueue;
|
this.messageQueue = messageQueue;
|
||||||
this.errorHandler = errorHandler;
|
this.errorHandler = errorHandler;
|
||||||
this.listenAddress = new TransportAddress(socket.getLocalAddress(),
|
|
||||||
socket.getLocalPort(), socket.getUDPSocket() != null ?
|
Transport transport
|
||||||
Transport.UDP : Transport.TCP);
|
= socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
|
||||||
|
this.listenAddress
|
||||||
|
= new TransportAddress(socket.getLocalAddress(),
|
||||||
|
socket.getLocalPort(),
|
||||||
|
transport);
|
||||||
|
this.remoteAddress
|
||||||
|
= transport == Transport.UDP
|
||||||
|
? null
|
||||||
|
: new TransportAddress(socket.getTCPSocket().getInetAddress(),
|
||||||
|
socket.getTCPSocket().getPort(),
|
||||||
|
transport);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -198,7 +214,8 @@ class Connector
|
|||||||
{
|
{
|
||||||
logger.log(
|
logger.log(
|
||||||
Level.WARNING,
|
Level.WARNING,
|
||||||
"Connector died: " + listenAddress,
|
"Connector died: " + listenAddress + " -> "
|
||||||
|
+ remoteAddress,
|
||||||
ex);
|
ex);
|
||||||
|
|
||||||
stop();
|
stop();
|
||||||
@ -292,4 +309,16 @@ class Connector
|
|||||||
{
|
{
|
||||||
return listenAddress;
|
return listenAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the remote <tt>TransportAddress</tt> in case of TCP, or
|
||||||
|
* <tt>null</tt> in case of UDP.
|
||||||
|
*
|
||||||
|
* @return the remote <tt>TransportAddress</tt> in case of TCP, or
|
||||||
|
* <tt>null</tt> in case of UDP.
|
||||||
|
*/
|
||||||
|
TransportAddress getRemoteAddress()
|
||||||
|
{
|
||||||
|
return remoteAddress;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
package org.ice4j.stack;
|
package org.ice4j.stack;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
|
||||||
@ -23,6 +24,7 @@ import org.ice4j.socket.*;
|
|||||||
*
|
*
|
||||||
* @author Emil Ivov
|
* @author Emil Ivov
|
||||||
* @author Aakash Garg
|
* @author Aakash Garg
|
||||||
|
* @author Boris Grozev
|
||||||
*/
|
*/
|
||||||
class NetAccessManager
|
class NetAccessManager
|
||||||
implements ErrorHandler
|
implements ErrorHandler
|
||||||
@ -34,7 +36,14 @@ class NetAccessManager
|
|||||||
= Logger.getLogger(NetAccessManager.class.getName());
|
= Logger.getLogger(NetAccessManager.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All access points currently in use with UDP. The table maps
|
* Synchronization root for access to {@link #netUDPAccessPoints} and
|
||||||
|
* {@link #netTCPAccessPoints} and the lists contained in
|
||||||
|
* {@link #netTCPAccessPoints}.
|
||||||
|
*/
|
||||||
|
private final Object connectorsSyncRoot = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All access points currently in use with UDP. The table maps local
|
||||||
* <tt>TransportAddress</tt>es to <tt>Connector</tt>s.
|
* <tt>TransportAddress</tt>es to <tt>Connector</tt>s.
|
||||||
*
|
*
|
||||||
* Due to the final hashCode() method of InetSocketAddress, TransportAddress
|
* Due to the final hashCode() method of InetSocketAddress, TransportAddress
|
||||||
@ -46,16 +55,18 @@ class NetAccessManager
|
|||||||
= new Hashtable<TransportAddress, Connector>();
|
= new Hashtable<TransportAddress, Connector>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All access points currently in use with TCP. The table maps
|
* All access points currently in use with TCP. The table maps local
|
||||||
* <tt>TransportAddress</tt>es to <tt>Connector</tt>s.
|
* <tt>TransportAddress</tt>es to a list of <tt>Connector</tt>s with that
|
||||||
|
* local address. To find the required <tt>Connector</tt> in the list, the
|
||||||
|
* remote address needs to be used.
|
||||||
*
|
*
|
||||||
* Due to the final hashCode() method of InetSocketAddress, TransportAddress
|
* Due to the final hashCode() method of InetSocketAddress, TransportAddress
|
||||||
* cannot override and it causes problem to store both UDP and TCP address
|
* cannot override and it causes problem to store both UDP and TCP address
|
||||||
* (i.e. 192.168.0.3:5000/tcp has the same hashcode as 192.168.0.3:5000/udp
|
* (i.e. 192.168.0.3:5000/tcp has the same hashcode as 192.168.0.3:5000/udp
|
||||||
* because InetSocketAddress does not take into account transport).
|
* because InetSocketAddress does not take into account transport).
|
||||||
*/
|
*/
|
||||||
private Map<TransportAddress, Connector> netTCPAccessPoints
|
private Map<TransportAddress, List<Connector>> netTCPAccessPoints
|
||||||
= new Hashtable<TransportAddress, Connector>();
|
= new Hashtable<TransportAddress, List<Connector>>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A synchronized FIFO where incoming messages are stocked for processing.
|
* A synchronized FIFO where incoming messages are stocked for processing.
|
||||||
@ -235,7 +246,8 @@ class NetAccessManager
|
|||||||
Connector ap = (Connector)callingThread;
|
Connector ap = (Connector)callingThread;
|
||||||
|
|
||||||
//make sure nothing's left and notify user
|
//make sure nothing's left and notify user
|
||||||
removeSocket(ap.getListenAddress());
|
removeSocket(ap.getListenAddress(),
|
||||||
|
ap.getRemoteAddress());
|
||||||
logger.log(Level.WARNING, "Removing connector:" + ap, error);
|
logger.log(Level.WARNING, "Removing connector:" + ap, error);
|
||||||
}
|
}
|
||||||
else if( callingThread instanceof MessageProcessor )
|
else if( callingThread instanceof MessageProcessor )
|
||||||
@ -265,56 +277,119 @@ class NetAccessManager
|
|||||||
protected void addSocket(IceSocketWrapper socket)
|
protected void addSocket(IceSocketWrapper socket)
|
||||||
{
|
{
|
||||||
//no null check - let it through as a NullPointerException
|
//no null check - let it through as a NullPointerException
|
||||||
|
Transport transport
|
||||||
|
= socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
|
||||||
TransportAddress localAddr
|
TransportAddress localAddr
|
||||||
= new TransportAddress(
|
= new TransportAddress(
|
||||||
socket.getLocalAddress(),
|
socket.getLocalAddress(),
|
||||||
socket.getLocalPort(),
|
socket.getLocalPort(),
|
||||||
socket.getUDPSocket() != null ? Transport.UDP :
|
transport);
|
||||||
Transport.TCP);
|
|
||||||
|
|
||||||
if (socket.getUDPSocket() != null &&
|
synchronized (connectorsSyncRoot)
|
||||||
!netUDPAccessPoints.containsKey(localAddr))
|
|
||||||
{
|
{
|
||||||
Connector ap = new Connector(socket, messageQueue, this);
|
switch (transport)
|
||||||
|
{
|
||||||
|
case UDP:
|
||||||
|
if (!netUDPAccessPoints.containsKey(localAddr))
|
||||||
|
{
|
||||||
|
Connector connector
|
||||||
|
= new Connector(socket, messageQueue, this);
|
||||||
|
|
||||||
netUDPAccessPoints.put(localAddr, ap);
|
netUDPAccessPoints.put(localAddr, connector);
|
||||||
ap.start();
|
connector.start();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case TCP:
|
||||||
|
List<Connector> connectors = netTCPAccessPoints.get(localAddr);
|
||||||
|
if (connectors == null)
|
||||||
|
{
|
||||||
|
connectors = new LinkedList<Connector>();
|
||||||
|
netTCPAccessPoints.put(localAddr, connectors);
|
||||||
|
}
|
||||||
|
|
||||||
|
Connector connector
|
||||||
|
= findTCPConnectorByRemoteAddress(
|
||||||
|
connectors,
|
||||||
|
new TransportAddress(
|
||||||
|
socket.getTCPSocket().getInetAddress(),
|
||||||
|
socket.getTCPSocket().getPort(),
|
||||||
|
Transport.TCP));
|
||||||
|
if (connector == null)
|
||||||
|
{
|
||||||
|
connector = new Connector(socket, messageQueue, this);
|
||||||
|
|
||||||
|
connectors.add(connector);
|
||||||
|
connector.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* From a list of <tt>Connector</tt>s with TCP sockets finds the one with
|
||||||
|
* remote address equal to <tt>dst</tt>.
|
||||||
|
* @param list the list of <tt>Connector</tt>s to search.
|
||||||
|
* @param dst the remote address of the connector to search for.
|
||||||
|
* @return the first <tt>Connector</tt> in <tt>list</tt> with a remote
|
||||||
|
* address equal to <tt>dst</tt>.
|
||||||
|
*/
|
||||||
|
private Connector findTCPConnectorByRemoteAddress(
|
||||||
|
List<Connector> list,
|
||||||
|
TransportAddress dst)
|
||||||
|
{
|
||||||
|
if (dst == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
for (Connector connector : list)
|
||||||
|
{
|
||||||
|
if (dst.equals(connector.getRemoteAddress()))
|
||||||
|
{
|
||||||
|
return connector;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (socket.getTCPSocket() != null &&
|
return null;
|
||||||
!netTCPAccessPoints.containsKey(localAddr))
|
|
||||||
{
|
|
||||||
Connector ap = new Connector(socket, messageQueue, this);
|
|
||||||
|
|
||||||
netTCPAccessPoints.put(localAddr, ap);
|
|
||||||
ap.start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops and deletes the specified access point.
|
* Stops and deletes the specified access point.
|
||||||
*
|
*
|
||||||
* @param address the address of the connector to remove.
|
* @param src the local address of the connector to remove.
|
||||||
|
* @param dst the remote address of the connector to remote. For TCP only,
|
||||||
|
* use <tt>null</tt> for UDP.
|
||||||
*/
|
*/
|
||||||
protected void removeSocket(TransportAddress address)
|
protected void removeSocket(TransportAddress src,
|
||||||
|
TransportAddress dst)
|
||||||
{
|
{
|
||||||
Connector ap;
|
Connector connector = null;
|
||||||
|
|
||||||
switch (address.getTransport())
|
synchronized (connectorsSyncRoot)
|
||||||
{
|
{
|
||||||
case TCP:
|
switch (src.getTransport())
|
||||||
ap = netTCPAccessPoints.remove(address);
|
{
|
||||||
break;
|
case UDP:
|
||||||
case UDP:
|
connector = netUDPAccessPoints.remove(src);
|
||||||
ap = netUDPAccessPoints.remove(address);
|
break;
|
||||||
break;
|
case TCP:
|
||||||
default:
|
List<Connector> connectors = netTCPAccessPoints.remove(src);
|
||||||
ap = null;
|
if (connectors != null)
|
||||||
break;
|
{
|
||||||
|
connector
|
||||||
|
= findTCPConnectorByRemoteAddress(connectors, dst);
|
||||||
|
|
||||||
|
if (connector != null)
|
||||||
|
{
|
||||||
|
connectors.remove(connector);
|
||||||
|
if (connectors.isEmpty())
|
||||||
|
netTCPAccessPoints.remove(src);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ap != null)
|
if(connector != null)
|
||||||
ap.stop();
|
connector.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -404,6 +479,37 @@ class NetAccessManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the <tt>Connector</tt> responsible for a particular source
|
||||||
|
* address and a particular destination address.
|
||||||
|
*
|
||||||
|
* @param src the source address.
|
||||||
|
* @param dst the destination address.
|
||||||
|
* Returns the <tt>Connector</tt> responsible for a particular source
|
||||||
|
* address and a particular destination address, or <tt>null</tt> if there's
|
||||||
|
* none.
|
||||||
|
*/
|
||||||
|
private Connector getConnector(TransportAddress src,
|
||||||
|
TransportAddress dst)
|
||||||
|
{
|
||||||
|
synchronized (connectorsSyncRoot)
|
||||||
|
{
|
||||||
|
switch (src.getTransport())
|
||||||
|
{
|
||||||
|
case UDP:
|
||||||
|
return netUDPAccessPoints.get(src);
|
||||||
|
case TCP:
|
||||||
|
List<Connector> connectors = netTCPAccessPoints.get(src);
|
||||||
|
if (connectors != null)
|
||||||
|
{
|
||||||
|
return findTCPConnectorByRemoteAddress(connectors, dst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
//--------------- SENDING MESSAGES -----------------------------------------
|
//--------------- SENDING MESSAGES -----------------------------------------
|
||||||
/**
|
/**
|
||||||
* Sends the specified stun message through the specified access point.
|
* Sends the specified stun message through the specified access point.
|
||||||
@ -426,16 +532,11 @@ class NetAccessManager
|
|||||||
{
|
{
|
||||||
byte[] bytes = stunMessage.encode(stunStack);
|
byte[] bytes = stunMessage.encode(stunStack);
|
||||||
|
|
||||||
Connector ap = null;
|
Connector ap = getConnector(srcAddr, remoteAddr);
|
||||||
|
|
||||||
if (srcAddr.getTransport() == Transport.UDP)
|
|
||||||
ap = netUDPAccessPoints.get(srcAddr);
|
|
||||||
else if (srcAddr.getTransport() == Transport.TCP)
|
|
||||||
ap = netTCPAccessPoints.get(srcAddr);
|
|
||||||
if (ap == null)
|
if (ap == null)
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"No socket has been added for source address: " + srcAddr);
|
"No socket has been added for: " + srcAddr + " <-> " + remoteAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ap.sendMessage(bytes, remoteAddr);
|
ap.sendMessage(bytes, remoteAddr);
|
||||||
@ -463,12 +564,7 @@ class NetAccessManager
|
|||||||
{
|
{
|
||||||
byte[] bytes = channelData.encode();
|
byte[] bytes = channelData.encode();
|
||||||
|
|
||||||
Connector ap = null;
|
Connector ap = getConnector(srcAddr, remoteAddr);
|
||||||
|
|
||||||
if (srcAddr.getTransport() == Transport.UDP)
|
|
||||||
ap = netUDPAccessPoints.get(srcAddr);
|
|
||||||
else if (srcAddr.getTransport() == Transport.TCP)
|
|
||||||
ap = netTCPAccessPoints.get(srcAddr);
|
|
||||||
if (ap == null)
|
if (ap == null)
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
@ -498,11 +594,7 @@ class NetAccessManager
|
|||||||
throws IllegalArgumentException,
|
throws IllegalArgumentException,
|
||||||
IOException, StunException
|
IOException, StunException
|
||||||
{
|
{
|
||||||
Connector ap = null;
|
Connector ap = getConnector(srcAddr, remoteAddr);
|
||||||
if (srcAddr.getTransport() == Transport.UDP)
|
|
||||||
ap = netUDPAccessPoints.get(srcAddr);
|
|
||||||
else if (srcAddr.getTransport() == Transport.TCP)
|
|
||||||
ap = netTCPAccessPoints.get(srcAddr);
|
|
||||||
if (ap == null)
|
if (ap == null)
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
|
|||||||
@ -122,15 +122,32 @@ public class StunStack
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops and deletes the connector listening on the specified local address.
|
* Stops and deletes the connector listening on the specified local address.
|
||||||
|
* Note this removes connectors with UDP sockets only, use
|
||||||
|
* {@link #removeSocket(org.ice4j.TransportAddress, org.ice4j.TransportAddress)}
|
||||||
|
* with the appropriate remote address for TCP.
|
||||||
*
|
*
|
||||||
* @param localAddr the access point to remove
|
* @param localAddr the local address of the socket to remove.
|
||||||
*/
|
*/
|
||||||
public void removeSocket(TransportAddress localAddr)
|
public void removeSocket(TransportAddress localAddr)
|
||||||
{
|
{
|
||||||
//first cancel all transactions using this address.
|
removeSocket(localAddr, null);
|
||||||
cancelTransactionsForAddress(localAddr);
|
}
|
||||||
|
|
||||||
netAccessManager.removeSocket(localAddr);
|
/**
|
||||||
|
* Stops and deletes the connector listening on the specified local address
|
||||||
|
* and remote address.
|
||||||
|
*
|
||||||
|
* @param localAddr the local address of the socket to remove.
|
||||||
|
* @param remoteAddr the remote address of the socket to remove. Use
|
||||||
|
* <tt>null</tt> for UDP.
|
||||||
|
*/
|
||||||
|
public void removeSocket(TransportAddress localAddr,
|
||||||
|
TransportAddress remoteAddr)
|
||||||
|
{
|
||||||
|
//first cancel all transactions using this address.
|
||||||
|
cancelTransactionsForAddress(localAddr, remoteAddr);
|
||||||
|
|
||||||
|
netAccessManager.removeSocket(localAddr, remoteAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -239,8 +256,13 @@ public class StunStack
|
|||||||
*
|
*
|
||||||
* @param localAddr the <tt>TransportAddress</tt> that we'd like to remove
|
* @param localAddr the <tt>TransportAddress</tt> that we'd like to remove
|
||||||
* transactions for.
|
* transactions for.
|
||||||
|
* @param remoteAddr the remote <tt>TransportAddress</tt> that we'd like to
|
||||||
|
* remove transactions for. If <tt>null</tt>, then it will not be taken
|
||||||
|
* into account (that is, all transactions with for <tt>localAddr</tt> will
|
||||||
|
* be cancelled).
|
||||||
*/
|
*/
|
||||||
private void cancelTransactionsForAddress(TransportAddress localAddr)
|
private void cancelTransactionsForAddress(TransportAddress localAddr,
|
||||||
|
TransportAddress remoteAddr)
|
||||||
{
|
{
|
||||||
List<StunClientTransaction> clientTransactionsToCancel = null;
|
List<StunClientTransaction> clientTransactionsToCancel = null;
|
||||||
|
|
||||||
@ -253,7 +275,9 @@ public class StunStack
|
|||||||
{
|
{
|
||||||
StunClientTransaction tran = clientTransactionsIter.next();
|
StunClientTransaction tran = clientTransactionsIter.next();
|
||||||
|
|
||||||
if (tran.getLocalAddress().equals(localAddr))
|
if (tran.getLocalAddress().equals(localAddr)
|
||||||
|
&& (remoteAddr == null
|
||||||
|
|| remoteAddr.equals(tran.getRemoteAddress())))
|
||||||
{
|
{
|
||||||
clientTransactionsIter.remove();
|
clientTransactionsIter.remove();
|
||||||
|
|
||||||
@ -299,14 +323,18 @@ public class StunStack
|
|||||||
|| (sendingAddr != null
|
|| (sendingAddr != null
|
||||||
&& sendingAddr.equals(localAddr)))
|
&& sendingAddr.equals(localAddr)))
|
||||||
{
|
{
|
||||||
serverTransactionsIter.remove();
|
if (remoteAddr == null
|
||||||
|
|| remoteAddr.equals(tran.getRequestSourceAddress()))
|
||||||
if (serverTransactionsToExpire == null)
|
|
||||||
{
|
{
|
||||||
serverTransactionsToExpire
|
serverTransactionsIter.remove();
|
||||||
= new LinkedList<StunServerTransaction>();
|
|
||||||
|
if (serverTransactionsToExpire == null)
|
||||||
|
{
|
||||||
|
serverTransactionsToExpire
|
||||||
|
= new LinkedList<StunServerTransaction>();
|
||||||
|
}
|
||||||
|
serverTransactionsToExpire.add(tran);
|
||||||
}
|
}
|
||||||
serverTransactionsToExpire.add(tran);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user