mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-10 10:49:41 +02:00
Work on keep-alive interval. Fixes SMACK-123 and SMACK-121. Also general work on SmackConfiguration class.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2253 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
a7e7152b52
commit
167d40ac20
2 changed files with 82 additions and 39 deletions
|
@ -74,7 +74,6 @@ class PacketWriter {
|
|||
private List listeners = new ArrayList();
|
||||
private Thread listenerThread;
|
||||
private LinkedList sentPackets = new LinkedList();
|
||||
private Timer keepAliveTimer = new Timer(true);
|
||||
|
||||
/**
|
||||
* Creates a new packet writer with the specified connection.
|
||||
|
@ -102,9 +101,14 @@ class PacketWriter {
|
|||
listenerThread.setName("Smack Writer Listener Processor");
|
||||
listenerThread.setDaemon(true);
|
||||
|
||||
// Schedule a keep-alive task for once every 30 seconds. It will write
|
||||
// Schedule a keep-alive task to run if the feature is enabled. will write
|
||||
// out a space character each time it runs to keep the TCP/IP connection open.
|
||||
keepAliveTimer.scheduleAtFixedRate(new KeepAliveTask(), 30000, 30000);
|
||||
int keepAliveInterval = SmackConfiguration.getKeepAliveInterval();
|
||||
if (keepAliveInterval > 0) {
|
||||
Thread keepAliveThread = new Thread(new KeepAliveTask(keepAliveInterval));
|
||||
keepAliveThread.setDaemon(true);
|
||||
keepAliveThread.start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -227,7 +231,6 @@ class PacketWriter {
|
|||
finally {
|
||||
try {
|
||||
writer.close();
|
||||
keepAliveTimer.cancel();
|
||||
}
|
||||
catch (Exception e) { }
|
||||
}
|
||||
|
@ -323,14 +326,26 @@ class PacketWriter {
|
|||
* A TimerTask that keeps connections to the server alive by sending a space
|
||||
* character. The
|
||||
*/
|
||||
private class KeepAliveTask extends TimerTask {
|
||||
private class KeepAliveTask implements Runnable {
|
||||
|
||||
private int delay;
|
||||
|
||||
public KeepAliveTask(int delay) {
|
||||
this.delay = delay;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (!done) {
|
||||
while (!done) {
|
||||
synchronized (writer) {
|
||||
try {
|
||||
writer.write(" ");
|
||||
}
|
||||
catch (Exception e) { }
|
||||
try {
|
||||
// Sleep 30 seconds.
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
catch (InterruptedException ie) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue