|
|
@@ -1,32 +1,39 @@
|
|
|
package com.knacki.lib.libirc.api;
|
|
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.io.OutputStreamWriter;
|
|
|
import java.net.Socket;
|
|
|
import java.net.SocketException;
|
|
|
+import java.net.SocketTimeoutException;
|
|
|
import java.util.ArrayDeque;
|
|
|
import java.util.Iterator;
|
|
|
+import java.util.concurrent.ConcurrentLinkedDeque;
|
|
|
+import java.util.logging.Logger;
|
|
|
|
|
|
/**
|
|
|
* Created by isundil on 10/31/16.
|
|
|
*/
|
|
|
public class IrcSocket implements IrcISocket {
|
|
|
+ private final static Logger log = Logger.getLogger(IrcSocket.class.getName());
|
|
|
+
|
|
|
protected String addr;
|
|
|
protected short port;
|
|
|
protected Socket sock;
|
|
|
protected Thread thread;
|
|
|
private OutputStreamWriter out;
|
|
|
- private ArrayDeque<String> delayMsg;
|
|
|
+ private ConcurrentLinkedDeque<String> delayMsg;
|
|
|
|
|
|
protected MessageCallback mcb;
|
|
|
protected ConnectCallback ccb;
|
|
|
|
|
|
- public IrcSocket(String addr, short port) {
|
|
|
+ public IrcSocket(String addr, int port) {
|
|
|
this.addr = addr;
|
|
|
- this.port = port;
|
|
|
- delayMsg = new ArrayDeque<>();
|
|
|
+ this.port = (short) port;
|
|
|
+ delayMsg = new ConcurrentLinkedDeque<>();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -37,6 +44,7 @@ public class IrcSocket implements IrcISocket {
|
|
|
|
|
|
try {
|
|
|
sock = new Socket(addr, port);
|
|
|
+ sock.setSoTimeout(750);
|
|
|
input = new InputStreamReader(sock.getInputStream());
|
|
|
out = new OutputStreamWriter(sock.getOutputStream());
|
|
|
}
|
|
|
@@ -60,8 +68,7 @@ public class IrcSocket implements IrcISocket {
|
|
|
sock.close();
|
|
|
thread.interrupt();
|
|
|
}
|
|
|
- catch (IOException e) {
|
|
|
- }
|
|
|
+ catch (IOException e) { }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -70,23 +77,26 @@ public class IrcSocket implements IrcISocket {
|
|
|
}
|
|
|
|
|
|
private void runThread(BufferedReader in) {
|
|
|
- while (true) {
|
|
|
+ while (!Thread.interrupted()) {
|
|
|
String line;
|
|
|
|
|
|
+ try {
|
|
|
+ for (Iterator<String> s = delayMsg.iterator(); s.hasNext(); ) {
|
|
|
+ final String _line = s.next();
|
|
|
+
|
|
|
+ out.write(_line + "\n");
|
|
|
+ s.remove();
|
|
|
+ log.info("Sent " + _line);
|
|
|
+ }
|
|
|
+ out.flush();
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
try {
|
|
|
line = in.readLine();
|
|
|
}
|
|
|
- catch (InterruptedException) {
|
|
|
- try {
|
|
|
- for (Iterator<String> lineIt = delayMsg.iterator(); lineIt.hasNext();) {
|
|
|
- out.write(lineIt.next() + "\n");
|
|
|
- lineIt.remove();
|
|
|
- }
|
|
|
- }
|
|
|
- catch (IOException e) {
|
|
|
- ccb.onConnectError(e);
|
|
|
- break;
|
|
|
- }
|
|
|
+ catch (SocketTimeoutException e) {
|
|
|
continue;
|
|
|
}
|
|
|
catch (SocketException e) {
|
|
|
@@ -104,4 +114,9 @@ public class IrcSocket implements IrcISocket {
|
|
|
mcb.onMessage(line);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void joinThread() throws InterruptedException {
|
|
|
+ thread.join();
|
|
|
+ }
|
|
|
}
|