Browse Source

Send QUIT on disconnect. (Close #7)

pull/114/head
Shane Mc Cormack 8 years ago
parent
commit
4e58a9c854

+ 2
- 0
common/src/com/dmdirc/parser/common/QueuePriority.java View File

@@ -26,6 +26,8 @@ package com.dmdirc.parser.common;
26 26
  * Queue Priority.
27 27
  */
28 28
 public enum QueuePriority {
29
+    /** Immediate priority (Skip Queue). */
30
+    IMMEDIATE,
29 31
     /** High priority. */
30 32
     HIGH,
31 33
     /** Normal priority. */

+ 20
- 13
irc/src/com/dmdirc/parser/irc/IRCParser.java View File

@@ -50,6 +50,7 @@ import com.dmdirc.parser.irc.IRCReader.ReadLine;
50 50
 import com.dmdirc.parser.irc.outputqueue.OutputQueue;
51 51
 
52 52
 import java.io.IOException;
53
+import java.io.PrintWriter;
53 54
 import java.net.InetAddress;
54 55
 import java.net.Socket;
55 56
 import java.net.URI;
@@ -205,11 +206,13 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
205 206
     protected SocketState currentSocketState = SocketState.NULL;
206 207
     /**
207 208
      * The underlying socket used for reading/writing to the IRC server.
208
-     * For normal sockets this will be the same as {@link #socket} but for SSL
209
+     * For normal sockets this will be the same as {@link #mySocket} but for SSL
209 210
      * connections this will be the underlying {@link Socket} while
210
-     * {@link #socket} will be an {@link SSLSocket}.
211
+     * {@link #mySocket} will be an {@link SSLSocket}.
211 212
      */
212 213
     private Socket rawSocket;
214
+    /** Socket that we can do stuff to. */
215
+    private Socket mySocket;
213 216
     /** Used for writing to the server. */
214 217
     private final OutputQueue out;
215 218
     /** The encoder to use to encode incoming lines. */
@@ -741,7 +744,6 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
741 744
 
742 745
         rawSocket = getSocketFactory().createSocket(connectUri.getHost(), connectUri.getPort());
743 746
 
744
-        final Socket socket;
745 747
         if (getURI().getScheme().endsWith("s")) {
746 748
             callDebugInfo(DEBUG_SOCKET, "Server is SSL.");
747 749
 
@@ -753,27 +755,27 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
753 755
             sc.init(myKeyManagers, myTrustManager, new SecureRandom());
754 756
 
755 757
             final SSLSocketFactory socketFactory = sc.getSocketFactory();
756
-            socket = socketFactory.createSocket(rawSocket, getURI().getHost(), getURI()
758
+            mySocket = socketFactory.createSocket(rawSocket, getURI().getHost(), getURI()
757 759
                     .getPort(), false);
758 760
 
759 761
             // Manually start a handshake so we get proper SSL errors here,
760 762
             // and so that we can control the connection timeout
761
-            final int timeout = socket.getSoTimeout();
762
-            socket.setSoTimeout(10000);
763
-            ((SSLSocket) socket).startHandshake();
764
-            socket.setSoTimeout(timeout);
763
+            final int timeout = mySocket.getSoTimeout();
764
+            mySocket.setSoTimeout(10000);
765
+            ((SSLSocket) mySocket).startHandshake();
766
+            mySocket.setSoTimeout(timeout);
765 767
 
766 768
             currentSocketState = SocketState.OPENING;
767 769
         } else {
768
-            socket = rawSocket;
770
+            mySocket = rawSocket;
769 771
         }
770 772
 
771 773
         callDebugInfo(DEBUG_SOCKET, "\t-> Opening socket output stream PrintWriter");
772
-        out.setOutputStream(socket.getOutputStream());
774
+        out.setOutputStream(mySocket.getOutputStream());
773 775
         out.setQueueEnabled(true);
774 776
         currentSocketState = SocketState.OPEN;
775 777
         callDebugInfo(DEBUG_SOCKET, "\t-> Opening socket input stream BufferedReader");
776
-        in = new IRCReader(socket.getInputStream(), encoder);
778
+        in = new IRCReader(mySocket.getInputStream(), encoder);
777 779
         callDebugInfo(DEBUG_SOCKET, "\t-> Socket Opened");
778 780
     }
779 781
 
@@ -1765,17 +1767,22 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
1765 1767
 
1766 1768
     @Override
1767 1769
     public void quit(final String reason) {
1768
-        sendString("QUIT", reason);
1770
+        // Don't attempt to send anything further.
1771
+        getOutputQueue().clearQueue();
1772
+        final String output = (reason == null || reason.isEmpty()) ? "QUIT" : "QUIT :" + reason;
1773
+        doSendString(output, QueuePriority.IMMEDIATE, true);
1774
+        // Don't bother queueing anything else.
1775
+        getOutputQueue().setDiscarding(true);
1769 1776
     }
1770 1777
 
1771 1778
     @Override
1772 1779
     public void disconnect(final String message) {
1773 1780
         super.disconnect(message);
1774 1781
         if (currentSocketState == SocketState.OPENING || currentSocketState == SocketState.OPEN) {
1775
-            currentSocketState = SocketState.CLOSING;
1776 1782
             if (got001) {
1777 1783
                 quit(message);
1778 1784
             }
1785
+            currentSocketState = SocketState.CLOSING;
1779 1786
         }
1780 1787
 
1781 1788
         try {

+ 24
- 1
irc/src/com/dmdirc/parser/irc/outputqueue/OutputQueue.java View File

@@ -38,6 +38,8 @@ public class OutputQueue {
38 38
     private PrintWriter out;
39 39
     /** Is queueing enabled? */
40 40
     private boolean queueEnabled = true;
41
+    /** Are we discarding all futher input? */
42
+    private boolean discarding = false;
41 43
     /** The output queue! */
42 44
     private final BlockingQueue<QueueItem> queue = new PriorityBlockingQueue<>();
43 45
     /** Object for synchronising access to the {@link #queueHandler}. */
@@ -111,6 +113,8 @@ public class OutputQueue {
111 113
         final boolean old = this.queueEnabled;
112 114
         this.queueEnabled = queueEnabled;
113 115
 
116
+        // If the new value is not the same as the old one, and we used to be enabled
117
+        // then flush the queue.
114 118
         if (old != queueEnabled && old) {
115 119
             synchronized (queueHandlerLock) {
116 120
                 if (queueHandler != null) {
@@ -129,6 +133,24 @@ public class OutputQueue {
129 133
         }
130 134
     }
131 135
 
136
+    /**
137
+     * Should we be discarding?
138
+     *
139
+     * @param newValue true to enable discarding.
140
+     */
141
+    public void setDiscarding(final boolean newValue) {
142
+        discarding = newValue;
143
+    }
144
+
145
+    /**
146
+     * Are we discarding?
147
+     *
148
+     * @return true if discarding
149
+     */
150
+    public boolean isDiscarding() {
151
+        return discarding;
152
+    }
153
+
132 154
     /**
133 155
      * Clear the queue and stop the thread that is sending stuff.
134 156
      */
@@ -174,11 +196,12 @@ public class OutputQueue {
174 196
      * @param priority Priority of item (ignored if queue is disabled)
175 197
      */
176 198
     public void sendLine(final String line, final QueuePriority priority) {
199
+        if (discarding) { return; }
177 200
         if (out == null) {
178 201
             throw new NullPointerException("No output stream has been set.");
179 202
         }
180 203
 
181
-        if (queueEnabled) {
204
+        if (queueEnabled && priority != QueuePriority.IMMEDIATE) {
182 205
             synchronized (queueHandlerLock) {
183 206
                 if (queueHandler == null || !queueHandler.isAlive()) {
184 207
                     queueHandler = queueFactory.getQueueHandler(this, queue, out);

Loading…
Cancel
Save