aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ugnich Anton2013-09-07 19:54:51 +0700
committerGravatar Ugnich Anton2013-09-07 19:54:51 +0700
commitb86eb130ef8a1b06a34454a47738e0a8ba9b50dd (patch)
tree644cd1e9df062f9b520dc2dee316af666ca4282a
parent265e629e5bc5ad8f9d2a8e94dce864ebe995bcef (diff)
XMPPConnection.buildTextFrame; SocketSubscribed.sendByteBuffer, close
-rw-r--r--src/com/juick/jabber/ws/SocketSubscribed.java24
-rw-r--r--src/com/juick/jabber/ws/WSKeepAlive.java32
-rw-r--r--src/com/juick/jabber/ws/XMPPConnection.java90
3 files changed, 49 insertions, 97 deletions
diff --git a/src/com/juick/jabber/ws/SocketSubscribed.java b/src/com/juick/jabber/ws/SocketSubscribed.java
index f865ce6a..6144380c 100644
--- a/src/com/juick/jabber/ws/SocketSubscribed.java
+++ b/src/com/juick/jabber/ws/SocketSubscribed.java
@@ -1,5 +1,6 @@
package com.juick.jabber.ws;
+import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
/**
@@ -24,4 +25,27 @@ public class SocketSubscribed {
this.VUID = VUID;
tsConnected = tsLastData = System.currentTimeMillis();
}
+
+ public boolean sendByteBuffer(ByteBuffer bb) {
+ boolean ret = false;
+ bb.rewind();
+ try {
+ sock.write(bb);
+ ret = true;
+ } catch (Exception e) {
+ close();
+ }
+ return ret;
+ }
+
+ public void close() {
+ try {
+ sock.socket().close();
+ } catch (Exception e) {
+ }
+ try {
+ sock.close();
+ } catch (Exception e) {
+ }
+ }
}
diff --git a/src/com/juick/jabber/ws/WSKeepAlive.java b/src/com/juick/jabber/ws/WSKeepAlive.java
index 2e36b100..2deef594 100644
--- a/src/com/juick/jabber/ws/WSKeepAlive.java
+++ b/src/com/juick/jabber/ws/WSKeepAlive.java
@@ -73,35 +73,11 @@ public class WSKeepAlive implements Runnable {
}
if (inactive > 180) {
- closeBytes.rewind();
- try {
- s.sock.write(closeBytes);
- } catch (Exception e) {
- } finally {
- try {
- s.sock.socket().close();
- } catch (Exception ex) {
- }
- try {
- s.sock.close();
- } catch (Exception ex) {
- }
- i.remove();
- }
+ s.sendByteBuffer(closeBytes);
+ s.close();
+ i.remove();
} else if (inactive > 60) {
- pingBytes.rewind();
- try {
- s.sock.write(pingBytes);
- } catch (Exception e) {
- System.err.println("WSKeepAlive ping: " + e);
- try {
- s.sock.socket().close();
- } catch (Exception ex) {
- }
- try {
- s.sock.close();
- } catch (Exception ex) {
- }
+ if (!s.sendByteBuffer(pingBytes)) {
i.remove();
}
}
diff --git a/src/com/juick/jabber/ws/XMPPConnection.java b/src/com/juick/jabber/ws/XMPPConnection.java
index a8af8c44..b85c8bee 100644
--- a/src/com/juick/jabber/ws/XMPPConnection.java
+++ b/src/com/juick/jabber/ws/XMPPConnection.java
@@ -81,34 +81,13 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.
private void onJuickPM(int uid_to, com.juick.Message jmsg) {
String json = com.juick.json.Message.toJSON(jmsg).toString();
- ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json);
- ByteBuffer bbMsg = ByteBuffer.allocate(10240);
- bbMsg.put((byte) 0x81);
- if (jsonbytes.limit() <= 125) {
- bbMsg.put((byte) jsonbytes.limit());
- } else {
- bbMsg.put((byte) 126);
- bbMsg.putShort((short) jsonbytes.limit());
- }
- bbMsg.put(jsonbytes);
- bbMsg.flip();
+ ByteBuffer bbMsg = buildTextFrame(json);
synchronized (Main.clients) {
for (Iterator<SocketSubscribed> i = Main.clients.iterator(); i.hasNext();) {
SocketSubscribed s = i.next();
if (s.VUID == uid_to) {
- bbMsg.rewind();
- try {
- s.sock.write(bbMsg);
- } catch (Exception e) {
- try {
- s.sock.socket().close();
- } catch (Exception ex) {
- }
- try {
- s.sock.close();
- } catch (Exception ex) {
- }
+ if (!s.sendByteBuffer(bbMsg)) {
i.remove();
}
}
@@ -118,17 +97,7 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.
private void onJuickMessagePost(com.juick.Message jmsg) {
String json = com.juick.json.Message.toJSON(jmsg).toString();
- ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json);
- ByteBuffer bbMsg = ByteBuffer.allocate(10240);
- bbMsg.put((byte) 0x81);
- if (jsonbytes.limit() <= 125) {
- bbMsg.put((byte) jsonbytes.limit());
- } else {
- bbMsg.put((byte) 126);
- bbMsg.putShort((short) jsonbytes.limit());
- }
- bbMsg.put(jsonbytes);
- bbMsg.flip();
+ ByteBuffer bbMsg = buildTextFrame(json);
ArrayList<Integer> uids = new ArrayList<Integer>();
String query = "SELECT suser_id FROM subscr_users WHERE user_id=" + jmsg.User.UID + " AND suser_id NOT IN (SELECT user_id FROM bl_tags INNER JOIN messages_tags USING(tag_id) WHERE message_id=" + jmsg.MID + ")";
@@ -154,18 +123,7 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.
for (Iterator<SocketSubscribed> i = Main.clients.iterator(); i.hasNext();) {
SocketSubscribed s = i.next();
if ((jmsg.Privacy >= 0 && (s.allMessages || s.UID == jmsg.User.UID)) || uids.contains(s.VUID)) {
- bbMsg.rewind();
- try {
- s.sock.write(bbMsg);
- } catch (Exception e) {
- try {
- s.sock.socket().close();
- } catch (Exception ex) {
- }
- try {
- s.sock.close();
- } catch (Exception ex) {
- }
+ if (!s.sendByteBuffer(bbMsg)) {
i.remove();
}
}
@@ -175,17 +133,7 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.
private void onJuickMessageReply(com.juick.Message jmsg) {
String json = com.juick.json.Message.toJSON(jmsg).toString();
- ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json);
- ByteBuffer bbMsg = ByteBuffer.allocate(10240);
- bbMsg.put((byte) 0x81);
- if (jsonbytes.limit() <= 125) {
- bbMsg.put((byte) jsonbytes.limit());
- } else {
- bbMsg.put((byte) 126);
- bbMsg.putShort((short) jsonbytes.limit());
- }
- bbMsg.put(jsonbytes);
- bbMsg.flip();
+ ByteBuffer bbMsg = buildTextFrame(json);
int privacy = MessagesQueries.getMessagePrivacy(sql, jmsg.MID);
@@ -193,22 +141,26 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.
for (Iterator<SocketSubscribed> i = Main.clients.iterator(); i.hasNext();) {
SocketSubscribed s = i.next();
if ((privacy >= 0 && s.allReplies) || s.MID == jmsg.MID) {
- bbMsg.rewind();
- try {
- s.sock.write(bbMsg);
- } catch (Exception e) {
- try {
- s.sock.socket().close();
- } catch (Exception ex) {
- }
- try {
- s.sock.close();
- } catch (Exception ex) {
- }
+ if (!s.sendByteBuffer(bbMsg)) {
i.remove();
}
}
}
}
}
+
+ private ByteBuffer buildTextFrame(String json) {
+ ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json);
+ ByteBuffer bbMsg = ByteBuffer.allocate(jsonbytes.limit() + 8);
+ bbMsg.put((byte) 0x81);
+ if (jsonbytes.limit() <= 125) {
+ bbMsg.put((byte) jsonbytes.limit());
+ } else {
+ bbMsg.put((byte) 126);
+ bbMsg.putShort((short) jsonbytes.limit());
+ }
+ bbMsg.put(jsonbytes);
+ bbMsg.flip();
+ return bbMsg;
+ }
}