Browse Source

Merge pull request #114 from ShaneMcC/DisconnectQuit

Send QUIT on disconnect. (Close #7)
pull/117/head
Chris Smith 8 years ago
parent
commit
e28b525377

+ 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. */

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

@@ -52,6 +52,7 @@ import com.dmdirc.parser.irc.events.IRCDataOutEvent;
52 52
 import com.dmdirc.parser.irc.outputqueue.OutputQueue;
53 53
 
54 54
 import java.io.IOException;
55
+import java.io.PrintWriter;
55 56
 import java.net.InetAddress;
56 57
 import java.net.Socket;
57 58
 import java.net.URI;
@@ -207,9 +208,9 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
207 208
     protected SocketState currentSocketState = SocketState.NULL;
208 209
     /**
209 210
      * The underlying socket used for reading/writing to the IRC server.
210
-     * For normal sockets this will be the same as {@link #socket} but for SSL
211
+     * For normal sockets this will be the same as {@link #mySocket} but for SSL
211 212
      * connections this will be the underlying {@link Socket} while
212
-     * {@link #socket} will be an {@link SSLSocket}.
213
+     * {@link #mySocket} will be an {@link SSLSocket}.
213 214
      */
214 215
     private Socket rawSocket;
215 216
     /** Used for writing to the server. */
@@ -739,7 +740,7 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
739 740
 
740 741
         rawSocket = getSocketFactory().createSocket(connectUri.getHost(), connectUri.getPort());
741 742
 
742
-        final Socket socket;
743
+        final Socket mySocket;
743 744
         if (getURI().getScheme().endsWith("s")) {
744 745
             callDebugInfo(DEBUG_SOCKET, "Server is SSL.");
745 746
 
@@ -751,27 +752,27 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
751 752
             sc.init(myKeyManagers, myTrustManager, new SecureRandom());
752 753
 
753 754
             final SSLSocketFactory socketFactory = sc.getSocketFactory();
754
-            socket = socketFactory.createSocket(rawSocket, getURI().getHost(), getURI()
755
+            mySocket = socketFactory.createSocket(rawSocket, getURI().getHost(), getURI()
755 756
                     .getPort(), false);
756 757
 
757 758
             // Manually start a handshake so we get proper SSL errors here,
758 759
             // and so that we can control the connection timeout
759
-            final int timeout = socket.getSoTimeout();
760
-            socket.setSoTimeout(10000);
761
-            ((SSLSocket) socket).startHandshake();
762
-            socket.setSoTimeout(timeout);
760
+            final int timeout = mySocket.getSoTimeout();
761
+            mySocket.setSoTimeout(10000);
762
+            ((SSLSocket) mySocket).startHandshake();
763
+            mySocket.setSoTimeout(timeout);
763 764
 
764 765
             currentSocketState = SocketState.OPENING;
765 766
         } else {
766
-            socket = rawSocket;
767
+            mySocket = rawSocket;
767 768
         }
768 769
 
769 770
         callDebugInfo(DEBUG_SOCKET, "\t-> Opening socket output stream PrintWriter");
770
-        out.setOutputStream(socket.getOutputStream());
771
+        out.setOutputStream(mySocket.getOutputStream());
771 772
         out.setQueueEnabled(true);
772 773
         currentSocketState = SocketState.OPEN;
773 774
         callDebugInfo(DEBUG_SOCKET, "\t-> Opening socket input stream BufferedReader");
774
-        in = new IRCReader(socket.getInputStream(), encoder);
775
+        in = new IRCReader(mySocket.getInputStream(), encoder);
775 776
         callDebugInfo(DEBUG_SOCKET, "\t-> Socket Opened");
776 777
     }
777 778
 
@@ -1724,17 +1725,22 @@ public class IRCParser extends BaseSocketAwareParser implements SecureParser, En
1724 1725
 
1725 1726
     @Override
1726 1727
     public void quit(final String reason) {
1727
-        sendString("QUIT", reason);
1728
+        // Don't attempt to send anything further.
1729
+        getOutputQueue().clearQueue();
1730
+        final String output = (reason == null || reason.isEmpty()) ? "QUIT" : "QUIT :" + reason;
1731
+        doSendString(output, QueuePriority.IMMEDIATE, true);
1732
+        // Don't bother queueing anything else.
1733
+        getOutputQueue().setDiscarding(true);
1728 1734
     }
1729 1735
 
1730 1736
     @Override
1731 1737
     public void disconnect(final String message) {
1732 1738
         super.disconnect(message);
1733 1739
         if (currentSocketState == SocketState.OPENING || currentSocketState == SocketState.OPEN) {
1734
-            currentSocketState = SocketState.CLOSING;
1735 1740
             if (got001) {
1736 1741
                 quit(message);
1737 1742
             }
1743
+            currentSocketState = SocketState.CLOSING;
1738 1744
         }
1739 1745
 
1740 1746
         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