diff options
author | 2013-09-07 19:54:51 +0700 | |
---|---|---|
committer | 2013-09-07 19:54:51 +0700 | |
commit | b86eb130ef8a1b06a34454a47738e0a8ba9b50dd (patch) | |
tree | 644cd1e9df062f9b520dc2dee316af666ca4282a /src/com/juick/jabber/ws/XMPPConnection.java | |
parent | 265e629e5bc5ad8f9d2a8e94dce864ebe995bcef (diff) |
XMPPConnection.buildTextFrame; SocketSubscribed.sendByteBuffer, close
Diffstat (limited to 'src/com/juick/jabber/ws/XMPPConnection.java')
-rw-r--r-- | src/com/juick/jabber/ws/XMPPConnection.java | 90 |
1 files changed, 21 insertions, 69 deletions
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; + } } |