feat: Implements MergingDatagramSocket#close().

This commit is contained in:
Boris Grozev 2016-11-29 16:12:11 -06:00
parent 82544254c4
commit 67f301eebc
3 changed files with 97 additions and 16 deletions

View File

@ -806,6 +806,8 @@ public class Component
localCandidateIter.remove();
}
}
getSocket().close();
}
/**

View File

@ -80,6 +80,11 @@ public class MergingDatagramSocket
*/
private SocketContainer active = null;
/**
* The flag which indicates whether this socket is closed.
*/
private boolean closed = false;
/**
* Initializes a new {@link MergingDatagramSocket} instance.
* @throws SocketException
@ -88,6 +93,37 @@ public class MergingDatagramSocket
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}
*/
@ -128,7 +164,6 @@ public class MergingDatagramSocket
{
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
* of the reception of the first packet in its queue (so that earlier
* 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
public void receive(DatagramPacket p)
throws SocketTimeoutException
throws SocketTimeoutException,
SocketClosedException
{
long start = System.currentTimeMillis();
int soTimeout = this.soTimeout;
@ -405,6 +444,11 @@ public class MergingDatagramSocket
{
do
{
if (isClosed())
{
throw new SocketClosedException();
}
// Find the input socket with the oldest packet
SocketContainer[] socketContainers = this.socketContainers;
SocketContainer socketToReceiveFrom = null;
@ -431,22 +475,20 @@ public class MergingDatagramSocket
// Otherwise wait on receiveLock.
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
{
if (soTimeout > 0)
{
long remaining
= start + soTimeout
- System.currentTimeMillis();
if (remaining <= 0)
throw new SocketTimeoutException();
receiveLock.wait(remaining);
}
else
{
receiveLock.wait();
}
receiveLock.wait(waitTimeout);
}
catch (InterruptedException ie)
{

View 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");
}
}