From b86eb130ef8a1b06a34454a47738e0a8ba9b50dd Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 7 Sep 2013 19:54:51 +0700 Subject: XMPPConnection.buildTextFrame; SocketSubscribed.sendByteBuffer, close --- src/com/juick/jabber/ws/XMPPConnection.java | 90 +++++++---------------------- 1 file changed, 21 insertions(+), 69 deletions(-) (limited to 'src/com/juick/jabber/ws/XMPPConnection.java') 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 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 uids = new ArrayList(); 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 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 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; + } } -- cgit v1.2.3