feat: Implements MergingDatagramSocket#close().
This commit is contained in:
parent
82544254c4
commit
67f301eebc
@ -806,6 +806,8 @@ public class Component
|
|||||||
localCandidateIter.remove();
|
localCandidateIter.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSocket().close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -80,6 +80,11 @@ public class MergingDatagramSocket
|
|||||||
*/
|
*/
|
||||||
private SocketContainer active = null;
|
private SocketContainer active = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The flag which indicates whether this socket is closed.
|
||||||
|
*/
|
||||||
|
private boolean closed = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new {@link MergingDatagramSocket} instance.
|
* Initializes a new {@link MergingDatagramSocket} instance.
|
||||||
* @throws SocketException
|
* @throws SocketException
|
||||||
@ -88,6 +93,37 @@ public class MergingDatagramSocket
|
|||||||
throws SocketException
|
throws SocketException
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isClosed()
|
||||||
|
{
|
||||||
|
return closed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close()
|
||||||
|
{
|
||||||
|
if (isClosed())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: close all containers
|
||||||
|
|
||||||
|
closed = true;
|
||||||
|
// XXX do we want to risk obtaining the lock here, or should we just
|
||||||
|
// let any thread in find out about the close after it's next timeout?
|
||||||
|
synchronized (receiveLock)
|
||||||
|
{
|
||||||
|
receiveLock.notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@ -128,7 +164,6 @@ public class MergingDatagramSocket
|
|||||||
{
|
{
|
||||||
throw new IOException("No active socket.");
|
throw new IOException("No active socket.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -386,10 +421,14 @@ public class MergingDatagramSocket
|
|||||||
* underlying sockets. The socket is chosen on the base of the timestamp
|
* underlying sockets. The socket is chosen on the base of the timestamp
|
||||||
* of the reception of the first packet in its queue (so that earlier
|
* of the reception of the first packet in its queue (so that earlier
|
||||||
* packets are received first).
|
* packets are received first).
|
||||||
|
* @throws SocketTimeoutException if a socket timeout is set, and the
|
||||||
|
* call fails to receive a packet within the timeout.
|
||||||
|
* @throws SocketClosedException if the socket is closed while
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void receive(DatagramPacket p)
|
public void receive(DatagramPacket p)
|
||||||
throws SocketTimeoutException
|
throws SocketTimeoutException,
|
||||||
|
SocketClosedException
|
||||||
{
|
{
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
int soTimeout = this.soTimeout;
|
int soTimeout = this.soTimeout;
|
||||||
@ -405,6 +444,11 @@ public class MergingDatagramSocket
|
|||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
if (isClosed())
|
||||||
|
{
|
||||||
|
throw new SocketClosedException();
|
||||||
|
}
|
||||||
|
|
||||||
// Find the input socket with the oldest packet
|
// Find the input socket with the oldest packet
|
||||||
SocketContainer[] socketContainers = this.socketContainers;
|
SocketContainer[] socketContainers = this.socketContainers;
|
||||||
SocketContainer socketToReceiveFrom = null;
|
SocketContainer socketToReceiveFrom = null;
|
||||||
@ -431,22 +475,20 @@ public class MergingDatagramSocket
|
|||||||
// Otherwise wait on receiveLock.
|
// Otherwise wait on receiveLock.
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
long waitTimeout = 500;
|
||||||
|
if (soTimeout > 0)
|
||||||
|
{
|
||||||
|
long remaining
|
||||||
|
= start + soTimeout
|
||||||
|
- System.currentTimeMillis();
|
||||||
|
if (remaining <= 0)
|
||||||
|
throw new SocketTimeoutException();
|
||||||
|
|
||||||
|
waitTimeout = Math.min(waitTimeout, remaining);
|
||||||
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (soTimeout > 0)
|
receiveLock.wait(waitTimeout);
|
||||||
{
|
|
||||||
long remaining
|
|
||||||
= start + soTimeout
|
|
||||||
- System.currentTimeMillis();
|
|
||||||
if (remaining <= 0)
|
|
||||||
throw new SocketTimeoutException();
|
|
||||||
|
|
||||||
receiveLock.wait(remaining);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
receiveLock.wait();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (InterruptedException ie)
|
catch (InterruptedException ie)
|
||||||
{
|
{
|
||||||
|
|||||||
37
src/main/java/org/ice4j/socket/SocketClosedException.java
Normal file
37
src/main/java/org/ice4j/socket/SocketClosedException.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||||
|
*
|
||||||
|
* Copyright @ 2015-2016 Atlassian Pty Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.ice4j.socket;
|
||||||
|
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An exception that indicates that a socket is closed.
|
||||||
|
*/
|
||||||
|
public class SocketClosedException
|
||||||
|
extends SocketException
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Initializes a new {@link SocketClosedException}.
|
||||||
|
*/
|
||||||
|
public SocketClosedException()
|
||||||
|
{
|
||||||
|
// Keep the same message as the one used by jdk, since existing code
|
||||||
|
// might be matching against the string.
|
||||||
|
super("Socket closed");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user